Skip to content

Commit 4054d65

Browse files
Merge pull request #1263 from KhronosGroup/fix-1232
Deal with illegal names in types as well.
2 parents 79700d5 + f79c1e2 commit 4054d65

10 files changed

Lines changed: 317 additions & 24 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
struct Foo
2+
{
3+
float _abs;
4+
};
5+
6+
RWByteAddressBuffer _7 : register(u0);
7+
8+
void comp_main()
9+
{
10+
Foo _24;
11+
_24._abs = asfloat(_7.Load(0));
12+
Foo f;
13+
f._abs = _24._abs;
14+
int _abs = 10;
15+
_7.Store(4, asuint(f._abs));
16+
}
17+
18+
[numthreads(1, 1, 1)]
19+
void main()
20+
{
21+
comp_main();
22+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <metal_stdlib>
2+
#include <simd/simd.h>
3+
4+
using namespace metal;
5+
6+
struct Foo
7+
{
8+
float _abs;
9+
};
10+
11+
struct Foo_1
12+
{
13+
float _abs;
14+
};
15+
16+
struct SSBO
17+
{
18+
Foo_1 foo;
19+
Foo_1 foo2;
20+
};
21+
22+
kernel void main0(device SSBO& _7 [[buffer(0)]])
23+
{
24+
Foo f;
25+
f._abs = _7.foo._abs;
26+
int _abs = 10;
27+
_7.foo2._abs = f._abs;
28+
}
29+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#version 450
2+
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
3+
4+
struct Foo
5+
{
6+
float _abs;
7+
};
8+
9+
layout(binding = 0, std430) buffer SSBO
10+
{
11+
Foo foo;
12+
Foo foo2;
13+
} _7;
14+
15+
void main()
16+
{
17+
Foo f;
18+
f._abs = _7.foo._abs;
19+
int _abs = 10;
20+
_7.foo2._abs = f._abs;
21+
}
22+
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; SPIR-V
2+
; Version: 1.0
3+
; Generator: Khronos Glslang Reference Front End; 8
4+
; Bound: 31
5+
; Schema: 0
6+
OpCapability Shader
7+
%1 = OpExtInstImport "GLSL.std.450"
8+
OpMemoryModel Logical GLSL450
9+
OpEntryPoint GLCompute %main "main"
10+
OpExecutionMode %main LocalSize 1 1 1
11+
OpSource GLSL 450
12+
OpName %main "main"
13+
OpName %Foo "Foo"
14+
OpMemberName %Foo 0 "abs"
15+
OpName %f "f"
16+
OpName %Foo_0 "Foo"
17+
OpMemberName %Foo_0 0 "abs"
18+
OpName %SSBO "SSBO"
19+
OpMemberName %SSBO 0 "foo"
20+
OpMemberName %SSBO 1 "foo2"
21+
OpName %_ ""
22+
OpName %linear "abs"
23+
OpMemberDecorate %Foo_0 0 Offset 0
24+
OpMemberDecorate %SSBO 0 Offset 0
25+
OpMemberDecorate %SSBO 1 Offset 4
26+
OpDecorate %SSBO BufferBlock
27+
OpDecorate %_ DescriptorSet 0
28+
OpDecorate %_ Binding 0
29+
%void = OpTypeVoid
30+
%3 = OpTypeFunction %void
31+
%float = OpTypeFloat 32
32+
%Foo = OpTypeStruct %float
33+
%_ptr_Function_Foo = OpTypePointer Function %Foo
34+
%Foo_0 = OpTypeStruct %float
35+
%SSBO = OpTypeStruct %Foo_0 %Foo_0
36+
%_ptr_Uniform_SSBO = OpTypePointer Uniform %SSBO
37+
%_ = OpVariable %_ptr_Uniform_SSBO Uniform
38+
%int = OpTypeInt 32 1
39+
%int_0 = OpConstant %int 0
40+
%_ptr_Uniform_Foo_0 = OpTypePointer Uniform %Foo_0
41+
%_ptr_Function_float = OpTypePointer Function %float
42+
%_ptr_Function_int = OpTypePointer Function %int
43+
%int_10 = OpConstant %int 10
44+
%int_1 = OpConstant %int 1
45+
%_ptr_Uniform_float = OpTypePointer Uniform %float
46+
%main = OpFunction %void None %3
47+
%5 = OpLabel
48+
%f = OpVariable %_ptr_Function_Foo Function
49+
%linear = OpVariable %_ptr_Function_int Function
50+
%17 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_0
51+
%18 = OpLoad %Foo_0 %17
52+
%19 = OpCompositeExtract %float %18 0
53+
%21 = OpAccessChain %_ptr_Function_float %f %int_0
54+
OpStore %21 %19
55+
OpStore %linear %int_10
56+
%26 = OpLoad %Foo %f
57+
%27 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_1
58+
%28 = OpCompositeExtract %float %26 0
59+
%30 = OpAccessChain %_ptr_Uniform_float %27 %int_0
60+
OpStore %30 %28
61+
OpReturn
62+
OpFunctionEnd
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; SPIR-V
2+
; Version: 1.0
3+
; Generator: Khronos Glslang Reference Front End; 8
4+
; Bound: 31
5+
; Schema: 0
6+
OpCapability Shader
7+
%1 = OpExtInstImport "GLSL.std.450"
8+
OpMemoryModel Logical GLSL450
9+
OpEntryPoint GLCompute %main "main"
10+
OpExecutionMode %main LocalSize 1 1 1
11+
OpSource GLSL 450
12+
OpName %main "main"
13+
OpName %Foo "Foo"
14+
OpMemberName %Foo 0 "abs"
15+
OpName %f "f"
16+
OpName %Foo_0 "Foo"
17+
OpMemberName %Foo_0 0 "abs"
18+
OpName %SSBO "SSBO"
19+
OpMemberName %SSBO 0 "foo"
20+
OpMemberName %SSBO 1 "foo2"
21+
OpName %_ ""
22+
OpName %linear "abs"
23+
OpMemberDecorate %Foo_0 0 Offset 0
24+
OpMemberDecorate %SSBO 0 Offset 0
25+
OpMemberDecorate %SSBO 1 Offset 4
26+
OpDecorate %SSBO BufferBlock
27+
OpDecorate %_ DescriptorSet 0
28+
OpDecorate %_ Binding 0
29+
%void = OpTypeVoid
30+
%3 = OpTypeFunction %void
31+
%float = OpTypeFloat 32
32+
%Foo = OpTypeStruct %float
33+
%_ptr_Function_Foo = OpTypePointer Function %Foo
34+
%Foo_0 = OpTypeStruct %float
35+
%SSBO = OpTypeStruct %Foo_0 %Foo_0
36+
%_ptr_Uniform_SSBO = OpTypePointer Uniform %SSBO
37+
%_ = OpVariable %_ptr_Uniform_SSBO Uniform
38+
%int = OpTypeInt 32 1
39+
%int_0 = OpConstant %int 0
40+
%_ptr_Uniform_Foo_0 = OpTypePointer Uniform %Foo_0
41+
%_ptr_Function_float = OpTypePointer Function %float
42+
%_ptr_Function_int = OpTypePointer Function %int
43+
%int_10 = OpConstant %int 10
44+
%int_1 = OpConstant %int 1
45+
%_ptr_Uniform_float = OpTypePointer Uniform %float
46+
%main = OpFunction %void None %3
47+
%5 = OpLabel
48+
%f = OpVariable %_ptr_Function_Foo Function
49+
%linear = OpVariable %_ptr_Function_int Function
50+
%17 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_0
51+
%18 = OpLoad %Foo_0 %17
52+
%19 = OpCompositeExtract %float %18 0
53+
%21 = OpAccessChain %_ptr_Function_float %f %int_0
54+
OpStore %21 %19
55+
OpStore %linear %int_10
56+
%26 = OpLoad %Foo %f
57+
%27 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_1
58+
%28 = OpCompositeExtract %float %26 0
59+
%30 = OpAccessChain %_ptr_Uniform_float %27 %int_0
60+
OpStore %30 %28
61+
OpReturn
62+
OpFunctionEnd
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
; SPIR-V
2+
; Version: 1.0
3+
; Generator: Khronos Glslang Reference Front End; 8
4+
; Bound: 31
5+
; Schema: 0
6+
OpCapability Shader
7+
%1 = OpExtInstImport "GLSL.std.450"
8+
OpMemoryModel Logical GLSL450
9+
OpEntryPoint GLCompute %main "main"
10+
OpExecutionMode %main LocalSize 1 1 1
11+
OpSource GLSL 450
12+
OpName %main "main"
13+
OpName %Foo "Foo"
14+
OpMemberName %Foo 0 "abs"
15+
OpName %f "f"
16+
OpName %Foo_0 "Foo"
17+
OpMemberName %Foo_0 0 "abs"
18+
OpName %SSBO "SSBO"
19+
OpMemberName %SSBO 0 "foo"
20+
OpMemberName %SSBO 1 "foo2"
21+
OpName %_ ""
22+
OpName %linear "abs"
23+
OpMemberDecorate %Foo_0 0 Offset 0
24+
OpMemberDecorate %SSBO 0 Offset 0
25+
OpMemberDecorate %SSBO 1 Offset 4
26+
OpDecorate %SSBO BufferBlock
27+
OpDecorate %_ DescriptorSet 0
28+
OpDecorate %_ Binding 0
29+
%void = OpTypeVoid
30+
%3 = OpTypeFunction %void
31+
%float = OpTypeFloat 32
32+
%Foo = OpTypeStruct %float
33+
%_ptr_Function_Foo = OpTypePointer Function %Foo
34+
%Foo_0 = OpTypeStruct %float
35+
%SSBO = OpTypeStruct %Foo_0 %Foo_0
36+
%_ptr_Uniform_SSBO = OpTypePointer Uniform %SSBO
37+
%_ = OpVariable %_ptr_Uniform_SSBO Uniform
38+
%int = OpTypeInt 32 1
39+
%int_0 = OpConstant %int 0
40+
%_ptr_Uniform_Foo_0 = OpTypePointer Uniform %Foo_0
41+
%_ptr_Function_float = OpTypePointer Function %float
42+
%_ptr_Function_int = OpTypePointer Function %int
43+
%int_10 = OpConstant %int 10
44+
%int_1 = OpConstant %int 1
45+
%_ptr_Uniform_float = OpTypePointer Uniform %float
46+
%main = OpFunction %void None %3
47+
%5 = OpLabel
48+
%f = OpVariable %_ptr_Function_Foo Function
49+
%linear = OpVariable %_ptr_Function_int Function
50+
%17 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_0
51+
%18 = OpLoad %Foo_0 %17
52+
%19 = OpCompositeExtract %float %18 0
53+
%21 = OpAccessChain %_ptr_Function_float %f %int_0
54+
OpStore %21 %19
55+
OpStore %linear %int_10
56+
%26 = OpLoad %Foo %f
57+
%27 = OpAccessChain %_ptr_Uniform_Foo_0 %_ %int_1
58+
%28 = OpCompositeExtract %float %26 0
59+
%30 = OpAccessChain %_ptr_Uniform_float %27 %int_0
60+
OpStore %30 %28
61+
OpReturn
62+
OpFunctionEnd

spirv_glsl.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,6 +2241,36 @@ void CompilerGLSL::emit_entry_point_declarations()
22412241
{
22422242
}
22432243

2244+
void CompilerGLSL::replace_illegal_names(const unordered_set<string> &keywords)
2245+
{
2246+
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) {
2247+
if (is_hidden_variable(var))
2248+
return;
2249+
2250+
auto *meta = ir.find_meta(var.self);
2251+
if (!meta)
2252+
return;
2253+
2254+
auto &m = meta->decoration;
2255+
if (m.alias.compare(0, 3, "gl_") == 0 || keywords.find(m.alias) != end(keywords))
2256+
m.alias = join("_", m.alias);
2257+
});
2258+
2259+
ir.for_each_typed_id<SPIRType>([&](uint32_t, const SPIRType &type) {
2260+
auto *meta = ir.find_meta(type.self);
2261+
if (!meta)
2262+
return;
2263+
2264+
auto &m = meta->decoration;
2265+
if (m.alias.compare(0, 3, "gl_") == 0 || keywords.find(m.alias) != end(keywords))
2266+
m.alias = join("_", m.alias);
2267+
2268+
for (auto &memb : meta->members)
2269+
if (memb.alias.compare(0, 3, "gl_") == 0 || keywords.find(memb.alias) != end(keywords))
2270+
memb.alias = join("_", memb.alias);
2271+
});
2272+
}
2273+
22442274
void CompilerGLSL::replace_illegal_names()
22452275
{
22462276
// clang-format off
@@ -2295,14 +2325,7 @@ void CompilerGLSL::replace_illegal_names()
22952325
};
22962326
// clang-format on
22972327

2298-
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, const SPIRVariable &var) {
2299-
if (!is_hidden_variable(var))
2300-
{
2301-
auto &m = ir.meta[var.self].decoration;
2302-
if (m.alias.compare(0, 3, "gl_") == 0 || keywords.find(m.alias) != end(keywords))
2303-
m.alias = join("_", m.alias);
2304-
}
2305-
});
2328+
replace_illegal_names(keywords);
23062329
}
23072330

23082331
void CompilerGLSL::replace_fragment_output(SPIRVariable &var)

spirv_glsl.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,7 @@ class CompilerGLSL : public Compiler
590590
bool check_atomic_image(uint32_t id);
591591

592592
virtual void replace_illegal_names();
593+
void replace_illegal_names(const std::unordered_set<std::string> &keywords);
593594
virtual void emit_entry_point_declarations();
594595

595596
void replace_fragment_output(SPIRVariable &var);

spirv_hlsl.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,15 +1113,7 @@ void CompilerHLSL::replace_illegal_names()
11131113
"line", "linear", "matrix", "point", "row_major", "sampler",
11141114
};
11151115

1116-
ir.for_each_typed_id<SPIRVariable>([&](uint32_t, SPIRVariable &var) {
1117-
if (!is_hidden_variable(var))
1118-
{
1119-
auto &m = ir.meta[var.self].decoration;
1120-
if (keywords.find(m.alias) != end(keywords))
1121-
m.alias = join("_", m.alias);
1122-
}
1123-
});
1124-
1116+
CompilerGLSL::replace_illegal_names(keywords);
11251117
CompilerGLSL::replace_illegal_names();
11261118
}
11271119

0 commit comments

Comments
 (0)