Skip to content

Commit 6b12674

Browse files
s-perronJaddyen
authored andcommitted
[SPIRV] Allow __spirv_SpecConstant in Vulkan shaders (llvm#143543)
There is a builtin __spirv_SpecConstant that the SPIR-V backend expands into a specialization constant. However, it is currently only enable for OpenCL shaders, and not the graphic shaders. We want to use it for specialization constants coming from HLSL, so we are enabling it for graphic shaders as well. Implements llvm/wg-hlsl#287 Fixes llvm#142991
1 parent fa78acf commit 6b12674

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVBuiltins.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -674,6 +674,8 @@ defm : DemangledNativeBuiltin<"ndrange_3D", OpenCL_std, Enqueue, 1, 3, OpBuildND
674674

675675
// Spec constant builtin records:
676676
defm : DemangledNativeBuiltin<"__spirv_SpecConstant", OpenCL_std, SpecConstant, 2, 2, OpSpecConstant>;
677+
defm : DemangledNativeBuiltin<"__spirv_SpecConstant", GLSL_std_450,
678+
SpecConstant, 2, 2, OpSpecConstant>;
677679
defm : DemangledNativeBuiltin<"__spirv_SpecConstantComposite", OpenCL_std, SpecConstant, 1, 0, OpSpecConstantComposite>;
678680

679681
// Async Copy and Prefetch builtin records:
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv1.6-unknown-vulkan1.3-compute %s -o - | FileCheck %s
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv1.6-unknown-vulkan1.3-compute %s -o - -filetype=obj | spirv-val --target-env vulkan1.3 %}
3+
4+
; CHECK-DAG: OpDecorate [[bool_const:%[0-9]+]] SpecId 1
5+
; CHECK-DAG: OpDecorate [[short_const:%[0-9]+]] SpecId 2
6+
; CHECK-DAG: OpDecorate [[int_const:%[0-9]+]] SpecId 3
7+
; CHECK-DAG: OpDecorate [[long_const:%[0-9]+]] SpecId 4
8+
; CHECK-DAG: OpDecorate [[float_const:%[0-9]+]] SpecId 8
9+
; CHECK-DAG: OpDecorate [[double_const:%[0-9]+]] SpecId 9
10+
; CHECK-DAG: OpDecorate [[enum_const:%[0-9]+]] SpecId 10
11+
12+
; CHECK-DAG: [[bool_const]] = OpSpecConstantTrue {{%[0-9]+}}
13+
; CHECK-DAG: [[short_const]] = OpSpecConstant {{%[0-9]+}} 4
14+
; CHECK-DAG: [[int_const]] = OpSpecConstant {{%[0-9]+}} 5
15+
; CHECK-DAG: [[long_const]] = OpSpecConstant {{%[0-9]+}} 8
16+
; CHECK-DAG: [[float_const]] = OpSpecConstant {{%[0-9]+}} 1112014848
17+
; CHECK-DAG: [[double_const]] = OpSpecConstant {{%[0-9]+}} 0 1079574528
18+
; CHECK-DAG: [[enum_const]] = OpSpecConstant {{%[0-9]+}} 30
19+
20+
@_ZL10bool_const = internal addrspace(10) global i32 0, align 4
21+
@_ZL11short_const = internal addrspace(10) global i16 0, align 2
22+
@_ZL9int_const = internal addrspace(10) global i32 0, align 4
23+
@_ZL10long_const = internal addrspace(10) global i64 0, align 8
24+
@_ZL11float_const = internal addrspace(10) global float 0.000000e+00, align 4
25+
@_ZL12double_const = internal addrspace(10) global double 0.000000e+00, align 8
26+
@_ZL10enum_const = internal addrspace(10) global i32 0, align 4
27+
28+
; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(readwrite, argmem: none, inaccessiblemem: none)
29+
define void @main() local_unnamed_addr #0 {
30+
entry:
31+
; CHECK: [[b:%[0-9]+]] = OpSelect {{%[0-9]+}} [[bool_const]]
32+
; CHECK: OpStore {{%[0-9]+}} [[b]]
33+
%0 = tail call spir_func i1 @_Z20__spirv_SpecConstantib(i32 1, i1 true)
34+
%storedv.i.i = zext i1 %0 to i32
35+
store i32 %storedv.i.i, ptr addrspace(10) @_ZL10bool_const, align 4
36+
37+
; CHECK: OpStore {{%[0-9]+}} [[short_const]]
38+
%2 = tail call spir_func i16 @_Z20__spirv_SpecConstantis(i32 2, i16 4)
39+
store i16 %2, ptr addrspace(10) @_ZL11short_const, align 2
40+
41+
; CHECK: OpStore {{%[0-9]+}} [[int_const]]
42+
%4 = tail call spir_func i32 @_Z20__spirv_SpecConstantii(i32 3, i32 5)
43+
store i32 %4, ptr addrspace(10) @_ZL9int_const, align 4
44+
45+
46+
; CHECK: OpStore {{%[0-9]+}} [[long_const]]
47+
%6 = tail call spir_func i64 @_Z20__spirv_SpecConstantix(i32 4, i64 8)
48+
store i64 %6, ptr addrspace(10) @_ZL10long_const, align 8
49+
50+
; CHECK: OpStore {{%[0-9]+}} [[float_const]]
51+
%14 = tail call reassoc nnan ninf nsz arcp afn spir_func float @_Z20__spirv_SpecConstantif(i32 8, float 5.000000e+01)
52+
store float %14, ptr addrspace(10) @_ZL11float_const, align 4
53+
54+
; CHECK: OpStore {{%[0-9]+}} [[double_const]]
55+
%16 = tail call reassoc nnan ninf nsz arcp afn spir_func double @_Z20__spirv_SpecConstantid(i32 9, double 1.000000e+02)
56+
store double %16, ptr addrspace(10) @_ZL12double_const, align 8
57+
58+
; CHECK: OpStore {{%[0-9]+}} [[enum_const]]
59+
%18 = tail call spir_func i32 @_Z20__spirv_SpecConstantii(i32 10, i32 30)
60+
store i32 %18, ptr addrspace(10) @_ZL10enum_const, align 4
61+
ret void
62+
}
63+
64+
65+
declare i1 @_Z20__spirv_SpecConstantib(i32, i1)
66+
declare i8 @_Z20__spirv_SpecConstantia(i32, i8)
67+
declare i16 @_Z20__spirv_SpecConstantis(i32, i16)
68+
declare i32 @_Z20__spirv_SpecConstantii(i32, i32)
69+
declare i64 @_Z20__spirv_SpecConstantix(i32, i64)
70+
declare float @_Z20__spirv_SpecConstantif(i32, float)
71+
declare double @_Z20__spirv_SpecConstantid(i32, double)
72+
73+
attributes #0 = { "hlsl.numthreads"="1,1,1" "hlsl.shader"="compute" }

0 commit comments

Comments
 (0)