Skip to content

Commit a74bfc0

Browse files
authored
[mlir][tosa] Fix select folder when operands are broadcast (#165481)
This commit addresses a crash in the dialects folder. The currently folder assumes no broadcasting of the input operand happens and therefore the folder can complain that the returned value was not the same shape as the result. For now, this commit ensures no folding happens when broadcasting is involved. In the future, folding with a broadcast could likely be supported by inserting a `tosa.tile` operation before returning the operand. This type of transformation is likely better suited for a canonicalization pass. This commit only aims to avoid the crash.
1 parent 364fe55 commit a74bfc0

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,24 @@ OpFoldResult SliceOp::fold(FoldAdaptor adaptor) {
14851485
return {};
14861486
}
14871487

1488+
static bool
1489+
mayRequireBroadcast(ValueTypeRange<mlir::OperandRange> operandTypes) {
1490+
const auto isDynamic = [](Type ty) {
1491+
const auto shapedTy = llvm::dyn_cast<ShapedType>(ty);
1492+
return !shapedTy || !shapedTy.hasStaticShape();
1493+
};
1494+
1495+
return llvm::any_of(operandTypes, isDynamic) ||
1496+
failed(verifyCompatibleShapes(operandTypes));
1497+
}
1498+
14881499
OpFoldResult tosa::SelectOp::fold(FoldAdaptor adaptor) {
1500+
// Select allows operand shapes to be broadcast to the output shape. For
1501+
// now, don't support folding when we cannot prove no broadcasting is
1502+
// involved.
1503+
if (mayRequireBroadcast(getOperandTypes()))
1504+
return {};
1505+
14891506
if (getOnTrue() == getOnFalse())
14901507
return getOnTrue();
14911508

mlir/test/Dialect/Tosa/canonicalize.mlir

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,48 @@ func.func @select_not_pred(%arg0: tensor<2x3xi1>, %arg1: tensor<2x3xi32>, %arg2:
643643

644644
// -----
645645

646+
// CHECK-LABEL: @select_broadcast_same_value_no_fold
647+
func.func @select_broadcast_same_value_no_fold(%arg0: tensor<2x2xi1>, %arg1: tensor<1x1xf32>) -> tensor<2x2xf32> {
648+
// CHECK: tosa.select %arg0, %arg1, %arg1
649+
%0 = tosa.select %arg0, %arg1, %arg1 : (tensor<2x2xi1>, tensor<1x1xf32>, tensor<1x1xf32>) -> tensor<2x2xf32>
650+
return %0 : tensor<2x2xf32>
651+
}
652+
653+
// -----
654+
655+
// CHECK-LABEL: @select_broadcast_true_value_no_fold
656+
func.func @select_broadcast_true_value_no_fold(%arg0: tensor<1x1xf32>, %arg1: tensor<2x2xf32>) -> tensor<?x?xf32> {
657+
// CHECK: %[[CONST:.*]] = "tosa.const"
658+
%0 = "tosa.const"() {values = dense<1> : tensor<2x2xi1>} : () -> tensor<2x2xi1>
659+
// CHECK: tosa.select %[[CONST]], %arg0, %arg1
660+
%1 = tosa.select %0, %arg0, %arg1 : (tensor<2x2xi1>, tensor<1x1xf32>, tensor<2x2xf32>) -> tensor<?x?xf32>
661+
return %1 : tensor<?x?xf32>
662+
}
663+
664+
// -----
665+
666+
// CHECK-LABEL: @select_broadcast_false_value_no_fold
667+
func.func @select_broadcast_false_value_no_fold(%arg0: tensor<2x2xf32>, %arg1: tensor<1x1xf32>) -> tensor<2x2xf32> {
668+
// CHECK: %[[CONST:.*]] = "tosa.const"
669+
%0 = "tosa.const"() {values = dense<0> : tensor<2x2xi1>} : () -> tensor<2x2xi1>
670+
// CHECK: tosa.select %[[CONST]], %arg0, %arg1
671+
%1 = tosa.select %0, %arg0, %arg1 : (tensor<2x2xi1>, tensor<2x2xf32>, tensor<1x1xf32>) -> tensor<2x2xf32>
672+
return %1 : tensor<2x2xf32>
673+
}
674+
675+
// -----
676+
677+
// CHECK-LABEL: @select_broadcast_false_value_dynamic_operand_no_fold
678+
func.func @select_broadcast_false_value_dynamic_operand_no_fold(%arg0: tensor<2x?xf32>, %arg1: tensor<2x2xf32>) -> tensor<2x2xf32> {
679+
// CHECK: %[[CONST:.*]] = "tosa.const"
680+
%0 = "tosa.const"() {values = dense<0> : tensor<2x2xi1>} : () -> tensor<2x2xi1>
681+
// CHECK: tosa.select %[[CONST]], %arg0, %arg1
682+
%1 = tosa.select %0, %arg0, %arg1 : (tensor<2x2xi1>, tensor<2x?xf32>, tensor<2x2xf32>) -> tensor<2x2xf32>
683+
return %1 : tensor<2x2xf32>
684+
}
685+
686+
// -----
687+
646688
// CHECK-LABEL: @reduce_all_fold
647689
func.func @reduce_all_fold(%arg0: tensor<?x1xf32>) -> tensor<?x1xf32> {
648690
// CHECK: return %arg0

0 commit comments

Comments
 (0)