|
25 | 25 |
|
26 | 26 | using namespace mlir;
|
27 | 27 |
|
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. |
30 | 30 | static void
|
31 | 31 | remapInlinedLocations(iterator_range<Region::iterator> inlinedBlocks,
|
32 | 32 | 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; |
39 | 40 | }
|
40 |
| - op->setLoc(it->second); |
| 41 | + return it->second; |
41 | 42 | };
|
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 | + } |
44 | 59 | }
|
45 | 60 |
|
46 | 61 | static void remapInlinedOperands(iterator_range<Region::iterator> inlinedBlocks,
|
|
0 commit comments