LLVM 23.0.0git
SPIRVMergeRegionExitTargets.cpp
Go to the documentation of this file.
1//===-- SPIRVMergeRegionExitTargets.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// Merge the multiple exit targets of a convergence region into a single block.
10// Each exit target will be assigned a constant value, and a phi node + switch
11// will allow the new exit target to re-route to the correct basic block.
12//
13//===----------------------------------------------------------------------===//
14
17#include "SPIRV.h"
18#include "SPIRVSubtarget.h"
19#include "SPIRVUtils.h"
20#include "llvm/ADT/DenseMap.h"
23#include "llvm/IR/Dominators.h"
24#include "llvm/IR/IRBuilder.h"
25#include "llvm/IR/Intrinsics.h"
30
31using namespace llvm;
32
33namespace {
34
35// Run the pass on the given convergence region, ignoring the sub-regions.
36// Returns true if the CFG changed, false otherwise.
37static bool runOnConvergenceRegionNoRecurse(LoopInfo &LI,
39 // Gather all the exit targets for this region.
41 for (BasicBlock *Exit : CR->Exits) {
42 for (BasicBlock *Target : successors(Exit)) {
43 if (CR->Blocks.count(Target) == 0)
44 ExitTargets.insert(Target);
45 }
46 }
47
48 // If we have zero or one exit target, nothing do to.
49 if (ExitTargets.size() <= 1)
50 return false;
51
52 // Create the new single exit target.
53 auto F = CR->Entry->getParent();
54 auto NewExitTarget = BasicBlock::Create(F->getContext(), "new.exit", F);
55 IRBuilder<> Builder(NewExitTarget);
56
57 AllocaInst *Variable = createVariable(*F, Builder.getInt32Ty());
58
59 // CodeGen output needs to be stable. Using the set as-is would order
60 // the targets differently depending on the allocation pattern.
61 // Sorting per basic-block ordering in the function.
62 std::vector<BasicBlock *> SortedExitTargets;
63 std::vector<BasicBlock *> SortedExits;
64 for (BasicBlock &BB : *F) {
65 if (ExitTargets.count(&BB) != 0)
66 SortedExitTargets.push_back(&BB);
67 if (CR->Exits.count(&BB) != 0)
68 SortedExits.push_back(&BB);
69 }
70
71 // Creating one constant per distinct exit target. This will be route to the
72 // correct target.
74 for (BasicBlock *Target : SortedExitTargets)
75 TargetToValue.insert(
76 std::make_pair(Target, Builder.getInt32(TargetToValue.size())));
77
78 // Creating one variable per exit node, set to the constant matching the
79 // targeted external block.
80 std::vector<std::pair<BasicBlock *, Value *>> ExitToVariable;
81 for (auto Exit : SortedExits) {
82 llvm::Value *Value = createExitVariable(Exit, TargetToValue);
83 IRBuilder<> B2(Exit);
84 B2.SetInsertPoint(Exit->getFirstInsertionPt());
85 B2.CreateStore(Value, Variable);
86 ExitToVariable.emplace_back(std::make_pair(Exit, Value));
87 }
88
89 llvm::Value *Load = Builder.CreateLoad(Builder.getInt32Ty(), Variable);
90
91 // Creating the switch to jump to the correct exit target.
92 llvm::SwitchInst *Sw = Builder.CreateSwitch(Load, SortedExitTargets[0],
93 SortedExitTargets.size() - 1);
94 for (size_t i = 1; i < SortedExitTargets.size(); i++) {
95 BasicBlock *BB = SortedExitTargets[i];
96 Sw->addCase(TargetToValue[BB], BB);
97 }
98
99 // Fix exit branches to redirect to the new exit.
100 for (auto Exit : CR->Exits) {
101 Instruction *T = Exit->getTerminator();
102 for (auto I = succ_begin(T), E = succ_end(T); I != E; ++I)
103 if (ExitTargets.contains(*I))
104 I.getUse()->set(NewExitTarget);
105 }
106
107 CR = CR->Parent;
108 while (CR) {
109 CR->Blocks.insert(NewExitTarget);
110 CR = CR->Parent;
111 }
112
113 return true;
114}
115
116/// Run the pass on the given convergence region and sub-regions (DFS).
117/// Returns true if a region/sub-region was modified, false otherwise.
118/// This returns as soon as one region/sub-region has been modified.
119static bool runOnConvergenceRegion(LoopInfo &LI, SPIRV::ConvergenceRegion *CR) {
120 for (auto *Child : CR->Children)
121 if (runOnConvergenceRegion(LI, Child))
122 return true;
123
124 return runOnConvergenceRegionNoRecurse(LI, CR);
125}
126
127#if !NDEBUG
128/// Validates each edge exiting the region has the same destination basic
129/// block.
130static void validateRegionExits(const SPIRV::ConvergenceRegion *CR) {
131 for (auto *Child : CR->Children)
132 validateRegionExits(Child);
133
134 std::unordered_set<BasicBlock *> ExitTargets;
135 for (auto *Exit : CR->Exits) {
136 for (auto *BB : successors(Exit)) {
137 if (CR->Blocks.count(BB) == 0)
138 ExitTargets.insert(BB);
139 }
140 }
141
142 assert(ExitTargets.size() <= 1);
143}
144#endif
145
146static bool runImpl(Function &F, LoopInfo &LI,
148 auto *TopLevelRegion = RegionInfo.getWritableTopLevelRegion();
149
150 // FIXME: very inefficient method: each time a region is modified, we bubble
151 // back up, and recompute the whole convergence region tree. Once the
152 // algorithm is completed and test coverage good enough, rewrite this pass
153 // to be efficient instead of simple.
154 bool Modified = false;
155 while (runOnConvergenceRegion(LI, TopLevelRegion)) {
156 Modified = true;
157 }
158
159#if !defined(NDEBUG) || defined(EXPENSIVE_CHECKS)
160 validateRegionExits(TopLevelRegion);
161#endif
162 return Modified;
163}
164
165class SPIRVMergeRegionExitTargetsLegacy : public FunctionPass {
166public:
167 static char ID;
168
169 SPIRVMergeRegionExitTargetsLegacy() : FunctionPass(ID) {}
170
171 bool runOnFunction(Function &F) override {
172 LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
173 auto &RegionInfo = getAnalysis<SPIRVConvergenceRegionAnalysisWrapperPass>()
174 .getRegionInfo();
175 return runImpl(F, LI, RegionInfo);
176 }
177
178 void getAnalysisUsage(AnalysisUsage &AU) const override {
179 AU.addRequired<DominatorTreeWrapperPass>();
180 AU.addRequired<LoopInfoWrapperPass>();
181 AU.addRequired<SPIRVConvergenceRegionAnalysisWrapperPass>();
182
183 AU.addPreserved<SPIRVConvergenceRegionAnalysisWrapperPass>();
184 FunctionPass::getAnalysisUsage(AU);
185 }
186};
187} // namespace
188
196
197char SPIRVMergeRegionExitTargetsLegacy::ID = 0;
198
199INITIALIZE_PASS_BEGIN(SPIRVMergeRegionExitTargetsLegacy,
200 "split-region-exit-blocks",
201 "SPIRV split region exit blocks", false, false)
202INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
206
207INITIALIZE_PASS_END(SPIRVMergeRegionExitTargetsLegacy,
208 "split-region-exit-blocks",
209 "SPIRV split region exit blocks", false, false)
210
212 return new SPIRVMergeRegionExitTargetsLegacy();
213}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file defines the DenseMap class.
static bool runOnFunction(Function &F, bool PostInlining)
static bool runImpl(Function &F, const TargetLowering &TLI, const LibcallLoweringInfo &Libcalls, AssumptionCache *AC)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
This file defines the SmallPtrSet class.
an instruction to allocate memory on the stack
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
unsigned size() const
Definition DenseMap.h:174
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
Legacy analysis pass which computes a DominatorTree.
Definition Dominators.h:306
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2868
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:587
The legacy pass manager's analysis pass to compute loop information.
Definition LoopInfo.h:612
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses none()
Convenience factory function for the empty preserved set.
Definition Analysis.h:115
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
SmallVector< ConvergenceRegion * > Children
size_type size() const
Definition SmallPtrSet.h:99
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Multiway switch.
LLVM_ABI void addCase(ConstantInt *OnVal, BasicBlock *Dest)
Add an entry to the switch instruction.
Target - Wrapper for Target specific information.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
auto successors(const MachineBasicBlock *BB)
AllocaInst * createVariable(Function &F, Type *Type)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
Value * createExitVariable(BasicBlock *BB, const DenseMap< BasicBlock *, ConstantInt * > &TargetToValue)
FunctionPass * createSPIRVMergeRegionExitTargetsPass()
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.