Skip to content

[MLIR][Transforms] Update block arg locations during inlining #106064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 26 additions & 11 deletions mlir/lib/Transforms/Utils/InliningUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,37 @@

using namespace mlir;

/// Remap locations from the inlined blocks with CallSiteLoc locations with the
/// provided caller location.
/// Remap all locations reachable from the inlined blocks with CallSiteLoc
/// locations with the provided caller location.
static void
remapInlinedLocations(iterator_range<Region::iterator> inlinedBlocks,
Location callerLoc) {
DenseMap<Location, Location> mappedLocations;
auto remapOpLoc = [&](Operation *op) {
auto it = mappedLocations.find(op->getLoc());
if (it == mappedLocations.end()) {
auto newLoc = CallSiteLoc::get(op->getLoc(), callerLoc);
it = mappedLocations.try_emplace(op->getLoc(), newLoc).first;
DenseMap<Location, LocationAttr> mappedLocations;
auto remapLoc = [&](Location loc) {
auto [it, inserted] = mappedLocations.try_emplace(loc);
// Only query the attribute uniquer once per callsite attribute.
if (inserted) {
auto newLoc = CallSiteLoc::get(loc, callerLoc);
it->getSecond() = newLoc;
}
op->setLoc(it->second);
return it->second;
};
for (auto &block : inlinedBlocks)
block.walk(remapOpLoc);

AttrTypeReplacer attrReplacer;
attrReplacer.addReplacement(
[&](LocationAttr loc) -> std::pair<LocationAttr, WalkResult> {
return {remapLoc(loc), WalkResult::skip()};
});

for (Block &block : inlinedBlocks) {
for (BlockArgument &arg : block.getArguments())
if (LocationAttr newLoc = remapLoc(arg.getLoc()))
arg.setLoc(newLoc);

for (Operation &op : block)
attrReplacer.recursivelyReplaceElementsIn(&op, /*replaceAttrs=*/false,
/*replaceLocs=*/true);
}
}

static void remapInlinedOperands(iterator_range<Region::iterator> inlinedBlocks,
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Transforms/inlining.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ func.func @func_with_block_args_location(%arg0 : i32) {

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

Expand Down
Loading