LLVM 22.0.0git
SPIRVConvergenceRegionAnalysis.h
Go to the documentation of this file.
1//===- SPIRVConvergenceRegionAnalysis.h ------------------------*- 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// The analysis determines the convergence region for each basic block of
10// the module, and provides a tree-like structure describing the region
11// hierarchy.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVCONVERGENCEREGIONANALYSIS_H
16#define LLVM_LIB_TARGET_SPIRV_SPIRVCONVERGENCEREGIONANALYSIS_H
17
19#include "llvm/Analysis/CFG.h"
21#include "llvm/IR/Dominators.h"
22#include <optional>
23#include <unordered_set>
24
25namespace llvm {
26class IntrinsicInst;
27class SPIRVSubtarget;
28class MachineFunction;
29class MachineModuleInfo;
30
31namespace SPIRV {
32
33// Returns the first convergence intrinsic found in |BB|, |nullopt| otherwise.
34std::optional<IntrinsicInst *> getConvergenceToken(BasicBlock *BB);
35std::optional<const IntrinsicInst *> getConvergenceToken(const BasicBlock *BB);
36
37// Describes a hierarchy of convergence regions.
38// A convergence region defines a CFG for which the execution flow can diverge
39// starting from the entry block, but should reconverge back before the end of
40// the exit blocks.
42 DominatorTree &DT;
43 LoopInfo &LI;
44
45public:
46 // The parent region of this region, if any.
48 // The sub-regions contained in this region, if any.
50 // The convergence instruction linked to this region, if any.
51 std::optional<IntrinsicInst *> ConvergenceToken = std::nullopt;
52 // The only block with a predecessor outside of this region.
53 BasicBlock *Entry = nullptr;
54 // All the blocks with an edge leaving this convergence region.
56 // All the blocks that belongs to this region, including its subregions'.
58
59 // Creates a single convergence region encapsulating the whole function |F|.
61
62 // Creates a single convergence region defined by entry and exits nodes, a
63 // list of blocks, and possibly a convergence token.
65 std::optional<IntrinsicInst *> ConvergenceToken,
68
70 : DT(CR.DT), LI(CR.LI), Parent(std::move(CR.Parent)),
73 Entry(std::move(CR.Entry)), Exits(std::move(CR.Exits)),
74 Blocks(std::move(CR.Blocks)) {}
75
77
79 ConvergenceRegion(const ConvergenceRegion &other) = delete;
81
82 // Returns true if the given basic block belongs to this region, or to one of
83 // its subregion.
84 bool contains(const BasicBlock *BB) const { return Blocks.count(BB) != 0; }
85
86 void releaseMemory();
87
88 // Write to the debug output this region's hierarchy.
89 // |IndentSize| defines the number of tabs to print before any new line.
90 void dump(const unsigned IndentSize = 0) const;
91};
92
93// Holds a ConvergenceRegion hierarchy.
95 // The convergence region this structure holds.
96 ConvergenceRegion *TopLevelRegion;
97
98public:
99 ConvergenceRegionInfo() : TopLevelRegion(nullptr) {}
100
101 // Creates a new ConvergenceRegionInfo. Ownership of the TopLevelRegion is
102 // passed to this object.
104 : TopLevelRegion(TopLevelRegion) {}
105
107
110
112 : TopLevelRegion(LHS.TopLevelRegion) {
113 if (TopLevelRegion != LHS.TopLevelRegion) {
115 TopLevelRegion = LHS.TopLevelRegion;
116 }
117 LHS.TopLevelRegion = nullptr;
118 }
119
121 if (TopLevelRegion != LHS.TopLevelRegion) {
123 TopLevelRegion = LHS.TopLevelRegion;
124 }
125 LHS.TopLevelRegion = nullptr;
126 return *this;
127 }
128
130 if (TopLevelRegion == nullptr)
131 return;
132
133 TopLevelRegion->releaseMemory();
134 delete TopLevelRegion;
135 TopLevelRegion = nullptr;
136 }
137
138 const ConvergenceRegion *getTopLevelRegion() const { return TopLevelRegion; }
140 return TopLevelRegion;
141 }
142};
143
144} // namespace SPIRV
145
146// Wrapper around the function above to use it with the legacy pass manager.
149
150public:
151 static char ID;
152
154
155 void getAnalysisUsage(AnalysisUsage &AU) const override {
156 AU.setPreservesAll();
159 };
160
161 bool runOnFunction(Function &F) override;
162
164 const SPIRV::ConvergenceRegionInfo &getRegionInfo() const { return CRI; }
165};
166
167// Wrapper around the function above to use it with the new pass manager.
169 : public AnalysisInfoMixin<SPIRVConvergenceRegionAnalysis> {
171 static AnalysisKey Key;
172
173public:
175
177};
178
179namespace SPIRV {
180ConvergenceRegionInfo getConvergenceRegions(Function &F, DominatorTree &DT,
181 LoopInfo &LI);
182} // namespace SPIRV
183
184} // namespace llvm
185#endif // LLVM_LIB_TARGET_SPIRV_SPIRVCONVERGENCEREGIONANALYSIS_H
#define F(x, y, z)
Definition: MD5.cpp:55
spirv structurize SPIRV
This file defines the SmallPtrSet class.
Value * LHS
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:255
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
Legacy analysis pass which computes a DominatorTree.
Definition: Dominators.h:322
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:165
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:314
The legacy pass manager's analysis pass to compute loop information.
Definition: LoopInfo.h:597
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
const SPIRV::ConvergenceRegionInfo & getRegionInfo() const
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
Result run(Function &F, FunctionAnalysisManager &AM)
ConvergenceRegionInfo(ConvergenceRegionInfo &&LHS)
ConvergenceRegion * getWritableTopLevelRegion() const
const ConvergenceRegion * getTopLevelRegion() const
ConvergenceRegionInfo & operator=(ConvergenceRegionInfo &&LHS)
ConvergenceRegionInfo & operator=(const ConvergenceRegionInfo &LHS)=delete
ConvergenceRegionInfo(const ConvergenceRegionInfo &LHS)=delete
ConvergenceRegionInfo(ConvergenceRegion *TopLevelRegion)
SmallVector< ConvergenceRegion * > Children
ConvergenceRegion & operator=(const ConvergenceRegion &other)=delete
void dump(const unsigned IndentSize=0) const
bool contains(const BasicBlock *BB) const
ConvergenceRegion(const ConvergenceRegion &other)=delete
SmallPtrSet< BasicBlock *, 2 > Exits
std::optional< IntrinsicInst * > ConvergenceToken
ConvergenceRegion & operator=(ConvergenceRegion &&CR)=delete
SmallPtrSet< BasicBlock *, 8 > Blocks
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
ConvergenceRegionInfo getConvergenceRegions(Function &F, DominatorTree &DT, LoopInfo &LI)
std::optional< IntrinsicInst * > getConvergenceToken(BasicBlock *BB)
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:851
A CRTP mix-in that provides informational APIs needed for analysis passes.
Definition: PassManager.h:93
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition: Analysis.h:29