Skip to content

[MLIR] Fold TOSA truthy boolean to i32 with 1 instead of -1 #149009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

badumbatish
Copy link
Contributor

Fixes #148846.

I don't think there's a spec on https://mlir.llvm.org/docs/Dialects/TOSA/#tosacast-mlirtosacastop that details how casting from i1 to i32 should happen but from the issue and from the godbolt https://godbolt.org/z/qTEraE3nh, it seems reasonable to implement this.

Would love some feedback on this as its my first time contributing to MLIR specifically

@llvmbot
Copy link
Member

llvmbot commented Jul 16, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-tosa

Author: jjasmine (badumbatish)

Changes

Fixes #148846.

I don't think there's a spec on https://mlir.llvm.org/docs/Dialects/TOSA/#tosacast-mlirtosacastop that details how casting from i1 to i32 should happen but from the issue and from the godbolt https://godbolt.org/z/qTEraE3nh, it seems reasonable to implement this.

Would love some feedback on this as its my first time contributing to MLIR specifically


Full diff: https://github.com/llvm/llvm-project/pull/149009.diff

2 Files Affected:

  • (modified) mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp (+2-1)
  • (modified) mlir/test/Dialect/Tosa/constant-op-fold.mlir (+10)
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
index 2dd45d27157cb..8f698e4b0dffb 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaCanonicalizations.cpp
@@ -1303,7 +1303,8 @@ OpFoldResult CastOp::fold(FoldAdaptor adaptor) {
 
       if (trunc) {
         intVal = intVal.trunc(bitwidth);
-      } else if (unsignIn) {
+      } else if (unsignIn || inETy.getIntOrFloatBitWidth() == 1) {
+        // Casting from i1 to iX will treat it as unsigned.
         intVal = intVal.zext(bitwidth);
       } else {
         intVal = intVal.sext(bitwidth);
diff --git a/mlir/test/Dialect/Tosa/constant-op-fold.mlir b/mlir/test/Dialect/Tosa/constant-op-fold.mlir
index d9d188dd25061..e803105f719db 100644
--- a/mlir/test/Dialect/Tosa/constant-op-fold.mlir
+++ b/mlir/test/Dialect/Tosa/constant-op-fold.mlir
@@ -594,6 +594,16 @@ func.func @cast_int_to_int_sign() -> tensor<i32> {
   return %cast : tensor<i32>
 }
 
+
+// CHECK: func.func @cast_i1_true_to_i32
+func.func @cast_i1_true_to_i32() -> tensor<i32> {
+  %splat = "tosa.const"() {values = dense<true> : tensor<i1>} : () -> tensor<i1>
+  // CHECK: %[[SPLAT:.+]] = "tosa.const"() <{values = dense<1> : tensor<i32>}
+  %cast = tosa.cast %splat : (tensor<i1>) -> tensor<i32>
+  // CHECK: return %[[SPLAT]]
+  return %cast : tensor<i32>
+}
+
 // -----
 
 // CHECK-LABEL: @reverse_splat

@CoTinker
Copy link
Contributor

This issue is fixed by #147078.

@badumbatish
Copy link
Contributor Author

Ah this is unfortunate. I'm glad we share the same direction of implementation! Will close

@badumbatish badumbatish deleted the ok branch July 16, 2025 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[MLIR] Boolean true incorrectly cast to -1 via tosa.cast from i1 to i32
3 participants