Skip to content

Commit b0ddc6c

Browse files
Simplify slice computation.
Signed-off-by: MaheshRavishankar <[email protected]>
1 parent 1697e33 commit b0ddc6c

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

mlir/lib/Transforms/Utils/RegionUtils.cpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ LogicalResult mlir::moveOperationDependencies(RewriterBase &rewriter,
10831083
// depends on. Prune the slice to only include operations not already
10841084
// dominated by the `insertionPoint`
10851085
BackwardSliceOptions options;
1086-
options.inclusive = true;
1086+
options.inclusive = false;
10871087
options.omitUsesFromAbove = false;
10881088
// Since current support is to only move within a same basic block,
10891089
// the slices dont need to look past block arguments.
@@ -1092,20 +1092,7 @@ LogicalResult mlir::moveOperationDependencies(RewriterBase &rewriter,
10921092
return !dominance.properlyDominates(sliceBoundaryOp, insertionPoint);
10931093
};
10941094
llvm::SetVector<Operation *> slice;
1095-
1096-
// Get the defined slice for operands.
1097-
for (Value operand : op->getOperands()) {
1098-
getBackwardSlice(operand, &slice, options);
1099-
}
1100-
auto regions = op->getRegions();
1101-
if (!regions.empty()) {
1102-
// If op has region, get the defined slice for all captured values.
1103-
llvm::SetVector<Value> capturedVals;
1104-
mlir::getUsedValuesDefinedAbove(regions, capturedVals);
1105-
for (Value value : capturedVals) {
1106-
getBackwardSlice(value, &slice, options);
1107-
}
1108-
}
1095+
getBackwardSlice(op, &slice, options);
11091096

11101097
// If the slice contains `insertionPoint` cannot move the dependencies.
11111098
if (slice.contains(insertionPoint)) {
@@ -1114,8 +1101,8 @@ LogicalResult mlir::moveOperationDependencies(RewriterBase &rewriter,
11141101
"cannot move dependencies before operation in backward slice of op");
11151102
}
11161103

1117-
// Sort the slice topologically, and move in topological order.
1118-
mlir::topologicalSort(slice);
1104+
// We should move the slice in topological order, but `getBackwardSlice`
1105+
// already does that. So no need to sort again.
11191106
for (Operation *op : slice) {
11201107
rewriter.moveOpBefore(op, insertionPoint);
11211108
}

mlir/test/Transforms/move-operation-deps.mlir

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,42 @@ module attributes {transform.with_named_sequence} {
125125

126126
// -----
127127

128+
// Current implementation omits following basic block argument when
129+
// computing slices. Verify that this gives expected result.
130+
func.func @ignore_basic_block_arguments() -> f32 {
131+
^bb0():
132+
%8 = "test"() : () -> (f32)
133+
return %8: f32
134+
^bb1(%bbArg : f32):
135+
%0 = "before"() : () -> (f32)
136+
%1 = "moved_op"() ({
137+
"yield"(%bbArg) : (f32) -> ()
138+
}) : () -> (f32)
139+
%2 = "foo"() ({
140+
"yield"(%1) : (f32) -> ()
141+
}) : () -> (f32)
142+
return %2 : f32
143+
}
144+
145+
module attributes {transform.with_named_sequence} {
146+
transform.named_sequence @__transform_main(%arg0 : !transform.any_op {transform.readonly}) {
147+
%op1 = transform.structured.match ops{["foo"]} in %arg0
148+
: (!transform.any_op) -> !transform.any_op
149+
%op2 = transform.structured.match ops{["before"]} in %arg0
150+
: (!transform.any_op) -> !transform.any_op
151+
transform.test.move_operand_deps %op1 before %op2
152+
: !transform.any_op, !transform.any_op
153+
transform.yield
154+
}
155+
}
156+
// CHECK-LABEL: func @ignore_basic_block_arguments()
157+
// CHECK: %[[MOVED_1:.+]] = "moved_op"
158+
// CHECK: "before"
159+
// CHECK: %[[FOO:.+]] = "foo"
160+
// CHECK: return %[[FOO]]
161+
162+
// -----
163+
128164
// Fail when the "before" operation is part of the operation slice.
129165
func.func @do_not_move_slice() -> f32 {
130166
%0 = "before"() : () -> (f32)

0 commit comments

Comments
 (0)