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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Dec 23 21:11:56 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir-tosa

@llvm/pr-subscribers-mlir

Author: NohHyeon Kwon (swote-git)

<details>
<summary>Changes</summary>

## Summary

This PR fixes a crash(#<!-- -->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>
}
```


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


2 Files Affected:

- (modified) mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp (+5-4) 
- (added) mlir/test/Dialect/Tosa/tosa_validation_init.mlir (+21) 


``````````diff
diff --git a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
index 530c6ae85287c..591d4bfab9c73 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -1262,6 +1262,11 @@ 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());
@@ -1269,10 +1274,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_init.mlir b/mlir/test/Dialect/Tosa/tosa_validation_init.mlir
new file mode 100644
index 0000000000000..9fb43769b7230
--- /dev/null
+++ b/mlir/test/Dialect/Tosa/tosa_validation_init.mlir
@@ -0,0 +1,21 @@
+// RUN: mlir-opt %s -split-input-file -tosa-validate | FileCheck %s
+
+// --- First case: without TOSA Operation (#173370 bug reproduction) ---
+
+// 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: 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>
+}
\ No newline at end of file

``````````

</details>


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


More information about the Mlir-commits mailing list