Skip to content

Commit 2bc038e

Browse files
committed
TosaToLinalg: Prefer to emit identity maps (#386)
When deciding whether to emit a map like `#map = affine_map<(d0, d1, d2, d3) -> (0, d1, d2, d3)>` or `#map = affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>` for an operand of a `linalg.generic` when lowering element-wise TOSA ops, prefer the latter unless broadcasting of the operand is really needed. This helps later transformations which often require the affine map to be a projected permuatation.
1 parent e899930 commit 2bc038e

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

mlir/lib/Conversion/TosaToLinalg/TosaToLinalg.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -882,8 +882,14 @@ emitElementwiseComputation(ConversionPatternRewriter &rewriter, Location loc,
882882
auto shape = cast<ShapedType>(operand.getType()).getShape();
883883
SmallVector<AffineExpr> affineExprs;
884884
for (auto it : llvm::enumerate(shape)) {
885-
auto affineExpr = it.value() == 1 ? rewriter.getAffineConstantExpr(0)
886-
: rewriter.getAffineDimExpr(it.index());
885+
// Prefer producting identity maps whenever possible (i.e. no broadcasting
886+
// needed) because some transforms (like reshape folding)
887+
// do not support affine constant exprs.
888+
bool requiresBroadcast =
889+
(it.value() == 1 && resultType.getDimSize(it.index()) != 1);
890+
auto affineExpr = requiresBroadcast
891+
? rewriter.getAffineConstantExpr(0)
892+
: rewriter.getAffineDimExpr(it.index());
887893
affineExprs.push_back(affineExpr);
888894
}
889895
return AffineMap::get(rank, 0, affineExprs, rewriter.getContext());

mlir/test/Conversion/TosaToLinalg/tosa-to-linalg.mlir

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,26 @@ func.func @test_add_1d_broadcast_static_to_static(%arg0: tensor<1xf32>, %arg1: t
253253

254254
// -----
255255

256+
// CHECK: #[[$MAP:.+]] = affine_map<(d0) -> (d0)>
257+
// CHECK-LABEL: @test_add_1d_matching_no_broadcast
258+
// CHECK-SAME: %[[ARG0:[0-9a-zA-Z_]*]]:
259+
// CHECK-SAME: %[[ARG1:[0-9a-zA-Z_]*]]:
260+
func.func @test_add_1d_matching_no_broadcast(%arg0: tensor<1xf32>, %arg1: tensor<1xf32>) -> tensor<1xf32> {
261+
262+
// CHECK: %[[VAL_0:.*]] = tensor.empty() : tensor<1xf32>
263+
// CHECK: %[[RESULT:.*]] = linalg.generic {indexing_maps = [#[[$MAP]], #[[$MAP]], #[[$MAP]]], iterator_types = ["parallel"]} ins(%[[ARG0]], %[[ARG1]] : tensor<1xf32>, tensor<1xf32>) outs(%[[VAL_0]] : tensor<1xf32>) {
264+
// CHECK: ^bb0(%[[VAL_1:.*]]: f32, %[[VAL_2:.*]]: f32, %[[VAL_3:.*]]: f32):
265+
// CHECK: %[[VAL_4:.*]] = arith.addf %[[VAL_1]], %[[VAL_2]] : f32
266+
// CHECK: linalg.yield %[[VAL_4]] : f32
267+
// CHECK: } -> tensor<1xf32>
268+
%0 = tosa.add %arg0, %arg1 : (tensor<1xf32>, tensor<1xf32>) -> tensor<1xf32>
269+
270+
// CHECK: return %[[RESULT]] : tensor<1xf32>
271+
return %0 : tensor<1xf32>
272+
}
273+
274+
// -----
275+
256276
// CHECK: #[[$MAP0:.+]] = affine_map<(d0) -> (d0)>
257277
// CHECK-LABEL: @test_add_1d_matching_static
258278
// CHECK-SAME: %[[ARG0:[0-9a-zA-Z_]*]]:

0 commit comments

Comments
 (0)