28#define DEBUG_TYPE "spirv-convergence-region-analysis"
35 "SPIRV convergence regions analysis",
true,
true)
40 "convergence-region",
"SPIRV convergence regions analysis",
45template <
typename BasicBlockType,
typename IntrinsicInstType>
46std::optional<IntrinsicInstType *>
48 static_assert(std::is_const_v<IntrinsicInstType> ==
49 std::is_const_v<BasicBlockType>,
50 "Constness must match between input and output.");
51 static_assert(std::is_same_v<BasicBlock, std::remove_const_t<BasicBlockType>>,
52 "Input must be a basic block.");
54 std::is_same_v<IntrinsicInst, std::remove_const_t<IntrinsicInstType>>,
55 "Output type must be an intrinsic instruction.");
86 while (Candidate != NextCandidate && NextCandidate !=
nullptr) {
87 Candidate = NextCandidate;
88 NextCandidate =
nullptr;
94 for (
auto *Child : Candidate->
Children) {
95 if (Child->Blocks.count(Entry) != 0) {
96 NextCandidate = Child;
105std::optional<IntrinsicInst *>
107 return getConvergenceTokenInternal<BasicBlock, IntrinsicInst>(BB);
110std::optional<const IntrinsicInst *>
112 return getConvergenceTokenInternal<const BasicBlock, const IntrinsicInst>(BB);
117 : DT(DT), LI(LI),
Parent(nullptr) {
118 Entry = &
F.getEntryBlock();
133 for ([[maybe_unused]]
auto *BB : this->
Exits)
142 Child->releaseMemory();
149 const std::string Indent(IndentSize,
'\t');
150 dbgs() << Indent <<
this <<
": {\n";
151 dbgs() << Indent <<
" Parent: " <<
Parent <<
"\n";
159 if (
Entry->getName() !=
"")
160 dbgs() << Indent <<
" Entry: " <<
Entry->getName() <<
"\n";
162 dbgs() << Indent <<
" Entry: " <<
Entry <<
"\n";
164 dbgs() << Indent <<
" Exits: { ";
165 for (
const auto &Exit :
Exits) {
166 if (Exit->getName() !=
"")
167 dbgs() << Exit->getName() <<
", ";
169 dbgs() << Exit <<
", ";
173 dbgs() << Indent <<
" Blocks: { ";
175 if (
Block->getName() !=
"")
182 dbgs() << Indent <<
" Children: {\n";
184 Child->dump(IndentSize + 2);
185 dbgs() << Indent <<
" }\n";
187 dbgs() << Indent <<
"}\n";
191class ConvergenceRegionAnalyzer {
194 : DT(DT), LI(LI),
F(
F) {}
205 if (!LI.isLoopHeader(To))
208 auto *L = LI.getLoopFor(To);
209 if (L->contains(From) && L->isLoopLatch(From))
215 class ForwardPostOrderTraversal
217 GraphTraits<BasicBlock *>> {
218 const ConvergenceRegionAnalyzer &Analyzer;
221 ForwardPostOrderTraversal(
const ConvergenceRegionAnalyzer &Analyzer,
223 : Analyzer(Analyzer) {
227 bool insertEdge(std::optional<BasicBlock *> From, BasicBlock *To) {
228 return !(From && Analyzer.isBackEdge(*From, To)) &&
229 Visited.insert(To).second;
233 SmallPtrSet<BasicBlock *, 16> Visited;
236 SmallPtrSet<BasicBlock *, 0>
237 findPathsToMatch(LoopInfo &LI, BasicBlock *From,
238 std::function<
bool(
const BasicBlock *)> isMatch)
const {
239 ForwardPostOrderTraversal Traversal(*
this, From);
243 SmallPtrSet<BasicBlock *, 0>
Output;
244 for (
auto *BB : Traversal) {
245 bool ReachesMatch =
false;
247 for (
unsigned i = 0; i <
Terminator->getNumSuccessors(); ++i) {
249 if (!isBackEdge(BB, To) &&
Output.contains(To))
253 if (isMatch(BB) || ReachesMatch)
259 for (
auto *LoopBB :
L->getBlocks())
267 SmallPtrSet<BasicBlock *, 2>
268 findExitNodes(
const SmallPtrSetImpl<BasicBlock *> &RegionBlocks) {
269 SmallPtrSet<BasicBlock *, 2> Exits;
271 for (
auto *
B : RegionBlocks) {
273 for (
unsigned i = 0; i <
Terminator->getNumSuccessors(); ++i) {
275 if (RegionBlocks.count(Child) == 0)
284 ConvergenceRegionInfo analyze() {
285 ConvergenceRegion *TopLevelRegion =
new ConvergenceRegion(DT, LI,
F);
286 std::queue<Loop *> ToProcess;
290 while (ToProcess.size() != 0) {
291 auto *
L = ToProcess.front();
297 L->getExitingBlocks(LoopExits);
298 if (CT.has_value()) {
299 for (
auto *Exit : LoopExits) {
300 auto N = findPathsToMatch(LI, Exit, [&CT](
const BasicBlock *
block) {
302 if (Token == std::nullopt)
304 return Token.value() == CT.value();
306 RegionBlocks.insert_range(
N);
310 auto RegionExits = findExitNodes(RegionBlocks);
311 ConvergenceRegion *
Region =
new ConvergenceRegion(
312 DT, LI, CT,
L->getHeader(), std::move(RegionBlocks),
313 std::move(RegionExits));
315 assert(
Region->Parent !=
nullptr &&
"This is impossible.");
316 Region->Parent->Children.push_back(Region);
319 return ConvergenceRegionInfo(TopLevelRegion);
332 ConvergenceRegionAnalyzer Analyzer(
F, DT, LI);
333 return Analyzer.analyze();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static ConvergenceRegion * findParentRegion(ConvergenceRegion *Start, BasicBlock *Entry)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
unify loop Fixup each natural loop to have a single exit block
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Analysis pass that exposes the LoopInfo for a function.
bool isLoopHeader(const BlockT *BB) const
SmallVector< LoopT *, 4 > getLoopsInPreorder() const
Return all of the loops in the function in preorder across the loop nests, with siblings in forward p...
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
The legacy pass manager's analysis pass to compute loop information.
AnalysisType & getAnalysis() const
getAnalysis<AnalysisType>() - This function is used by subclasses to get to the analysis information ...
CRTP base class for post-order traversal.
bool runOnFunction(Function &F) override
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
SPIRVConvergenceRegionAnalysisWrapperPass()
Result run(Function &F, FunctionAnalysisManager &AM)
SPIRV::ConvergenceRegionInfo Result
SmallVector< ConvergenceRegion * > Children
ConvergenceRegion(DominatorTree &DT, LoopInfo &LI, Function &F)
void dump(const unsigned IndentSize=0) const
SmallPtrSet< BasicBlock *, 2 > Exits
std::optional< IntrinsicInst * > ConvergenceToken
ConvergenceRegion * Parent
SmallPtrSet< BasicBlock *, 8 > Blocks
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
ConvergenceRegionInfo getConvergenceRegions(Function &F, DominatorTree &DT, LoopInfo &LI)
std::optional< IntrinsicInst * > getConvergenceToken(BasicBlock *BB)
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.
constexpr from_range_t from_range
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
Implement std::hash so that hash_code can be used in STL containers.
std::optional< IntrinsicInstType * > getConvergenceTokenInternal(BasicBlockType *BB)
A special type used by analysis passes to provide an address that identifies that particular analysis...