Skip to content

Commit 6f092e5

Browse files
authored
[MLIR][Transforms] Update block arg locations during inlining (#106064)
This commit changes the inlining to also update the locations of block arguments. Not updating these locations leads to LLVM IR verification issues when exporting converted block arguments to phi nodes. This lack of location update was not visible due to ignoring the argument locations until recently. Relevant change: #105534
1 parent 2f0d326 commit 6f092e5

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

mlir/lib/Transforms/Utils/InliningUtils.cpp

+26-11
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,37 @@
2525

2626
using namespace mlir;
2727

28-
/// Remap locations from the inlined blocks with CallSiteLoc locations with the
29-
/// provided caller location.
28+
/// Remap all locations reachable from the inlined blocks with CallSiteLoc
29+
/// locations with the provided caller location.
3030
static void
3131
remapInlinedLocations(iterator_range<Region::iterator> inlinedBlocks,
3232
Location callerLoc) {
33-
DenseMap<Location, Location> mappedLocations;
34-
auto remapOpLoc = [&](Operation *op) {
35-
auto it = mappedLocations.find(op->getLoc());
36-
if (it == mappedLocations.end()) {
37-
auto newLoc = CallSiteLoc::get(op->getLoc(), callerLoc);
38-
it = mappedLocations.try_emplace(op->getLoc(), newLoc).first;
33+
DenseMap<Location, LocationAttr> mappedLocations;
34+
auto remapLoc = [&](Location loc) {
35+
auto [it, inserted] = mappedLocations.try_emplace(loc);
36+
// Only query the attribute uniquer once per callsite attribute.
37+
if (inserted) {
38+
auto newLoc = CallSiteLoc::get(loc, callerLoc);
39+
it->getSecond() = newLoc;
3940
}
40-
op->setLoc(it->second);
41+
return it->second;
4142
};
42-
for (auto &block : inlinedBlocks)
43-
block.walk(remapOpLoc);
43+
44+
AttrTypeReplacer attrReplacer;
45+
attrReplacer.addReplacement(
46+
[&](LocationAttr loc) -> std::pair<LocationAttr, WalkResult> {
47+
return {remapLoc(loc), WalkResult::skip()};
48+
});
49+
50+
for (Block &block : inlinedBlocks) {
51+
for (BlockArgument &arg : block.getArguments())
52+
if (LocationAttr newLoc = remapLoc(arg.getLoc()))
53+
arg.setLoc(newLoc);
54+
55+
for (Operation &op : block)
56+
attrReplacer.recursivelyReplaceElementsIn(&op, /*replaceAttrs=*/false,
57+
/*replaceLocs=*/true);
58+
}
4459
}
4560

4661
static void remapInlinedOperands(iterator_range<Region::iterator> inlinedBlocks,

mlir/test/Transforms/inlining.mlir

+2-2
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@ func.func @func_with_block_args_location(%arg0 : i32) {
215215

216216
// INLINE-LOC-LABEL: func @func_with_block_args_location_callee1
217217
// INLINE-LOC: cf.br
218-
// INLINE-LOC: ^bb{{[0-9]+}}(%{{.*}}: i32 loc("foo")
218+
// INLINE-LOC: ^bb{{[0-9]+}}(%{{.*}}: i32 loc(callsite("foo" at "bar"))
219219
func.func @func_with_block_args_location_callee1(%arg0 : i32) {
220-
call @func_with_block_args_location(%arg0) : (i32) -> ()
220+
call @func_with_block_args_location(%arg0) : (i32) -> () loc("bar")
221221
return
222222
}
223223

0 commit comments

Comments
 (0)