Skip to content

Commit d7276bf

Browse files
authored
[SYCL] Cast to correct address space when a function object is passed (#3303)
When a function object is passed as an argument to a call, type mismatches may be address space mismatches and these cannot be bitcast away. Check if it is an address space mismatch and if so, use the appropriate address space cast. Signed-off-by: Premanand M Rao <[email protected]>
1 parent c7d8867 commit d7276bf

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

clang/include/clang/Driver/Options.td

+5-1
Original file line numberDiff line numberDiff line change
@@ -5524,7 +5524,11 @@ def fsycl_int_header_EQ : Joined<["-"], "fsycl-int-header=">,
55245524
def fsycl_std_layout_kernel_params: Flag<["-"], "fsycl-std-layout-kernel-params">,
55255525
HelpText<"Enable standard layout requirement for SYCL kernel parameters.">,
55265526
MarshallingInfoFlag<LangOpts<"SYCLStdLayoutKernelParams">>;
5527-
defm sycl_allow_func_ptr: OptInFFlag<"sycl-allow-func-ptr", "Allow", "Disallow", " function pointers in SYCL device.", [CC1Option,CoreOption], LangOpts<"SYCLAllowFuncPtr">>;
5527+
defm sycl_allow_func_ptr: BoolFOption<"sycl-allow-func-ptr",
5528+
LangOpts<"SYCLAllowFuncPtr">, DefaultFalse,
5529+
PosFlag<SetTrue, [], "Allow">,
5530+
NegFlag<SetFalse, [], "Disallow">,
5531+
BothFlags<[CC1Option, CoreOption], " function pointers in SYCL device.">>;
55285532
def fenable_sycl_dae : Flag<["-"], "fenable-sycl-dae">,
55295533
HelpText<"Enable Dead Argument Elimination in SPIR kernels">,
55305534
MarshallingInfoFlag<LangOpts<"EnableDAEInSpirKernels">>;

clang/lib/CodeGen/CGCall.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -4774,9 +4774,14 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo,
47744774
// If the argument doesn't match, perform a bitcast to coerce it. This
47754775
// can happen due to trivial type mismatches.
47764776
if (FirstIRArg < IRFuncTy->getNumParams() &&
4777-
V->getType() != IRFuncTy->getParamType(FirstIRArg))
4778-
V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
4779-
4777+
V->getType() != IRFuncTy->getParamType(FirstIRArg)) {
4778+
if (V->getType()->getPointerAddressSpace() !=
4779+
IRFuncTy->getParamType(FirstIRArg)->getPointerAddressSpace())
4780+
V = Builder.CreateAddrSpaceCast(V,
4781+
IRFuncTy->getParamType(FirstIRArg));
4782+
else
4783+
V = Builder.CreateBitCast(V, IRFuncTy->getParamType(FirstIRArg));
4784+
}
47804785
IRCallArgs[FirstIRArg] = V;
47814786
break;
47824787
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// 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
2+
3+
// Test that the type of function object invoked from the kernel has
4+
// the right address space.
5+
6+
#include "sycl.hpp"
7+
8+
using namespace cl::sycl;
9+
queue q;
10+
11+
// CHECK: define linkonce_odr spir_func i32 @{{.*}}invoke_function{{.*}}(i32 () addrspace(4)* %f)
12+
template <typename Callable>
13+
auto invoke_function(Callable &&f) {
14+
// CHECK: %f.addr = alloca i32 () addrspace(4)*, align 8
15+
// CHECK: %f.addr.ascast = addrspacecast i32 () addrspace(4)** %f.addr to i32 () addrspace(4)* addrspace(4)*
16+
// CHECK: store i32 () addrspace(4)* %f, i32 () addrspace(4)* addrspace(4)* %f.addr.ascast, align 8
17+
// CHECK: %0 = load i32 () addrspace(4)*, i32 () addrspace(4)* addrspace(4)* %f.addr.ascast, align 8
18+
// CHECK: %call = call spir_func addrspace(4) i32 %0()
19+
return f();
20+
}
21+
22+
// CHECK: define dso_local spir_func i32 @{{.*}}bar10{{.*}}()
23+
int bar10() { return 10; }
24+
25+
int main() {
26+
kernel_single_task<class KernelName>(
27+
[=]() {
28+
invoke_function(bar10);
29+
});
30+
return 0;
31+
}

0 commit comments

Comments
 (0)