Skip to content

Commit 2095a7c

Browse files
committed
Reduce complexity of searching circular function calls
1 parent cb647ec commit 2095a7c

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

mlir/lib/Dialect/Bufferization/Transforms/OneShotModuleBufferize.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,15 +343,25 @@ static LogicalResult getFuncOpsOrderedByCalls(
343343

344344
// Iteratively remove function operations that do not call any of the
345345
// functions remaining in the callCounter map and add them to ordered list.
346-
while (!numberCallOpsContainedInFuncOp.empty()) {
347-
auto it = llvm::find_if(numberCallOpsContainedInFuncOp,
348-
[](auto entry) { return entry.getSecond() == 0; });
349-
if (it == numberCallOpsContainedInFuncOp.end())
350-
break;
351-
orderedFuncOps.push_back(it->getFirst());
352-
for (auto callee : calledBy[it->getFirst()])
353-
numberCallOpsContainedInFuncOp[callee]--;
354-
numberCallOpsContainedInFuncOp.erase(it);
346+
SmallVector<func::FuncOp> worklist;
347+
348+
for (const auto &entry : numberCallOpsContainedInFuncOp) {
349+
if (entry.second == 0)
350+
worklist.push_back(entry.first);
351+
}
352+
353+
while (!worklist.empty()) {
354+
func::FuncOp func = worklist.pop_back_val();
355+
orderedFuncOps.push_back(func);
356+
357+
for (func::FuncOp caller : calledBy[func]) {
358+
auto &count = numberCallOpsContainedInFuncOp[caller];
359+
360+
if (--count == 0)
361+
worklist.push_back(caller);
362+
}
363+
364+
numberCallOpsContainedInFuncOp.erase(func);
355365
}
356366

357367
// Put all other functions in the list of remaining functions. These are

0 commit comments

Comments
 (0)