Skip to content

Commit

Permalink
[SYCL] Add option fsycl-allow-func-ptr which defaults to false
Browse files Browse the repository at this point in the history
Signed-off-by: Vladimir Lazarev <[email protected]>
Signed-off-by: Blower, Melanie <[email protected]>
  • Loading branch information
Blower, Melanie authored and vladimirlaz committed Mar 22, 2019
1 parent 02f6d68 commit e878f1d
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ LANGOPT(GPURelocatableDeviceCode, 1, 0, "generate relocatable device code")

LANGOPT(SYCLIsDevice , 1, 0, "Generate code for SYCL device")
LANGOPT(SYCLUseBitcode , 1, 0, "Generate bitcode for SYCL")
LANGOPT(SYCLAllowFuncPtr , 1, 0, "Allow function pointers in SYCL device code")

LANGOPT(SizedDeallocation , 1, 0, "sized deallocation")
LANGOPT(AlignedAllocation , 1, 0, "aligned allocation")
Expand Down
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/CC1Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ def fsycl_int_header : Separate<["-"], "fsycl-int-header">,
HelpText<"Generate SYCL integration header into this file.">;
def fsycl_int_header_EQ : Joined<["-"], "fsycl-int-header=">,
Alias<fsycl_int_header>;
def fsycl_allow_func_ptr : Flag<["-"], "fsycl-allow-func-ptr">,
HelpText<"Allow function pointers in SYCL device.">;
def fno_sycl_allow_func_ptr : Flag<["-"], "fno-sycl-allow-func-ptr">;

} // let Flags = [CC1Option]

Expand Down
4 changes: 4 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3550,6 +3550,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
options::OPT_fno_sycl_use_bitcode, true)) {
CmdArgs.push_back("-fsycl-use-bitcode");
}
if (Args.hasFlag(options::OPT_fsycl_allow_func_ptr,
options::OPT_fno_sycl_allow_func_ptr, false)) {
CmdArgs.push_back("-fsycl-allow-func-ptr");
}
}

if (IsOpenMPDevice) {
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2893,6 +2893,8 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK,
Opts.SYCLIsDevice = Args.hasArg(options::OPT_fsycl_is_device);
Opts.SYCLUseBitcode = Args.hasFlag(options::OPT_fsycl_use_bitcode,
options::OPT_fno_sycl_use_bitcode, false);
Opts.SYCLAllowFuncPtr = Args.hasFlag(options::OPT_fsycl_allow_func_ptr,
options::OPT_fno_sycl_allow_func_ptr, false);

// Set CUDA mode for OpenMP target NVPTX if specified in options
Opts.OpenMPCUDAMode = Opts.OpenMPIsDevice && T.isNVPTX() &&
Expand Down
3 changes: 1 addition & 2 deletions clang/lib/Sema/SemaSYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ class MarkDeviceFunction : public RecursiveASTVisitor<MarkDeviceFunction> {
SemaRef.AddSyclKernel(Def);
}
}
} else {
} else if (!SemaRef.getLangOpts().SYCLAllowFuncPtr)
SemaRef.Diag(e->getExprLoc(), diag::err_sycl_restrict) <<
KernelCallFunctionPointer;
}
return true;
}

Expand Down
8 changes: 7 additions & 1 deletion clang/test/SemaSYCL/sycl-restrict.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// RUN: %clang_cc1 -fcxx-exceptions -fsycl-is-device -Wno-return-type -verify -fsyntax-only -x c++ -emit-llvm-only -std=c++17 %s
// RUN: %clang_cc1 -fcxx-exceptions -fsycl-is-device -fno-sycl-allow-func-ptr -Wno-return-type -verify -fsyntax-only -x c++ -emit-llvm-only -std=c++17 %s
// RUN: %clang_cc1 -fcxx-exceptions -fsycl-is-device -DALLOW_FP=1 -fsycl-allow-func-ptr -Wno-return-type -verify -fsyntax-only -x c++ -emit-llvm-only -std=c++17 %s


namespace std {
Expand Down Expand Up @@ -104,7 +106,11 @@ void usage( myFuncDef functionPtr ) {

eh_not_ok();

// expected-error@+1 {{SYCL kernel cannot call through a function pointer}}
#if ALLOW_FP
// No error message for function pointer.
#else
// expected-error@+2 {{SYCL kernel cannot call through a function pointer}}
#endif
if ((*functionPtr)(1,2))
// expected-note@+3{{used here}}
// expected-error@+2 {{SYCL kernel cannot use a global variable}}
Expand Down

0 comments on commit e878f1d

Please sign in to comment.