[llvm] [SPIR-V] Fix crash on switch with odd-width condition type (PR #208026)
Arseniy Obolenskiy via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 10 00:04:13 PDT 2026
https://github.com/aobolensk updated https://github.com/llvm/llvm-project/pull/208026
>From ed8bec0976f4a97754b85bb445b9e84f3a466574 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Tue, 7 Jul 2026 17:22:13 +0200
Subject: [PATCH 1/3] [SPIR-V] Fix crash on switch with odd-width condition
type
getPreferredSwitchConditionType called getSimpleVT unconditionally, which asserts on extended EVTs like i31
Return i1 for non-simple types to keep CodeGenPrepare from widening the condition
---
llvm/lib/Target/SPIRV/SPIRVISelLowering.h | 7 ++++-
.../SPIRV/switch-odd-width-condition.ll | 27 +++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index c86e37b2b2528..0599360cf465b 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -64,9 +64,14 @@ class SPIRVTargetLowering : public TargetLowering {
// the standard.
void finalizeLowering(MachineFunction &MF) const override;
+ // Return a width no larger than the condition so CodeGenPrepare never widens
+ // it. Odd-width integers are extended EVTs, so getSimpleVT would fail, so
+ // falling back to i1.
MVT getPreferredSwitchConditionType(LLVMContext &Context,
EVT ConditionVT) const override {
- return ConditionVT.getSimpleVT();
+ if (ConditionVT.isSimple())
+ return ConditionVT.getSimpleVT();
+ return MVT::i1;
}
bool enforcePtrTypeCompatibility(MachineInstr &I, unsigned PtrOpIdx,
diff --git a/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
new file mode 100644
index 0000000000000..3b2b5d3d1d378
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
@@ -0,0 +1,27 @@
+; A switch on an odd-width integer type.
+
+; RUN: llc -verify-machineinstrs -O2 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O2 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: %[[#I32:]] = OpTypeInt 32 0
+; CHECK: %[[#COND:]] = OpFunctionParameter %[[#I32]]
+; CHECK: OpSwitch %[[#COND]] %[[#]] 0 %[[#]] 1 %[[#]] 2 %[[#]]
+
+define spir_kernel void @fuzz_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i31 %n) {
+entry:
+ switch i31 %n, label %d [
+ i31 0, label %m
+ i31 1, label %b
+ i31 2, label %c
+ ]
+b:
+ br label %m
+c:
+ br label %m
+d:
+ br label %m
+m:
+ %r = phi i31 [ 40, %d ], [ 20, %b ], [ 30, %c ], [ 1, %entry ]
+ store i31 %r, ptr addrspace(1) %out, align 4
+ ret void
+}
>From 6d7db73e485803aca99acc01bba1f933d18e57b2 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Wed, 8 Jul 2026 10:37:31 +0200
Subject: [PATCH 2/3] add test
---
.../SPIRV/switch-odd-width-condition.ll | 27 +++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
index 3b2b5d3d1d378..f161a5e7f8521 100644
--- a/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
+++ b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
@@ -25,3 +25,30 @@ m:
store i31 %r, ptr addrspace(1) %out, align 4
ret void
}
+
+; The switch condition is also used by another instruction.
+
+; CHECK: %[[#COND2:]] = OpFunctionParameter %[[#I32]]
+; CHECK: OpSwitch %[[#COND2]] %[[#]] 0 %[[#]] 1 %[[#]] 2 %[[#]]
+
+define spir_kernel void @cond_used_elsewhere(ptr addrspace(1) %out, i31 %n) {
+entry:
+ switch i31 %n, label %d [
+ i31 0, label %a
+ i31 1, label %b
+ i31 2, label %c
+ ]
+a:
+ br label %m
+b:
+ br label %m
+c:
+ br label %m
+d:
+ br label %m
+m:
+ %r = phi i31 [ 40, %d ], [ 20, %a ], [ 30, %b ], [ 50, %c ]
+ %add = add i31 %r, %n
+ store i31 %add, ptr addrspace(1) %out, align 4
+ ret void
+}
>From e3523a07b68df3c87fddd3f6c480b6be425da3f3 Mon Sep 17 00:00:00 2001
From: Arseniy Obolenskiy <arseniy.obolenskiy at amd.com>
Date: Fri, 10 Jul 2026 09:04:00 +0200
Subject: [PATCH 3/3] Address comments
---
llvm/lib/Target/SPIRV/SPIRVISelLowering.h | 5 ---
.../i128-switch-condition-type.ll | 32 +++++++++++++++++++
.../SPIRV/switch-odd-width-condition.ll | 4 +--
3 files changed, 34 insertions(+), 7 deletions(-)
create mode 100644 llvm/test/CodeGen/SPIRV/extensions/SPV_ALTERA_arbitrary_precision_integers/i128-switch-condition-type.ll
diff --git a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
index 0599360cf465b..ff843649fd6d3 100644
--- a/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
+++ b/llvm/lib/Target/SPIRV/SPIRVISelLowering.h
@@ -64,13 +64,8 @@ class SPIRVTargetLowering : public TargetLowering {
// the standard.
void finalizeLowering(MachineFunction &MF) const override;
- // Return a width no larger than the condition so CodeGenPrepare never widens
- // it. Odd-width integers are extended EVTs, so getSimpleVT would fail, so
- // falling back to i1.
MVT getPreferredSwitchConditionType(LLVMContext &Context,
EVT ConditionVT) const override {
- if (ConditionVT.isSimple())
- return ConditionVT.getSimpleVT();
return MVT::i1;
}
diff --git a/llvm/test/CodeGen/SPIRV/extensions/SPV_ALTERA_arbitrary_precision_integers/i128-switch-condition-type.ll b/llvm/test/CodeGen/SPIRV/extensions/SPV_ALTERA_arbitrary_precision_integers/i128-switch-condition-type.ll
new file mode 100644
index 0000000000000..9d9df9bf47c56
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/extensions/SPV_ALTERA_arbitrary_precision_integers/i128-switch-condition-type.ll
@@ -0,0 +1,32 @@
+; The fix for the crash on odd-width switch condition types is orthogonal to
+; the arbitrary precision integers extension: with the extension enabled, an
+; odd-width type still lowers to itself rather than being widened.
+
+; RUN: llc -verify-machineinstrs -O2 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_ALTERA_arbitrary_precision_integers %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O2 -mtriple=spirv64-unknown-unknown --spirv-ext=+SPV_ALTERA_arbitrary_precision_integers %s -o - -filetype=obj | spirv-val %}
+
+; CHECK: %[[#I128:]] = OpTypeInt 128 0
+; CHECK: %[[#COND:]] = OpFunctionParameter %[[#I128]]
+; CHECK: OpSwitch %[[#COND]] %[[#]] 0 0 0 0 %[[#]] 1 0 0 0 %[[#]] 2 0 0 0 %[[#]]
+
+define spir_func void @apint_switch(i128 %n, ptr %out) {
+entry:
+ switch i128 %n, label %d [
+ i128 0, label %a
+ i128 1, label %b
+ i128 2, label %c
+ ]
+a:
+ br label %m
+b:
+ br label %m
+c:
+ br label %m
+d:
+ br label %m
+m:
+ %r = phi i128 [ 40, %d ], [ 20, %a ], [ 30, %b ], [ 50, %c ]
+ %add = add i128 %r, %n
+ store i128 %add, ptr %out
+ ret void
+}
diff --git a/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
index f161a5e7f8521..d5c678e1d1809 100644
--- a/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
+++ b/llvm/test/CodeGen/SPIRV/switch-odd-width-condition.ll
@@ -7,7 +7,7 @@
; CHECK: %[[#COND:]] = OpFunctionParameter %[[#I32]]
; CHECK: OpSwitch %[[#COND]] %[[#]] 0 %[[#]] 1 %[[#]] 2 %[[#]]
-define spir_kernel void @fuzz_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i31 %n) {
+define spir_func void @fuzz_kernel(ptr addrspace(1) %in, ptr addrspace(1) %out, i31 %n) {
entry:
switch i31 %n, label %d [
i31 0, label %m
@@ -31,7 +31,7 @@ m:
; CHECK: %[[#COND2:]] = OpFunctionParameter %[[#I32]]
; CHECK: OpSwitch %[[#COND2]] %[[#]] 0 %[[#]] 1 %[[#]] 2 %[[#]]
-define spir_kernel void @cond_used_elsewhere(ptr addrspace(1) %out, i31 %n) {
+define spir_func void @cond_used_elsewhere(ptr addrspace(1) %out, i31 %n) {
entry:
switch i31 %n, label %d [
i31 0, label %a
More information about the llvm-commits
mailing list