Skip to content

Commit b19e2e4

Browse files
[SYCL] Fix function pointer address space (#5924)
Commits 57fd86d (https://reviews.llvm.org/D77119) and 4eaf584 (https://reviews.llvm.org/D111566) set address space of function pointers unconditionally to program address space. A follow-up commit ed5b42b (https://reviews.llvm.org/D119045) modified the code to retain explicitly specified address spaces for function pointer. This introduced a regression in cases where function pointers were captured as kernel arguments, since openCL kernel generation sets global address space explicitly to all pointers. This patch reverts the behavior for function pointers captured as kernel arguments. Signed-off-by: Elizabeth Andrews <[email protected]>
1 parent d2982c6 commit b19e2e4

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

clang/lib/Sema/SemaSYCL.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2122,15 +2122,18 @@ class SyclKernelDeclCreator : public SyclKernelFieldHandler {
21222122

21232123
bool handlePointerType(FieldDecl *FD, QualType FieldTy) final {
21242124
// USM allows to use raw pointers instead of buffers/accessors, but these
2125-
// pointers point to the specially allocated memory. For pointer fields we
2126-
// add a kernel argument with the same type as field but global address
2127-
// space, because OpenCL requires it.
2125+
// pointers point to the specially allocated memory. For pointer fields,
2126+
// except for function pointer fields, we add a kernel argument with the
2127+
// same type as field but global address space, because OpenCL requires it.
2128+
// Function pointers should have program address space. This is set in
2129+
// CodeGen.
21282130
QualType PointeeTy = FieldTy->getPointeeType();
21292131
Qualifiers Quals = PointeeTy.getQualifiers();
21302132
auto AS = Quals.getAddressSpace();
21312133
// Leave global_device and global_host address spaces as is to help FPGA
21322134
// device in memory allocations
2133-
if (AS != LangAS::sycl_global_device && AS != LangAS::sycl_global_host)
2135+
if (!PointeeTy->isFunctionType() && AS != LangAS::sycl_global_device &&
2136+
AS != LangAS::sycl_global_host)
21342137
Quals.setAddressSpace(LangAS::sycl_global);
21352138
PointeeTy = SemaRef.getASTContext().getQualifiedType(
21362139
PointeeTy.getUnqualifiedType(), Quals);

clang/test/CodeGenSYCL/functionptr-addrspace.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,16 @@ int main() {
2121
invoke_function(r, &a);
2222
invoke_function(f, &a);
2323
});
24+
25+
// Test function pointer as kernel argument. Function pointers should have program address space i.e. 0.
26+
27+
int (*fptr)();
28+
int *ptr;
29+
30+
// define dso_local spir_kernel void @{{.*}}fake_kernel_2{{.*}}(i32 ()* align 4 %_arg_fptr, i32 addrspace(1)* align 4 %_arg_ptr)
31+
kernel_single_task<class fake_kernel_2>([=]() {
32+
invoke_function(fptr, ptr);
33+
});
34+
2435
return 0;
2536
}

0 commit comments

Comments
 (0)