LLVM 23.0.0git
StripConvergenceIntrinsics.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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 strips convergence intrinsics and convergencectrl operand bundles,
10// as those are only useful when modifying the CFG during IR passes.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/STLExtras.h"
18#include "llvm/IR/Intrinsics.h"
20#include "llvm/Pass.h"
22
23using namespace llvm;
24
26 SmallVector<IntrinsicInst *> ConvergenceIntrinsics;
27 bool Changed = false;
28
29 for (BasicBlock &BB : F) {
31 auto *CI = dyn_cast<CallInst>(&I);
32 if (!CI)
33 continue;
34
35 // Strip a convergencectrl operand bundle if present. Note that
36 // convergence intrinsics (e.g. convergence.loop) may use a
37 // convergencectrl bundle.
38 if (CI->getOperandBundle(LLVMContext::OB_convergencectrl)) {
39 auto *NewCall = CallBase::removeOperandBundle(
40 CI, LLVMContext::OB_convergencectrl, CI->getIterator());
41 NewCall->copyMetadata(*CI);
42 CI->replaceAllUsesWith(NewCall);
43 CI->eraseFromParent();
44 CI = cast<CallInst>(NewCall);
45 Changed = true;
46 }
47
48 // Collect convergence intrinsics for deferred removal.
49 if (auto *II = dyn_cast<IntrinsicInst>(CI))
50 if (II->getIntrinsicID() == Intrinsic::experimental_convergence_entry ||
51 II->getIntrinsicID() == Intrinsic::experimental_convergence_loop ||
52 II->getIntrinsicID() == Intrinsic::experimental_convergence_anchor)
53 ConvergenceIntrinsics.push_back(II);
54 }
55 }
56
57 // Erase all convergence intrinsics now that convergence tokens are no
58 // longer in use.
59 for (IntrinsicInst *II : ConvergenceIntrinsics)
60 II->eraseFromParent();
61 Changed |= !ConvergenceIntrinsics.empty();
62
63 return Changed;
64}
65
74
75namespace {
76class StripConvergenceIntrinsicsLegacyPass : public FunctionPass {
77public:
78 static char ID;
79
80 StripConvergenceIntrinsicsLegacyPass() : FunctionPass(ID) {
83 }
84
85 bool runOnFunction(Function &F) override {
87 }
88};
89} // namespace
90
91char StripConvergenceIntrinsicsLegacyPass::ID = 0;
92INITIALIZE_PASS(StripConvergenceIntrinsicsLegacyPass,
93 "strip-convergence-intrinsics",
94 "Strip convergence intrinsics and operand bundles", false,
95 false)
96
98 return new StripConvergenceIntrinsicsLegacyPass();
99}
static bool runOnFunction(Function &F, bool PostInlining)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static bool stripConvergenceIntrinsics(Function &F)
This pass strips convergence intrinsics and operand bundles as those are only useful when modifying t...
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
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
A wrapper class for inspecting calls to intrinsic functions.
static LLVM_ABI PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
PreservedAnalyses run(Function &F, FunctionAnalysisManager &)
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI void initializeStripConvergenceIntrinsicsLegacyPassPass(PassRegistry &)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
LLVM_ABI FunctionPass * createStripConvergenceIntrinsicsPass()
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.