LLVM 22.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 "SPIRVUtils.h"
19#include "llvm/IR/Intrinsics.h"
22
23using namespace llvm;
24
25namespace {
26class SPIRVStripConvergentIntrinsics : public FunctionPass {
27public:
28 static char ID;
29
30 SPIRVStripConvergentIntrinsics() : FunctionPass(ID) {}
31
32 virtual bool runOnFunction(Function &F) override {
33 DenseSet<Instruction *> ToRemove;
34
35 // Is the instruction is a convergent intrinsic, add it to kill-list and
36 // returns true. Returns false otherwise.
37 auto CleanupIntrinsic = [&](IntrinsicInst *II) {
38 if (II->getIntrinsicID() != Intrinsic::experimental_convergence_entry &&
39 II->getIntrinsicID() != Intrinsic::experimental_convergence_loop &&
40 II->getIntrinsicID() != Intrinsic::experimental_convergence_anchor)
41 return false;
42
43 II->replaceAllUsesWith(UndefValue::get(II->getType()));
44 ToRemove.insert(II);
45 return true;
46 };
47
48 // Replace the given CallInst by a similar CallInst with no convergencectrl
49 // attribute.
50 auto CleanupCall = [&](CallInst *CI) {
51 auto OB = CI->getOperandBundle(LLVMContext::OB_convergencectrl);
52 if (!OB.has_value())
53 return;
54
55 auto *NewCall = CallBase::removeOperandBundle(
56 CI, LLVMContext::OB_convergencectrl, CI->getIterator());
57 NewCall->copyMetadata(*CI);
58 CI->replaceAllUsesWith(NewCall);
59 ToRemove.insert(CI);
60 };
61
62 for (BasicBlock &BB : F) {
63 for (Instruction &I : BB) {
64 if (auto *II = dyn_cast<IntrinsicInst>(&I))
65 if (CleanupIntrinsic(II))
66 continue;
67 if (auto *CI = dyn_cast<CallInst>(&I))
68 CleanupCall(CI);
69 }
70 }
71
72 // All usages must be removed before their definition is removed.
73 for (Instruction *I : ToRemove)
74 I->eraseFromParent();
75
76 return ToRemove.size() != 0;
77 }
78};
79} // namespace
80
81char SPIRVStripConvergentIntrinsics::ID = 0;
82INITIALIZE_PASS(SPIRVStripConvergentIntrinsics, "strip-convergent-intrinsics",
83 "SPIRV strip convergent intrinsics", false, false)
84
86 return new SPIRVStripConvergentIntrinsics();
87}
ReachingDefInfo InstSet & ToRemove
static bool runOnFunction(Function &F, bool PostInlining)
#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:56
static LLVM_ABI CallBase * removeOperandBundle(CallBase *CB, uint32_t ID, InsertPosition InsertPt=nullptr)
Create a clone of CB with operand bundle ID removed.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
@ OB
OB - OneByte - Set if this instruction has a one byte opcode.
This is an optimization pass for GlobalISel generic memory operations.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
FunctionPass * createSPIRVStripConvergenceIntrinsicsPass()