Skip to content

Commit 5480fc6

Browse files
authored
[mlir][tosa] Interpret boolean values correctly in cast folder (#147078)
Previously the cast folder would sign extend boolean values, leading "true" to be casted to a value of -1 instead of 1. This change ensures i1 values are zero extended, since i1 is used as a boolean value in TOSA. According to the TOSA spec, the result of a boolean cast with value "true" to another integer type should give a result of 1. Fixes #57951
1 parent dbb6ed7 commit 5480fc6

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,15 +1295,17 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
12951295
}
12961296

12971297
if (llvm::isa<IntegerType>(inETy) && llvm::isa<IntegerType>(outETy)) {
1298-
auto unsignIn = llvm::cast<IntegerType>(inETy).isUnsignedInteger();
1298+
const auto inIntType = llvm::cast<IntegerType>(inETy);
1299+
auto unsignIn = inIntType.isUnsignedInteger();
12991300
bool trunc =
13001301
inETy.getIntOrFloatBitWidth() > outETy.getIntOrFloatBitWidth();
13011302
auto intVal = operand.getSplatValue<APInt>();
13021303
auto bitwidth = outETy.getIntOrFloatBitWidth();
13031304

13041305
if (trunc) {
13051306
intVal = intVal.trunc(bitwidth);
1306-
} else if (unsignIn) {
1307+
// i1 types are boolean in TOSA
1308+
} else if (unsignIn || inIntType.isInteger(1)) {
13071309
intVal = intVal.zext(bitwidth);
13081310
} else {
13091311
intVal = intVal.sext(bitwidth);

mlir/test/Dialect/Tosa/canonicalize.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,3 +1338,14 @@ func.func @no_fold_mul_result_exceeds_i32() -> tensor<i32> {
13381338
%3 = tosa.mul %0, %1, %2 : (tensor<i32>, tensor<i32>, tensor<1xi8>) -> tensor<i32>
13391339
return %3 : tensor<i32>
13401340
}
1341+
1342+
// -----
1343+
1344+
// CHECK-LABEL: @test_fold_i1_to_i32_cast
1345+
// CHECK: %[[OUT:.*]] = "tosa.const"() <{values = dense<1> : tensor<i32>}> : () -> tensor<i32>
1346+
// CHECK: return %[[OUT]] : tensor<i32>
1347+
func.func @test_fold_i1_to_i32_cast() -> tensor<i32> {
1348+
%0 = "tosa.const"() <{values = dense<1> : tensor<i1>}> : () -> tensor<i1>
1349+
%1 = "tosa.cast"(%0) : (tensor<i1>) -> tensor<i32>
1350+
return %1 : tensor<i32>
1351+
}

0 commit comments

Comments
 (0)