LLVM 24.0.0git
GenericUniformityImpl.h
Go to the documentation of this file.
1//===- GenericUniformityImpl.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// This template implementation resides in a separate file so that it
10// does not get injected into every .cpp file that includes the
11// generic header.
12//
13// DO NOT INCLUDE THIS FILE WHEN MERELY USING UNIFORMITYINFO.
14//
15// This file should only be included by files that implement a
16// specialization of the relvant templates. Currently these are:
17// - UniformityAnalysis.cpp
18// - MachineUniformityAnalysis.cpp
19//
20// Note: The DEBUG_TYPE macro should be defined before using this
21// file so that any use of LLVM_DEBUG is associated with the
22// including file rather than this file.
23//
24//===----------------------------------------------------------------------===//
25///
26/// \file
27/// \brief Implementation of uniformity analysis.
28///
29/// The algorithm is a fixed point iteration that starts with the assumption
30/// that all control flow and all values are uniform. Starting from sources of
31/// divergence (whose discovery must be implemented by a CFG- or even
32/// target-specific derived class), divergence of values is propagated from
33/// definition to uses in a straight-forward way. The main complexity lies in
34/// the propagation of the impact of divergent control flow on the divergence of
35/// values (sync dependencies).
36///
37/// NOTE: In general, no interface exists for a transform to update
38/// (Machine)UniformityInfo. Additionally, (Machine)CycleAnalysis is a
39/// transitive dependence, but it also does not provide an interface for
40/// updating itself. Given that, transforms should not preserve uniformity in
41/// their getAnalysisUsage() callback.
42///
43//===----------------------------------------------------------------------===//
44
45#ifndef LLVM_ADT_GENERICUNIFORMITYIMPL_H
46#define LLVM_ADT_GENERICUNIFORMITYIMPL_H
47
49
50#include "llvm/ADT/DenseSet.h"
51#include "llvm/ADT/STLExtras.h"
52#include "llvm/ADT/SetVector.h"
57
58#define DEBUG_TYPE "uniformity"
59
60namespace llvm {
61
62// Forward decl from llvm/CodeGen/MachineInstr.h
63class MachineInstr;
64
65/// Construct a specially modified post-order traversal of cycles.
66///
67/// The ModifiedPO is contructed using a virtually modified CFG as follows:
68///
69/// 1. The successors of pre-entry nodes (predecessors of an cycle
70/// entry that are outside the cycle) are replaced by the
71/// successors of the successors of the header.
72/// 2. Successors of the cycle header are replaced by the exit blocks
73/// of the cycle.
74///
75/// Effectively, we produce a depth-first numbering with the following
76/// properties:
77///
78/// 1. Nodes after a cycle are numbered earlier than the cycle header.
79/// 2. The header is numbered earlier than the nodes in the cycle.
80/// 3. The numbering of the nodes within the cycle forms an interval
81/// starting with the header.
82///
83/// Effectively, the virtual modification arranges the nodes in a
84/// cycle as a DAG with the header as the sole leaf, and successors of
85/// the header as the roots. A reverse traversal of this numbering has
86/// the following invariant on the unmodified original CFG:
87///
88/// Each node is visited after all its predecessors, except if that
89/// predecessor is the cycle header.
90///
91template <typename ContextT> class ModifiedPostOrder {
92public:
93 using BlockT = typename ContextT::BlockT;
94 using FunctionT = typename ContextT::FunctionT;
95 using DominatorTreeT = typename ContextT::DominatorTreeT;
96
98 using const_iterator = typename std::vector<BlockT *>::const_iterator;
99
100 ModifiedPostOrder(const ContextT &C) : Context(C) {}
101
102 bool empty() const { return Order.empty(); }
103 size_t size() const { return Order.size(); }
104
105 void clear() { Order.clear(); }
106 void compute(const CycleInfoT &CI);
107
108 unsigned count(BlockT *BB) const {
109 unsigned Num = GraphTraits<BlockT *>::getNumber(BB);
110 return Num < POIndex.size() && POIndex[Num] != InvalidIndex;
111 }
112 const BlockT *operator[](size_t Idx) const { return Order[Idx]; }
113
114 void appendBlock(const BlockT &BB, bool IsReducibleCycleHeader = false) {
115 unsigned Num = GraphTraits<const BlockT *>::getNumber(&BB);
116 POIndex[Num] = Order.size();
117 Order.push_back(&BB);
118 LLVM_DEBUG(dbgs() << "ModifiedPO(" << POIndex[Num]
119 << "): " << Context.print(&BB) << "\n");
120 if (IsReducibleCycleHeader)
121 ReducibleCycleHeaders.insert(&BB);
122 }
123
124 unsigned getIndex(const BlockT *BB) const {
126 assert(Num < POIndex.size() && POIndex[Num] != InvalidIndex);
127 return POIndex[Num];
128 }
129
130 bool isReducibleCycleHeader(const BlockT *BB) const {
131 return ReducibleCycleHeaders.contains(BB);
132 }
133
134private:
135 static constexpr unsigned InvalidIndex = -1u;
136
138 SmallVector<unsigned> POIndex;
139 SmallPtrSet<const BlockT *, 32> ReducibleCycleHeaders;
140 const ContextT &Context;
141
142 void computeCyclePO(const CycleInfoT &CI, CycleRef C,
144
145 void computeStackPO(SmallVectorImpl<const BlockT *> &Stack,
146 const CycleInfoT &CI, CycleRef C,
148};
149
150template <typename> class DivergencePropagator;
151
152/// \class GenericSyncDependenceAnalysis
153///
154/// \brief Locate join blocks for disjoint paths starting at a divergent branch.
155///
156/// An analysis per divergent branch that returns the set of basic
157/// blocks whose phi nodes become divergent due to divergent control.
158/// These are the blocks that are reachable by two disjoint paths from
159/// the branch, or cycle exits reachable along a path that is disjoint
160/// from a path to the cycle latch.
161
162// --- Above line is not a doxygen comment; intentionally left blank ---
163//
164// Originally implemented in SyncDependenceAnalysis.cpp for DivergenceAnalysis.
165//
166// The SyncDependenceAnalysis is used in the UniformityAnalysis to model
167// control-induced divergence in phi nodes.
168//
169// -- Reference --
170// The algorithm is an extension of Section 5 of
171//
172// An abstract interpretation for SPMD divergence
173// on reducible control flow graphs.
174// Julian Rosemann, Simon Moll and Sebastian Hack
175// POPL '21
176//
177//
178// -- Sync dependence --
179// Sync dependence characterizes the control flow aspect of the
180// propagation of branch divergence. For example,
181//
182// %cond = icmp slt i32 %tid, 10
183// br i1 %cond, label %then, label %else
184// then:
185// br label %merge
186// else:
187// br label %merge
188// merge:
189// %a = phi i32 [ 0, %then ], [ 1, %else ]
190//
191// Suppose %tid holds the thread ID. Although %a is not data dependent on %tid
192// because %tid is not on its use-def chains, %a is sync dependent on %tid
193// because the branch "br i1 %cond" depends on %tid and affects which value %a
194// is assigned to.
195//
196//
197// -- Reduction to SSA construction --
198// There are two disjoint paths from A to X, if a certain variant of SSA
199// construction places a phi node in X under the following set-up scheme.
200//
201// This variant of SSA construction ignores incoming undef values.
202// That is paths from the entry without a definition do not result in
203// phi nodes.
204//
205// entry
206// / \
207// A \
208// / \ Y
209// B C /
210// \ / \ /
211// D E
212// \ /
213// F
214//
215// Assume that A contains a divergent branch. We are interested
216// in the set of all blocks where each block is reachable from A
217// via two disjoint paths. This would be the set {D, F} in this
218// case.
219// To generally reduce this query to SSA construction we introduce
220// a virtual variable x and assign to x different values in each
221// successor block of A.
222//
223// entry
224// / \
225// A \
226// / \ Y
227// x = 0 x = 1 /
228// \ / \ /
229// D E
230// \ /
231// F
232//
233// Our flavor of SSA construction for x will construct the following
234//
235// entry
236// / \
237// A \
238// / \ Y
239// x0 = 0 x1 = 1 /
240// \ / \ /
241// x2 = phi E
242// \ /
243// x3 = phi
244//
245// The blocks D and F contain phi nodes and are thus each reachable
246// by two disjoint paths from A.
247//
248// -- Remarks --
249// * In case of cycle exits we need to check for temporal divergence.
250// To this end, we check whether the definition of x differs between the
251// cycle exit and the cycle header (_after_ SSA construction).
252//
253// * In the presence of irreducible control flow, the fixed point is
254// reached only after multiple iterations. This is because labels
255// reaching the header of a cycle must be repropagated through the
256// cycle. This is true even in a reducible cycle, since the labels
257// may have been produced by a nested irreducible cycle.
258//
259// * Note that SyncDependenceAnalysis is not concerned with the points
260// of convergence in an irreducible cycle. Its only purpose is to
261// identify join blocks. The "diverged entry" criterion is
262// separately applied on join blocks to determine if an entire
263// irreducible cycle is assumed to be divergent.
264//
265// * Relevant related work:
266// A simple algorithm for global data flow analysis problems.
267// Matthew S. Hecht and Jeffrey D. Ullman.
268// SIAM Journal on Computing, 4(4):519–532, December 1975.
269//
270template <typename ContextT> class GenericSyncDependenceAnalysis {
271public:
272 using BlockT = typename ContextT::BlockT;
273 using DominatorTreeT = typename ContextT::DominatorTreeT;
274 using FunctionT = typename ContextT::FunctionT;
275 using ValueRefT = typename ContextT::ValueRefT;
276 using InstructionT = typename ContextT::InstructionT;
277
279
282
283 /// Information discovered by the sync dependence analysis for each
284 /// divergent branch.
286 // Join points of diverged paths.
288 // Divergent cycle exits
290 };
291
293
294 GenericSyncDependenceAnalysis(const ContextT &Context,
295 const DominatorTreeT &DT, const CycleInfoT &CI);
296
297 /// \brief Computes divergent join points and cycle exits caused by branch
298 /// divergence in \p Term.
299 ///
300 /// This returns a pair of sets:
301 /// * The set of blocks which are reachable by disjoint paths from
302 /// \p Term.
303 /// * The set also contains cycle exits if there two disjoint paths:
304 /// one from \p Term to the cycle exit and another from \p Term to
305 /// the cycle header.
306 const DivergenceDescriptor &getJoinBlocks(const BlockT *DivTermBlock);
307
308private:
309 static inline DivergenceDescriptor EmptyDivergenceDesc;
310
311 ModifiedPO CyclePO;
312
313 const DominatorTreeT &DT;
314 const CycleInfoT &CI;
315
317 CachedControlDivDescs;
318};
319
320/// \brief Analysis that identifies uniform values in a data-parallel
321/// execution.
322///
323/// This analysis propagates divergence in a data-parallel context
324/// from sources of divergence to all users. It can be instantiated
325/// for an IR that provides a suitable SSAContext.
326template <typename ContextT> class GenericUniformityAnalysisImpl {
327public:
328 using BlockT = typename ContextT::BlockT;
329 using FunctionT = typename ContextT::FunctionT;
330 using ValueRefT = typename ContextT::ValueRefT;
331 using ConstValueRefT = typename ContextT::ConstValueRefT;
332 using UseT = typename ContextT::UseT;
333 using InstructionT = typename ContextT::InstructionT;
334 using DominatorTreeT = typename ContextT::DominatorTreeT;
335
337
340 typename SyncDependenceAnalysisT::DivergenceDescriptor;
341
343 std::tuple<ConstValueRefT, InstructionT *, CycleRef>;
344
347 : Context(CI.getSSAContext()), F(*Context.getFunction()), CI(CI),
348 TTI(TTI), DT(DT), SDA(Context, DT, CI) {}
349
351
352 const FunctionT &getFunction() const { return F; }
353
354 const CycleInfoT &getCycleInfo() const { return CI; }
355
356 /// \brief Mark \p UniVal as a value that is always uniform.
357 void addUniformOverride(const InstructionT &Instr);
358
359 /// \brief Examine \p I for divergent outputs and add to the worklist.
360 void markDivergent(const InstructionT &I);
361
362 /// \brief Mark \p DivVal as a divergent value by removing it from
363 /// UniformValues. \returns Whether the tracked divergence state of
364 /// \p DivVal changed.
365 bool markDivergent(ConstValueRefT DivVal);
366
367 /// \brief Mark outputs of \p Instr as divergent.
368 /// \returns Whether the tracked divergence state of any output has changed.
369 bool markDefsDivergent(const InstructionT &Instr);
370
371 /// \brief Propagate divergence to all instructions in the region.
372 /// Divergence is seeded by calls to \p markDivergent.
373 void compute();
374
375 /// \brief Whether \p Val will always return a uniform value regardless of its
376 /// operands
377 bool isAlwaysUniform(const InstructionT &Instr) const;
378
379 bool hasDivergentDefs(const InstructionT &I) const;
380
382 assert(I.isTerminator() && "Expected a terminator instruction!");
383 return DivergentTermBlocks.contains(I.getParent());
384 };
385
386 /// \brief Whether \p Val is divergent at its definition.
387 /// When the target has no branch divergence, compute() is never called
388 /// and everything is uniform. Otherwise, values not in UniformValues
389 /// (e.g. newly created) are conservatively treated as divergent.
392 return false;
393 // Only values that were present during analysis are tracked in
394 // UniformValues (Instructions/Arguments for IR, Registers for MIR).
395 // Other values (e.g. constants, globals) are always uniform but are
396 // not added to UniformValues; this check avoids false divergence.
397 if (ContextT::isAlwaysUniform(V))
398 return false;
399 return !UniformValues.contains(V);
400 }
401
402 bool isDivergentUse(const UseT &U) const;
403
404 bool hasDivergentTerminator(const BlockT &B) const {
405 return DivergentTermBlocks.contains(&B);
406 }
407
408 /// Call before erasing \p V, or a later instruction reusing its address
409 /// may be misclassified as uniform.
411
412 void print(raw_ostream &Out) const;
413
414 /// Print divergent arguments and return true if any were found.
415 /// IR specialization iterates F.args(); default is a no-op.
416 bool printDivergentArgs(raw_ostream &Out) const;
417
419
421
422 /// Check if an instruction with Custom uniformity can be proven uniform
423 /// based on its operands. This queries the target-specific callback.
424 bool isCustomUniform(const InstructionT &I) const;
425
426 /// \brief Add an instruction that requires custom uniformity analysis.
428
429protected:
430 const ContextT &Context;
431 const FunctionT &F;
433 const TargetTransformInfo *TTI = nullptr;
434
435 // Whether the target has branch divergence. Set at the start of compute(),
436 // which is only called when the target has branch divergence. When false,
437 // isDivergent() returns false for all values.
439
441
442 // Values known to be uniform. Populated in initialize() with all values,
443 // then values are removed as divergence is propagated. After analysis,
444 // values not in this set are conservatively treated as divergent.
446
447 // Internal worklist for divergence propagation.
448 std::vector<const InstructionT *> Worklist;
449
450 // Set of instructions that require custom uniformity analysis based on
451 // operand uniformity.
453
454 /// \brief Mark \p Term as divergent and push all Instructions that become
455 /// divergent as a result on the worklist.
456 void analyzeControlDivergence(const InstructionT &Term);
457
458private:
459 const DominatorTreeT &DT;
460
461 // Recognized cycles with divergent exits.
462 SmallSetVector<CycleRef, 8> DivergentExitCycles;
463
464 // Cycles assumed to be divergent.
465 //
466 // We don't use a set here because every insertion needs an explicit
467 // traversal of all existing members.
468 SmallVector<CycleRef> AssumedDivergent;
469
470 // The SDA links divergent branches to divergent control-flow joins.
472
473 // Set of known-uniform values.
475
476 /// \brief Mark all nodes in \p JoinBlock as divergent and push them on
477 /// the worklist.
478 void taintAndPushAllDefs(const BlockT &JoinBlock);
479
480 /// \brief Mark all phi nodes in \p JoinBlock as divergent and push them on
481 /// the worklist.
482 void taintAndPushPhiNodes(const BlockT &JoinBlock);
483
484 /// \brief Identify all Instructions that become divergent because \p DivExit
485 /// is a divergent cycle exit of \p DivCycle. Mark those instructions as
486 /// divergent and push them on the worklist.
487 void propagateCycleExitDivergence(const BlockT &DivExit, CycleRef DivCycle);
488
489 /// Mark as divergent all external uses of values defined in \p DefCycle.
490 void analyzeCycleExitDivergence(CycleRef DefCycle);
491
492 /// \brief Mark as divergent all uses of \p I that are outside \p DefCycle.
493 void propagateTemporalDivergence(const InstructionT &I, CycleRef DefCycle);
494
495 /// \brief Push all users of \p Val (in the region) to the worklist.
496 void pushUsers(const InstructionT &I);
497 void pushUsers(ConstValueRefT V);
498
499 bool usesValueFromCycle(const InstructionT &I, CycleRef DefCycle) const;
500
501 /// \brief Whether \p Def is divergent when read in \p ObservingBlock.
502 bool isTemporalDivergent(const BlockT &ObservingBlock,
503 const InstructionT &Def) const;
504};
505
506template <typename ImplT>
508 delete Impl;
509}
510
511/// Compute divergence starting with a divergent branch.
512template <typename ContextT> class DivergencePropagator {
513public:
514 using BlockT = typename ContextT::BlockT;
515 using DominatorTreeT = typename ContextT::DominatorTreeT;
516 using FunctionT = typename ContextT::FunctionT;
517 using ValueRefT = typename ContextT::ValueRefT;
518
520
524 typename SyncDependenceAnalysisT::DivergenceDescriptor;
525
530 const ContextT &Context;
531
532 // Track blocks that receive a new label. Every time we relabel a
533 // cycle header, we make another pass over the modified post-order in
534 // order to propagate the header label. The bit vector also allows
535 // us to skip labels that have not changed.
537
538 // divergent join and cycle exit descriptor.
539 std::unique_ptr<DivergenceDescriptorT> DivDesc;
540
541 // Dominating definition at each block on diverged paths, indexed by block
542 // number. Transient to this propagation; not stored in the descriptor.
543 // * label(B) == C : C is the dominating definition at B
544 // * label(B) == nullptr : we haven't seen B yet
545 // * label(B) == B : B is a join of disjoint paths, an immediate
546 // successor of the divergent term, or the term
547 // itself
549
550 const BlockT *&label(const BlockT *BB) {
552 }
553
555 const CycleInfoT &CI, const BlockT &DivTermBlock)
557 Context(CI.getSSAContext()), DivDesc(new DivergenceDescriptorT),
559 GraphTraits<const FunctionT *>::getMaxNumber(CI.getFunction()),
560 nullptr) {}
561
563 Out << "Propagator::BlockLabels {\n";
564 for (int BlockIdx = (int)CyclePOT.size() - 1; BlockIdx >= 0; --BlockIdx) {
565 const auto *Block = CyclePOT[BlockIdx];
566 const auto *Label = label(Block);
567 Out << Context.print(Block) << "(" << BlockIdx << ") : ";
568 if (!Label) {
569 Out << "<null>\n";
570 } else {
571 Out << Context.print(Label) << "\n";
572 }
573 }
574 Out << "}\n";
575 }
576
577 // Push a definition (\p PushedLabel) to \p SuccBlock and return whether this
578 // causes a divergent join.
579 bool computeJoin(const BlockT &SuccBlock, const BlockT &PushedLabel) {
580 const auto *OldLabel = label(&SuccBlock);
581
582 LLVM_DEBUG(dbgs() << "labeling " << Context.print(&SuccBlock) << ":\n"
583 << "\tpushed label: " << Context.print(&PushedLabel)
584 << "\n"
585 << "\told label: " << Context.print(OldLabel) << "\n");
586
587 // Early exit if there is no change in the label.
588 if (OldLabel == &PushedLabel)
589 return false;
590
591 if (OldLabel != &SuccBlock) {
592 auto SuccIdx = CyclePOT.getIndex(&SuccBlock);
593 // Assigning a new label, mark this in FreshLabels.
594 LLVM_DEBUG(dbgs() << "\tfresh label: " << SuccIdx << "\n");
595 FreshLabels.set(SuccIdx);
596 }
597
598 // This is not a join if the succ was previously unlabeled.
599 if (!OldLabel) {
600 LLVM_DEBUG(dbgs() << "\tnew label: " << Context.print(&PushedLabel)
601 << "\n");
602 label(&SuccBlock) = &PushedLabel;
603 return false;
604 }
605
606 // This is a new join. Label the join block as itself, and not as
607 // the pushed label.
608 LLVM_DEBUG(dbgs() << "\tnew label: " << Context.print(&SuccBlock) << "\n");
609 label(&SuccBlock) = &SuccBlock;
610
611 return true;
612 }
613
614 // visiting a virtual cycle exit edge from the cycle header --> temporal
615 // divergence on join
616 bool visitCycleExitEdge(const BlockT &ExitBlock, const BlockT &Label) {
617 if (!computeJoin(ExitBlock, Label))
618 return false;
619
620 // Identified a divergent cycle exit
621 DivDesc->CycleDivBlocks.insert(&ExitBlock);
622 LLVM_DEBUG(dbgs() << "\tDivergent cycle exit: " << Context.print(&ExitBlock)
623 << "\n");
624 return true;
625 }
626
627 // process \p SuccBlock with reaching definition \p Label
628 bool visitEdge(const BlockT &SuccBlock, const BlockT &Label) {
629 if (!computeJoin(SuccBlock, Label))
630 return false;
631
632 // Divergent, disjoint paths join.
633 DivDesc->JoinDivBlocks.insert(&SuccBlock);
634 LLVM_DEBUG(dbgs() << "\tDivergent join: " << Context.print(&SuccBlock)
635 << "\n");
636 return true;
637 }
638
639 std::unique_ptr<DivergenceDescriptorT> computeJoinPoints() {
641
642 LLVM_DEBUG(dbgs() << "SDA:computeJoinPoints: "
643 << Context.print(&DivTermBlock) << "\n");
644
645 int DivTermIdx = CyclePOT.getIndex(&DivTermBlock);
646 CycleRef DivTermCycle = CI.getCycle(&DivTermBlock);
647
648 // Locate the largest ancestor cycle that is not reducible and does not
649 // contain a reducible ancestor. This is done with a lambda that is defined
650 // and invoked in the same statement.
651 CycleRef IrreducibleAncestor = [this](CycleRef C) -> CycleRef {
652 if (!C)
653 return CycleRef();
654 if (CI.isReducible(C))
655 return CycleRef();
656 while (CycleRef P = CI.getParentCycle(C)) {
657 if (CI.isReducible(P))
658 return C;
659 C = P;
660 }
661 assert(!CI.getParentCycle(C));
662 assert(!CI.isReducible(C));
663 return C;
664 }(DivTermCycle);
665
666 // Bootstrap with branch targets
667 for (const auto *SuccBlock : successors(&DivTermBlock)) {
668 if (DivTermCycle && !CI.contains(DivTermCycle, SuccBlock)) {
669 // If DivTerm exits the cycle immediately, computeJoin() might
670 // not reach SuccBlock with a different label. We need to
671 // check for this exit now.
672 DivDesc->CycleDivBlocks.insert(SuccBlock);
673 LLVM_DEBUG(dbgs() << "\tImmediate divergent cycle exit: "
674 << Context.print(SuccBlock) << "\n");
675 }
676 visitEdge(*SuccBlock, *SuccBlock);
677 }
678
679 // Technically propagation can continue until it reaches the last node.
680 //
681 // For efficiency, propagation can stop if FreshLabels.count()==1. But
682 // For irreducible cycles, let propagation continue until it reaches
683 // out of irreducible cycles (see code for details.)
684 while (true) {
685 auto BlockIdx = FreshLabels.find_last();
686 if (BlockIdx == -1)
687 break;
688
689 const auto *Block = CyclePOT[BlockIdx];
690 // If no irreducible cycle, stop if freshLabels.count() == 1 and Block
691 // is the IPD. If it is in any irreducible cycle, continue propagation.
692 if (FreshLabels.count() == 1 &&
693 (!IrreducibleAncestor || !CI.contains(IrreducibleAncestor, Block)))
694 break;
695
696 LLVM_DEBUG(dbgs() << "Current labels:\n"; printDefs(dbgs()));
697
698 FreshLabels.reset(BlockIdx);
699 if (BlockIdx == DivTermIdx) {
700 LLVM_DEBUG(dbgs() << "Skipping DivTermBlock\n");
701 continue;
702 }
703
704 LLVM_DEBUG(dbgs() << "visiting " << Context.print(Block) << " at index "
705 << BlockIdx << "\n");
706
707 const auto *Label = label(Block);
708 assert(Label);
709
710 // If the current block is the header of a reducible cycle, then the label
711 // should be propagated to the cycle exits. If this cycle contains the
712 // branch, then those exits are divergent exits. This is true for any DFS.
713 //
714 // If some DFS has a reducible cycle C with header H, then for
715 // any other DFS, H is the header of a cycle C' that is a
716 // superset of C.
717 //
718 // - For a divergent branch inside the subgraph C, any join node inside
719 // C is either H, or some node encountered by paths within C, without
720 // passing through H.
721 //
722 // - For a divergent branch outside the subgraph C, H is the only node
723 // in C reachable from multiple paths since it is the only entry to C.
724 LLVM_DEBUG(dbgs() << "Check for reducible cycle: " << Context.print(Block)
725 << '\n');
726 if (CyclePOT.isReducibleCycleHeader(Block)) {
727 CycleRef BlockCycle = CI.getCycle(Block);
728 LLVM_DEBUG(dbgs() << CI.print(BlockCycle) << '\n');
729 SmallVector<BlockT *, 4> BlockCycleExits;
730 CI.getExitBlocks(BlockCycle, BlockCycleExits);
731 bool BranchIsInside = CI.contains(BlockCycle, &DivTermBlock);
732 for (auto *BlockCycleExit : BlockCycleExits) {
733 if (BranchIsInside)
734 visitCycleExitEdge(*BlockCycleExit, *Label);
735 // Propagate the label to the exit block.
736 visitEdge(*BlockCycleExit, *Label);
737 }
738 } else {
739 for (const auto *SuccBlock : successors(Block))
740 visitEdge(*SuccBlock, *Label);
741 }
742 }
743
744 LLVM_DEBUG(dbgs() << "Final labeling:\n"; printDefs(dbgs()));
745
746 // Check every cycle containing DivTermBlock for exit divergence.
747 // A cycle has exit divergence if the label of an exit block does
748 // not match the label of its header.
749 for (auto C = CI.getCycle(&DivTermBlock); C; C = CI.getParentCycle(C)) {
750 if (CI.isReducible(C)) {
751 // The exit divergence of a reducible cycle is recorded while
752 // propagating labels.
753 continue;
754 }
756 CI.getExitBlocks(C, Exits);
757 auto *Header = CI.getHeader(C);
758 auto *HeaderLabel = label(Header);
759 for (const auto *Exit : Exits) {
760 if (label(Exit) != HeaderLabel) {
761 // Identified a divergent cycle exit
762 DivDesc->CycleDivBlocks.insert(Exit);
763 LLVM_DEBUG(dbgs() << "\tDivergent cycle exit: " << Context.print(Exit)
764 << "\n");
765 }
766 }
767 }
768
769 return std::move(DivDesc);
770 }
771};
772
773template <typename ContextT>
775 const ContextT &Context, const DominatorTreeT &DT, const CycleInfoT &CI)
776 : CyclePO(Context), DT(DT), CI(CI) {
777 CyclePO.compute(CI);
778}
779
780template <typename ContextT>
782 const BlockT *DivTermBlock) -> const DivergenceDescriptor & {
783 // trivial case
784 if (succ_size(DivTermBlock) <= 1) {
785 return EmptyDivergenceDesc;
786 }
787
788 // already available in cache?
789 auto ItCached = CachedControlDivDescs.find(DivTermBlock);
790 if (ItCached != CachedControlDivDescs.end())
791 return *ItCached->second;
792
793 // compute all join points
794 DivergencePropagatorT Propagator(CyclePO, DT, CI, *DivTermBlock);
795 auto DivDesc = Propagator.computeJoinPoints();
796
797 auto PrintBlockSet = [&](ConstBlockSet &Blocks) {
798 return Printable([&](raw_ostream &Out) {
799 Out << "[";
800 ListSeparator LS;
801 for (const auto *BB : Blocks) {
802 Out << LS << CI.getSSAContext().print(BB);
803 }
804 Out << "]\n";
805 });
806 };
807
809 dbgs() << "\nResult (" << CI.getSSAContext().print(DivTermBlock)
810 << "):\n JoinDivBlocks: " << PrintBlockSet(DivDesc->JoinDivBlocks)
811 << " CycleDivBlocks: " << PrintBlockSet(DivDesc->CycleDivBlocks)
812 << "\n");
813 (void)PrintBlockSet;
814
815 auto ItInserted =
816 CachedControlDivDescs.try_emplace(DivTermBlock, std::move(DivDesc));
817 assert(ItInserted.second);
818 return *ItInserted.first->second;
819}
820
821template <typename ContextT>
823 const InstructionT &I) {
824 if (isAlwaysUniform(I))
825 return;
826 // For custom uniformity candidates, check if the instruction can be
827 // proven uniform based on which operands are uniform/divergent.
828 // The candidate will be re-evaluated as operands become divergent.
829 if (CustomUniformityCandidates.contains(&I)) {
830 if (isCustomUniform(I))
831 return;
832 }
833 bool Marked = false;
834 if (I.isTerminator()) {
835 Marked = DivergentTermBlocks.insert(I.getParent()).second;
836 if (Marked) {
837 LLVM_DEBUG(dbgs() << "marked divergent term block: "
838 << Context.print(I.getParent()) << "\n");
839 }
840 } else {
841 Marked = markDefsDivergent(I);
842 }
843
844 if (Marked)
845 Worklist.push_back(&I);
846}
847
848template <typename ContextT>
850 ConstValueRefT Val) {
851 if (UniformValues.erase(Val)) {
852 LLVM_DEBUG(dbgs() << "marked divergent: " << Context.print(Val) << "\n");
853 return true;
854 }
855 return false;
856}
857
858template <typename ContextT>
860 const InstructionT &Instr) {
861 UniformOverrides.insert(&Instr);
862}
863
864template <typename ContextT>
869
870// Mark as divergent all external uses of values defined in \p DefCycle.
871//
872// A value V defined by a block B inside \p DefCycle may be used outside the
873// cycle only if the use is a PHI in some exit block, or B dominates some exit
874// block. Thus, we check uses as follows:
875//
876// - Check all PHIs in all exit blocks for inputs defined inside \p DefCycle.
877// - For every block B inside \p DefCycle that dominates at least one exit
878// block, check all uses outside \p DefCycle.
879//
880// FIXME: This function does not distinguish between divergent and uniform
881// exits. For each divergent exit, only the values that are live at that exit
882// need to be propagated as divergent at their use outside the cycle.
883template <typename ContextT>
884void GenericUniformityAnalysisImpl<ContextT>::analyzeCycleExitDivergence(
885 CycleRef DefCycle) {
887 CI.getExitBlocks(DefCycle, Exits);
888 for (auto *Exit : Exits) {
889 for (auto &Phi : Exit->phis()) {
890 if (usesValueFromCycle(Phi, DefCycle)) {
891 markDivergent(Phi);
892 }
893 }
894 }
895
896 for (auto *BB : CI.getBlocks(DefCycle)) {
897 if (!llvm::any_of(Exits,
898 [&](BlockT *Exit) { return DT.dominates(BB, Exit); }))
899 continue;
900 for (auto &II : *BB) {
901 propagateTemporalDivergence(II, DefCycle);
902 }
903 }
904}
905
906template <typename ContextT>
907void GenericUniformityAnalysisImpl<ContextT>::propagateCycleExitDivergence(
908 const BlockT &DivExit, CycleRef InnerDivCycle) {
909 LLVM_DEBUG(dbgs() << "\tpropCycleExitDiv " << Context.print(&DivExit)
910 << "\n");
911 CycleRef DivCycle = InnerDivCycle;
912 CycleRef OuterDivCycle = DivCycle;
913
914 // Find outer-most cycle that does not contain \p DivExit
915 while (DivCycle && !CI.contains(DivCycle, &DivExit)) {
916 LLVM_DEBUG(dbgs() << " Found exiting cycle: "
917 << Context.print(CI.getHeader(DivCycle)) << "\n");
918 OuterDivCycle = DivCycle;
919 DivCycle = CI.getParentCycle(DivCycle);
920 }
921 LLVM_DEBUG(dbgs() << "\tOuter-most exiting cycle: "
922 << Context.print(CI.getHeader(OuterDivCycle)) << "\n");
923
924 if (!DivergentExitCycles.insert(OuterDivCycle))
925 return;
926
927 // Exit divergence does not matter if the cycle itself is assumed to
928 // be divergent.
929 for (auto C : AssumedDivergent) {
930 if (CI.contains(C, OuterDivCycle))
931 return;
932 }
933
934 analyzeCycleExitDivergence(OuterDivCycle);
935}
936
937template <typename ContextT>
938void GenericUniformityAnalysisImpl<ContextT>::taintAndPushAllDefs(
939 const BlockT &BB) {
940 LLVM_DEBUG(dbgs() << "taintAndPushAllDefs " << Context.print(&BB) << "\n");
941 for (const auto &I : instrs(BB)) {
942 // Terminators do not produce values; they are divergent only if
943 // the condition is divergent. That is handled when the divergent
944 // condition is placed in the worklist.
945 if (I.isTerminator())
946 break;
947
948 markDivergent(I);
949 }
950}
951
952/// Mark divergent phi nodes in a join block
953template <typename ContextT>
954void GenericUniformityAnalysisImpl<ContextT>::taintAndPushPhiNodes(
955 const BlockT &JoinBlock) {
956 LLVM_DEBUG(dbgs() << "taintAndPushPhiNodes in " << Context.print(&JoinBlock)
957 << "\n");
958 for (const auto &Phi : JoinBlock.phis()) {
959 // FIXME: The non-undef value is not constant per se; it just happens to be
960 // uniform and may not dominate this PHI. So assuming that the same value
961 // reaches along all incoming edges may itself be undefined behaviour. This
962 // particular interpretation of the undef value was added to
963 // DivergenceAnalysis in the following review:
964 //
965 // https://reviews.llvm.org/D19013
966 if (ContextT::isConstantOrUndefValuePhi(Phi))
967 continue;
968 markDivergent(Phi);
969 }
970}
971
972/// Add \p Candidate to \p Cycles if it is not already contained in \p Cycles.
973///
974/// \return true iff \p Candidate was added to \p Cycles.
975template <typename CycleInfoT>
976bool insertIfNotContained(const CycleInfoT &CI, SmallVector<CycleRef> &Cycles,
977 CycleRef Candidate) {
978 if (llvm::any_of(Cycles,
979 [&](CycleRef C) { return CI.contains(C, Candidate); }))
980 return false;
981 Cycles.push_back(Candidate);
982 return true;
983}
984
985/// Return the outermost cycle made divergent by branch outside it.
986///
987/// If two paths that diverged outside an irreducible cycle join
988/// inside that cycle, then that whole cycle is assumed to be
989/// divergent. This does not apply if the cycle is reducible.
990template <typename CycleInfoT, typename BlockT>
991CycleRef getExtDivCycle(const CycleInfoT &CI, CycleRef Cycle,
992 const BlockT *DivTermBlock, const BlockT *JoinBlock) {
993 assert(Cycle);
994 assert(CI.contains(Cycle, JoinBlock));
995
996 if (CI.contains(Cycle, DivTermBlock))
997 return CycleRef();
998
999 CycleRef OriginalCycle = Cycle;
1000 CycleRef Parent = CI.getParentCycle(Cycle);
1001 while (Parent && !CI.contains(Parent, DivTermBlock)) {
1002 Cycle = Parent;
1003 Parent = CI.getParentCycle(Cycle);
1004 }
1005
1006 // If the original cycle is not the outermost cycle, then the outermost cycle
1007 // is irreducible. If the outermost cycle were reducible, then external
1008 // diverged paths would not reach the original inner cycle.
1009 (void)OriginalCycle;
1010 assert(Cycle == OriginalCycle || !CI.isReducible(Cycle));
1011
1012 if (CI.isReducible(Cycle)) {
1013 assert(CI.getHeader(Cycle) == JoinBlock);
1014 return CycleRef();
1015 }
1016
1017 LLVM_DEBUG(dbgs() << "cycle made divergent by external branch\n");
1018 return Cycle;
1019}
1020
1021/// Return the outermost cycle made divergent by branch inside it.
1022///
1023/// This checks the "diverged entry" criterion defined in the
1024/// docs/ConvergenceAnalysis.html.
1025template <typename ContextT, typename CycleInfoT, typename BlockT,
1026 typename DominatorTreeT>
1027CycleRef getIntDivCycle(const CycleInfoT &CI, CycleRef Cycle,
1028 const BlockT *DivTermBlock, const BlockT *JoinBlock,
1029 const DominatorTreeT &DT, ContextT &Context) {
1030 LLVM_DEBUG(dbgs() << "examine join " << Context.print(JoinBlock)
1031 << " for internal branch " << Context.print(DivTermBlock)
1032 << "\n");
1033 if (DT.properlyDominates(DivTermBlock, JoinBlock))
1034 return CycleRef();
1035
1036 // Find the smallest common cycle, if one exists.
1037 assert(Cycle && CI.contains(Cycle, JoinBlock));
1038 while (Cycle && !CI.contains(Cycle, DivTermBlock)) {
1039 Cycle = CI.getParentCycle(Cycle);
1040 }
1041 if (!Cycle || CI.isReducible(Cycle))
1042 return CycleRef();
1043
1044 if (DT.properlyDominates(CI.getHeader(Cycle), JoinBlock))
1045 return CycleRef();
1046
1047 LLVM_DEBUG(dbgs() << " header " << Context.print(CI.getHeader(Cycle))
1048 << " does not dominate join\n");
1049
1050 CycleRef Parent = CI.getParentCycle(Cycle);
1051 while (Parent && !DT.properlyDominates(CI.getHeader(Parent), JoinBlock)) {
1052 LLVM_DEBUG(dbgs() << " header " << Context.print(CI.getHeader(Parent))
1053 << " does not dominate join\n");
1054 Cycle = Parent;
1055 Parent = CI.getParentCycle(Parent);
1056 }
1057
1058 LLVM_DEBUG(dbgs() << " cycle made divergent by internal branch\n");
1059 return Cycle;
1060}
1061
1062template <typename ContextT, typename CycleInfoT, typename BlockT,
1063 typename DominatorTreeT>
1064CycleRef
1065getOutermostDivergentCycle(const CycleInfoT &CI, CycleRef Cycle,
1066 const BlockT *DivTermBlock, const BlockT *JoinBlock,
1067 const DominatorTreeT &DT, ContextT &Context) {
1068 if (!Cycle)
1069 return CycleRef();
1070
1071 // First try to expand Cycle to the largest that contains JoinBlock
1072 // but not DivTermBlock.
1073 CycleRef Ext = getExtDivCycle(CI, Cycle, DivTermBlock, JoinBlock);
1074
1075 // Continue expanding to the largest cycle that contains both.
1076 CycleRef Int =
1077 getIntDivCycle(CI, Cycle, DivTermBlock, JoinBlock, DT, Context);
1078
1079 if (Int)
1080 return Int;
1081 return Ext;
1082}
1083
1084template <typename ContextT>
1085bool GenericUniformityAnalysisImpl<ContextT>::isTemporalDivergent(
1086 const BlockT &ObservingBlock, const InstructionT &Def) const {
1087 const BlockT *DefBlock = Def.getParent();
1088 for (auto C = CI.getCycle(DefBlock); C && !CI.contains(C, &ObservingBlock);
1089 C = CI.getParentCycle(C)) {
1090 if (DivergentExitCycles.contains(C)) {
1091 return true;
1092 }
1093 }
1094 return false;
1095}
1096
1097template <typename ContextT>
1099 const InstructionT &Term) {
1100 const auto *DivTermBlock = Term.getParent();
1101 DivergentTermBlocks.insert(DivTermBlock);
1102 LLVM_DEBUG(dbgs() << "analyzeControlDiv " << Context.print(DivTermBlock)
1103 << "\n");
1104
1105 // Don't propagate divergence from unreachable blocks.
1106 if (!DT.isReachableFromEntry(DivTermBlock))
1107 return;
1108
1109 const auto &DivDesc = SDA.getJoinBlocks(DivTermBlock);
1110 SmallVector<CycleRef> DivCycles;
1111
1112 // Iterate over all blocks now reachable by a disjoint path join
1113 for (const auto *JoinBlock : DivDesc.JoinDivBlocks) {
1114 CycleRef C = CI.getCycle(JoinBlock);
1115 LLVM_DEBUG(dbgs() << "visiting join block " << Context.print(JoinBlock)
1116 << "\n");
1117 if (CycleRef Outermost = getOutermostDivergentCycle(
1118 CI, C, DivTermBlock, JoinBlock, DT, Context)) {
1119 LLVM_DEBUG(dbgs() << "found divergent cycle\n");
1120 DivCycles.push_back(Outermost);
1121 continue;
1122 }
1123 taintAndPushPhiNodes(*JoinBlock);
1124 }
1125
1126 // Sort by order of decreasing depth. This allows later cycles to be skipped
1127 // because they are already contained in earlier ones.
1128 llvm::sort(DivCycles, [this](CycleRef A, CycleRef B) {
1129 return CI.getDepth(A) > CI.getDepth(B);
1130 });
1131
1132 // Cycles that are assumed divergent due to the diverged entry
1133 // criterion potentially contain temporal divergence depending on
1134 // the DFS chosen. Conservatively, all values produced in such a
1135 // cycle are assumed divergent. "Cycle invariant" values may be
1136 // assumed uniform, but that requires further analysis.
1137 for (auto C : DivCycles) {
1138 if (!insertIfNotContained(CI, AssumedDivergent, C))
1139 continue;
1140 LLVM_DEBUG(dbgs() << "process divergent cycle\n");
1141 for (const BlockT *BB : CI.getBlocks(C)) {
1142 taintAndPushAllDefs(*BB);
1143 }
1144 }
1145
1146 CycleRef BranchCycle = CI.getCycle(DivTermBlock);
1147 assert(DivDesc.CycleDivBlocks.empty() || BranchCycle);
1148 for (const auto *DivExitBlock : DivDesc.CycleDivBlocks) {
1149 propagateCycleExitDivergence(*DivExitBlock, BranchCycle);
1150 }
1151}
1152
1153template <typename ContextT>
1155 HasBranchDivergence = true;
1156
1157 // All values on the Worklist are divergent.
1158 // Their users may not have been updated yet.
1159 while (!Worklist.empty()) {
1160 const InstructionT *I = Worklist.back();
1161 Worklist.pop_back();
1162
1163 LLVM_DEBUG(dbgs() << "worklist pop: " << Context.print(I) << "\n");
1164
1165 if (I->isTerminator()) {
1167 continue;
1168 }
1169
1170 // propagate value divergence to users
1171 assert(hasDivergentDefs(*I) && "Worklist invariant violated!");
1172 pushUsers(*I);
1173 }
1174}
1175
1176template <typename ContextT>
1181
1182template <typename ContextT>
1184 const InstructionT &Instr) const {
1185 return UniformOverrides.contains(&Instr);
1186}
1187
1188template <typename ContextT>
1193
1194template <typename ContextT>
1196 const DominatorTreeT &DT, const CycleInfoT &CI,
1197 const TargetTransformInfo *TTI) {
1198 DA.reset(new ImplT{DT, CI, TTI});
1199}
1200
1201template <typename ContextT>
1203 // When we print Value, LLVM IR instruction, we want to print extra new line.
1204 // In LLVM IR print function for Value does not print new line at the end.
1205 // In MIR print for MachineInstr prints new line at the end.
1206 constexpr bool IsMIR = std::is_same<InstructionT, MachineInstr>::value;
1207 std::string NewLine = IsMIR ? "" : "\n";
1208
1209 bool FoundDivergence = false;
1210
1211 FoundDivergence |= printDivergentArgs(OS);
1212
1213 if (!AssumedDivergent.empty()) {
1214 FoundDivergence = true;
1215 OS << "CYCLES ASSUMED DIVERGENT:\n";
1216 for (auto C : AssumedDivergent) {
1217 OS << " " << CI.print(C) << '\n';
1218 }
1219 }
1220
1221 if (!DivergentExitCycles.empty()) {
1222 FoundDivergence = true;
1223 OS << "CYCLES WITH DIVERGENT EXIT:\n";
1224 for (auto C : DivergentExitCycles) {
1225 OS << " " << CI.print(C) << '\n';
1226 }
1227 }
1228
1229 if (!TemporalDivergenceList.empty()) {
1230 FoundDivergence = true;
1231 OS << "\nTEMPORAL DIVERGENCE LIST:\n";
1232
1233 for (auto [Val, UseInst, C] : TemporalDivergenceList) {
1234 OS << "Value :" << Context.print(Val) << NewLine
1235 << "Used by :" << Context.print(UseInst) << NewLine
1236 << "Outside cycle :" << CI.print(C) << "\n\n";
1237 }
1238 }
1239
1240 for (auto &Block : F) {
1241 OS << "\nBLOCK " << Context.print(&Block) << '\n';
1242
1243 OS << "DEFINITIONS\n";
1245 Context.appendBlockDefs(Defs, Block);
1246 for (auto Value : Defs) {
1247 if (isDivergent(Value)) {
1248 FoundDivergence = true;
1249 OS << " DIVERGENT: ";
1250 } else {
1251 OS << " ";
1252 }
1253 OS << Context.print(Value) << NewLine;
1254 }
1255
1256 OS << "TERMINATORS\n";
1258 Context.appendBlockTerms(Terms, Block);
1259 bool DivergentTerminators = hasDivergentTerminator(Block);
1260 if (DivergentTerminators)
1261 FoundDivergence = true;
1262 for (auto *T : Terms) {
1263 if (DivergentTerminators)
1264 OS << " DIVERGENT: ";
1265 else
1266 OS << " ";
1267 OS << Context.print(T) << NewLine;
1268 }
1269
1270 OS << "END BLOCK\n";
1271 }
1272
1273 if (!FoundDivergence)
1274 OS << "ALL VALUES UNIFORM\n";
1275}
1276
1277template <typename ContextT>
1281 return make_range(DA->TemporalDivergenceList.begin(),
1282 DA->TemporalDivergenceList.end());
1283}
1284
1285template <typename ContextT>
1286const typename ContextT::FunctionT &
1288 return DA->getFunction();
1289}
1290
1291template <typename ContextT>
1294 return DA->getCycleInfo();
1295}
1296
1297/// Whether \p V is divergent at its definition.
1298/// A default-constructed instance (no analysis computed) reports everything
1299/// as uniform, which is conservatively correct for non-divergent targets.
1300template <typename ContextT>
1302 return DA && DA->isDivergent(V);
1303}
1304
1305template <typename ContextT>
1307 const InstructionT *I) const {
1308 assert(I->isTerminator() && "Expected a terminator instruction!");
1309 return DA && DA->isDivergentTerminator(*I);
1310}
1311
1312template <typename ContextT>
1314 return DA && DA->isDivergentUse(U);
1315}
1316
1317template <typename ContextT>
1319 return DA && DA->hasDivergentTerminator(B);
1320}
1321
1322template <typename ContextT>
1324 if (DA)
1325 DA->forgetValue(V);
1326}
1327
1328/// \brief T helper function for printing.
1329template <typename ContextT>
1331 if (!DA) {
1332 Out << " Uniformity analysis not computed (no branch divergence).\n";
1333 return;
1334 }
1335 DA->print(Out);
1336}
1337
1338template <typename ContextT>
1339void llvm::ModifiedPostOrder<ContextT>::computeStackPO(
1340 SmallVectorImpl<const BlockT *> &Stack, const CycleInfoT &CI, CycleRef C,
1342 LLVM_DEBUG(dbgs() << "inside computeStackPO\n");
1343 while (!Stack.empty()) {
1344 auto *NextBB = Stack.back();
1345 if (Finalized.count(NextBB)) {
1346 Stack.pop_back();
1347 continue;
1348 }
1349 LLVM_DEBUG(dbgs() << " visiting " << CI.getSSAContext().print(NextBB)
1350 << "\n");
1351 CycleRef NestedCycle = CI.getCycle(NextBB);
1352 if (C != NestedCycle &&
1353 (!C || (NestedCycle && CI.contains(C, NestedCycle)))) {
1354 LLVM_DEBUG(dbgs() << " found a cycle\n");
1355 while (CI.getParentCycle(NestedCycle) != C)
1356 NestedCycle = CI.getParentCycle(NestedCycle);
1357
1358 SmallVector<BlockT *, 3> NestedExits;
1359 CI.getExitBlocks(NestedCycle, NestedExits);
1360 bool PushedNodes = false;
1361 for (auto *NestedExitBB : NestedExits) {
1362 LLVM_DEBUG(dbgs() << " examine exit: "
1363 << CI.getSSAContext().print(NestedExitBB) << "\n");
1364 if (C && !CI.contains(C, NestedExitBB))
1365 continue;
1366 if (Finalized.count(NestedExitBB))
1367 continue;
1368 PushedNodes = true;
1369 Stack.push_back(NestedExitBB);
1370 LLVM_DEBUG(dbgs() << " pushed exit: "
1371 << CI.getSSAContext().print(NestedExitBB) << "\n");
1372 }
1373 if (!PushedNodes) {
1374 // All loop exits finalized -> finish this node
1375 Stack.pop_back();
1376 computeCyclePO(CI, NestedCycle, Finalized);
1377 }
1378 continue;
1379 }
1380
1381 LLVM_DEBUG(dbgs() << " no nested cycle, going into DAG\n");
1382 // DAG-style
1383 bool PushedNodes = false;
1384 for (auto *SuccBB : successors(NextBB)) {
1385 LLVM_DEBUG(dbgs() << " examine succ: "
1386 << CI.getSSAContext().print(SuccBB) << "\n");
1387 if (C && !CI.contains(C, SuccBB))
1388 continue;
1389 if (Finalized.count(SuccBB))
1390 continue;
1391 PushedNodes = true;
1392 Stack.push_back(SuccBB);
1393 LLVM_DEBUG(dbgs() << " pushed succ: " << CI.getSSAContext().print(SuccBB)
1394 << "\n");
1395 }
1396 if (!PushedNodes) {
1397 // Never push nodes twice
1398 LLVM_DEBUG(dbgs() << " finishing node: "
1399 << CI.getSSAContext().print(NextBB) << "\n");
1400 Stack.pop_back();
1401 Finalized.insert(NextBB);
1402 appendBlock(*NextBB);
1403 }
1404 }
1405 LLVM_DEBUG(dbgs() << "exited computeStackPO\n");
1406}
1407
1408template <typename ContextT>
1409void ModifiedPostOrder<ContextT>::computeCyclePO(
1410 const CycleInfoT &CI, CycleRef C,
1412 LLVM_DEBUG(dbgs() << "inside computeCyclePO\n");
1414 auto *CycleHeader = CI.getHeader(C);
1415
1416 LLVM_DEBUG(dbgs() << " noted header: "
1417 << CI.getSSAContext().print(CycleHeader) << "\n");
1418 assert(!Finalized.count(CycleHeader));
1419 Finalized.insert(CycleHeader);
1420
1421 // Visit the header last
1422 LLVM_DEBUG(dbgs() << " finishing header: "
1423 << CI.getSSAContext().print(CycleHeader) << "\n");
1424 appendBlock(*CycleHeader, CI.isReducible(C));
1425
1426 // Initialize with immediate successors
1427 for (auto *BB : successors(CycleHeader)) {
1428 LLVM_DEBUG(dbgs() << " examine succ: " << CI.getSSAContext().print(BB)
1429 << "\n");
1430 if (!CI.contains(C, BB))
1431 continue;
1432 if (BB == CycleHeader)
1433 continue;
1434 if (!Finalized.count(BB)) {
1435 LLVM_DEBUG(dbgs() << " pushed succ: " << CI.getSSAContext().print(BB)
1436 << "\n");
1437 Stack.push_back(BB);
1438 }
1439 }
1440
1441 // Compute PO inside region
1442 computeStackPO(Stack, CI, C, Finalized);
1443
1444 LLVM_DEBUG(dbgs() << "exited computeCyclePO\n");
1445}
1446
1447/// \brief Generically compute the modified post order.
1448template <typename ContextT>
1452 auto *F = CI.getFunction();
1453 POIndex.assign(GraphTraits<const FunctionT *>::getMaxNumber(F), InvalidIndex);
1454 Stack.reserve(24); // FIXME made-up number
1455 Stack.push_back(&F->front());
1456 computeStackPO(Stack, CI, CycleRef(), Finalized);
1457}
1458
1459} // namespace llvm
1460
1461#undef DEBUG_TYPE
1462
1463#endif // LLVM_ADT_GENERICUNIFORMITYIMPL_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file defines the DenseSet and SmallDenseSet classes.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define T
uint64_t IntrinsicInst * II
#define P(N)
This file contains some templates that are useful if you are working with the STL at all.
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallPtrSet class.
This file defines the SparseBitVector class.
This file contains some functions that are useful when dealing with strings.
#define LLVM_DEBUG(...)
Definition Debug.h:119
static const unsigned InvalidIndex
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
Opaque handle to a cycle within a GenericCycleInfo that wraps the cycle's preorder index.
Implements a dense probed hash-table based set.
Definition DenseSet.h:283
Compute divergence starting with a divergent branch.
GenericSyncDependenceAnalysis< ContextT > SyncDependenceAnalysisT
const BlockT *& label(const BlockT *BB)
typename ContextT::DominatorTreeT DominatorTreeT
bool computeJoin(const BlockT &SuccBlock, const BlockT &PushedLabel)
std::unique_ptr< DivergenceDescriptorT > DivDesc
void printDefs(raw_ostream &Out)
typename ContextT::FunctionT FunctionT
GenericCycleInfo< ContextT > CycleInfoT
SmallVector< const BlockT * > BlockLabels
ModifiedPostOrder< ContextT > ModifiedPO
std::unique_ptr< DivergenceDescriptorT > computeJoinPoints()
bool visitCycleExitEdge(const BlockT &ExitBlock, const BlockT &Label)
typename ContextT::ValueRefT ValueRefT
typename ContextT::BlockT BlockT
DivergencePropagator(const ModifiedPO &CyclePOT, const DominatorTreeT &DT, const CycleInfoT &CI, const BlockT &DivTermBlock)
bool visitEdge(const BlockT &SuccBlock, const BlockT &Label)
typename SyncDependenceAnalysisT::DivergenceDescriptor DivergenceDescriptorT
Cycle information for a function.
Locate join blocks for disjoint paths starting at a divergent branch.
GenericSyncDependenceAnalysis(const ContextT &Context, const DominatorTreeT &DT, const CycleInfoT &CI)
ModifiedPostOrder< ContextT > ModifiedPO
DivergencePropagator< ContextT > DivergencePropagatorT
SmallPtrSet< const BlockT *, 4 > ConstBlockSet
typename ContextT::DominatorTreeT DominatorTreeT
GenericCycleInfo< ContextT > CycleInfoT
typename ContextT::FunctionT FunctionT
typename ContextT::InstructionT InstructionT
typename ContextT::ValueRefT ValueRefT
const DivergenceDescriptor & getJoinBlocks(const BlockT *DivTermBlock)
Computes divergent join points and cycle exits caused by branch divergence in Term.
SmallVector< TemporalDivergenceTuple, 8 > TemporalDivergenceList
bool isAlwaysUniform(const InstructionT &Instr) const
Whether Val will always return a uniform value regardless of its operands.
bool isCustomUniform(const InstructionT &I) const
Check if an instruction with Custom uniformity can be proven uniform based on its operands.
typename ContextT::ValueRefT ValueRefT
void analyzeControlDivergence(const InstructionT &Term)
Mark Term as divergent and push all Instructions that become divergent as a result on the worklist.
void addCustomUniformityCandidate(const InstructionT *I)
Add an instruction that requires custom uniformity analysis.
bool isDivergent(ConstValueRefT V) const
Whether Val is divergent at its definition.
void recordTemporalDivergence(ConstValueRefT, const InstructionT *, CycleRef)
bool isDivergentUse(const UseT &U) const
bool hasDivergentDefs(const InstructionT &I) const
SmallPtrSet< const InstructionT *, 8 > CustomUniformityCandidates
typename ContextT::InstructionT InstructionT
bool printDivergentArgs(raw_ostream &Out) const
Print divergent arguments and return true if any were found.
void forgetValue(ConstValueRefT V)
Call before erasing V, or a later instruction reusing its address may be misclassified as uniform.
typename ContextT::ConstValueRefT ConstValueRefT
typename ContextT::DominatorTreeT DominatorTreeT
void compute()
Propagate divergence to all instructions in the region.
bool hasDivergentTerminator(const BlockT &B) const
GenericUniformityAnalysisImpl(const DominatorTreeT &DT, const CycleInfoT &CI, const TargetTransformInfo *TTI)
bool markDefsDivergent(const InstructionT &Instr)
Mark outputs of Instr as divergent.
typename ContextT::FunctionT FunctionT
std::vector< const InstructionT * > Worklist
void markDivergent(const InstructionT &I)
Examine I for divergent outputs and add to the worklist.
SmallPtrSet< const BlockT *, 32 > DivergentTermBlocks
bool isDivergentTerminator(const InstructionT &I) const
GenericCycleInfo< ContextT > CycleInfoT
void addUniformOverride(const InstructionT &Instr)
Mark UniVal as a value that is always uniform.
std::tuple< ConstValueRefT, InstructionT *, CycleRef > TemporalDivergenceTuple
typename SyncDependenceAnalysisT::DivergenceDescriptor DivergenceDescriptorT
GenericSyncDependenceAnalysis< ContextT > SyncDependenceAnalysisT
bool hasDivergentTerminator(const BlockT &B)
void print(raw_ostream &Out) const
T helper function for printing.
bool isDivergentAtDef(ConstValueRefT V) const
Whether V is divergent at its definition.
std::tuple< ConstValueRefT, InstructionT *, CycleRef > TemporalDivergenceTuple
typename ContextT::ConstValueRefT ConstValueRefT
typename ContextT::BlockT BlockT
bool isDivergentAtUse(const UseT &U) const
Whether U is divergent at its use.
const CycleInfoT & getCycleInfo() const
The cycle info this analysis was computed with.
typename ContextT::InstructionT InstructionT
const FunctionT & getFunction() const
The GPU kernel this analysis result is for.
bool isDivergentTerminator(const InstructionT *I) const
void forgetValue(ConstValueRefT V)
Call before erasing V, or a later instruction reusing its address may be misclassified as uniform.
GenericCycleInfo< ContextT > CycleInfoT
typename ContextT::DominatorTreeT DominatorTreeT
iterator_range< TemporalDivergenceTuple * > getTemporalDivergenceList() const
A helper class to return the specified delimiter string after the first invocation of operator String...
Construct a specially modified post-order traversal of cycles.
typename ContextT::FunctionT FunctionT
bool isReducibleCycleHeader(const BlockT *BB) const
void appendBlock(const BlockT &BB, bool IsReducibleCycleHeader=false)
const BlockT * operator[](size_t Idx) const
ModifiedPostOrder(const ContextT &C)
unsigned count(BlockT *BB) const
void compute(const CycleInfoT &CI)
Generically compute the modified post order.
GenericCycleInfo< ContextT > CycleInfoT
unsigned getIndex(const BlockT *BB) const
typename std::vector< BlockT * >::const_iterator const_iterator
typename ContextT::DominatorTreeT DominatorTreeT
typename ContextT::BlockT BlockT
Simple wrapper around std::function<void(raw_ostream&)>.
Definition Printable.h:38
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
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.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:346
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This pass provides access to the codegen interfaces that are needed for IR-level transformations.
LLVM Value Representation.
Definition Value.h:75
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
This is an optimization pass for GlobalISel generic memory operations.
bool insertIfNotContained(const CycleInfoT &CI, SmallVector< CycleRef > &Cycles, CycleRef Candidate)
Add Candidate to Cycles if it is not already contained in Cycles.
CycleRef getExtDivCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock)
Return the outermost cycle made divergent by branch outside it.
auto successors(const MachineBasicBlock *BB)
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1762
CycleRef getOutermostDivergentCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
auto succ_size(const MachineBasicBlock *BB)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
TargetTransformInfo TTI
auto instrs(const MachineBasicBlock &BB)
CycleRef getIntDivCycle(const CycleInfoT &CI, CycleRef Cycle, const BlockT *DivTermBlock, const BlockT *JoinBlock, const DominatorTreeT &DT, ContextT &Context)
Return the outermost cycle made divergent by branch inside it.
Information discovered by the sync dependence analysis for each divergent branch.