Skip to content

Commit e52f5d9

Browse files
authored
Fix builtin vars translation (#1738)
The translator was crashing in case if builin GV was accessed via GEP without AS cast due to incorrect assumption. Signed-off-by: Sidorov, Dmitry <[email protected]>
1 parent 6417ada commit e52f5d9

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/SPIRV/SPIRVUtil.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,12 @@ bool lowerBuiltinVariableToCall(GlobalVariable *GV,
20212021
llvm_unreachable("Unexpected pattern!");
20222022
}
20232023
}
2024+
} else if (auto *GEP = dyn_cast<GetElementPtrInst>(UI)) {
2025+
GEPs.push_back(GEP);
2026+
for (auto *GEPUser : GEP->users()) {
2027+
if (!ReplaceIfLoad(GEPUser))
2028+
llvm_unreachable("Unexpected pattern!");
2029+
}
20242030
} else if (!ReplaceIfLoad(UI)) {
20252031
llvm_unreachable("Unexpected pattern!");
20262032
}

test/transcoding/builtin_vars_gep.ll

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -o %t.spv
3+
; RUN: llvm-spirv %t.spv -to-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
4+
; RUN: spirv-val %t.spv
5+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv -o %t.out.bc
6+
; RUN: llvm-dis %t.out.bc -o - | FileCheck %s --check-prefix=CHECK-OCL-IR
7+
; RUN: llvm-spirv -r -emit-opaque-pointers %t.spv --spirv-target-env=SPV-IR -o %t.out.bc
8+
; RUN: llvm-dis %t.out.bc -o - | FileCheck %s --check-prefix=CHECK-SPV-IR
9+
10+
; Check that produced builtin-call-based SPV-IR is recognized by the translator
11+
; RUN: llvm-spirv -spirv-text %t.out.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
12+
13+
; CHECK-SPIRV: Decorate [[Id:[0-9]+]] BuiltIn 28
14+
; CHECK-SPIRV: Variable {{[0-9]+}} [[Id:[0-9]+]]
15+
16+
target datalayout = "e-p:32:32-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
17+
target triple = "spir-unknown-unknown"
18+
19+
@__spirv_BuiltInGlobalInvocationId = external dso_local local_unnamed_addr addrspace(1) constant <3 x i64>, align 32
20+
21+
; Function Attrs: nounwind readnone
22+
define spir_kernel void @f() {
23+
entry:
24+
%0 = load i64, i64 addrspace(1)* getelementptr (<3 x i64>, <3 x i64> addrspace(1)* @__spirv_BuiltInGlobalInvocationId, i64 0, i64 0), align 32
25+
; CHECK-OCL-IR: %[[#ID1:]] = call spir_func i64 @_Z13get_global_idj(i32 0)
26+
; CHECK-OCL-IR: %[[#VEC1:]] = insertelement <3 x i64> undef, i64 %[[#ID1]], i32 0
27+
; CHECK-OCL-IR: %[[#ID2:]] = call spir_func i64 @_Z13get_global_idj(i32 1)
28+
; CHECK-OCL-IR: %[[#VEC2:]] = insertelement <3 x i64> %[[#VEC1]], i64 %[[#ID2]], i32 1
29+
; CHECK-OCL-IR: %[[#ID3:]] = call spir_func i64 @_Z13get_global_idj(i32 2)
30+
; CHECK-OCL-IR: %[[#VEC3:]] = insertelement <3 x i64> %[[#VEC2]], i64 %[[#ID3]], i32 2
31+
; CHECK-OCL-IR: %[[#]] = extractelement <3 x i64> %[[#VEC3]], i64 0
32+
33+
; CHECK-SPV-IR: %[[#ID1:]] = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 0)
34+
; CHECK-SPV-IR: %[[#VEC1:]] = insertelement <3 x i64> undef, i64 %[[#ID1]], i32 0
35+
; CHECK-SPV-IR: %[[#ID2:]] = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 1)
36+
; CHECK-SPV-IR: %[[#VEC2:]] = insertelement <3 x i64> %[[#VEC1]], i64 %[[#ID2]], i32 1
37+
; CHECK-SPV-IR: %[[#ID3:]] = call spir_func i64 @_Z33__spirv_BuiltInGlobalInvocationIdi(i32 2)
38+
; CHECK-SPV-IR: %[[#VEC3:]] = insertelement <3 x i64> %[[#VEC2]], i64 %[[#ID3]], i32 2
39+
; CHECK-SPV-IR: %[[#]] = extractelement <3 x i64> %[[#VEC3]], i64 0
40+
41+
ret void
42+
}

0 commit comments

Comments
 (0)