[Mlir-commits] [mlir] [mlir][quant] Fix printer to preserve non-signless storage type (PR #187300)
German Gambon
llvmlistbot at llvm.org
Wed Mar 18 08:22:44 PDT 2026
https://github.com/ggambon created https://github.com/llvm/llvm-project/pull/187300
## Summary
- Fix `IntegerType::getStorageTypeName()` to print the actual type (e.g. `ui8`, `si8`) when the storage type is non-signless, instead of always using the `isSigned` flag to choose between `i`/`u` prefix
- Without the fix, bytecode serialization roundtrip breaks for types that don't have custom bytecode serializers and contain quant types, since the fallback text printer coerces non-signless storage types to signless (e.g. `ui8` → `u8`, `si8` → `i8`)
- Add roundtrip tests for `ui8` and `si8` storage types
## Test plan
- [x] All 13 quant dialect tests pass
- [x] Bytecode roundtrip test passes
- [x] New `ui8`/`si8` test cases pass with fix, fail without
🤖 Generated with [Claude Code](https://claude.com/claude-code)
>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] 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}>
More information about the Mlir-commits
mailing list