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

NohHyeon Kwon llvmlistbot at llvm.org
Mon Jan 5 03:15:09 PST 2026


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 1/3] [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>
+}

>From 2ea3bed37cfc66b37fa0bc10991071737b9be3bf Mon Sep 17 00:00:00 2001
From: swote-git <kst7703 at gmail.com>
Date: Sun, 4 Jan 2026 23:28:21 +0900
Subject: [PATCH 2/3] test: move tests to tosa-validation-valid

---
 .../Dialect/Tosa/tosa-validation-valid.mlir   | 16 ++++++++++++++
 .../Dialect/Tosa/tosa_validation_init.mlir    | 21 -------------------
 2 files changed, 16 insertions(+), 21 deletions(-)
 delete mode 100644 mlir/test/Dialect/Tosa/tosa_validation_init.mlir

diff --git a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
index 663159e75d1a6..dadce059b04ff 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
@@ -29,3 +29,19 @@ 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
+}
+
+// -----
+
+// CHECK-LABEL: test_validate_with_tosa
+func.func @test_validate_with_tosa(%arg0: tensor<1x2x3xf32>, %arg1: tensor<1x2x3xf32>) -> tensor<1x2x3xf32> {
+  %0 = tosa.add %arg0, %arg1 : (tensor<1x2x3xf32>, tensor<1x2x3xf32>) -> tensor<1x2x3xf32>
+  return %0 : tensor<1x2x3xf32>
+}
diff --git a/mlir/test/Dialect/Tosa/tosa_validation_init.mlir b/mlir/test/Dialect/Tosa/tosa_validation_init.mlir
deleted file mode 100644
index 5c4a8e010a911..0000000000000
--- a/mlir/test/Dialect/Tosa/tosa_validation_init.mlir
+++ /dev/null
@@ -1,21 +0,0 @@
-// 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>
-}

>From 7efc8253f643a8cb0a0d8909b12d937d1fc11c29 Mon Sep 17 00:00:00 2001
From: swote-git <kst7703 at gmail.com>
Date: Mon, 5 Jan 2026 20:14:48 +0900
Subject: [PATCH 3/3] test: remove unnecessary test

---
 mlir/test/Dialect/Tosa/tosa-validation-valid.mlir | 8 --------
 1 file changed, 8 deletions(-)

diff --git a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
index dadce059b04ff..036a3d4d0ba2e 100644
--- a/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
+++ b/mlir/test/Dialect/Tosa/tosa-validation-valid.mlir
@@ -37,11 +37,3 @@ func.func @test_validate_without_tosa(%arg0: f32) -> f32 {
   %0 = math.asin %arg0 : f32
   return %0 : f32
 }
-
-// -----
-
-// CHECK-LABEL: test_validate_with_tosa
-func.func @test_validate_with_tosa(%arg0: tensor<1x2x3xf32>, %arg1: tensor<1x2x3xf32>) -> tensor<1x2x3xf32> {
-  %0 = tosa.add %arg0, %arg1 : (tensor<1x2x3xf32>, tensor<1x2x3xf32>) -> tensor<1x2x3xf32>
-  return %0 : tensor<1x2x3xf32>
-}



More information about the Mlir-commits mailing list