[Mlir-commits] [mlir] [mlir][SPIR-V] Fix empty optional deref in spirv.Switch verifier (PR #203561)

Arseniy Obolenskiy llvmlistbot at llvm.org
Fri Jun 12 08:19:29 PDT 2026


https://github.com/aobolensk created https://github.com/llvm/llvm-project/pull/203561

None

>From 430e907261a39c6cb4ddb313783b16c79d74a87d Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 12 Jun 2026 17:18:26 +0200
Subject: [PATCH] [mlir][SPIR-V] Fix empty optional deref in spirv.Switch
 verifier

---
 mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp     | 14 ++++++++------
 mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir | 16 ++++++++++++++++
 2 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp b/mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp
index 5c2d538a00b17..0ad88dee9fe9e 100644
--- a/mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp
@@ -266,12 +266,14 @@ LogicalResult SwitchOp::verify() {
   if (!literals && targets.empty())
     return success();
 
-  Type selectorType = getSelector().getType();
-  Type literalType = literals->getType().getElementType();
-  if (literalType != selectorType)
-    return emitOpError() << "'selector' type (" << selectorType
-                         << ") should match literals type (" << literalType
-                         << ")";
+  if (literals) {
+    Type selectorType = getSelector().getType();
+    Type literalType = literals->getType().getElementType();
+    if (literalType != selectorType)
+      return emitOpError() << "'selector' type (" << selectorType
+                           << ") should match literals type (" << literalType
+                           << ")";
+  }
 
   if (literals && literals->size() != static_cast<int64_t>(targets.size()))
     return emitOpError() << "number of literals (" << literals->size()
diff --git a/mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir b/mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir
index 81dce9822db48..189040a0745fc 100644
--- a/mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir
+++ b/mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir
@@ -1143,6 +1143,22 @@ func.func @switch_operands(%selector : i32, %operand : i32) {
   spirv.Return
 }
 
+func.func @switch_targets_no_literals(%selector: i32) -> () {
+  // CHECK: spirv.Switch {{%.*}} : i32, [
+  // CHECK-NEXT: default: ^bb1
+  "spirv.Switch"(%selector)[^default, ^target]
+    {case_operand_segments = array<i32: 0>,
+     operandSegmentSizes = array<i32: 1, 0, 0>} : (i32) -> ()
+^default:
+  spirv.Branch ^merge
+
+^target:
+  spirv.Branch ^merge
+
+^merge:
+  spirv.Return
+}
+
 // -----
 
 func.func @switch_float_selector(%selector: f32) -> () {



More information about the Mlir-commits mailing list