Skip to content

Commit c0be43f

Browse files
committed
[clang][SPIRV] Don't addrspacecast nullptr for function pointer types
Signed-off-by: Nick Sarnie <[email protected]>
1 parent 61e5bc3 commit c0be43f

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

clang/lib/CodeGen/Targets/SPIR.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,16 @@ CommonSPIRTargetCodeGenInfo::getNullPointer(const CodeGen::CodeGenModule &CGM,
260260
LangAS AS = QT->getUnqualifiedDesugaredType()->isNullPtrType()
261261
? LangAS::Default
262262
: QT->getPointeeType().getAddressSpace();
263+
unsigned ASAsInt = static_cast<unsigned>(AS);
264+
unsigned FirstTargetASAsInt =
265+
static_cast<unsigned>(LangAS::FirstTargetAddressSpace);
266+
unsigned CodeSectionINTELAS = FirstTargetASAsInt + 9;
267+
// As per SPV_INTEL_function_pointers, it is illegal to addrspacecast
268+
// function pointers to/from the generic AS.
269+
bool IsFunctionPtrAS =
270+
CGM.getTriple().isSPIRV() && ASAsInt == CodeSectionINTELAS;
263271
if (AS == LangAS::Default || AS == LangAS::opencl_generic ||
264-
AS == LangAS::opencl_constant)
272+
AS == LangAS::opencl_constant || IsFunctionPtrAS)
265273
return llvm::ConstantPointerNull::get(PT);
266274

267275
auto &Ctx = CGM.getContext();
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// RUN: %clang_cc1 -triple spirv64-intel %s -emit-llvm -o - | FileCheck -check-prefix=CHECK-WITH-64 %s
2-
// RUN: %clang_cc1 -triple spirv32-intel %s -emit-llvm -o - | FileCheck -check-prefix=CHECK-WITH-32 %s
1+
// RUN: %clang_cc1 -triple spirv64-intel %s -emit-llvm -o - | FileCheck -check-prefixes=CHECK-WITH,CHECK-WITH-64 %s
2+
// RUN: %clang_cc1 -triple spirv32-intel %s -emit-llvm -o - | FileCheck -check-prefixes=CHECK-WITH,CHECK-WITH-32 %s
33
// RUN: %clang_cc1 -triple spir-intel %s -emit-llvm -o - | FileCheck -check-prefix=CHECK-WITHOUT %s
44
// RUN: %clang_cc1 -triple spir64-intel %s -emit-llvm -o - | FileCheck -check-prefix=CHECK-WITHOUT %s
55

@@ -9,3 +9,11 @@
99
// CHECK-WITHOUT: spir_func void @foo(ptr noundef %param) #0 {
1010
void foo(int *param) {
1111
}
12+
13+
typedef __attribute__((address_space(9))) void * FnPtrTy;
14+
15+
// CHECK-WITH: %{{.*}} = icmp eq ptr addrspace(9) %{{.*}}, null
16+
int bar() {
17+
FnPtrTy FnPtr = (FnPtrTy)foo;
18+
return FnPtr == 0;
19+
}

0 commit comments

Comments
 (0)