[SPIR-V] Reject OpSelect with scalar result and vector condition#193745
Open
[SPIR-V] Reject OpSelect with scalar result and vector condition#193745
Conversation
Per SPIR-V spec, scalar Result Type requires a scalar boolean condition. So, vector cond branches under a scalar result are unreachable
Member
|
@llvm/pr-subscribers-backend-spir-v Author: Arseniy Obolenskiy (aobolensk) ChangesPer SPIR-V spec, scalar Result Type requires a scalar bool condition. So, vector cond branches under a scalar result are unreachable Full diff: https://github.com/llvm/llvm-project/pull/193745.diff 3 Files Affected:
diff --git a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
index 8e3040c2667f6..c0925cfbba9d3 100644
--- a/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
+++ b/llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
@@ -4007,12 +4007,15 @@ bool SPIRVInstructionSelector::selectSelect(Register ResVReg,
Opcode = IsScalarBool ? SPIRV::OpSelectVISCond : SPIRV::OpSelectVIVCond;
}
} else {
+ if (!IsScalarBool)
+ report_fatal_error("OpSelect with a scalar result requires a scalar "
+ "boolean condition");
if (IsFloatTy) {
- Opcode = IsScalarBool ? SPIRV::OpSelectSFSCond : SPIRV::OpSelectVFVCond;
+ Opcode = SPIRV::OpSelectSFSCond;
} else if (IsPtrTy) {
- Opcode = IsScalarBool ? SPIRV::OpSelectSPSCond : SPIRV::OpSelectVPVCond;
+ Opcode = SPIRV::OpSelectSPSCond;
} else {
- Opcode = IsScalarBool ? SPIRV::OpSelectSISCond : SPIRV::OpSelectVIVCond;
+ Opcode = SPIRV::OpSelectSISCond;
}
}
BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcode))
diff --git a/llvm/test/CodeGen/SPIRV/select-invalid-vector-cond.ll b/llvm/test/CodeGen/SPIRV/select-invalid-vector-cond.ll
new file mode 100644
index 0000000000000..a92d25f43ea94
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/select-invalid-vector-cond.ll
@@ -0,0 +1,16 @@
+; RUN: not --crash llc -O0 -mtriple=spirv32-unknown-unknown %s -o /dev/null 2>&1 | FileCheck %s
+; RUN: not --crash llc -O0 -mtriple=spirv64-unknown-unknown %s -o /dev/null 2>&1 | FileCheck %s
+
+; __spirv_Select can pair a vector boolean condition with scalar operands,
+; which is malformed for OpSelect and must be diagnosed.
+
+; CHECK: LLVM ERROR: OpSelect with a scalar result requires a scalar boolean condition
+
+define spir_kernel void @bad_select(i32 %a, i32 %b, ptr addrspace(1) %out, <4 x i1> %cond) {
+entry:
+ %call = call spir_func i32 @_Z14__spirv_SelectDv4_bii(<4 x i1> %cond, i32 %a, i32 %b)
+ store i32 %call, ptr addrspace(1) %out
+ ret void
+}
+
+declare spir_func i32 @_Z14__spirv_SelectDv4_bii(<4 x i1>, i32, i32)
diff --git a/llvm/test/CodeGen/SPIRV/select.ll b/llvm/test/CodeGen/SPIRV/select.ll
new file mode 100644
index 0000000000000..019f713f739d4
--- /dev/null
+++ b/llvm/test/CodeGen/SPIRV/select.ll
@@ -0,0 +1,77 @@
+; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
+; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}
+
+; OpSelect: condition and object component counts must match.
+
+; CHECK-DAG: %[[#Bool:]] = OpTypeBool
+; CHECK-DAG: %[[#I32:]] = OpTypeInt 32 0
+; CHECK-DAG: %[[#F32:]] = OpTypeFloat 32
+; CHECK-DAG: %[[#I8:]] = OpTypeInt 8 0
+; CHECK-DAG: %[[#PtrI8:]] = OpTypePointer Function %[[#I8]]
+; CHECK-DAG: %[[#V4I32:]] = OpTypeVector %[[#I32]] 4
+; CHECK-DAG: %[[#V4F32:]] = OpTypeVector %[[#F32]] 4
+; CHECK-DAG: %[[#V4Bool:]] = OpTypeVector %[[#Bool]] 4
+
+; Scalar result, scalar cond.
+; CHECK: OpFunction
+; CHECK: %[[#SC:]] = OpFunctionParameter %[[#Bool]]
+; CHECK: %[[#SA:]] = OpFunctionParameter %[[#I32]]
+; CHECK: %[[#SB:]] = OpFunctionParameter %[[#I32]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#I32]] %[[#SC]] %[[#SA]] %[[#SB]]
+define i32 @sel_i32_scond(i1 %c, i32 %a, i32 %b) {
+ %r = select i1 %c, i32 %a, i32 %b
+ ret i32 %r
+}
+
+; CHECK: OpFunction
+; CHECK: %[[#FC:]] = OpFunctionParameter %[[#Bool]]
+; CHECK: %[[#FA:]] = OpFunctionParameter %[[#F32]]
+; CHECK: %[[#FB:]] = OpFunctionParameter %[[#F32]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#F32]] %[[#FC]] %[[#FA]] %[[#FB]]
+define float @sel_f32_scond(i1 %c, float %a, float %b) {
+ %r = select i1 %c, float %a, float %b
+ ret float %r
+}
+
+; CHECK: OpFunction
+; CHECK: %[[#PC:]] = OpFunctionParameter %[[#Bool]]
+; CHECK: %[[#PA:]] = OpFunctionParameter %[[#PtrI8]]
+; CHECK: %[[#PB:]] = OpFunctionParameter %[[#PtrI8]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#PtrI8]] %[[#PC]] %[[#PA]] %[[#PB]]
+define ptr @sel_ptr_scond(i1 %c, ptr %a, ptr %b) {
+ %r = select i1 %c, ptr %a, ptr %b
+ ret ptr %r
+}
+
+; Vector result, scalar (broadcast) cond.
+; CHECK: OpFunction
+; CHECK: %[[#VSC:]] = OpFunctionParameter %[[#Bool]]
+; CHECK: %[[#VSA:]] = OpFunctionParameter %[[#V4I32]]
+; CHECK: %[[#VSB:]] = OpFunctionParameter %[[#V4I32]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#V4I32]] %[[#VSC]] %[[#VSA]] %[[#VSB]]
+define <4 x i32> @sel_v4i32_scond(i1 %c, <4 x i32> %a, <4 x i32> %b) {
+ %r = select i1 %c, <4 x i32> %a, <4 x i32> %b
+ ret <4 x i32> %r
+}
+
+; Vector result, vector cond.
+; CHECK: OpFunction
+; CHECK: %[[#VVIC:]] = OpFunctionParameter %[[#V4Bool]]
+; CHECK: %[[#VVIA:]] = OpFunctionParameter %[[#V4I32]]
+; CHECK: %[[#VVIB:]] = OpFunctionParameter %[[#V4I32]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#V4I32]] %[[#VVIC]] %[[#VVIA]] %[[#VVIB]]
+define <4 x i32> @sel_v4i32_vcond(<4 x i1> %c, <4 x i32> %a, <4 x i32> %b) {
+ %r = select <4 x i1> %c, <4 x i32> %a, <4 x i32> %b
+ ret <4 x i32> %r
+}
+
+; CHECK: OpFunction
+; CHECK: %[[#VVFC:]] = OpFunctionParameter %[[#V4Bool]]
+; CHECK: %[[#VVFA:]] = OpFunctionParameter %[[#V4F32]]
+; CHECK: %[[#VVFB:]] = OpFunctionParameter %[[#V4F32]]
+; CHECK: %{{[0-9]+}} = OpSelect %[[#V4F32]] %[[#VVFC]] %[[#VVFA]] %[[#VVFB]]
+define <4 x float> @sel_v4f32_vcond(<4 x i1> %c, <4 x float> %a, <4 x float> %b) {
+ %r = select <4 x i1> %c, <4 x float> %a, <4 x float> %b
+ ret <4 x float> %r
+}
+
|
🐧 Linux x64 Test Results
✅ The build succeeded and all tests passed. |
🪟 Windows x64 Test Results
✅ The build succeeded and all tests passed. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Per SPIR-V spec, scalar Result Type requires a scalar bool condition. So, vector cond branches under a scalar result are unreachable