Path: blob/main/contrib/llvm-project/llvm/lib/Target/NVPTX/NVPTXLowerUnreachable.cpp
35271 views
//===-- NVPTXLowerUnreachable.cpp - Lower unreachables to exit =====--===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// PTX does not have a notion of `unreachable`, which results in emitted basic9// blocks having an edge to the next block:10//11// block1:12// call @does_not_return();13// // unreachable14// block2:15// // ptxas will create a CFG edge from block1 to block216//17// This may result in significant changes to the control flow graph, e.g., when18// LLVM moves unreachable blocks to the end of the function. That's a problem19// in the context of divergent control flow, as `ptxas` uses the CFG to20// determine divergent regions, and some intructions may not be executed21// divergently.22//23// For example, `bar.sync` is not allowed to be executed divergently on Pascal24// or earlier. If we start with the following:25//26// entry:27// // start of divergent region28// @%p0 bra cont;29// @%p1 bra unlikely;30// ...31// bra.uni cont;32// unlikely:33// ...34// // unreachable35// cont:36// // end of divergent region37// bar.sync 0;38// bra.uni exit;39// exit:40// ret;41//42// it is transformed by the branch-folder and block-placement passes to:43//44// entry:45// // start of divergent region46// @%p0 bra cont;47// @%p1 bra unlikely;48// ...49// bra.uni cont;50// cont:51// bar.sync 0;52// bra.uni exit;53// unlikely:54// ...55// // unreachable56// exit:57// // end of divergent region58// ret;59//60// After moving the `unlikely` block to the end of the function, it has an edge61// to the `exit` block, which widens the divergent region and makes the62// `bar.sync` instruction happen divergently.63//64// To work around this, we add an `exit` instruction before every `unreachable`,65// as `ptxas` understands that exit terminates the CFG. We do only do this if66// `unreachable` is not lowered to `trap`, which has the same effect (although67// with current versions of `ptxas` only because it is emited as `trap; exit;`).68//69//===----------------------------------------------------------------------===//7071#include "NVPTX.h"72#include "llvm/IR/Function.h"73#include "llvm/IR/InlineAsm.h"74#include "llvm/IR/Instructions.h"75#include "llvm/IR/Type.h"76#include "llvm/Pass.h"7778using namespace llvm;7980namespace llvm {81void initializeNVPTXLowerUnreachablePass(PassRegistry &);82}8384namespace {85class NVPTXLowerUnreachable : public FunctionPass {86StringRef getPassName() const override;87bool runOnFunction(Function &F) override;88bool isLoweredToTrap(const UnreachableInst &I) const;8990public:91static char ID; // Pass identification, replacement for typeid92NVPTXLowerUnreachable(bool TrapUnreachable, bool NoTrapAfterNoreturn)93: FunctionPass(ID), TrapUnreachable(TrapUnreachable),94NoTrapAfterNoreturn(NoTrapAfterNoreturn) {}9596private:97bool TrapUnreachable;98bool NoTrapAfterNoreturn;99};100} // namespace101102char NVPTXLowerUnreachable::ID = 1;103104INITIALIZE_PASS(NVPTXLowerUnreachable, "nvptx-lower-unreachable",105"Lower Unreachable", false, false)106107StringRef NVPTXLowerUnreachable::getPassName() const {108return "add an exit instruction before every unreachable";109}110111// =============================================================================112// Returns whether a `trap` intrinsic should be emitted before I.113//114// This is a copy of the logic in SelectionDAGBuilder::visitUnreachable().115// =============================================================================116bool NVPTXLowerUnreachable::isLoweredToTrap(const UnreachableInst &I) const {117if (!TrapUnreachable)118return false;119if (!NoTrapAfterNoreturn)120return true;121const CallInst *Call = dyn_cast_or_null<CallInst>(I.getPrevNode());122return Call && Call->doesNotReturn();123}124125// =============================================================================126// Main function for this pass.127// =============================================================================128bool NVPTXLowerUnreachable::runOnFunction(Function &F) {129if (skipFunction(F))130return false;131// Early out iff isLoweredToTrap() always returns true.132if (TrapUnreachable && !NoTrapAfterNoreturn)133return false;134135LLVMContext &C = F.getContext();136FunctionType *ExitFTy = FunctionType::get(Type::getVoidTy(C), false);137InlineAsm *Exit = InlineAsm::get(ExitFTy, "exit;", "", true);138139bool Changed = false;140for (auto &BB : F)141for (auto &I : BB) {142if (auto unreachableInst = dyn_cast<UnreachableInst>(&I)) {143if (isLoweredToTrap(*unreachableInst))144continue; // trap is emitted as `trap; exit;`.145CallInst::Create(ExitFTy, Exit, "", unreachableInst->getIterator());146Changed = true;147}148}149return Changed;150}151152FunctionPass *llvm::createNVPTXLowerUnreachablePass(bool TrapUnreachable,153bool NoTrapAfterNoreturn) {154return new NVPTXLowerUnreachable(TrapUnreachable, NoTrapAfterNoreturn);155}156157158