Skip to content

Commit 5e17394

Browse files
committed
[SPIR-V] Add partial order tests, assert reducible
Add testing for the visitor and added a note explaining irreducible CFG are not supported. Related to #116692 Signed-off-by: Nathan Gauër <[email protected]>
1 parent dddeec4 commit 5e17394

File tree

6 files changed

+532
-212
lines changed

6 files changed

+532
-212
lines changed

llvm/lib/Target/SPIRV/SPIRVUtils.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,8 +519,11 @@ bool PartialOrderingVisitor::CanBeVisited(BasicBlock *BB) const {
519519
}
520520

521521
size_t PartialOrderingVisitor::GetNodeRank(BasicBlock *BB) const {
522-
size_t result = 0;
522+
auto It = BlockToOrder.find(BB);
523+
if (It != BlockToOrder.end())
524+
return It->second.Rank;
523525

526+
size_t result = 0;
524527
for (BasicBlock *P : predecessors(BB)) {
525528
// Ignore back-edges.
526529
if (DT.dominates(BB, P))
@@ -552,15 +555,20 @@ size_t PartialOrderingVisitor::visit(BasicBlock *BB, size_t Unused) {
552555
ToVisit.push(BB);
553556
Queued.insert(BB);
554557

558+
size_t QueueIndex = 0;
555559
while (ToVisit.size() != 0) {
556560
BasicBlock *BB = ToVisit.front();
557561
ToVisit.pop();
558562

559563
if (!CanBeVisited(BB)) {
560564
ToVisit.push(BB);
565+
assert(QueueIndex < ToVisit.size() &&
566+
"No valid candidate in the queue. Is the graph reducible?");
567+
QueueIndex++;
561568
continue;
562569
}
563570

571+
QueueIndex = 0;
564572
size_t Rank = GetNodeRank(BB);
565573
OrderInfo Info = {Rank, BlockToOrder.size()};
566574
BlockToOrder.emplace(BB, Info);

llvm/lib/Target/SPIRV/SPIRVUtils.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SPIRVSubtarget;
4141
// ignores back-edges. The cycle is visited from the entry in the same
4242
// topological-like ordering.
4343
//
44+
// Note: this visitor REQUIRES a reducible graph.
45+
//
4446
// This means once we visit a node, we know all the possible ancestors have been
4547
// visited.
4648
//
@@ -84,10 +86,11 @@ class PartialOrderingVisitor {
8486
// Visits |BB| with the current rank being |Rank|.
8587
size_t visit(BasicBlock *BB, size_t Rank);
8688

87-
size_t GetNodeRank(BasicBlock *BB) const;
8889
bool CanBeVisited(BasicBlock *BB) const;
8990

9091
public:
92+
size_t GetNodeRank(BasicBlock *BB) const;
93+
9194
// Build the visitor to operate on the function F.
9295
PartialOrderingVisitor(Function &F);
9396

llvm/test/CodeGen/SPIRV/structurizer/cf.if.nested.ll

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,28 @@
3434
; CHECK: %[[#bb30:]] = OpLabel
3535
; CHECK: OpSelectionMerge %[[#bb31:]] None
3636
; CHECK: OpBranchConditional %[[#]] %[[#bb32:]] %[[#bb33:]]
37-
; CHECK: %[[#bb32:]] = OpLabel
37+
; CHECK: %[[#bb32]] = OpLabel
3838
; CHECK: OpSelectionMerge %[[#bb34:]] None
39-
; CHECK: OpBranchConditional %[[#]] %[[#bb35:]] %[[#bb34:]]
40-
; CHECK: %[[#bb33:]] = OpLabel
39+
; CHECK: OpBranchConditional %[[#]] %[[#bb35:]] %[[#bb34]]
40+
; CHECK: %[[#bb33]] = OpLabel
4141
; CHECK: OpSelectionMerge %[[#bb36:]] None
4242
; CHECK: OpBranchConditional %[[#]] %[[#bb37:]] %[[#bb38:]]
43-
; CHECK: %[[#bb35:]] = OpLabel
44-
; CHECK: OpBranch %[[#bb34:]]
45-
; CHECK: %[[#bb37:]] = OpLabel
46-
; CHECK: OpBranch %[[#bb36:]]
47-
; CHECK: %[[#bb38:]] = OpLabel
43+
; CHECK: %[[#bb35]] = OpLabel
44+
; CHECK: OpBranch %[[#bb34]]
45+
; CHECK: %[[#bb37]] = OpLabel
46+
; CHECK: OpBranch %[[#bb36]]
47+
; CHECK: %[[#bb38]] = OpLabel
4848
; CHECK: OpSelectionMerge %[[#bb39:]] None
49-
; CHECK: OpBranchConditional %[[#]] %[[#bb40:]] %[[#bb39:]]
50-
; CHECK: %[[#bb34:]] = OpLabel
51-
; CHECK: OpBranch %[[#bb31:]]
52-
; CHECK: %[[#bb40:]] = OpLabel
53-
; CHECK: OpBranch %[[#bb39:]]
54-
; CHECK: %[[#bb39:]] = OpLabel
55-
; CHECK: OpBranch %[[#bb36:]]
56-
; CHECK: %[[#bb36:]] = OpLabel
57-
; CHECK: OpBranch %[[#bb31:]]
58-
; CHECK: %[[#bb31:]] = OpLabel
49+
; CHECK: OpBranchConditional %[[#]] %[[#bb40:]] %[[#bb39]]
50+
; CHECK: %[[#bb34]] = OpLabel
51+
; CHECK: OpBranch %[[#bb31]]
52+
; CHECK: %[[#bb40]] = OpLabel
53+
; CHECK: OpBranch %[[#bb39]]
54+
; CHECK: %[[#bb39]] = OpLabel
55+
; CHECK: OpBranch %[[#bb36]]
56+
; CHECK: %[[#bb36]] = OpLabel
57+
; CHECK: OpBranch %[[#bb31]]
58+
; CHECK: %[[#bb31]] = OpLabel
5959
; CHECK: OpReturnValue %[[#]]
6060
; CHECK: OpFunctionEnd
6161
; CHECK: %[[#func_26:]] = OpFunction %[[#void:]] DontInline %[[#]]

0 commit comments

Comments
 (0)