[Mlir-commits] [mlir] adc79c0 - [mlir][tosa] Fix crash in validation pass when dialect is not loaded (#173449)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Jan 6 01:17:41 PST 2026


Author: NohHyeon Kwon
Date: 2026-01-06T09:17:37Z
New Revision: adc79c09dd77a84d5359793fafb3e0d44dcafb57

URL: https://github.com/llvm/llvm-project/commit/adc79c09dd77a84d5359793fafb3e0d44dcafb57
DIFF: https://github.com/llvm/llvm-project/commit/adc79c09dd77a84d5359793fafb3e0d44dcafb57.diff

LOG: [mlir][tosa] Fix crash in validation pass when dialect is not loaded (#173449)

## Summary

This PR fixes #173370 in the `tosa-validate` pass that occurs when the
input IR does not contain any TOSA operations.

**Crash Message:**
```text
LLVM ERROR: can't create Attribute 'mlir::tosa::TargetEnvAttr' because storage uniquer isn't initialized: the dialect was likely not loaded, or the attribute wasn't added with addAttributes<...>() in the Dialect::initialize() method.
```
## Problem

When `mlir-opt` parses an input file without TOSA operations, the
`TosaDialect` is not lazily loaded. However, the `TosaValidation` pass
previously called `lookupTargetEnvOrDefault` (which attempts to create a
`TargetEnvAttr`) before checking if the dialect was loaded. This
resulted in an assertion failure because the attribute storage uniquer
was not initialized.

## Solution

I resolved the issue by placing the `TosaDialect` declaration at the
very top of `runOnOperation`.
This ensures that `lookupTargetEnvOrDefault` is not accessed when the
dialect is uninitialized, preventing the crash.


## Test

Added two test in `mlir/test/Dialect/Tosa/tosa_validation_init.mlir`.

First case is without TOSA operation.

```
// CHECK-LABEL: func.func @test_validation_pass_init
func.func @test_validation_pass_init(%arg0: tensor<1xf32>) -> tensor<1xf32> {
  // CHECK: math.asin
  %0 = math.asin %arg0 : tensor<1xf32>
  return %0 : tensor<1xf32>
}
```

Second case is with TOSA Operation.
```
// CHECK-LABEL: func.func @test_tosa_ops
func.func @test_tosa_ops(%arg0: tensor<1x2x3x4xf32>, %arg1: tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32> {
  // CHECK: tosa.add
  %0 = tosa.add %arg0, %arg1 : (tensor<1x2x3x4xf32>, tensor<1x2x3x4xf32>) -> tensor<1x2x3x4xf32>
  return %0 : tensor<1x2x3x4xf32>
}
```

Added: 
    

Modified: 
    mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
    mlir/test/Dialect/Tosa/tosa-validation-valid.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 897cc87529eca..e0943059e80ab 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -1264,6 +1264,10 @@ bool TosaValidation::isValidElementType(Type type, const bool allowUnsigned) {
 
 void TosaValidation::runOnOperation() {
   ModuleOp modOp = getOperation();
+  TosaDialect *tosaDialect = getContext().getLoadedDialect<TosaDialect>();
+  if (!tosaDialect)
+    return;
+
   const TargetEnvAttr targetEnvAttr = lookupTargetEnvOrDefault(modOp);
   const auto maybeTargetEnv =
       tosa::TargetEnv::createTargetEnvFromAttr(targetEnvAttr, modOp.getLoc());
@@ -1271,10 +1275,6 @@ void TosaValidation::runOnOperation() {
     return signalPassFailure();
   targetEnv = *maybeTargetEnv;
 
-  TosaDialect *tosaDialect = getContext().getLoadedDialect<TosaDialect>();
-  if (!tosaDialect)
-    return;
-
   modOp.walk([&](Operation *op) {
     if (op->getDialect() != tosaDialect)
       return;

diff  --git a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
index 663159e75d1a6..036a3d4d0ba2e 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
@@ -29,3 +29,11 @@ func.func @test_rescale_output_unsigned(%arg0: tensor<1x1xi8>) -> (tensor<1x1xui
   %r = tosa.rescale %arg0, %1, %0, %3, %2 {input_unsigned = false, output_unsigned = true, per_channel = false, rounding_mode = SINGLE_ROUND, scale32 = true} : (tensor<1x1xi8>, tensor<1xi32>, tensor<1xi8>, tensor<1xi8>, tensor<1xi8>) -> tensor<1x1xui8>
   return %r : tensor<1x1xui8>
 }
+
+// -----
+
+// CHECK-LABEL: test_validate_without_tosa
+func.func @test_validate_without_tosa(%arg0: f32) -> f32 {
+  %0 = math.asin %arg0 : f32
+  return %0 : f32
+}


        


More information about the Mlir-commits mailing list