Skip to content

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

Closed
@sweead

Description

@sweead

test commit: 87e39c3

Description:

When using tosa.cast to convert a tensor of type tensor<...xi1> (i.e., 1-bit boolean values) to a wider integer type such as tensor<...xi32>, the cast operation performs sign-extension rather than zero-extension. As a result: A boolean value true (represented as 1 in i1) is sign-extended to -1 in i32 (i.e., 0xFFFFFFFF).

Steps to Reproduce:

Minimal MLIR program (test.mlir):

  func.func private @printMemrefI32(tensor<*xi32>)
  func.func @main() {
    %0 = "tosa.const"() <{values = dense<true> : tensor<2x3x1x2xi1>}> : () -> tensor<2x3x1x2xi1>
    %1 = tosa.cast %0 : (tensor<2x3x1x2xi1>) -> tensor<2x3x1x2xi32>
    %cast = tensor.cast %1 : tensor<2x3x1x2xi32> to tensor<*xi32>
    call @printMemrefI32(%cast) : (tensor<*xi32>) -> ()
    return
  }

Command:

/home/workdir/llvm-project/build/bin/mlir-opt test.mlir -tosa-to-linalg-pipeline  -tosa-to-arith  -sparsifier   | \ 
/home/workdir/llvm-project/build/bin/mlir-runner -e main -entry-point-result=void -shared-libs=/home/workdir/llvm-project/build/lib/libmlir_runner_utils.so

Output:

[[[[-1,     -1]], 
  [[-1,     -1]], 
  [[-1,     -1]]], 
 [[[-1,     -1]], 
  [[-1,     -1]], 
  [[-1,     -1]]]]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions