[Mlir-commits] [mlir] [mlir][SPIRV] Diagnose missing return value (PR #210566)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jul 23 23:24:47 PDT 2026


https://github.com/qyingwu updated https://github.com/llvm/llvm-project/pull/210566

>From 6b10020be20510204756ac34199600e578d963f1 Mon Sep 17 00:00:00 2001
From: qyingwu <qiyingwu at utexas.edu>
Date: Sat, 18 Jul 2026 19:10:29 -0700
Subject: [PATCH] [mlir][SPIRV] Diagnose missing return value

---
 mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp        |  6 +++++-
 .../test-legalize-erased-op-with-uses.mlir    | 21 ++++++++++++++++++-
 2 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
index 99bc3913c56c6..6bd91e7dfd8df 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVOps.cpp
@@ -1226,7 +1226,11 @@ LogicalResult spirv::FuncOp::verifyBody() {
                    "returns 1 value but enclosing function requires ")
                << fnType.getNumResults() << " results";
 
-      auto retOperandType = retOp.getValue().getType();
+      Value retOperand = retOp.getValue();
+      if (!retOperand)
+        return retOp.emitOpError("return value is missing");
+
+      auto retOperandType = retOperand.getType();
       auto fnResultType = fnType.getResult(0);
       if (retOperandType != fnResultType)
         return retOp.emitOpError(" return value's type (")
diff --git a/mlir/test/Transforms/test-legalize-erased-op-with-uses.mlir b/mlir/test/Transforms/test-legalize-erased-op-with-uses.mlir
index 031442b0ee2da..bf92397a9aa49 100644
--- a/mlir/test/Transforms/test-legalize-erased-op-with-uses.mlir
+++ b/mlir/test/Transforms/test-legalize-erased-op-with-uses.mlir
@@ -1,4 +1,4 @@
-// RUN: mlir-opt %s -test-legalize-unknown-root-patterns -verify-diagnostics
+// RUN: mlir-opt %s -test-legalize-unknown-root-patterns -split-input-file -verify-diagnostics
 
 // Test that an error is emitted when an operation is marked as "erased", but
 // has users that live across the conversion.
@@ -8,3 +8,22 @@ func.func @remove_all_ops(%arg0: i32) -> i32 {
   // expected-note at below {{see existing live user here}}
   return %0 : i32
 }
+
+// -----
+
+// Test that diagnostics for a failed conversion do not crash when printing
+// malformed SPIR-V IR produced during unresolved materialization cleanup.
+module {
+  spirv.func @remove_test_ops() -> i32 "None" {
+    %cst1_i32 = spirv.Constant 1 : i32
+    // expected-error at below {{failed to legalize unresolved materialization from () to ('i32') that remained live after conversion}}
+    %0 = test.with_bounds {smax = 0 : si32, smin = 0 : si32, umax = 0 : i32, umin = 0 : ui32} : i32
+    %1 = builtin.unrealized_conversion_cast %cst1_i32 : i32 to i32
+    // expected-note at below {{see existing live user here}}
+    %2 = spirv.IAdd %0, %1 : i32
+    %3 = builtin.unrealized_conversion_cast %2 : i32 to i32
+    %4 = test.reflect_bounds %3 : i32
+    %5 = builtin.unrealized_conversion_cast %4 : i32 to i32
+    spirv.ReturnValue %5 : i32
+  }
+}



More information about the Mlir-commits mailing list