[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: https://github.com/llvm/llvm-project/pull/105534
This commit is contained in:
Christian Ulmann
2024-08-26 15:23:39 +02:00
committed by GitHub
parent 2f0d32692e
commit 6f092e501b
2 changed files with 28 additions and 13 deletions

View File

@@ -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,

View File

@@ -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
}