[Mlir-commits] [mlir] [mlir][tosa] Allow numeric values to be specified for mxint8 constants (PR #200762)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Jun 1 02:09:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Luke Hutton (lhutton1)

<details>
<summary>Changes</summary>

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.

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


4 Files Affected:

- (modified) mlir/include/mlir/Dialect/Tosa/IR/TosaTypesBase.td (+7-1) 
- (modified) mlir/lib/Dialect/Tosa/IR/TosaOps.cpp (+31) 
- (modified) mlir/test/Dialect/Tosa/ops.mlir (+8-1) 
- (modified) mlir/test/Dialect/Tosa/verifier.mlir (+18) 


``````````diff
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..e8d170e955c47 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -711,6 +711,37 @@ 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");
+  int8_t value;
+  std::memcpy(&value, rawData.data(), sizeof(value));
+  return IntegerAttr::get(IntegerType::get(getContext(), 8), value);
+}
+
+LogicalResult mlir::tosa::mxint8Type::convertFromAttribute(
+    Attribute attr, SmallVectorImpl<char> &result) const {
+  const auto intAttr = dyn_cast<IntegerAttr>(attr);
+  if (!intAttr || intAttr.getType().isUnsignedInteger())
+    return failure();
+  const std::optional<int64_t> maybeValue = intAttr.getValue().trySExtValue();
+  if (!maybeValue)
+    return failure();
+  const int64_t value = maybeValue.value();
+  if (value < std::numeric_limits<int8_t>::min() ||
+      value > std::numeric_limits<int8_t>::max())
+    return failure();
+  const int8_t value8 = static_cast<int8_t>(value);
+  result.append(reinterpret_cast<const char *>(&value8),
+                reinterpret_cast<const char *>(&value8) + sizeof(value8));
+  return success();
+}
+
 //===----------------------------------------------------------------------===//
 // TOSA Operator Verifiers.
 //===----------------------------------------------------------------------===//
diff --git a/mlir/test/Dialect/Tosa/ops.mlir b/mlir/test/Dialect/Tosa/ops.mlir
index 5c368b3da4ff5..320b03678e827 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, -128]>} : () -> 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..abd2c76991b81 100644
--- a/mlir/test/Dialect/Tosa/verifier.mlir
+++ b/mlir/test/Dialect/Tosa/verifier.mlir
@@ -2057,3 +2057,21 @@ 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>
 }
+
+// -----
+
+// CHECK-LABEL: test_const_mxint8_uint8
+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>
+}
+
+// -----
+
+// CHECK-LABEL: test_const_mxint8_int64_bounds_check
+func.func @test_const_mxint8_int64_bounds_check(%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>
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/200762


More information about the Mlir-commits mailing list