Skip to content

Commit 3e8b47a

Browse files
committed
Cleanup impl & add test
1 parent 2920933 commit 3e8b47a

File tree

3 files changed

+36
-15
lines changed

3 files changed

+36
-15
lines changed

mlir/lib/Analysis/SliceAnalysis.cpp

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,13 @@ static void getBackwardSliceImpl(Operation *op,
9292
if (options.filter && !options.filter(op))
9393
return;
9494

95-
for (const auto &en : llvm::enumerate(op->getOperands())) {
95+
auto operands = op->getOperands();
96+
SetVector<Value> valuesToFollow(operands.begin(), operands.end());
97+
if (!options.omitUsesFromAbove) {
98+
getUsedValuesDefinedAbove(op->getRegions(), valuesToFollow);
99+
}
100+
101+
for (const auto &en : llvm::enumerate(valuesToFollow)) {
96102
auto operand = en.value();
97103
if (auto *definingOp = operand.getDefiningOp()) {
98104
if (backwardSlice->count(definingOp) == 0)
@@ -116,19 +122,6 @@ static void getBackwardSliceImpl(Operation *op,
116122
}
117123
}
118124

119-
// Visit values that are defined above.
120-
if (!options.omitUsesFromAbove) {
121-
visitUsedValuesDefinedAbove(op->getRegions(), [&](OpOperand *operand) {
122-
if (Operation *definingOp = operand->get().getDefiningOp()) {
123-
getBackwardSliceImpl(definingOp, backwardSlice, options);
124-
return;
125-
}
126-
Operation *bbAargOwner =
127-
cast<BlockArgument>(operand->get()).getOwner()->getParentOp();
128-
getBackwardSliceImpl(bbAargOwner, backwardSlice, options);
129-
});
130-
}
131-
132125
backwardSlice->insert(op);
133126
}
134127

mlir/test/IR/slice.mlir

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: mlir-opt -slice-analysis-test %s | FileCheck %s
1+
// RUN: mlir-opt -slice-analysis-test -split-input-file %s | FileCheck %s
22

33
func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
44
%a = memref.alloc(%arg0, %arg2) : memref<?x?xf32>
@@ -33,3 +33,29 @@ func.func @slicing_linalg_op(%arg0 : index, %arg1 : index, %arg2 : index) {
3333
// CHECK-DAG: %[[B:.+]] = memref.alloc(%[[ARG2]], %[[ARG1]]) : memref<?x?xf32>
3434
// CHECK-DAG: %[[C:.+]] = memref.alloc(%[[ARG0]], %[[ARG1]]) : memref<?x?xf32>
3535
// CHECK: return
36+
37+
// -----
38+
39+
#map = affine_map<(d0, d1) -> (d0, d1)>
40+
func.func @slice_use_from_above(%arg0: tensor<5x5xf32>, %arg1: tensor<5x5xf32>) {
41+
%0 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%arg0 : tensor<5x5xf32>) outs(%arg1 : tensor<5x5xf32>) {
42+
^bb0(%in: f32, %out: f32):
43+
%2 = arith.addf %in, %in : f32
44+
linalg.yield %2 : f32
45+
} -> tensor<5x5xf32>
46+
%collapsed = tensor.collapse_shape %0 [[0, 1]] : tensor<5x5xf32> into tensor<25xf32>
47+
%1 = linalg.generic {indexing_maps = [#map, #map], iterator_types = ["parallel", "parallel"]} ins(%0 : tensor<5x5xf32>) outs(%arg1 : tensor<5x5xf32>) {
48+
^bb0(%in: f32, %out: f32):
49+
%c2 = arith.constant 2 : index
50+
%extracted = tensor.extract %collapsed[%c2] : tensor<25xf32>
51+
%2 = arith.addf %extracted, %extracted : f32
52+
linalg.yield %2 : f32
53+
} -> tensor<5x5xf32>
54+
return
55+
}
56+
57+
// CHECK-LABEL: func @slice_use_from_above__backward_slice__0
58+
// CHECK-SAME: %[[ARG0:[a-zA-Z0-9_]+]]: tensor
59+
// CHECK: %[[A:.+]] = linalg.generic {{.*}} ins(%[[ARG0]]
60+
// CHECK: %[[B:.+]] = tensor.collapse_shape %[[A]]
61+
// CHECK: return

mlir/test/lib/IR/TestSlicing.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ static LogicalResult createBackwardSliceFunction(Operation *op,
3939
SetVector<Operation *> slice;
4040
BackwardSliceOptions options;
4141
options.omitBlockArguments = omitBlockArguments;
42+
// TODO: Make this default.
43+
options.omitUsesFromAbove = false;
4244
getBackwardSlice(op, &slice, options);
4345
for (Operation *slicedOp : slice)
4446
builder.clone(*slicedOp, mapper);

0 commit comments

Comments
 (0)