[Mlir-commits] [mlir] [mlir][tosa] Fix crash in validation pass when dialect is not loaded (PR #173449)
NohHyeon Kwon
llvmlistbot at llvm.org
Tue Dec 23 21:11:24 PST 2025
https://github.com/swote-git created https://github.com/llvm/llvm-project/pull/173449
## 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>
}
```
>From 65315eb8eecb3efb0052b5011328ac21a70381b8 Mon Sep 17 00:00:00 2001
From: swote-git <kst7703 at gmail.com>
Date: Wed, 24 Dec 2025 14:04:18 +0900
Subject: [PATCH] [mlir][tosa] Fix crash in validation pass when dialect is not
loaded
The `tosa-validate` pass previously crashed with an "LLVM ERROR: can't create Attribute..." if the TOSA dialect was not loaded (e.g., when the input IR contained no TOSA operations). This occurred because `lookupTargetEnvOrDefault` attempted to access the attribute storage uniquer before the dialect was initialized.
This patch fixes the issue by moving the check for the loaded TosaDialect to the beginning of `runOnOperation`.
If the dialect is not loaded, the pass now safely returns early, as there are no TOSA operations to validate.
Added a test to verify the fix.
Fixes #173370
---
.../Tosa/Transforms/TosaValidation.cpp | 9 ++++----
.../Dialect/Tosa/tosa_validation_init.mlir | 21 +++++++++++++++++++
2 files changed, 26 insertions(+), 4 deletions(-)
create mode 100644 mlir/test/Dialect/Tosa/tosa_validation_init.mlir
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
More information about the Mlir-commits
mailing list