[llvm] [NVVMReflect] Force dead branch elimination in NVVMReflect (PR #81189)

Artem Belevich via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 8 14:14:43 PST 2024


================
@@ -171,13 +175,70 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) {
     } else if (ReflectArg == "__CUDA_ARCH") {
       ReflectVal = SmVersion * 10;
     }
+
+    // If the immediate user is a simple comparison we want to simplify it.
+    for (User *U : Call->users())
+      if (ICmpInst *I = dyn_cast<ICmpInst>(U))
+        ToSimplify.push_back(I);
+
----------------
Artem-B wrote:

This is overly conservative, IMO. E.g. it may not handle something like `switch(__nvvm_reflect("__CUDA_ARCH"))`, or `if ((__nvvm_reflect("__CUDA_ARCH") / 10) == 70)`

I think, ideally, we may want to iterate upwards the use tree, as long as the current subtree evaluates to a constant, until we reach a switch/branch/select where we can now pick the correct branch.

On one hand it may be an overkill, and we could just document that this improvement works for `if` only. Even that would implicitly rely on the pass happening very early  in the pipeline, before LLVM gets to possibly convert that `if` into `select`. if-only solution may be OK for now, but if would be great to make sure its behavior is consistent for other forms of conditionals.

I'm OK with the patch in this form, but we should add a TODO outlining the still  missing pieces.

Maybe add a few test cases showing where it does not work at the moment.


https://github.com/llvm/llvm-project/pull/81189


More information about the llvm-commits mailing list