1 //===-- NVPTXLowerUnreachable.cpp - Lower unreachables to exit =====--===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // PTX does not have a notion of `unreachable`, which results in emitted basic
10 // blocks having an edge to the next block:
13 // call @does_not_return();
16 // // ptxas will create a CFG edge from block1 to block2
18 // This may result in significant changes to the control flow graph, e.g., when
19 // LLVM moves unreachable blocks to the end of the function. That's a problem
20 // in the context of divergent control flow, as `ptxas` uses the CFG to
21 // determine divergent regions, and some intructions may not be executed
24 // For example, `bar.sync` is not allowed to be executed divergently on Pascal
25 // or earlier. If we start with the following:
28 // // start of divergent region
37 // // end of divergent region
43 // it is transformed by the branch-folder and block-placement passes to:
46 // // start of divergent region
58 // // end of divergent region
61 // After moving the `unlikely` block to the end of the function, it has an edge
62 // to the `exit` block, which widens the divergent region and makes the
63 // `bar.sync` instruction happen divergently.
65 // To work around this, we add an `exit` instruction before every `unreachable`,
66 // as `ptxas` understands that exit terminates the CFG. We do only do this if
67 // `unreachable` is not lowered to `trap`, which has the same effect (although
68 // with current versions of `ptxas` only because it is emited as `trap; exit;`).
70 //===----------------------------------------------------------------------===//
73 #include "llvm/IR/Function.h"
74 #include "llvm/IR/InlineAsm.h"
75 #include "llvm/IR/Instructions.h"
76 #include "llvm/IR/Type.h"
77 #include "llvm/Pass.h"
82 void initializeNVPTXLowerUnreachablePass(PassRegistry
&);
86 class NVPTXLowerUnreachable
: public FunctionPass
{
87 StringRef
getPassName() const override
;
88 bool runOnFunction(Function
&F
) override
;
89 bool isLoweredToTrap(const UnreachableInst
&I
) const;
92 static char ID
; // Pass identification, replacement for typeid
93 NVPTXLowerUnreachable(bool TrapUnreachable
, bool NoTrapAfterNoreturn
)
94 : FunctionPass(ID
), TrapUnreachable(TrapUnreachable
),
95 NoTrapAfterNoreturn(NoTrapAfterNoreturn
) {}
99 bool NoTrapAfterNoreturn
;
103 char NVPTXLowerUnreachable::ID
= 1;
105 INITIALIZE_PASS(NVPTXLowerUnreachable
, "nvptx-lower-unreachable",
106 "Lower Unreachable", false, false)
108 StringRef
NVPTXLowerUnreachable::getPassName() const {
109 return "add an exit instruction before every unreachable";
112 // =============================================================================
113 // Returns whether a `trap` intrinsic should be emitted before I.
115 // This is a copy of the logic in SelectionDAGBuilder::visitUnreachable().
116 // =============================================================================
117 bool NVPTXLowerUnreachable::isLoweredToTrap(const UnreachableInst
&I
) const {
118 if (!TrapUnreachable
)
120 if (!NoTrapAfterNoreturn
)
122 const CallInst
*Call
= dyn_cast_or_null
<CallInst
>(I
.getPrevNode());
123 return Call
&& Call
->doesNotReturn();
126 // =============================================================================
127 // Main function for this pass.
128 // =============================================================================
129 bool NVPTXLowerUnreachable::runOnFunction(Function
&F
) {
132 // Early out iff isLoweredToTrap() always returns true.
133 if (TrapUnreachable
&& !NoTrapAfterNoreturn
)
136 LLVMContext
&C
= F
.getContext();
137 FunctionType
*ExitFTy
= FunctionType::get(Type::getVoidTy(C
), false);
138 InlineAsm
*Exit
= InlineAsm::get(ExitFTy
, "exit;", "", true);
140 bool Changed
= false;
143 if (auto unreachableInst
= dyn_cast
<UnreachableInst
>(&I
)) {
144 if (isLoweredToTrap(*unreachableInst
))
145 continue; // trap is emitted as `trap; exit;`.
146 CallInst::Create(ExitFTy
, Exit
, "", unreachableInst
);
153 FunctionPass
*llvm::createNVPTXLowerUnreachablePass(bool TrapUnreachable
,
154 bool NoTrapAfterNoreturn
) {
155 return new NVPTXLowerUnreachable(TrapUnreachable
, NoTrapAfterNoreturn
);