[Mlir-commits] [mlir] [mlir][tosa] Allow numeric values to be specified for mxint8 constants (PR #200762)
Luke Hutton
llvmlistbot at llvm.org
Tue Jun 2 02:18:01 PDT 2026
https://github.com/lhutton1 updated https://github.com/llvm/llvm-project/pull/200762
>From 539e0e58c813e53e52595a033b18a5ec65de30e0 Mon Sep 17 00:00:00 2001
From: Luke Hutton <luke.hutton at arm.com>
Date: Mon, 1 Jun 2026 09:47:13 +0100
Subject: [PATCH] [mlir][tosa] Allow numeric values to be specified for mxint8
constants
This commit uses the DenseElementTypeInterface to allow signless numeric
values to be specified for mxint8 constants. This is more user-friendly
than the previous hex representation.
Change-Id: Ife313b1d0d04dda02ed6eeee4f3fb4fe6e4eb80d
---
.../mlir/Dialect/Tosa/IR/TosaTypesBase.td | 8 ++++++-
mlir/lib/Dialect/Tosa/IR/TosaOps.cpp | 23 +++++++++++++++++++
mlir/test/Dialect/Tosa/ops.mlir | 9 +++++++-
mlir/test/Dialect/Tosa/verifier.mlir | 16 +++++++++++++
4 files changed, 54 insertions(+), 2 deletions(-)
diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
index 5b5189c84f4a3..10ddd3438aedd 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td
@@ -14,6 +14,7 @@
#define TOSA_TYPES_BASE
include "mlir/IR/AttrTypeBase.td"
+include "mlir/IR/BuiltinTypeInterfaces.td"
include "mlir/IR/OpBase.td"
include "mlir/Dialect/Tosa/IR/TosaOpBase.td"
@@ -90,7 +91,12 @@ def Tosa_QuantizedInt : AnyTypeOf<[Tosa_QuantizedType<"uint8", [8], 0>,
// MLIR doesn't have a builtin type for mxint8 yet. For now declared it as a
// custom TOSA type. This may be changed in the future.
-def Tosa_MXInt8 : Tosa_Type<"mxint8", "mxint8"> {
+def Tosa_MXInt8
+ : Tosa_Type<"mxint8", "mxint8",
+ [DeclareTypeInterfaceMethods<
+ DenseElementTypeInterface, ["getDenseElementBitSize",
+ "convertToAttribute",
+ "convertFromAttribute"]>]> {
let summary = "INT8 type as defined by OCP-MX";
let description = [{
8-bit integer format with an implicit 1/64 scale defined by OCP-MX.
diff --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 351dc1ae0dcd5..4fedb29117499 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -711,6 +711,29 @@ LogicalResult verifyConvOutputSize(
return success();
}
+//===----------------------------------------------------------------------===//
+// TOSA block scaling utilities.
+//===----------------------------------------------------------------------===//
+size_t mlir::tosa::mxint8Type::getDenseElementBitSize() const { return 8; }
+
+Attribute
+mlir::tosa::mxint8Type::convertToAttribute(ArrayRef<char> rawData) const {
+ assert(rawData.size() == 1 && "expected 1 byte for tosa.mxint8 element");
+ const auto intType = IntegerType::get(getContext(), 8);
+ return intType.convertToAttribute(rawData);
+}
+
+LogicalResult mlir::tosa::mxint8Type::convertFromAttribute(
+ Attribute attr, SmallVectorImpl<char> &result) const {
+ const auto intAttr = dyn_cast<IntegerAttr>(attr);
+ if (!intAttr)
+ return failure();
+ const Type attrType = intAttr.getType();
+ if (!attrType.isSignlessInteger(8))
+ return failure();
+ return cast<IntegerType>(attrType).convertFromAttribute(attr, result);
+}
+
//===----------------------------------------------------------------------===//
// TOSA Operator Verifiers.
//===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Tosa/ops.mlir b/mlir/test/Dialect/Tosa/ops.mlir
index 5c368b3da4ff5..597d92825fd47 100644
--- a/mlir/test/Dialect/Tosa/ops.mlir
+++ b/mlir/test/Dialect/Tosa/ops.mlir
@@ -1611,10 +1611,17 @@ func.func @test_cast_to_block_scaled_mxint8(%arg0: tensor<4x32xf32>) -> (tensor<
return %0#0, %0#1 : tensor<4x32x!tosa.mxint8>, tensor<4x1xf8E8M0FNU>
}
+// -----
+// CHECK-LABEL: test_const_mxint8_hex
+func.func @test_const_mxint8_hex(%arg0 : index) -> tensor<2x!tosa.mxint8> {
+ %0 = "tosa.const"() {values = dense<"0x007F"> : tensor<2x!tosa.mxint8>} : () -> tensor<2x!tosa.mxint8>
+ return %0 : tensor<2x!tosa.mxint8>
+}
+
// -----
// CHECK-LABEL: test_const_mxint8
func.func @test_const_mxint8(%arg0 : index) -> tensor<2x!tosa.mxint8> {
- %0 = "tosa.const"() {values = dense<"0x007F"> : tensor<2x!tosa.mxint8>} : () -> tensor<2x!tosa.mxint8>
+ %0 = "tosa.const"() {values = dense<tensor<2x!tosa.mxint8> : [127 : i8, -128 : i8]>} : () -> tensor<2x!tosa.mxint8>
return %0 : tensor<2x!tosa.mxint8>
}
diff --git a/mlir/test/Dialect/Tosa/verifier.mlir b/mlir/test/Dialect/Tosa/verifier.mlir
index b3ae5b7a5d5f3..9b5faf575971b 100644
--- a/mlir/test/Dialect/Tosa/verifier.mlir
+++ b/mlir/test/Dialect/Tosa/verifier.mlir
@@ -2057,3 +2057,19 @@ func.func @test_maxpool2d_adaptive_unexpected_output_width(%arg0: tensor<1x32x32
(tensor<1x32x32x8xf32>, !tosa.shape<2>, !tosa.shape<2>, !tosa.shape<4>) -> tensor<1x32x2x8xf32>
return %0 : tensor<1x32x2x8xf32>
}
+
+// -----
+
+func.func @test_const_mxint8_uint8(%arg0 : index) -> tensor<2x!tosa.mxint8> {
+ // expected-error at +1 {{incompatible attribute for element type}}
+ %0 = "tosa.const"() {values = dense<tensor<2x!tosa.mxint8> : [127: ui8, 245: ui8]>} : () -> tensor<2x!tosa.mxint8>
+ return %0 : tensor<2x!tosa.mxint8>
+}
+
+// -----
+
+func.func @test_const_mxint8_int64(%arg0 : index) -> tensor<2x!tosa.mxint8> {
+ // expected-error at +1 {{incompatible attribute for element type}}
+ %0 = "tosa.const"() {values = dense<tensor<2x!tosa.mxint8> : [127, 245]>} : () -> tensor<2x!tosa.mxint8>
+ return %0 : tensor<2x!tosa.mxint8>
+}
More information about the Mlir-commits
mailing list