Skip to content

Commit 5e53bcc

Browse files
committed
Add check to early exit when OpenCL builtin is not found in the map
Signed-off-by: Arvind Sudarsanam <[email protected]>
1 parent a104311 commit 5e53bcc

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

lib/SPIRV/OCLToSPIRV.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -650,8 +650,10 @@ void OCLToSPIRVBase::visitCallAtomicCpp11(CallInst *CI, StringRef MangledName,
650650
void OCLToSPIRVBase::transAtomicBuiltin(CallInst *CI,
651651
OCLBuiltinTransInfo &Info) {
652652
llvm::Type *AtomicBuiltinsReturnType = CI->getType();
653-
auto SPIRVFunctionName =
654-
getSPIRVFuncName(OCLSPIRVBuiltinMap::map(Info.UniqName));
653+
Op OC = OpNop;
654+
if (!OCLSPIRVBuiltinMap::find(Info.UniqName, &OC))
655+
return;
656+
auto SPIRVFunctionName = getSPIRVFuncName(OC);
655657
bool NeedsNegate = false;
656658
if (AtomicBuiltinsReturnType->isFloatingPointTy()) {
657659
// Translate FP-typed atomic builtins. Currently we only need to

test/check_ocl_builtin.ll

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; Test that user defined/declared functions are not wrongly identified as OpenCL builtins.
2+
3+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
4+
target triple = "spir64-unknown-unknown"
5+
6+
; RUN: llvm-as %s -o %t.bc
7+
; RUN: llvm-spirv %t.bc -spirv-text -o %t.spt
8+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
9+
; RUN: llvm-spirv %t.bc -o %t.spv
10+
; RUN: spirv-val %t.spv
11+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
12+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
13+
14+
; CHECK-SPIRV: Name [[fname1:[0-9]+]] "_Z27atomic_fetch_and_add_uint32Pii"
15+
; CHECK-SPIRV-DAG: Name [[fname2:[0-9]+]] "_Z27atomic_fetch_and_sub_uint32Pii"
16+
; CHECK-SPIRV-DAG: FunctionCall {{[0-9]+}} {{[0-9]+}} [[fname2]] {{[0-9]+}} {{[0-9]+}}
17+
; CHECK-SPIRV-DAG: FunctionCall {{[0-9]+}} {{[0-9]+}} [[fname1]] {{[0-9]+}} {{[0-9]+}}
18+
19+
; CHECK-LLVM: call spir_func i32 @_Z27atomic_fetch_and_sub_uint32Pii
20+
; CHECK-LLVM-DAG: call spir_func i32 @_Z27atomic_fetch_and_add_uint32Pii
21+
22+
; Function Attrs: mustprogress norecurse nounwind
23+
define weak_odr dso_local spir_kernel void @_ZTSN4test_ocl_builtin(ptr addrspace(4) noundef %arrayidx.i311) {
24+
entry:
25+
%call.i312 = call spir_func noundef i32 @_Z27atomic_fetch_and_sub_uint32Pii(ptr addrspace(4) noundef %arrayidx.i311, i32 noundef 1)
26+
%call.i313 = call spir_func noundef i32 @_Z27atomic_fetch_and_add_uint32Pii(ptr addrspace(4) noundef %arrayidx.i311, i32 noundef 1)
27+
ret void
28+
}
29+
30+
; Function Attrs: convergent inlinehint mustprogress norecurse nounwind
31+
define linkonce_odr dso_local spir_func noundef i32 @_Z27atomic_fetch_and_sub_uint32Pii(ptr addrspace(4) noundef %p, i32 noundef %x) {
32+
entry:
33+
%call.i.i.i.i.i.i = tail call spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef %p, i32 noundef 5)
34+
%0 = addrspacecast ptr addrspace(1) %call.i.i.i.i.i.i to ptr addrspace(4)
35+
call spir_func void @__itt_offload_atomic_op_start(ptr addrspace(4) %0, i32 2, i32 0)
36+
%call3.i.i = tail call spir_func noundef i32 @_Z18__spirv_AtomicISubPU3AS1iN5__spv5Scope4FlagENS1_19MemorySemanticsMask4FlagEi(ptr addrspace(1) noundef %call.i.i.i.i.i.i, i32 noundef 1, i32 noundef 896, i32 noundef %x)
37+
%1 = addrspacecast ptr addrspace(1) %call.i.i.i.i.i.i to ptr addrspace(4)
38+
call spir_func void @__itt_offload_atomic_op_finish(ptr addrspace(4) %1, i32 2, i32 0)
39+
ret i32 %call3.i.i
40+
}
41+
42+
declare dso_local spir_func noundef i32 @_Z27atomic_fetch_and_add_uint32Pii(ptr addrspace(4) noundef %p, i32 noundef %x)
43+
44+
declare dso_local spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef, i32 noundef)
45+
46+
declare dso_local spir_func void @__itt_offload_atomic_op_start(ptr addrspace(4) noundef %object, i32 noundef %op_type, i32 noundef %mem_order)
47+
48+
declare dso_local spir_func noundef i32 @_Z18__spirv_AtomicISubPU3AS1iN5__spv5Scope4FlagENS1_19MemorySemanticsMask4FlagEi(ptr addrspace(1) noundef, i32 noundef, i32 noundef, i32 noundef)
49+
50+
declare dso_local spir_func void @__itt_offload_atomic_op_finish(ptr addrspace(4) noundef %object, i32 noundef %op_type, i32 noundef %mem_order)
51+
52+
!opencl.spir.version = !{!0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0, !0}
53+
!spirv.Source = !{!1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1, !1}
54+
55+
!0 = !{i32 1, i32 2}
56+
!1 = !{i32 4, i32 100000}

0 commit comments

Comments
 (0)