Skip to content

Commit d4eb67a

Browse files
authored
[StripDebugInfoWithPred] Use worklist instead of recursion (#8435)
As it caused stack overflow it is changed to use worklist. The behavior is slightly different since it flattens fused loc and deduplicates locations.
1 parent 0f6c244 commit d4eb67a

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

lib/Transforms/StripDebugInfoWithPred.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -34,23 +34,23 @@ struct StripDebugInfoWithPred
3434

3535
// Return stripped location for the given `loc`.
3636
mlir::Location getStrippedLoc(Location loc) {
37-
// If `pred` return true, strip the location.
38-
if (pred(loc))
39-
return UnknownLoc::get(loc.getContext());
40-
41-
if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
42-
SmallVector<mlir::Location> newLocations;
43-
newLocations.reserve(fusedLoc.getLocations().size());
44-
for (auto loc : fusedLoc.getLocations())
45-
newLocations.push_back(getStrippedLoc(loc));
46-
// NOTE: Don't use FusedLoc::get(&getContext(), newLocations,
47-
// fusedLoc.getMetadata()) to avoid a bytecode reader bug
48-
// llvm-project#99626.
49-
return FusedLoc::get(newLocations, fusedLoc.getMetadata(), &getContext());
37+
SetVector<Location> newLocations;
38+
SmallVector<Location, 8> worklist({loc});
39+
while (!worklist.empty()) {
40+
auto currentLoc = worklist.pop_back_val();
41+
if (auto fusedLoc = dyn_cast<FusedLoc>(currentLoc)) {
42+
for (auto loc : fusedLoc.getLocations())
43+
worklist.push_back(loc);
44+
} else {
45+
if (!pred(currentLoc))
46+
newLocations.insert(currentLoc);
47+
}
5048
}
5149

52-
// TODO: Handle other loc type.
53-
return loc;
50+
// NOTE: Don't use FusedLoc::get(&getContext(), locations, metadata) to
51+
// avoid a bytecode reader bug llvm-project#99626.
52+
return FusedLoc::get(newLocations.getArrayRef(), Attribute(),
53+
&getContext());
5454
}
5555

5656
void updateLocArray(Operation *op, StringRef attributeName) {

test/Transforms/strip-debuginfo-pred.mlir

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func.func @inline_notation() {
99
// CHECK: affine.for
1010
// CHECK-NEXT: loc("foo")
1111
affine.for %i0 = 0 to 8 {
12-
} loc(fused["foo", "foo.txt":10:8])
12+
} loc(fused["foo", "foo.txt":10:8, "foo"])
1313
return
1414
}
1515

0 commit comments

Comments
 (0)