Skip to content

Commit e8ede37

Browse files
authored
[LLHD][TCM] Ignore processes with CFG loops within a TR (#8196)
1 parent db08ceb commit e8ede37

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

lib/Dialect/LLHD/Transforms/TemporalCodeMotionPass.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,46 @@ void TemporalCodeMotionPass::runOnOperation() {
123123
(void)runOnProcess(proc); // Ignore processes that could not be lowered
124124
}
125125

126+
static LogicalResult checkForCFGLoop(llhd::ProcessOp procOp) {
127+
SmallVector<Block *> toCheck(
128+
llvm::map_range(procOp.getOps<llhd::WaitOp>(), [](llhd::WaitOp waitOp) {
129+
return waitOp.getSuccessor();
130+
}));
131+
toCheck.push_back(&procOp.getBody().front());
132+
133+
SmallVector<Block *> worklist;
134+
DenseSet<Block *> visited;
135+
for (auto *block : toCheck) {
136+
worklist.clear();
137+
visited.clear();
138+
worklist.push_back(block);
139+
140+
while (!worklist.empty()) {
141+
Block *curr = worklist.pop_back_val();
142+
if (isa<llhd::WaitOp>(curr->getTerminator()))
143+
continue;
144+
145+
visited.insert(curr);
146+
147+
for (auto *succ : curr->getSuccessors()) {
148+
if (visited.contains(succ))
149+
return failure();
150+
151+
worklist.push_back(succ);
152+
}
153+
}
154+
}
155+
156+
return success();
157+
}
158+
126159
LogicalResult TemporalCodeMotionPass::runOnProcess(llhd::ProcessOp procOp) {
160+
// Make sure there are no CFG loops that don't contain a block with a wait
161+
// terminator in the cycle because that's currently not supported by the
162+
// temporal region analysis and this pass.
163+
if (failed(checkForCFGLoop(procOp)))
164+
return failure();
165+
127166
llhd::TemporalRegionAnalysis trAnalysis =
128167
llhd::TemporalRegionAnalysis(procOp);
129168
unsigned numTRs = trAnalysis.getNumTemporalRegions();

lib/Dialect/LLHD/Transforms/TemporalRegions.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ void llhd::TemporalRegionAnalysis::recalculate(Operation *operation) {
9191
// If at least one predecessor has a wait terminator or at least one
9292
// predecessor has an unknown temporal region or not all predecessors have
9393
// the same TR, create a new TR
94+
// FIXME: `!allPredecessorTRsKnown` is a problem when there are CFG loops
95+
// in the process. If there is no wait op along any of the CFG edges of
96+
// such a loop we don't want to assign a new temporal region. However, we
97+
// also cannot just assume here that we can just assign the same TR, so
98+
// this might require a bigger change to the algorithm.
9499
} else if (!allPredecessorTRsKnown(block, workDone) ||
95100
anyPredecessorHasWait(block) ||
96101
!(std::adjacent_find(block->pred_begin(), block->pred_end(),

test/Dialect/LLHD/Transforms/temporal-code-motion.mlir

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,3 +205,18 @@ hw.module @more_than_one_TR_wait_terminator(in %cond: i1) {
205205
cf.br ^bb1
206206
}
207207
}
208+
209+
// CHECK-LABEL: @unsupportedLoop
210+
hw.module @unsupportedLoop() {
211+
// CHECK-NEXT: llhd.process {
212+
// CHECK-NEXT: cf.br ^bb
213+
// CHECK-NEXT: ^bb
214+
// CHECK-NEXT: llhd.wait ^bb
215+
// CHECK-NEXT: }
216+
llhd.process {
217+
cf.br ^bb1
218+
^bb1:
219+
llhd.wait ^bb1
220+
}
221+
hw.output
222+
}

0 commit comments

Comments
 (0)