[Mlir-commits] [mlir] [mlir][tosa] Preserve raw const data in signless conversion (PR #204324)

Davide Grohmann llvmlistbot at llvm.org
Wed Jun 17 02:23:29 PDT 2026


https://github.com/davidegrohmann created https://github.com/llvm/llvm-project/pull/204324

Use DenseElementsAttr::getFromRawBuffer when rebuilding tosa.const attributes in TosaConvertIntegerTypeToSignless. The previous DenseElementsAttr::get(type, ArrayRef<char>) call interpreted raw bytes as i8 elements, which asserted for integer constants wider than 8 bits.

Add regression coverage for ui16, ui32, and ui48 constants.

>From 3d1d33c141edee71f01ddaf1eddf9993c5cb4f8f Mon Sep 17 00:00:00 2001
From: Davide Grohmann <davide.grohmann at arm.com>
Date: Wed, 17 Jun 2026 11:00:40 +0200
Subject: [PATCH] [mlir][tosa] Preserve raw const data in signless conversion

Use DenseElementsAttr::getFromRawBuffer when rebuilding tosa.const
attributes in TosaConvertIntegerTypeToSignless. The previous
DenseElementsAttr::get(type, ArrayRef<char>) call interpreted raw
bytes as i8 elements, which asserted for integer constants wider than
8 bits.

Add regression coverage for ui16, ui32, and ui48 constants.

Signed-off-by: Davide Grohmann <davide.grohmann at arm.com>
Change-Id: Ie12d94f628a8c62560452424b4f6d35434c4aafb
---
 .../TosaConvertIntegerTypeToSignless.cpp      |  2 +-
 ...tosa-convert-integer-type-to-signless.mlir | 27 +++++++++++++++++++
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaConvertIntegerTypeToSignless.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaConvertIntegerTypeToSignless.cpp
index 4b131333b956a..dea47dbaec1e2 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaConvertIntegerTypeToSignless.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaConvertIntegerTypeToSignless.cpp
@@ -119,7 +119,7 @@ class ConvertTosaConstWithIntegerTensorType
 
     ElementsAttr newAttr = oldAttr;
     if (auto denseAttr = llvm::dyn_cast<DenseElementsAttr>(oldAttr)) {
-      newAttr = DenseElementsAttr::get(newTy, denseAttr.getRawData());
+      newAttr = DenseElementsAttr::getFromRawBuffer(newTy, denseAttr.getRawData());
     } else {
       return rewriter.notifyMatchFailure(op, "unknown elements attribute type");
     }
diff --git a/mlir/test/Dialect/Tosa/tosa-convert-integer-type-to-signless.mlir b/mlir/test/Dialect/Tosa/tosa-convert-integer-type-to-signless.mlir
index b7dbf9faf0936..e8cfc4562ea13 100644
--- a/mlir/test/Dialect/Tosa/tosa-convert-integer-type-to-signless.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-convert-integer-type-to-signless.mlir
@@ -65,6 +65,33 @@ func.func @test_unsigned_const_data() -> tensor<5xui8> {
 
 // -----
 
+// CHECK-LABEL: test_unsigned_const_data_i16
+// CHECK: "tosa.const"() <{values = dense<[-1, -2, 0, 1, -32768]> : tensor<5xi16>}> : () -> tensor<5xi16>
+func.func @test_unsigned_const_data_i16() -> tensor<5xui16> {
+  %0 = "tosa.const"() <{values = dense<[65535, 65534, 0, 1, 32768]> : tensor<5xui16>}> : () -> tensor<5xui16>
+  return %0 : tensor<5xui16>
+}
+
+// -----
+
+// CHECK-LABEL: test_unsigned_const_data_i32
+// CHECK: "tosa.const"() <{values = dense<[-1, -2, 0, 1, -2147483648]> : tensor<5xi32>}> : () -> tensor<5xi32>
+func.func @test_unsigned_const_data_i32() -> tensor<5xui32> {
+  %0 = "tosa.const"() <{values = dense<[4294967295, 4294967294, 0, 1, 2147483648]> : tensor<5xui32>}> : () -> tensor<5xui32>
+  return %0 : tensor<5xui32>
+}
+
+// -----
+
+// CHECK-LABEL: test_unsigned_const_data_i48
+// CHECK: "tosa.const"() <{values = dense<[-1, -2, 0, 1, -140737488355328]> : tensor<5xi48>}> : () -> tensor<5xi48>
+func.func @test_unsigned_const_data_i48() -> tensor<5xui48> {
+  %0 = "tosa.const"() <{values = dense<[281474976710655, 281474976710654, 0, 1, 140737488355328]> : tensor<5xui48>}> : () -> tensor<5xui48>
+  return %0 : tensor<5xui48>
+}
+
+// -----
+
 // CHECK-LABEL: test_no_change
 // CHECK: %arg0: tensor<13x21x3xi8>
 func.func @test_no_change(%arg0: tensor<13x21x3xi8>) -> tensor<13x21x3xi8> {



More information about the Mlir-commits mailing list