[Mlir-commits] [mlir] [mlir][tosa] Validate variable element types (PR #205873)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sat Jul 11 10:09:57 PDT 2026


https://github.com/mygitljf updated https://github.com/llvm/llvm-project/pull/205873

>From 62993eafa5d487b3a786867622e5a52a37da217e Mon Sep 17 00:00:00 2001
From: mygitljf <2410316423 at qq.com>
Date: Fri, 26 Jun 2026 01:54:59 +0000
Subject: [PATCH 1/3] [mlir][tosa] fallback unknown type

---
 .../mlir/Dialect/Tosa/IR/TosaProfileCompliance.h   |  2 +-
 .../Tosa/Transforms/TosaProfileCompliance.cpp      |  4 ++--
 .../Tosa/tosa-validation-version-1p1-invalid.mlir  | 14 ++++++++++++++
 3 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h b/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
index 0135a651be481..7c185b3a9a1f2 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
@@ -145,7 +145,7 @@ class TosaProfileCompliance {
   SmallVector<StringRef>
   stringifyProfile(const SmallVector<ArrayRef<T>> &profileSet);
 
-  static llvm::SmallString<7> stringifyTypeInfo(const TypeInfo &typeInfo);
+  static llvm::SmallString<16> stringifyTypeInfo(const TypeInfo &typeInfo);
 
 private:
   template <typename T>
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
index 0b4983bea4bc6..4b61782a468ec 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
@@ -687,7 +687,7 @@ SmallVector<StringRef> TosaProfileCompliance::stringifyProfile(
   return debugStrings;
 }
 
-llvm::SmallString<7>
+llvm::SmallString<16>
 TosaProfileCompliance::stringifyTypeInfo(const TypeInfo &typeInfo) {
   if (typeInfo.typeID == mlir::IntegerType::getTypeID()) {
     return {"i" + llvm::utostr(typeInfo.bitWidth)};
@@ -713,5 +713,5 @@ TosaProfileCompliance::stringifyTypeInfo(const TypeInfo &typeInfo) {
   } else if (typeInfo.typeID == tosa::mxint8Type::getTypeID()) {
     return {"mxint8"};
   }
-  llvm_unreachable("unknown type");
+  return {"unknown<" + llvm::utostr(typeInfo.bitWidth) + ">"};
 }
diff --git a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
index 5ce54980c8d61..7e341254e8ece 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
@@ -25,3 +25,17 @@ func.func @test_avg_pool2d_adaptive_non_const_output_zp(%arg0: tensor<1x32x32x8x
        (tensor<1x32x32x8xf32>, tensor<1xf32>, tensor<1xf32>, !tosa.shape<2>, !tosa.shape<2>, !tosa.shape<4>) -> tensor<1x32x32x8xf32>
   return %0 : tensor<1x32x32x8xf32>
 }
+
+// -----
+
+module {
+  // expected-error at +1 {{'tosa.variable' op illegal: operation operand/result data types did not align with any profile or extension, got (unknown<64>), did you mean (i8)? Otherwise, please refer to the 'supported data types' for 'tosa.variable' in the specification.}}
+  tosa.variable @dynamic_var : tensor<2x?xf64>
+}
+
+// -----
+
+module {
+  // expected-error at +1 {{'tosa.variable' op illegal: operation operand/result data types did not align with any profile or extension, got (unknown<64>), did you mean (i8)? Otherwise, please refer to the 'supported data types' for 'tosa.variable' in the specification.}}
+  tosa.variable @static_var : tensor<2x4xf64>
+}

>From 8362bde4d8143ba6062e45f818420aa6981be9ea Mon Sep 17 00:00:00 2001
From: mygitljf <2410316423 at qq.com>
Date: Fri, 10 Jul 2026 19:57:52 +0000
Subject: [PATCH 2/3] [mlir][tosa] Validate variable element types

---
 .../Dialect/Tosa/IR/TosaProfileCompliance.h   |  2 +-
 .../Tosa/Transforms/TosaProfileCompliance.cpp |  4 +-
 .../Tosa/Transforms/TosaValidation.cpp        | 54 +++++++++++++------
 .../tosa-validation-version-1p1-invalid.mlir  |  4 +-
 4 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h b/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
index 7c185b3a9a1f2..0135a651be481 100644
--- a/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
+++ b/mlir/include/mlir/Dialect/Tosa/IR/TosaProfileCompliance.h
@@ -145,7 +145,7 @@ class TosaProfileCompliance {
   SmallVector<StringRef>
   stringifyProfile(const SmallVector<ArrayRef<T>> &profileSet);
 
-  static llvm::SmallString<16> stringifyTypeInfo(const TypeInfo &typeInfo);
+  static llvm::SmallString<7> stringifyTypeInfo(const TypeInfo &typeInfo);
 
 private:
   template <typename T>
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
index 4b61782a468ec..0b4983bea4bc6 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaProfileCompliance.cpp
@@ -687,7 +687,7 @@ SmallVector<StringRef> TosaProfileCompliance::stringifyProfile(
   return debugStrings;
 }
 
-llvm::SmallString<16>
+llvm::SmallString<7>
 TosaProfileCompliance::stringifyTypeInfo(const TypeInfo &typeInfo) {
   if (typeInfo.typeID == mlir::IntegerType::getTypeID()) {
     return {"i" + llvm::utostr(typeInfo.bitWidth)};
@@ -713,5 +713,5 @@ TosaProfileCompliance::stringifyTypeInfo(const TypeInfo &typeInfo) {
   } else if (typeInfo.typeID == tosa::mxint8Type::getTypeID()) {
     return {"mxint8"};
   }
-  return {"unknown<" + llvm::utostr(typeInfo.bitWidth) + ">"};
+  llvm_unreachable("unknown type");
 }
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 5d4a2affbd7cd..d3abb7c2955c7 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -670,6 +670,10 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
   LogicalResult CheckVariable(Operation *op);
   LogicalResult CheckVariableReadOrWrite(Operation *op);
   bool isValidElementType(Type type, const bool allowUnsigned = false);
+  LogicalResult validateElementType(Operation *op, Type elementTy,
+                                    bool allowUnsigned = false);
+  LogicalResult validateOperationElementTypes(Operation *op,
+                                              bool allowUnsigned = false);
 
   SmallVector<
       std::function<LogicalResult(Operation *, const tosa::TargetEnv &)>>
@@ -1500,6 +1504,38 @@ bool TosaValidation::isValidElementType(Type type, const bool allowUnsigned) {
   return false;
 }
 
+LogicalResult TosaValidation::validateElementType(Operation *op, Type elementTy,
+                                                  bool allowUnsigned) {
+  if (isValidElementType(elementTy, allowUnsigned))
+    return success();
+
+  return op->emitOpError() << "is not profile-aligned: element type "
+                           << elementTy << " is not legal";
+}
+
+LogicalResult
+TosaValidation::validateOperationElementTypes(Operation *op,
+                                              bool allowUnsigned) {
+  for (Value operand : op->getOperands()) {
+    Type elementTy = getElementTypeOrSelf(operand);
+    if (failed(validateElementType(op, elementTy, allowUnsigned)))
+      return failure();
+  }
+
+  for (Type resultTy : op->getResultTypes()) {
+    Type elementTy = getElementTypeOrSelf(resultTy);
+    if (failed(validateElementType(op, elementTy, allowUnsigned)))
+      return failure();
+  }
+
+  if (auto variableOp = dyn_cast<tosa::VariableOp>(op)) {
+    if (failed(validateElementType(op, variableOp.getType(), allowUnsigned)))
+      return failure();
+  }
+
+  return success();
+}
+
 void TosaValidation::runOnOperation() {
   ModuleOp modOp = getOperation();
   TosaDialect *tosaDialect = getContext().getLoadedDialect<TosaDialect>();
@@ -1530,22 +1566,8 @@ void TosaValidation::runOnOperation() {
     //   protect rest of code against quantized element types
     const bool allowUnsigned =
         !strictOpSpecAlignment && isa<tosa::RescaleOp>(op);
-    for (Value operand : op->getOperands()) {
-      auto elementTy = getElementTypeOrSelf(operand);
-      if (!isValidElementType(elementTy, allowUnsigned)) {
-        op->emitOpError() << "is not profile-aligned: element type "
-                          << elementTy << " is not legal";
-        return signalPassFailure();
-      }
-    }
-    for (Type resultTy : op->getResultTypes()) {
-      auto elementTy = getElementTypeOrSelf(resultTy);
-      if (!isValidElementType(elementTy, allowUnsigned)) {
-        op->emitOpError() << "is not profile-aligned: element type "
-                          << elementTy << " is not legal";
-        return signalPassFailure();
-      }
-    }
+    if (failed(validateOperationElementTypes(op, allowUnsigned)))
+      return signalPassFailure();
 
     if (strictOpSpecAlignment &&
         failed(profileComp.checkProfile(op, targetEnv)))
diff --git a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
index 7e341254e8ece..19ccef0f52a41 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
@@ -29,13 +29,13 @@ func.func @test_avg_pool2d_adaptive_non_const_output_zp(%arg0: tensor<1x32x32x8x
 // -----
 
 module {
-  // expected-error at +1 {{'tosa.variable' op illegal: operation operand/result data types did not align with any profile or extension, got (unknown<64>), did you mean (i8)? Otherwise, please refer to the 'supported data types' for 'tosa.variable' in the specification.}}
+  // expected-error at +1 {{'tosa.variable' op is not profile-aligned: element type 'f64' is not legal}}
   tosa.variable @dynamic_var : tensor<2x?xf64>
 }
 
 // -----
 
 module {
-  // expected-error at +1 {{'tosa.variable' op illegal: operation operand/result data types did not align with any profile or extension, got (unknown<64>), did you mean (i8)? Otherwise, please refer to the 'supported data types' for 'tosa.variable' in the specification.}}
+  // expected-error at +1 {{'tosa.variable' op is not profile-aligned: element type 'f64' is not legal}}
   tosa.variable @static_var : tensor<2x4xf64>
 }

>From 6e7fcfabbb89c5f3592bb722dc4164ad91808b5f Mon Sep 17 00:00:00 2001
From: mygitljf <2410316423 at qq.com>
Date: Sun, 12 Jul 2026 01:12:22 +0000
Subject: [PATCH 3/3] [mlir][tosa] Refine variable element type validation

---
 .../Tosa/Transforms/TosaValidation.cpp        | 39 ++++++++-----------
 .../tosa-validation-version-1p1-invalid.mlir  |  7 ----
 2 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index d3abb7c2955c7..d211842c72de2 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -669,9 +669,8 @@ struct TosaValidation : public tosa::impl::TosaValidationBase<TosaValidation> {
 
   LogicalResult CheckVariable(Operation *op);
   LogicalResult CheckVariableReadOrWrite(Operation *op);
-  bool isValidElementType(Type type, const bool allowUnsigned = false);
-  LogicalResult validateElementType(Operation *op, Type elementTy,
-                                    bool allowUnsigned = false);
+  LogicalResult validateValidElementType(Operation *op, Type type,
+                                         bool allowUnsigned = false);
   LogicalResult validateOperationElementTypes(Operation *op,
                                               bool allowUnsigned = false);
 
@@ -1472,11 +1471,13 @@ LogicalResult TosaValidation::applyFunctionSignatureCheck(func::FuncOp op) {
   return success();
 }
 
-bool TosaValidation::isValidElementType(Type type, const bool allowUnsigned) {
+LogicalResult TosaValidation::validateValidElementType(Operation *op, Type type,
+                                                       bool allowUnsigned) {
   if (isa<FloatType>(type)) {
-    return isa<Float32Type, Float16Type, BFloat16Type, Float8E4M3FNType,
-               Float8E5M2Type, Float4E2M1FNType, Float6E2M3FNType,
-               Float6E3M2FNType, Float8E8M0FNUType>(type);
+    if (isa<Float32Type, Float16Type, BFloat16Type, Float8E4M3FNType,
+            Float8E5M2Type, Float4E2M1FNType, Float6E2M3FNType,
+            Float6E3M2FNType, Float8E8M0FNUType>(type))
+      return success();
   } else if (auto intTy = dyn_cast<IntegerType>(type)) {
     if (intTy.isSignless()) {
       switch (intTy.getWidth()) {
@@ -1487,30 +1488,23 @@ bool TosaValidation::isValidElementType(Type type, const bool allowUnsigned) {
       case 32:
       case 48:
       case 64:
-        return true;
+        return success();
       }
     } else if (allowUnsigned && intTy.isUnsigned()) {
       switch (intTy.getWidth()) {
       case 8:
       case 16:
       case 32:
-        return true;
+        return success();
       }
     }
   } else if (isa<tosa::shapeType>(type))
-    return true;
+    return success();
   else if (isa<tosa::mxint8Type>(type))
-    return true;
-  return false;
-}
-
-LogicalResult TosaValidation::validateElementType(Operation *op, Type elementTy,
-                                                  bool allowUnsigned) {
-  if (isValidElementType(elementTy, allowUnsigned))
     return success();
 
-  return op->emitOpError() << "is not profile-aligned: element type "
-                           << elementTy << " is not legal";
+  return op->emitOpError() << "is not profile-aligned: element type " << type
+                           << " is not legal";
 }
 
 LogicalResult
@@ -1518,18 +1512,19 @@ TosaValidation::validateOperationElementTypes(Operation *op,
                                               bool allowUnsigned) {
   for (Value operand : op->getOperands()) {
     Type elementTy = getElementTypeOrSelf(operand);
-    if (failed(validateElementType(op, elementTy, allowUnsigned)))
+    if (failed(validateValidElementType(op, elementTy, allowUnsigned)))
       return failure();
   }
 
   for (Type resultTy : op->getResultTypes()) {
     Type elementTy = getElementTypeOrSelf(resultTy);
-    if (failed(validateElementType(op, elementTy, allowUnsigned)))
+    if (failed(validateValidElementType(op, elementTy, allowUnsigned)))
       return failure();
   }
 
   if (auto variableOp = dyn_cast<tosa::VariableOp>(op)) {
-    if (failed(validateElementType(op, variableOp.getType(), allowUnsigned)))
+    if (failed(
+            validateValidElementType(op, variableOp.getType(), allowUnsigned)))
       return failure();
   }
 
diff --git a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
index 19ccef0f52a41..20e004077d291 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-version-1p1-invalid.mlir
@@ -32,10 +32,3 @@ module {
   // expected-error at +1 {{'tosa.variable' op is not profile-aligned: element type 'f64' is not legal}}
   tosa.variable @dynamic_var : tensor<2x?xf64>
 }
-
-// -----
-
-module {
-  // expected-error at +1 {{'tosa.variable' op is not profile-aligned: element type 'f64' is not legal}}
-  tosa.variable @static_var : tensor<2x4xf64>
-}



More information about the Mlir-commits mailing list