[llvm] [NVVMReflect] Force dead branch elimination in NVVMReflect (PR #81189)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 8 22:12:51 PST 2024
================
@@ -171,13 +175,71 @@ 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.
+ // TODO: This currently does not handle switch instructions.
+ for (User *U : Call->users())
+ if (ICmpInst *I = dyn_cast<ICmpInst>(U))
+ ToSimplify.push_back(I);
+
Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
ToRemove.push_back(Call);
}
for (Instruction *I : ToRemove)
I->eraseFromParent();
+ // The code guarded by __nvvm_reflect may be invalid for the target machine.
+ // We need to do some basic dead code elimination to trim invalid code before
+ // it reaches the backend at all optimization levels.
+ SmallVector<BranchInst *> Simplified;
+ for (ICmpInst *Cmp : ToSimplify) {
+ Constant *LHS = dyn_cast<Constant>(Cmp->getOperand(0));
+ Constant *RHS = dyn_cast<Constant>(Cmp->getOperand(1));
+
+ if (!LHS || !RHS)
+ continue;
+
+ // If the comparison is a compile time constant we simply propagate it.
+ Constant *C = ConstantFoldCompareInstOperands(
+ Cmp->getPredicate(), LHS, RHS, Cmp->getModule()->getDataLayout());
+
+ if (!C)
+ continue;
+
+ for (User *U : Cmp->users())
+ if (BranchInst *I = dyn_cast<BranchInst>(U))
+ Simplified.push_back(I);
+
+ Cmp->replaceAllUsesWith(C);
+ Cmp->eraseFromParent();
+ }
+
+ // Each instruction here is a conditional branch off of a constant true or
+ // false value. Simply replace it with an unconditional branch to the
+ // appropriate basic block and delete the rest if it is trivially dead.
+ DenseSet<Instruction *> Removed;
+ for (BranchInst *Branch : Simplified) {
----------------
arsenm wrote:
Oh, it's also in Local.h
https://github.com/llvm/llvm-project/pull/81189
More information about the llvm-commits
mailing list