Skip to content

Commit 76f722a

Browse files
committed
[NVPTX] Fix code generation for trap-unreachable.
https://reviews.llvm.org/D152789 added an `exit` op before each `unreachable`. This means we never get to the `trap` instruction. - When `trap-unreachable` is enabled and `no-trap-after-noreturn` is not, don't insert `exit` before each `unreachable`. - Lower ISD::TRAP to both `trap` and `exit` instead of just the former. The fix doesn't work with `no-trap-after-noreturn`, because the `unreachable`s not following a `noreturn` are lowered to `exit; trap; exit;`. An alternative approach would be to insert `trap`s in `NVPTXLowerUnreachablePass`, depending on the `trap-unreachable` and `no-trap-after-noreturn` settings. I think we would then want skip lowering ISD::TRAP, so that we don't end up with `trap; exit; trap;` sequences.
1 parent f7bf99f commit 76f722a

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

llvm/lib/Target/NVPTX/NVPTXInstrInfo.td

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3545,7 +3545,9 @@ def Callseq_End :
35453545
[(callseq_end timm:$amt1, timm:$amt2)]>;
35463546

35473547
// trap instruction
3548-
def trapinst : NVPTXInst<(outs), (ins), "trap;", [(trap)]>;
3548+
// Emit an `exit` as well to convey to ptxas that `trap` exits the CFG.
3549+
// This won't be necessary in a future version of ptxas.
3550+
def trapinst : NVPTXInst<(outs), (ins), "trap; exit;", [(trap)]>;
35493551

35503552
// Call prototype wrapper
35513553
def SDTCallPrototype : SDTypeProfile<0, 1, [SDTCisInt<0>]>;

llvm/lib/Target/NVPTX/NVPTXTargetMachine.cpp

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,6 @@ static cl::opt<bool> UseShortPointersOpt(
6363
"Use 32-bit pointers for accessing const/local/shared address spaces."),
6464
cl::init(false), cl::Hidden);
6565

66-
// FIXME: intended as a temporary debugging aid. Should be removed before it
67-
// makes it into the LLVM-17 release.
68-
static cl::opt<bool>
69-
ExitOnUnreachable("nvptx-exit-on-unreachable",
70-
cl::desc("Lower 'unreachable' as 'exit' instruction."),
71-
cl::init(true), cl::Hidden);
72-
7366
namespace llvm {
7467

7568
void initializeGenericToNVVMLegacyPassPass(PassRegistry &);
@@ -410,7 +403,8 @@ void NVPTXPassConfig::addIRPasses() {
410403
addPass(createSROAPass());
411404
}
412405

413-
if (ExitOnUnreachable)
406+
const auto &options = getNVPTXTargetMachine().Options;
407+
if (!options.TrapUnreachable || options.NoTrapAfterNoreturn)
414408
addPass(createNVPTXLowerUnreachablePass());
415409
}
416410

llvm/test/CodeGen/NVPTX/unreachable.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
; RUN: llc < %s -march=nvptx -mcpu=sm_20 -verify-machineinstrs | FileCheck %s
22
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 -verify-machineinstrs | FileCheck %s
3+
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 -verify-machineinstrs -trap-unreachable \
4+
; RUN: | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-TRAP
35
; RUN: %if ptxas && !ptxas-12.0 %{ llc < %s -march=nvptx -mcpu=sm_20 -verify-machineinstrs | %ptxas-verify %}
46
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 -verify-machineinstrs | %ptxas-verify %}
57

@@ -11,7 +13,9 @@ define void @kernel_func() {
1113
; CHECK: call.uni
1214
; CHECK: throw,
1315
call void @throw()
14-
; CHECK: exit
16+
; CHECK-TRAP-NOT: exit;
17+
; CHECK-TRAP: trap;
18+
; CHECK: exit;
1519
unreachable
1620
}
1721

0 commit comments

Comments
 (0)