[Mlir-commits] [mlir] [mlir][quant] Print actual quant storage type when signed (PR #187300)

German Gambon llvmlistbot at llvm.org
Thu Apr 2 08:17:02 PDT 2026


https://github.com/ggambon updated https://github.com/llvm/llvm-project/pull/187300

>From f49619b3d3896c728cc13b0e42c30ecea8c28f2f Mon Sep 17 00:00:00 2001
From: German Gambon <ggambon at tesla.com>
Date: Wed, 18 Mar 2026 08:09:01 -0700
Subject: [PATCH 1/2] Print actual quant storage type when signed

Without the fix, bytecode serialization roundtrip breaks for types that
don't have custom bytecode serializers and contain quant types, since
the fallback mechanism prints the type and the quant printer coerces
signed to signless types.  E.g.  `!custom<!quant.uniform<ui8:f32, 0.1>>`
will print as `u8` when serializatimg and later be created as a signless
`i8` when deserializing.
---
 mlir/include/mlir/IR/BuiltinTypes.td       |  8 +++++++-
 mlir/test/Dialect/Quant/parse-uniform.mlir | 18 ++++++++++++++++++
 2 files changed, 25 insertions(+), 1 deletion(-)

diff --git a/mlir/include/mlir/IR/BuiltinTypes.td b/mlir/include/mlir/IR/BuiltinTypes.td
index e7d0a03a85e7d..20c41c5f79729 100644
--- a/mlir/include/mlir/IR/BuiltinTypes.td
+++ b/mlir/include/mlir/IR/BuiltinTypes.td
@@ -680,7 +680,13 @@ def Builtin_Integer : Builtin_Type<"Integer", "integer",
 
     /// Get the storage type as a string.
     std::string getStorageTypeName(bool isSigned) const {
-      return (isSigned ? "i" : "u") + std::to_string(getWidth());
+      if (isSignless()) {
+        return (isSigned ? "i" : "u") + std::to_string(getWidth());
+      } else {
+        std::string s;
+        llvm::raw_string_ostream(s) << *this;
+        return s;
+      }
     }
 
     /// Check if this integer type uses packed representation.
diff --git a/mlir/test/Dialect/Quant/parse-uniform.mlir b/mlir/test/Dialect/Quant/parse-uniform.mlir
index 1431403aece41..a8b9e5707b474 100644
--- a/mlir/test/Dialect/Quant/parse-uniform.mlir
+++ b/mlir/test/Dialect/Quant/parse-uniform.mlir
@@ -128,6 +128,24 @@ func.func @parse() -> !qalias {
   return %0 : !qalias
 }
 
+// -----
+// Storage type: ui8 (explicit unsigned)
+// CHECK: !quant.uniform<ui8:f32, 1.000000e+00>
+!qalias = !quant.uniform<ui8:f32, 1.0>
+func.func @parse() -> !qalias {
+  %0 = "foo"() : () -> !qalias
+  return %0 : !qalias
+}
+
+// -----
+// Storage type: si8 (explicit signed)
+// CHECK: !quant.uniform<si8:f32, 1.000000e+00>
+!qalias = !quant.uniform<si8:f32, 1.0>
+func.func @parse() -> !qalias {
+  %0 = "foo"() : () -> !qalias
+  return %0 : !qalias
+}
+
 // -----
 // Per-axis scales and zero points (affine)
 // CHECK: !quant.uniform<u8:f32:1, {2.000000e+02:-120,9.987200e-01:127}>

>From 918c4b8dd9abdefef86d2cc3195be51958782bdf Mon Sep 17 00:00:00 2001
From: German Gambon <ggambon at tesla.com>
Date: Thu, 2 Apr 2026 08:08:42 -0700
Subject: [PATCH 2/2] Add bytecode round-trip test for signed quant storage
 types

---
 mlir/test/Dialect/Quant/Bytecode/types.mlir | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/mlir/test/Dialect/Quant/Bytecode/types.mlir b/mlir/test/Dialect/Quant/Bytecode/types.mlir
index 8c79b757eeb19..9655ca6a76bd9 100644
--- a/mlir/test/Dialect/Quant/Bytecode/types.mlir
+++ b/mlir/test/Dialect/Quant/Bytecode/types.mlir
@@ -10,6 +10,18 @@ module @parseAnyFullySpecified attributes {
   bytecode.test = !quant.any<i8<-8:7>:f32>
 } {}
 
+// CHECK-LABEL: parseAnyFullySpecifiedUi8
+module @parseAnyFullySpecifiedUi8 attributes {
+  // CHECK: bytecode.test = !quant.any<ui8<0:7>:f32>
+  bytecode.test = !quant.any<ui8<0:7>:f32>
+} {}
+
+// CHECK-LABEL: parseAnyFullySpecifiedSi8
+module @parseAnyFullySpecifiedSi8 attributes {
+  // CHECK: bytecode.test = !quant.any<si8<-8:7>:f32>
+  bytecode.test = !quant.any<si8<-8:7>:f32>
+} {}
+
 // CHECK-LABEL: parseAnyNoExpressedType
 module @parseAnyNoExpressedType attributes {
   // CHECK: bytecode.test = !quant.any<i8<-8:7>>



More information about the Mlir-commits mailing list