Skip to content

Commit 2fc3c5c

Browse files
committed
[mlir][vector] Prevent duplicating operations during vector distribute
We should distribute ops that have other uses than the yield op as this would duplicate those ops. Differential Revision: https://reviews.llvm.org/D143629
1 parent 3818237 commit 2fc3c5c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

mlir/lib/Dialect/Vector/Transforms/VectorDistribute.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,16 @@ static bool canBeHoisted(Operation *op,
228228
isMemoryEffectFree(op) && op->getNumRegions() == 0;
229229
}
230230

231-
/// Return a value yielded by `warpOp` which statifies the filter lamdba
232-
/// condition and is not dead.
231+
/// Return a value yielded by `warpOp` with no other uses which statifies the
232+
/// filter lamdba condition and is not dead.
233233
static OpOperand *getWarpResult(WarpExecuteOnLane0Op warpOp,
234234
const std::function<bool(Operation *)> &fn) {
235235
auto yield = cast<vector::YieldOp>(
236236
warpOp.getBodyRegion().getBlocks().begin()->getTerminator());
237237
for (OpOperand &yieldOperand : yield->getOpOperands()) {
238238
Value yieldValues = yieldOperand.get();
239239
Operation *definedOp = yieldValues.getDefiningOp();
240-
if (definedOp && fn(definedOp)) {
240+
if (definedOp && definedOp->hasOneUse() && fn(definedOp)) {
241241
if (!warpOp.getResult(yieldOperand.getOperandNumber()).use_empty())
242242
return &yieldOperand;
243243
}

mlir/test/Dialect/Vector/vector-warp-distribute.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,3 +1109,22 @@ func.func @vector_insert_2d_broadcast(%laneid: index) -> (vector<4x96xf32>) {
11091109
}
11101110
return %r : vector<4x96xf32>
11111111
}
1112+
// -----
1113+
1114+
// Verify that we don't duplicate the reduction.
1115+
// CHECK-PROP-LABEL: func @vector_reduction_no_duplicate(
1116+
// CHECK-PROP-SAME: %[[laneid:.*]]: index)
1117+
// CHECK-PROP: %[[warp_op:.*]] = vector.warp_execute_on_lane_0(%[[laneid]])[32] -> (f32) {
1118+
// CHECK-PROP: vector.reduction
1119+
// CHECK-PROP: vector.yield %{{.*}} : f32
1120+
// CHECK-PROP: }
1121+
// CHECK-PROP-NEXT: return %{{.*}} : f32
1122+
func.func @vector_reduction_no_duplicate(%laneid: index) -> (f32) {
1123+
%r = vector.warp_execute_on_lane_0(%laneid)[32] -> (f32) {
1124+
%0 = "some_def"() : () -> (vector<32xf32>)
1125+
%1 = vector.reduction <add>, %0 : vector<32xf32> into f32
1126+
"some_blocking_use"(%1) : (f32) -> ()
1127+
vector.yield %1 : f32
1128+
}
1129+
return %r : f32
1130+
}

0 commit comments

Comments
 (0)