LLVM 20.0.0git
SPIRVStripConvergentIntrinsics.cpp
Go to the documentation of this file.
1//===-- SPIRVStripConvergentIntrinsics.cpp ----------------------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This pass trims convergence intrinsics as those were only useful when
10// modifying the CFG during IR passes.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRV.h"
15#include "SPIRVSubtarget.h"
16#include "SPIRVTargetMachine.h"
17#include "SPIRVUtils.h"
19#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/Intrinsics.h"
22#include "llvm/IR/IntrinsicsSPIRV.h"
25
26using namespace llvm;
27
28namespace llvm {
30}
31
33public:
34 static char ID;
35
39 };
40
41 virtual bool runOnFunction(Function &F) override {
43
44 // Is the instruction is a convergent intrinsic, add it to kill-list and
45 // returns true. Returns false otherwise.
46 auto CleanupIntrinsic = [&](IntrinsicInst *II) {
47 if (II->getIntrinsicID() != Intrinsic::experimental_convergence_entry &&
48 II->getIntrinsicID() != Intrinsic::experimental_convergence_loop &&
49 II->getIntrinsicID() != Intrinsic::experimental_convergence_anchor)
50 return false;
51
52 II->replaceAllUsesWith(UndefValue::get(II->getType()));
53 ToRemove.insert(II);
54 return true;
55 };
56
57 // Replace the given CallInst by a similar CallInst with no convergencectrl
58 // attribute.
59 auto CleanupCall = [&](CallInst *CI) {
60 auto OB = CI->getOperandBundle(LLVMContext::OB_convergencectrl);
61 if (!OB.has_value())
62 return;
63
64 auto *NewCall = CallBase::removeOperandBundle(
65 CI, LLVMContext::OB_convergencectrl, CI->getIterator());
66 NewCall->copyMetadata(*CI);
67 CI->replaceAllUsesWith(NewCall);
68 ToRemove.insert(CI);
69 };
70
71 for (BasicBlock &BB : F) {
72 for (Instruction &I : BB) {
73 if (auto *II = dyn_cast<IntrinsicInst>(&I))
74 if (CleanupIntrinsic(II))
75 continue;
76 if (auto *CI = dyn_cast<CallInst>(&I))
77 CleanupCall(CI);
78 }
79 }
80
81 // All usages must be removed before their definition is removed.
82 for (Instruction *I : ToRemove)
83 I->eraseFromParent();
84
85 return ToRemove.size() != 0;
86 }
87};
88
90INITIALIZE_PASS(SPIRVStripConvergentIntrinsics, "strip-convergent-intrinsics",
91 "SPIRV strip convergent intrinsics", false, false)
92
95}
ReachingDefAnalysis InstSet & ToRemove
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
virtual bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
static CallBase * removeOperandBundle(CallBase *CB, uint32_t ID, InsertPosition InsertPt=nullptr)
Create a clone of CB with operand bundle ID removed.
This class represents a function call, abstracting a target machine's calling convention.
Implements a dense probed hash-table based set.
Definition: DenseSet.h:278
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
Definition: PassRegistry.h:37
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1859
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
FunctionPass * createSPIRVStripConvergenceIntrinsicsPass()
void initializeSPIRVStripConvergentIntrinsicsPass(PassRegistry &)