Skip to content

Commit 0800a36

Browse files
committed
Revert "[NVVMReflect] Force dead branch elimination in NVVMReflect (#81189)"
This reverts commit 9211e67. Summary: This seemed to crash one one of the CUDA math tests. Revert until it can be fixed.
1 parent c429f48 commit 0800a36

File tree

4 files changed

+1
-208
lines changed

4 files changed

+1
-208
lines changed

llvm/docs/NVPTXUsage.rst

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,6 @@ pipeline, immediately after the link stage. The ``internalize`` pass is also
296296
recommended to remove unused math functions from the resulting PTX. For an
297297
input IR module ``module.bc``, the following compilation flow is recommended:
298298

299-
The ``NVVMReflect`` pass will attempt to remove dead code even without
300-
optimizations. This allows potentially incompatible instructions to be avoided
301-
at all optimizations levels. This currently only works for simple conditionals
302-
like the above example.
303-
304299
1. Save list of external functions in ``module.bc``
305300
2. Link ``module.bc`` with ``libdevice.compute_XX.YY.bc``
306301
3. Internalize all functions not in list from (1)

llvm/lib/Target/NVPTX/NVVMReflect.cpp

Lines changed: 0 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
#include "NVPTX.h"
2222
#include "llvm/ADT/SmallVector.h"
23-
#include "llvm/Analysis/ConstantFolding.h"
2423
#include "llvm/IR/Constants.h"
2524
#include "llvm/IR/DerivedTypes.h"
2625
#include "llvm/IR/Function.h"
@@ -37,8 +36,6 @@
3736
#include "llvm/Support/raw_os_ostream.h"
3837
#include "llvm/Support/raw_ostream.h"
3938
#include "llvm/Transforms/Scalar.h"
40-
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
41-
#include "llvm/Transforms/Utils/Local.h"
4239
#include <sstream>
4340
#include <string>
4441
#define NVVM_REFLECT_FUNCTION "__nvvm_reflect"
@@ -90,7 +87,6 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) {
9087
}
9188

9289
SmallVector<Instruction *, 4> ToRemove;
93-
SmallVector<ICmpInst *, 4> ToSimplify;
9490

9591
// Go through the calls in this function. Each call to __nvvm_reflect or
9692
// llvm.nvvm.reflect should be a CallInst with a ConstantArray argument.
@@ -175,71 +171,13 @@ static bool runNVVMReflect(Function &F, unsigned SmVersion) {
175171
} else if (ReflectArg == "__CUDA_ARCH") {
176172
ReflectVal = SmVersion * 10;
177173
}
178-
179-
// If the immediate user is a simple comparison we want to simplify it.
180-
// TODO: This currently does not handle switch instructions.
181-
for (User *U : Call->users())
182-
if (ICmpInst *I = dyn_cast<ICmpInst>(U))
183-
ToSimplify.push_back(I);
184-
185174
Call->replaceAllUsesWith(ConstantInt::get(Call->getType(), ReflectVal));
186175
ToRemove.push_back(Call);
187176
}
188177

189178
for (Instruction *I : ToRemove)
190179
I->eraseFromParent();
191180

192-
// The code guarded by __nvvm_reflect may be invalid for the target machine.
193-
// We need to do some basic dead code elimination to trim invalid code before
194-
// it reaches the backend at all optimization levels.
195-
SmallVector<BranchInst *> Simplified;
196-
for (ICmpInst *Cmp : ToSimplify) {
197-
Constant *LHS = dyn_cast<Constant>(Cmp->getOperand(0));
198-
Constant *RHS = dyn_cast<Constant>(Cmp->getOperand(1));
199-
200-
if (!LHS || !RHS)
201-
continue;
202-
203-
// If the comparison is a compile time constant we simply propagate it.
204-
Constant *C = ConstantFoldCompareInstOperands(
205-
Cmp->getPredicate(), LHS, RHS, Cmp->getModule()->getDataLayout());
206-
207-
if (!C)
208-
continue;
209-
210-
for (User *U : Cmp->users())
211-
if (BranchInst *I = dyn_cast<BranchInst>(U))
212-
Simplified.push_back(I);
213-
214-
Cmp->replaceAllUsesWith(C);
215-
Cmp->eraseFromParent();
216-
}
217-
218-
// Each instruction here is a conditional branch off of a constant true or
219-
// false value. Simply replace it with an unconditional branch to the
220-
// appropriate basic block and delete the rest if it is trivially dead.
221-
DenseSet<Instruction *> Removed;
222-
for (BranchInst *Branch : Simplified) {
223-
if (Removed.contains(Branch))
224-
continue;
225-
226-
ConstantInt *C = dyn_cast<ConstantInt>(Branch->getCondition());
227-
if (!C || (!C->isOne() && !C->isZero()))
228-
continue;
229-
230-
BasicBlock *TrueBB =
231-
C->isOne() ? Branch->getSuccessor(0) : Branch->getSuccessor(1);
232-
BasicBlock *FalseBB =
233-
C->isOne() ? Branch->getSuccessor(1) : Branch->getSuccessor(0);
234-
235-
ReplaceInstWithInst(Branch, BranchInst::Create(TrueBB));
236-
if (FalseBB->use_empty() && FalseBB->hasNPredecessors(0) &&
237-
FalseBB->getFirstNonPHIOrDbg()) {
238-
Removed.insert(FalseBB->getFirstNonPHIOrDbg());
239-
changeToUnreachable(FalseBB->getFirstNonPHIOrDbg());
240-
}
241-
}
242-
243181
return ToRemove.size() > 0;
244182
}
245183

llvm/test/CodeGen/NVPTX/nvvm-reflect-arch-O0.ll

Lines changed: 0 additions & 141 deletions
This file was deleted.

llvm/test/CodeGen/NVPTX/nvvm-reflect-arch.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ define i32 @foo(float %a, float %b) {
1818
; SM35: ret i32 350
1919
ret i32 %reflect
2020
}
21+

0 commit comments

Comments
 (0)