Skip to content

Commit b084111

Browse files
MoxinilianDinistro
andauthored
[mlir][mem2reg] Fix Mem2Reg attempting to promote in graph regions (#104910)
Mem2Reg assumes SSA dependencies but did not check for graph regions. This fixes it. --------- Co-authored-by: Christian Ulmann <[email protected]>
1 parent 4d348f7 commit b084111

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

mlir/lib/Transforms/Mem2Reg.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "mlir/IR/Builders.h"
1414
#include "mlir/IR/Dominance.h"
1515
#include "mlir/IR/PatternMatch.h"
16+
#include "mlir/IR/RegionKindInterface.h"
1617
#include "mlir/IR/Value.h"
1718
#include "mlir/Interfaces/ControlFlowInterfaces.h"
1819
#include "mlir/Interfaces/MemorySlotInterfaces.h"
@@ -255,6 +256,18 @@ LogicalResult MemorySlotPromotionAnalyzer::computeBlockingUses(
255256
// delete itself). We thus need to start from the use of the slot pointer and
256257
// propagate further requests through the forward slice.
257258

259+
// Because this pass currently only supports analysing the parent region of
260+
// the slot pointer, if a promotable memory op that needs promotion is within
261+
// a graph region, the slot may only be used in a graph region and should
262+
// therefore be ignored.
263+
Region *slotPtrRegion = slot.ptr.getParentRegion();
264+
auto slotPtrRegionOp =
265+
dyn_cast<RegionKindInterface>(slotPtrRegion->getParentOp());
266+
if (slotPtrRegionOp &&
267+
slotPtrRegionOp.getRegionKind(slotPtrRegion->getRegionNumber()) ==
268+
RegionKind::Graph)
269+
return failure();
270+
258271
// First insert that all immediate users of the slot pointer must no longer
259272
// use it.
260273
for (OpOperand &use : slot.ptr.getUses()) {

mlir/test/Transforms/mem2reg.mlir

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt %s --pass-pipeline='builtin.module(func.func(mem2reg))' --split-input-file | FileCheck %s
1+
// RUN: mlir-opt %s --pass-pipeline='builtin.module(any(mem2reg))' --split-input-file | FileCheck %s
22

33
// Verifies that allocators with mutliple slots are handled properly.
44

@@ -26,3 +26,16 @@ func.func @multi_slot_alloca_only_second() -> (i32, i32) {
2626
%4 = memref.load %2[] : memref<i32>
2727
return %3, %4 : i32, i32
2828
}
29+
30+
// -----
31+
32+
// Checks that slots are not promoted if used in a graph region.
33+
34+
// CHECK-LABEL: test.isolated_graph_region
35+
test.isolated_graph_region {
36+
// CHECK: %{{[[:alnum:]]+}} = test.multi_slot_alloca
37+
%slot = test.multi_slot_alloca : () -> (memref<i32>)
38+
memref.store %a, %slot[] : memref<i32>
39+
%a = memref.load %slot[] : memref<i32>
40+
"test.foo"() : () -> ()
41+
}

mlir/test/lib/Dialect/Test/TestOpDefs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ RegionKind GraphRegionOp::getRegionKind(unsigned index) {
126126
return RegionKind::Graph;
127127
}
128128

129+
//===----------------------------------------------------------------------===//
130+
// IsolatedGraphRegionOp
131+
//===----------------------------------------------------------------------===//
132+
133+
RegionKind IsolatedGraphRegionOp::getRegionKind(unsigned index) {
134+
return RegionKind::Graph;
135+
}
136+
129137
//===----------------------------------------------------------------------===//
130138
// AffineScopeOp
131139
//===----------------------------------------------------------------------===//

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2048,6 +2048,18 @@ def GraphRegionOp : TEST_Op<"graph_region", [
20482048
let assemblyFormat = "attr-dict-with-keyword $region";
20492049
}
20502050

2051+
def IsolatedGraphRegionOp : TEST_Op<"isolated_graph_region", [
2052+
DeclareOpInterfaceMethods<RegionKindInterface>,
2053+
IsolatedFromAbove]> {
2054+
let summary = "isolated from above operation with a graph region";
2055+
let description = [{
2056+
Test op that defines a graph region which is isolated from above.
2057+
}];
2058+
2059+
let regions = (region AnyRegion:$region);
2060+
let assemblyFormat = "attr-dict-with-keyword $region";
2061+
}
2062+
20512063
def AffineScopeOp : TEST_Op<"affine_scope", [AffineScope]> {
20522064
let summary = "affine scope operation";
20532065
let description = [{

0 commit comments

Comments
 (0)