Skip to content

[mlir][tosa] Allow rank-0 vector operands in tosa.apply_scale - #199924

Merged
lhutton1 merged 2 commits into
llvm:mainfrom
ziereis:tosa_allow_0d_vectors
Jun 18, 2026
Merged

lhutton1 merged 2 commits into
llvm:mainfrom
ziereis:tosa_allow_0d_vectors

Conversation

@ziereis

@ziereis ziereis commented May 27, 2026 •

Copy link
Copy Markdown
Contributor

I was facing a bug that can be reproduced this way:

 // RUN:  mlir-opt --transform-interpreter tosa_apply_scale_rank0_repro.mlir
  #map        = affine_map<(d0) -> (d0)>
  #map_scalar = affine_map<(d0) -> ()>

  func.func @repro(%input: tensor<64xi32>, %scalar_t: tensor<i32>,
                   %out_init: tensor<64xi8>) -> tensor<64xi8> {
    %c31_i8     = arith.constant 31 : i8
    %cScale_i32 = arith.constant -1010580540 : i32

    %tile_out = linalg.generic
      { indexing_maps = [#map, #map_scalar, #map],
        iterator_types = ["parallel"] }
      ins(%input, %scalar_t : tensor<64xi32>, tensor<i32>)
      outs(%out_init : tensor<64xi8>) {
      ^bb0(%in: i32, %in_4: i32, %out: i8):
        %scaled = tosa.apply_scale %in_4, %cScale_i32, %c31_i8
                  { rounding_mode = SINGLE_ROUND } : (i32, i32, i8) -> i32
        %as_i8  = arith.trunci %scaled : i32 to i8
        linalg.yield %as_i8 : i8
    } -> tensor<64xi8>

    return %tile_out : tensor<64xi8>
  }

  module attributes {transform.with_named_sequence} {
    transform.named_sequence @__transform_main(%arg0: !transform.any_op {transform.readonly}) {
      %func = transform.structured.match ops{["func.func"]} in %arg0
              : (!transform.any_op) -> !transform.any_op
      %op   = transform.structured.match ops{["linalg.generic"]} in %arg0
              : (!transform.any_op) -> !transform.any_op
      transform.structured.vectorize %op vector_sizes [64] : !transform.any_op
      transform.apply_patterns to %func {
        transform.apply_patterns.vector.transfer_permutation_patterns
        transform.apply_patterns.vector.sink_ops
      } : !transform.any_op
      transform.yield
    }
  }

Which lead to the following error:

  error: 'tosa.apply_scale' op operand #0 must be signless-integer-like, but got 'vector<i32>'
  note: see current operation: %5 = "tosa.apply_scale"(%4, %1, %0) ... : (vector<i32>, vector<i32>, vector<i8>) -> vector<i32>

After this patch it gives the following IR:

  func.func @repro(%arg0: tensor<64xi32>, %arg1: tensor<i32>, %arg2: tensor<64xi8>) -> tensor<64xi8> {
    %cst = arith.constant dense<31> : vector<i8>
    %cst_0 = arith.constant dense<-1010580540> : vector<i32>
    %c0 = arith.constant 0 : index
    %0 = ub.poison : i32
    %1 = vector.transfer_read %arg1[], %0 : tensor<i32>, vector<i32>
    %2 = tosa.apply_scale %1, %cst_0, %cst {rounding_mode = SINGLE_ROUND} : (vector<i32>, vector<i32>, vector<i8>) -> vector<i32>
    %3 = arith.trunci %2 : vector<i32> to vector<i8>
    %4 = vector.broadcast %3 : vector<i8> to vector<64xi8>
    %5 = vector.transfer_write %4, %arg2[%c0] {in_bounds = [true]} : vector<64xi8>, tensor<64xi8>
    return %5 : tensor<64xi8>
  }

Claude suggested the best fix for this is to ease the "no 0 rank vectors" restriction on the tosa.apply_scale op so this is what this PR includes. I honestly can't 100% judge if this is the right way to fix this but i couldn't find anything that would speak against it.

Assisted by: Claude Code 4.7

@ziereis
ziereis marked this pull request as ready for review May 27, 2026 12:53
@llvmorg-github-actions

llvmorg-github-actions Bot commented May 27, 2026 •

Copy link
Copy Markdown

@llvm/pr-subscribers-mlir-tosa

@llvm/pr-subscribers-mlir

Author: ziereis

Changes

I was facing a bug that can be reproduced this way:

 // RUN:  mlir-opt --transform-interpreter tosa_apply_scale_rank0_repro.mlir
  #map        = affine_map&lt;(d0) -&gt; (d0)&gt;
  #map_scalar = affine_map&lt;(d0) -&gt; ()&gt;

  func.func @<!-- -->repro(%input: tensor&lt;64xi32&gt;, %scalar_t: tensor&lt;i32&gt;,
                   %out_init: tensor&lt;64xi8&gt;) -&gt; tensor&lt;64xi8&gt; {
    %c31_i8     = arith.constant 31 : i8
    %cScale_i32 = arith.constant -1010580540 : i32

    %tile_out = linalg.generic
      { indexing_maps = [#map, #map_scalar, #map],
        iterator_types = ["parallel"] }
      ins(%input, %scalar_t : tensor&lt;64xi32&gt;, tensor&lt;i32&gt;)
      outs(%out_init : tensor&lt;64xi8&gt;) {
      ^bb0(%in: i32, %in_4: i32, %out: i8):
        %scaled = tosa.apply_scale %in_4, %cScale_i32, %c31_i8
                  { rounding_mode = SINGLE_ROUND } : (i32, i32, i8) -&gt; i32
        %as_i8  = arith.trunci %scaled : i32 to i8
        linalg.yield %as_i8 : i8
    } -&gt; tensor&lt;64xi8&gt;

    return %tile_out : tensor&lt;64xi8&gt;
  }

  module attributes {transform.with_named_sequence} {
    transform.named_sequence @<!-- -->__transform_main(%arg0: !transform.any_op {transform.readonly}) {
      %func = transform.structured.match ops{["func.func"]} in %arg0
              : (!transform.any_op) -&gt; !transform.any_op
      %op   = transform.structured.match ops{["linalg.generic"]} in %arg0
              : (!transform.any_op) -&gt; !transform.any_op
      transform.structured.vectorize %op vector_sizes [64] : !transform.any_op
      transform.apply_patterns to %func {
        transform.apply_patterns.vector.transfer_permutation_patterns
        transform.apply_patterns.vector.sink_ops
      } : !transform.any_op
      transform.yield
    }
  }

Which lead to the following error:

  error: 'tosa.apply_scale' op operand #<!-- -->0 must be signless-integer-like, but got 'vector&lt;i32&gt;'
  note: see current operation: %5 = "tosa.apply_scale"(%4, %1, %0) ... : (vector&lt;i32&gt;, vector&lt;i32&gt;, vector&lt;i8&gt;) -&gt; vector&lt;i32&gt;

After this patch it gives the following IR:

  func.func @<!-- -->repro(%arg0: tensor&lt;64xi32&gt;, %arg1: tensor&lt;i32&gt;, %arg2: tensor&lt;64xi8&gt;) -&gt; tensor&lt;64xi8&gt; {
    %cst = arith.constant dense&lt;31&gt; : vector&lt;i8&gt;
    %cst_0 = arith.constant dense&lt;-1010580540&gt; : vector&lt;i32&gt;
    %c0 = arith.constant 0 : index
    %0 = ub.poison : i32
    %1 = vector.transfer_read %arg1[], %0 : tensor&lt;i32&gt;, vector&lt;i32&gt;
    %2 = tosa.apply_scale %1, %cst_0, %cst {rounding_mode = SINGLE_ROUND} : (vector&lt;i32&gt;, vector&lt;i32&gt;, vector&lt;i8&gt;) -&gt; vector&lt;i32&gt;
    %3 = arith.trunci %2 : vector&lt;i32&gt; to vector&lt;i8&gt;
    %4 = vector.broadcast %3 : vector&lt;i8&gt; to vector&lt;64xi8&gt;
    %5 = vector.transfer_write %4, %arg2[%c0] {in_bounds = [true]} : vector&lt;64xi8&gt;, tensor&lt;64xi8&gt;
    return %5 : tensor&lt;64xi8&gt;
  }

Claude suggested the best fix for this is to ease the "no 0 rank vectors" restriction on the tosa.apply_scale op so this is what this PR includes. I honestly can't 100% judge if this is the right way to fix this but i couldn't find anything that would speak against it.

Assisted by: Claude Code 4.7


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

3 Files Affected:

  • (modified) mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td (+1-1)
  • (modified) mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir (+10)
  • (modified) mlir/test/Dialect/Tosa/ops.mlir (+7)
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
index 5b5189c84f4a3..6bc4216c4b78d 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
@@ -244,7 +244,7 @@ def Tosa_MXFPScaleTensorAtLeast1D : AnyTypeOf<[
 
 class Tosa_TypeLike<list<Type> types, string description = ""> : TypeConstraint<Or<[
      AnyTypeOf<types>.predicate,
-     VectorOfNonZeroRankOf<types>.predicate,
+     VectorOfAnyRankOf<types>.predicate,
      TosaTensorOf<types>.predicate]>,
      description>;
 
diff --git a/mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir b/mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir
index f293138b625da..48d8d52ddc39a 100644
--- a/mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir
+++ b/mlir/test/Conversion/TosaToArith/tosa-to-arith.mlir
@@ -83,6 +83,16 @@ func.func @apply_scale_test_vector(%arg0 : vector<4xi32>, %arg1 : vector<4xi32>,
 
 // -----
 
+// CHECK-LABEL: @apply_scale_test_vector_rank_0
+// SCALE: tosa.apply_scale
+func.func @apply_scale_test_vector_rank_0(%arg0 : vector<i32>, %arg1 : vector<i32>, %arg2 : vector<i8>) -> (vector<i32>) {
+  // CHECK-NOT: "tosa.apply_scale"
+  %res = tosa.apply_scale %arg0, %arg1, %arg2 {rounding_mode = DOUBLE_ROUND} : (vector<i32>, vector<i32>, vector<i8>) -> vector<i32>
+  return %res : vector<i32>
+}
+
+// -----
+
 // CHECK-LABEL: @apply_scale_test_i48
 // SCALE: tosa.apply_scale
 func.func @apply_scale_test_i48(%arg0 : i48, %arg1 : i32, %arg2 : i8) -> (i32) {
diff --git a/mlir/test/Dialect/Tosa/ops.mlir b/mlir/test/Dialect/Tosa/ops.mlir
index 5c368b3da4ff5..d98736a37afa4 100644
--- a/mlir/test/Dialect/Tosa/ops.mlir
+++ b/mlir/test/Dialect/Tosa/ops.mlir
@@ -1777,3 +1777,10 @@ func.func @test_assert_equal_shape() {
   tosa.assert_equal_shape %0, %1 {allow_broadcast = true} : (!tosa.shape<2>, !tosa.shape<2>) -> ()
   return
 }
+
+// -----
+// CHECK-LABEL: test_apply_scale_rank_0_vector
+func.func @test_apply_scale_rank_0_vector(%arg0: vector<i32>, %arg1: vector<i32>, %arg2: vector<i8>) -> vector<i32> {
+  %0 = tosa.apply_scale %arg0, %arg1, %arg2 {rounding_mode = SINGLE_ROUND} : (vector<i32>, vector<i32>, vector<i8>) -> vector<i32>
+  return %0 : vector<i32>
+}

@egebeysel
egebeysel requested a review from banach-space May 28, 2026 09:34
@banach-space
banach-space requested a review from lhutton1 May 28, 2026 17:51

@lhutton1 lhutton1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ziereis, I'm not very familiar with the legalizations from TOSA, and therefore don't have much context on the tosa.apply_scale op. I left a nitpick, otherwise the change LGTM.

cc @sjarus who may have more thoughts here

Comment thread mlir/test/Dialect/Tosa/ops.mlir Outdated

// -----
// CHECK-LABEL: test_apply_scale_rank_0_vector
func.func @test_apply_scale_rank_0_vector(%arg0: vector<i32>, %arg1: vector<i32>, %arg2: vector<i8>) -> vector<i32> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I don't think we need this test. This op is more of a utility to aid lowering and should only be produced during conversion to linalg. Perhaps it's worth adding a new test case in tosa-to-linalg to demonstrate apply_scale is correctly constructed for scalar values?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review, i removed the tests. I tried adding a test to tosa-to-linalg however i can't get the regular linalg conversion to emit 0-d vectors, it will always just convert into scalars. Really the only way i found to get to the 0-d vector shape is with the reproducer i attached in the PR description. I think it might be a good idea to put this somewhere as test but im not sure where would be a good fit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the review, i removed the tests. I tried adding a test to tosa-to-linalg however i can't get the regular linalg conversion to emit 0-d vectors, it will always just convert into scalars. Really the only way i found to get to the 0-d vector shape is with the reproducer i attached in the PR description. I think it might be a good idea to put this somewhere as test but im not sure where would be a good fit.

@ziereis

ziereis commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for the review @lhutton1. Sorry for only getting back to you so late.

@sjarus could you please have a look?

@lhutton1 lhutton1 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ziereis!

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.

2 participants