[Mlir-commits] [mlir] [mlir][SPIR-V] Add roundtrip and validation tests for spirv.Switch (NFC) (PR #200572)

Arseniy Obolenskiy llvmlistbot at llvm.org
Sat May 30 05:59:52 PDT 2026


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

Add missing `spirv-val` tests for spirv.Switch operation

>From 12057220898a53195975b106c7597ffaa6dd4e12 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Sat, 30 May 2026 14:55:07 +0200
Subject: [PATCH] [NFC][mlir][SPIR-V] Add roundtrip and validation tests for
 spirv.Switch

---
 mlir/test/Target/SPIRV/switch.mlir | 94 ++++++++++++++++++++++++++++++
 1 file changed, 94 insertions(+)
 create mode 100644 mlir/test/Target/SPIRV/switch.mlir

diff --git a/mlir/test/Target/SPIRV/switch.mlir b/mlir/test/Target/SPIRV/switch.mlir
new file mode 100644
index 0000000000000..11f8fca613d80
--- /dev/null
+++ b/mlir/test/Target/SPIRV/switch.mlir
@@ -0,0 +1,94 @@
+// RUN: mlir-translate -no-implicit-module -test-spirv-roundtrip -split-input-file %s | FileCheck %s
+
+// RUN: %if spirv-tools %{ rm -rf %t %}
+// RUN: %if spirv-tools %{ mkdir %t %}
+// RUN: %if spirv-tools %{ mlir-translate --no-implicit-module --serialize-spirv --split-input-file --spirv-save-validation-files-with-prefix=%t/module %s %}
+// RUN: %if spirv-tools %{ spirv-val %t %}
+
+// Multi-way switch routing results through a function variable.
+
+spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
+// CHECK-LABEL: @switch
+  spirv.func @switch(%cond: i32) -> () "None" {
+    %zero = spirv.Constant 0 : i32
+    %var = spirv.Variable init(%zero) : !spirv.ptr<i32, Function>
+
+// CHECK:        spirv.mlir.selection
+    spirv.mlir.selection {
+// CHECK-NEXT:     spirv.Switch %{{.*}} : i32, [
+// CHECK-NEXT:       default: ^[[DEFAULT:.+]],
+// CHECK-NEXT:       2: ^[[CASE2:.+]],
+// CHECK-NEXT:       5: ^[[CASE5:.+]]
+// CHECK-NEXT:     ]
+      spirv.Switch %cond : i32, [
+        default: ^default,
+        2: ^case2,
+        5: ^case5
+      ]
+
+// The deserializer emits the target blocks in branch order: default first,
+// then the case blocks.
+// CHECK-NEXT:   ^[[DEFAULT]]:
+// CHECK-NEXT:     spirv.Constant 30
+// CHECK-NEXT:     spirv.Store
+// CHECK-NEXT:     spirv.Branch ^[[MERGE:.+]]
+// CHECK-NEXT:   ^[[CASE2]]:
+// CHECK-NEXT:     spirv.Constant 10
+// CHECK-NEXT:     spirv.Store
+// CHECK-NEXT:     spirv.Branch ^[[MERGE]]
+// CHECK-NEXT:   ^[[CASE5]]:
+// CHECK-NEXT:     spirv.Constant 20
+// CHECK-NEXT:     spirv.Store
+// CHECK-NEXT:     spirv.Branch ^[[MERGE]]
+// CHECK-NEXT:   ^[[MERGE]]:
+// CHECK-NEXT:     spirv.mlir.merge
+    ^case2:
+      %ten = spirv.Constant 10 : i32
+      spirv.Store "Function" %var, %ten : i32
+      spirv.Branch ^merge
+
+    ^case5:
+      %twenty = spirv.Constant 20 : i32
+      spirv.Store "Function" %var, %twenty : i32
+      spirv.Branch ^merge
+
+    ^default:
+      %thirty = spirv.Constant 30 : i32
+      spirv.Store "Function" %var, %thirty : i32
+      spirv.Branch ^merge
+
+    ^merge:
+      spirv.mlir.merge
+    }
+    spirv.Return
+  }
+}
+
+// -----
+
+// Switch with only a default target (no case literals).
+
+spirv.module Logical GLSL450 requires #spirv.vce<v1.0, [Shader, Linkage], []> {
+// CHECK-LABEL: @switch_default_only
+  spirv.func @switch_default_only(%cond: i32) -> () "None" {
+// CHECK:        spirv.mlir.selection
+    spirv.mlir.selection {
+// CHECK-NEXT:     spirv.Switch %{{.*}} : i32, [
+// CHECK-NEXT:       default: ^[[DEFAULT:.+]]]
+      spirv.Switch %cond : i32, [
+        default: ^default
+      ]
+
+// CHECK-NEXT:   ^[[DEFAULT]]:
+    ^default:
+// CHECK-NEXT:     spirv.Branch ^[[MERGE:.+]]
+      spirv.Branch ^merge
+
+// CHECK-NEXT:   ^[[MERGE]]:
+    ^merge:
+// CHECK-NEXT:     spirv.mlir.merge
+      spirv.mlir.merge
+    }
+    spirv.Return
+  }
+}



More information about the Mlir-commits mailing list