Skip to content

Commit d97bc38

Browse files
authored
Reapply "Extend getBackwardSlice to track values captured… (llvm#114452)
This commit fixes the failure in the original PR when building with shared libs. The problem is that `visitUsedValuesDefinedAbove` is defined in `MLIRTransformUtils`, but that lib depends on this lib (`MLIRAnalysis`). To fix, I dropped the use of `visitUsedValuesDefinedAbove` and use `Region::walk` to traverse values defined above. Reapplies PR llvm#113478 Reverts PR llvm#114432 This reverts commit a9a8351.
1 parent e083a33 commit d97bc38

File tree

4 files changed

+56
-6
lines changed

4 files changed

+56
-6
lines changed

mlir/include/mlir/Analysis/SliceAnalysis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ struct BackwardSliceOptions : public SliceOptions {
4747
/// backward slice computation traverses block arguments and asserts that the
4848
/// parent op has a single region with a single block.
4949
bool omitBlockArguments = false;
50+
51+
/// When omitUsesFromAbove is true, the backward slice computation omits
52+
/// traversing values that are captured from above.
53+
/// TODO: this should default to `false` after users have been updated.
54+
bool omitUsesFromAbove = true;
5055
};
5156

5257
using ForwardSliceOptions = SliceOptions;

mlir/lib/Analysis/SliceAnalysis.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "mlir/IR/Operation.h"
1717
#include "mlir/Interfaces/SideEffectInterfaces.h"
1818
#include "mlir/Support/LLVM.h"
19+
#include "llvm/ADT/STLExtras.h"
1920
#include "llvm/ADT/SetVector.h"
2021
#include "llvm/ADT/SmallPtrSet.h"
2122

@@ -91,14 +92,13 @@ static void getBackwardSliceImpl(Operation *op,
9192
if (options.filter && !options.filter(op))
9293
return;
9394

94-
for (const auto &en : llvm::enumerate(op->getOperands())) {
95-
auto operand = en.value();
96-
if (auto *definingOp = operand.getDefiningOp()) {
95+
auto processValue = [&](Value value) {
96+
if (auto *definingOp = value.getDefiningOp()) {
9797
if (backwardSlice->count(definingOp) == 0)
9898
getBackwardSliceImpl(definingOp, backwardSlice, options);
99-
} else if (auto blockArg = dyn_cast<BlockArgument>(operand)) {
99+
} else if (auto blockArg = dyn_cast<BlockArgument>(value)) {
100100
if (options.omitBlockArguments)
101-
continue;
101+
return;
102102

103103
Block *block = blockArg.getOwner();
104104
Operation *parentOp = block->getParentOp();
@@ -113,7 +113,24 @@ static void getBackwardSliceImpl(Operation *op,
113113
} else {
114114
llvm_unreachable("No definingOp and not a block argument.");
115115
}
116+
};
117+
118+
if (!options.omitUsesFromAbove) {
119+
llvm::for_each(op->getRegions(), [&](Region &region) {
120+
// Walk this region recursively to collect the regions that descend from
121+
// this op's nested regions (inclusive).
122+
SmallPtrSet<Region *, 4> descendents;
123+
region.walk(
124+
[&](Region *childRegion) { descendents.insert(childRegion); });
125+
region.walk([&](Operation *op) {
126+
for (OpOperand &operand : op->getOpOperands()) {
127+
if (!descendents.contains(operand.get().getParentRegion()))
128+
processValue(operand.get());
129+
}
130+
});
131+
});
116132
}
133+
llvm::for_each(op->getOperands(), processValue);
117134

118135
backwardSlice->insert(op);
119136
}

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)