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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Jun 12 08:20:39 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir

Author: Arseniy Obolenskiy (aobolensk)

<details>
<summary>Changes</summary>



---
Full diff: https://github.com/llvm/llvm-project/pull/203561.diff


2 Files Affected:

- (modified) mlir/lib/Dialect/SPIRV/IR/ControlFlowOps.cpp (+8-6) 
- (modified) mlir/test/Dialect/SPIRV/IR/control-flow-ops.mlir (+16) 


``````````diff
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) -> () {

``````````

</details>


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


More information about the Mlir-commits mailing list