[Mlir-commits] [mlir] [mlir][tosa] Fix crash in validation pass when dialect is not loaded (PR #173449)
NohHyeon Kwon
llvmlistbot at llvm.org
Wed Dec 24 09:11:40 PST 2025
https://github.com/swote-git updated https://github.com/llvm/llvm-project/pull/173449
>From a397aaed19f0d062f3253da55394f2126b084915 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 | 8 +++----
.../Dialect/Tosa/tosa_validation_init.mlir | 21 +++++++++++++++++++
2 files changed, 25 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..6cdac42eb036b 100644
--- a/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
+++ b/mlir/lib/Dialect/Tosa/Transforms/TosaValidation.cpp
@@ -1262,6 +1262,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());
@@ -1269,10 +1273,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..5c4a8e010a911
--- /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>
+}
More information about the Mlir-commits
mailing list