Skip to content

Commit 4e13c90

Browse files
committed
Fix KhronosGroup#2329: don't use invalid initializers.
1 parent 2068869 commit 4e13c90

File tree

6 files changed

+176
-2
lines changed

6 files changed

+176
-2
lines changed

SPIRV/GlslangToSpv.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ void TGlslangToSpvTraverser::visitSymbol(glslang::TIntermSymbol* symbol)
17241724
spv::StorageClass sc = builder.getStorageClass(id);
17251725
// Before SPIR-V 1.4, we only want to include Input and Output.
17261726
// Starting with SPIR-V 1.4, we want all globals.
1727-
if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && sc != spv::StorageClassFunction) ||
1727+
if ((glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_4 && builder.isGlobalStorage(id)) ||
17281728
(sc == spv::StorageClassInput || sc == spv::StorageClassOutput)) {
17291729
iOSet.insert(id);
17301730
}

SPIRV/SpvBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2722,7 +2722,7 @@ Id Builder::accessChainLoad(Decoration precision, Decoration nonUniform, Id resu
27222722
setPrecision(id, precision);
27232723
} else {
27242724
Id lValue = NoResult;
2725-
if (spvVersion >= Spv_1_4) {
2725+
if (spvVersion >= Spv_1_4 && isValidInitializer(accessChain.base)) {
27262726
// make a new function variable for this r-value, using an initializer,
27272727
// and mark it as NonWritable so that downstream it can be detected as a lookup
27282728
// table

SPIRV/SpvBuilder.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ class Builder {
248248
{ return module.getInstruction(resultId)->getImmediateOperand(0); }
249249
StorageClass getStorageClass(Id resultId) const { return getTypeStorageClass(getTypeId(resultId)); }
250250

251+
bool isVariableOpCode(Op opcode) const { return opcode == OpVariable; }
252+
bool isVariable(Id resultId) const { return isVariableOpCode(getOpCode(resultId)); }
253+
bool isGlobalStorage(Id resultId) const { return getStorageClass(resultId) != StorageClassFunction; }
254+
bool isGlobalVariable(Id resultId) const { return isVariable(resultId) && isGlobalStorage(resultId); }
255+
// See if a resultId is valid for use as an initializer.
256+
bool isValidInitializer(Id resultId) const { return isConstant(resultId) || isGlobalVariable(resultId); }
257+
251258
int getScalarTypeWidth(Id typeId) const
252259
{
253260
Id scalarTypeId = getScalarTypeId(typeId);
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
spv.ext.World3x4.rahit
2+
// Module Version 10400
3+
// Generated by (magic number): 8000a
4+
// Id's are bound by 90
5+
6+
Capability RayTracingProvisionalKHR
7+
Extension "SPV_KHR_ray_tracing"
8+
1: ExtInstImport "GLSL.std.450"
9+
MemoryModel Logical GLSL450
10+
EntryPoint AnyHitKHR 4 "main" 43 60 78 87 89
11+
Source GLSL 460
12+
SourceExtension "GL_EXT_ray_tracing"
13+
Name 4 "main"
14+
Name 8 "k"
15+
Name 12 "ndx"
16+
Name 14 "col"
17+
Name 25 "row"
18+
Name 39 "p"
19+
Name 43 "gl_LaunchIDEXT"
20+
Name 53 "r"
21+
Name 60 "gl_WorldToObject3x4EXT"
22+
Name 66 "indexable"
23+
Name 72 "c"
24+
Name 78 "result"
25+
Name 87 "attribs"
26+
Name 89 "hitValue"
27+
Decorate 43(gl_LaunchIDEXT) BuiltIn LaunchIdKHR
28+
Decorate 60(gl_WorldToObject3x4EXT) BuiltIn WorldToObjectKHR
29+
Decorate 78(result) DescriptorSet 0
30+
Decorate 78(result) Binding 0
31+
Decorate 89(hitValue) Location 0
32+
2: TypeVoid
33+
3: TypeFunction 2
34+
6: TypeFloat 32
35+
7: TypePointer Function 6(float)
36+
9: 6(float) Constant 1233125376
37+
10: TypeInt 32 1
38+
11: TypePointer Function 10(int)
39+
13: 10(int) Constant 4294967295
40+
15: 10(int) Constant 0
41+
22: 10(int) Constant 3
42+
23: TypeBool
43+
32: 10(int) Constant 4
44+
35: 10(int) Constant 1
45+
37: TypeVector 10(int) 3
46+
38: TypePointer Function 37(ivec3)
47+
40: TypeInt 32 0
48+
41: TypeVector 40(int) 3
49+
42: TypePointer Input 41(ivec3)
50+
43(gl_LaunchIDEXT): 42(ptr) Variable Input
51+
44: TypeVector 40(int) 2
52+
47: TypeVector 10(int) 2
53+
55: TypeVector 6(float) 4
54+
56: TypeMatrix 55(fvec4) 3
55+
57: TypeVector 6(float) 3
56+
58: TypeMatrix 57(fvec3) 4
57+
59: TypePointer Input 58
58+
60(gl_WorldToObject3x4EXT): 59(ptr) Variable Input
59+
65: TypePointer Function 56
60+
70: TypeVector 10(int) 4
61+
71: TypePointer Function 70(ivec4)
62+
76: TypeImage 10(int) 3D nonsampled format:R32i
63+
77: TypePointer UniformConstant 76
64+
78(result): 77(ptr) Variable UniformConstant
65+
86: TypePointer HitAttributeKHR 57(fvec3)
66+
87(attribs): 86(ptr) Variable HitAttributeKHR
67+
88: TypePointer IncomingRayPayloadKHR 57(fvec3)
68+
89(hitValue): 88(ptr) Variable IncomingRayPayloadKHR
69+
4(main): 2 Function None 3
70+
5: Label
71+
8(k): 7(ptr) Variable Function
72+
12(ndx): 11(ptr) Variable Function
73+
14(col): 11(ptr) Variable Function
74+
25(row): 11(ptr) Variable Function
75+
39(p): 38(ptr) Variable Function
76+
53(r): 7(ptr) Variable Function
77+
66(indexable): 65(ptr) Variable Function
78+
72(c): 71(ptr) Variable Function
79+
Store 8(k) 9
80+
Store 12(ndx) 13
81+
Store 14(col) 15
82+
Branch 16
83+
16: Label
84+
LoopMerge 18 19 None
85+
Branch 20
86+
20: Label
87+
21: 10(int) Load 14(col)
88+
24: 23(bool) SLessThan 21 22
89+
BranchConditional 24 17 18
90+
17: Label
91+
Store 25(row) 15
92+
Branch 26
93+
26: Label
94+
LoopMerge 28 29 None
95+
Branch 30
96+
30: Label
97+
31: 10(int) Load 25(row)
98+
33: 23(bool) SLessThan 31 32
99+
BranchConditional 33 27 28
100+
27: Label
101+
34: 10(int) Load 12(ndx)
102+
36: 10(int) IAdd 34 35
103+
Store 12(ndx) 36
104+
45: 41(ivec3) Load 43(gl_LaunchIDEXT)
105+
46: 44(ivec2) VectorShuffle 45 45 0 1
106+
48: 47(ivec2) Bitcast 46
107+
49: 10(int) Load 12(ndx)
108+
50: 10(int) CompositeExtract 48 0
109+
51: 10(int) CompositeExtract 48 1
110+
52: 37(ivec3) CompositeConstruct 50 51 49
111+
Store 39(p) 52
112+
54: 6(float) Load 8(k)
113+
61: 58 Load 60(gl_WorldToObject3x4EXT)
114+
62: 56 Transpose 61
115+
63: 10(int) Load 14(col)
116+
64: 10(int) Load 25(row)
117+
Store 66(indexable) 62
118+
67: 7(ptr) AccessChain 66(indexable) 63 64
119+
68: 6(float) Load 67
120+
69: 6(float) FMul 54 68
121+
Store 53(r) 69
122+
73: 6(float) Load 53(r)
123+
74: 10(int) ConvertFToS 73
124+
75: 70(ivec4) CompositeConstruct 74 15 15 35
125+
Store 72(c) 75
126+
79: 76 Load 78(result)
127+
80: 37(ivec3) Load 39(p)
128+
81: 70(ivec4) Load 72(c)
129+
ImageWrite 79 80 81 SignExtend
130+
Branch 29
131+
29: Label
132+
82: 10(int) Load 25(row)
133+
83: 10(int) IAdd 82 35
134+
Store 25(row) 83
135+
Branch 26
136+
28: Label
137+
Branch 19
138+
19: Label
139+
84: 10(int) Load 14(col)
140+
85: 10(int) IAdd 84 35
141+
Store 14(col) 85
142+
Branch 16
143+
18: Label
144+
Return
145+
FunctionEnd

Test/spv.ext.World3x4.rahit

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#version 460
2+
#extension GL_EXT_ray_tracing : enable
3+
4+
hitAttributeEXT vec3 attribs;
5+
layout(location = 0) rayPayloadInEXT vec3 hitValue;
6+
layout(set = 0, binding = 0, r32i) uniform iimage3D result;
7+
8+
void main()
9+
{
10+
float k = 1048576.0f;
11+
int ndx = -1;
12+
for (int col = 0; col < 3; col++)
13+
for (int row = 0; row < 4; row++)
14+
{
15+
ndx++;
16+
ivec3 p = ivec3(gl_LaunchIDEXT.xy, ndx);
17+
float r = k * gl_WorldToObject3x4EXT[col][row];
18+
ivec4 c = ivec4(int(r),0,0,1);
19+
imageStore(result, p, c);
20+
}
21+
}

gtests/Spv.FromFile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ INSTANTIATE_TEST_CASE_P(
560560
"spv.ext.RayGenShader.rgen",
561561
"spv.ext.RayGenShader11.rgen",
562562
"spv.ext.RayGenShaderArray.rgen",
563+
"spv.ext.World3x4.rahit",
563564
})),
564565
FileNameAsCustomTestSuffix
565566
);

0 commit comments

Comments
 (0)