Skip to content

Commit 0317fa8

Browse files
authored
Ignore UserSemantic decorations on BuiltIn variables (#2742)
UserSemantic decorations on BuiltIn variables would be added into `llvm.global.annotations`. During lowering of builtin variables to function calls, `replaceUsesOfBuiltinVar` would not know how to handle the annotation and crash. Right now there is no clear use case for UserSemantic decorations on BuiltIn variables, so just ignore the decoration and drop it during translation from SPIR-V to LLVM. Test-case-by: Ben Ashbaugh <[email protected]>
1 parent 33150bf commit 0317fa8

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3911,6 +3911,13 @@ void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) {
39113911
if (AnnotationCall && !AnnotationCall->getType()->isVoidTy())
39123912
ValueMap[BV] = AnnotationCall;
39133913
} else if (auto *GV = dyn_cast<GlobalVariable>(V)) {
3914+
// Do not add annotations for builtin variables if they will be translated
3915+
// to function calls.
3916+
SPIRVBuiltinVariableKind Kind;
3917+
if (BM->getBuiltinFormat() == BuiltinFormat::Function &&
3918+
isSPIRVBuiltinVariable(GV, &Kind))
3919+
return;
3920+
39143921
std::vector<SmallString<256>> AnnotStrVec;
39153922
generateIntelFPGAAnnotation(BV, AnnotStrVec);
39163923

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
; REQUIRES: spirv-as
2+
; RUN: spirv-as --target-env spv1.4 -o %t.spv %s
3+
; RUN: spirv-val %t.spv
4+
; RUN: llvm-spirv -r --spirv-target-env=CL2.0 %t.spv -o - | llvm-dis | FileCheck --check-prefix=CHECK-OCL %s
5+
; RUN: llvm-spirv -r --spirv-target-env=SPV-IR %t.spv -o - | llvm-dis | FileCheck --check-prefix=CHECK-SPV-IR %s
6+
7+
; Ensure that UserSemantic decorations on BuiltIn variables do not prevent successful translation.
8+
9+
; CHECK-OCL: call spir_func i64 @_Z13get_global_idj(i32 0)
10+
; CHECK-SPV-IR: call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 0)
11+
12+
OpCapability Addresses
13+
OpCapability Linkage
14+
OpCapability Kernel
15+
OpCapability Int64
16+
OpMemoryModel Physical64 OpenCL
17+
OpEntryPoint Kernel %usersemantic_test "usersemantic_test" %global_id
18+
OpDecorate %global_id LinkageAttributes "global_id" Import
19+
OpDecorate %global_id Constant
20+
OpDecorate %global_id BuiltIn GlobalInvocationId
21+
; Basic decoration:
22+
OpDecorateString %global_id UserSemantic "FOO"
23+
; Duplicate decorations are allowed as long as the string is different.
24+
OpDecorateString %global_id UserSemantic "BAR"
25+
; Try one more string with punctuation.
26+
OpDecorateString %global_id UserSemantic "FOO? BAR. BAZ!"
27+
%ulong = OpTypeInt 64 0
28+
%uint = OpTypeInt 32 0
29+
%v3ulong = OpTypeVector %ulong 3
30+
%_ptr_Input_v3ulong = OpTypePointer Input %v3ulong
31+
%void = OpTypeVoid
32+
%_ptr_CrossWorkgroup_uint = OpTypePointer CrossWorkgroup %uint
33+
%9 = OpTypeFunction %void %_ptr_CrossWorkgroup_uint
34+
%global_id = OpVariable %_ptr_Input_v3ulong Input
35+
%usersemantic_test = OpFunction %void None %9
36+
%dst = OpFunctionParameter %_ptr_CrossWorkgroup_uint
37+
%entry = OpLabel
38+
%index = OpLoad %v3ulong %global_id Aligned 32
39+
%call = OpCompositeExtract %ulong %index 0
40+
%conv = OpUConvert %uint %call
41+
%idxprom = OpSConvert %ulong %conv
42+
%arrayidx = OpInBoundsPtrAccessChain %_ptr_CrossWorkgroup_uint %dst %idxprom
43+
OpStore %arrayidx %conv Aligned 4
44+
OpReturn
45+
OpFunctionEnd

0 commit comments

Comments
 (0)