Skip to content

[SYCL] Cast to correct address space when a function object is passed #3303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -5524,7 +5524,11 @@ def fsycl_int_header_EQ : Joined<["-"], "fsycl-int-header=">,
def fsycl_std_layout_kernel_params: Flag<["-"], "fsycl-std-layout-kernel-params">,
HelpText<"Enable standard layout requirement for SYCL kernel parameters.">,
MarshallingInfoFlag<LangOpts<"SYCLStdLayoutKernelParams">>;
defm sycl_allow_func_ptr: OptInFFlag<"sycl-allow-func-ptr", "Allow", "Disallow", " function pointers in SYCL device.", [CC1Option,CoreOption], LangOpts<"SYCLAllowFuncPtr">>;
defm sycl_allow_func_ptr: BoolFOption<"sycl-allow-func-ptr",
LangOpts<"SYCLAllowFuncPtr">, DefaultFalse,
PosFlag<SetTrue, [], "Allow">,
NegFlag<SetFalse, [], "Disallow">,
BothFlags<[CC1Option, CoreOption], " function pointers in SYCL device.">>;
def fenable_sycl_dae : Flag<["-"], "fenable-sycl-dae">,
HelpText<"Enable Dead Argument Elimination in SPIR kernels">,
MarshallingInfoFlag<LangOpts<"EnableDAEInSpirKernels">>;
Expand Down
11 changes: 8 additions & 3 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4774,9 +4774,14 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
// If the argument doesn't match, perform a bitcast to coerce it. This
// can happen due to trivial type mismatches.
if (FirstIRArg < IRFuncTy->getNumParams() &&
V->getType() != IRFuncTy->getParamType(FirstIRArg))
V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));

V->getType() != IRFuncTy->getParamType(FirstIRArg)) {
if (V->getType()->getPointerAddressSpace() !=
IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace())
V = Builder.CreateAddrSpaceCast(V,
IRFuncTy->getParamType(FirstIRArg));
else
V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
}
IRCallArgs[FirstIRArg] = V;
break;
}
Expand Down
31 changes: 31 additions & 0 deletions clang/test/CodeGenSYCL/invoke-function-addrspace.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %clang_cc1 -sycl-std=2020 -fsycl-is-device -fsycl-allow-func-ptr -internal-isystem %S/Inputs -disable-llvm-passes -triple spir64-unknown-unknown-sycldevice -emit-llvm -o - %s | FileCheck %s

// Test that the type of function object invoked from the kernel has
// the right address space.

#include "sycl.hpp"

using namespace cl::sycl;
queue q;

// CHECK: define linkonce_odr spir_func i32 @{{.*}}invoke_function{{.*}}(i32 () addrspace(4)* %f)
template <typename Callable>
auto invoke_function(Callable &&f) {
// CHECK: %f.addr = alloca i32 () addrspace(4)*, align 8
// CHECK: %f.addr.ascast = addrspacecast i32 () addrspace(4)** %f.addr to i32 () addrspace(4)* addrspace(4)*
// CHECK: store i32 () addrspace(4)* %f, i32 () addrspace(4)* addrspace(4)* %f.addr.ascast, align 8
// CHECK: %0 = load i32 () addrspace(4)*, i32 () addrspace(4)* addrspace(4)* %f.addr.ascast, align 8
// CHECK: %call = call spir_func addrspace(4) i32 %0()
return f();
}

// CHECK: define dso_local spir_func i32 @{{.*}}bar10{{.*}}()
int bar10() { return 10; }

int main() {
kernel_single_task<class KernelName>(
[=]() {
invoke_function(bar10);
});
return 0;
}