Skip to content

[Driver][SYCL] refactor -fsycl-device-only behaviors #2713

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
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
55 changes: 32 additions & 23 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,10 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
// the -fsycl-targets, -fsycl-add-targets or -fsycl-link-targets option.
// If -fsycl is supplied without any of these we will assume SPIR-V.
// Use of -fsycl-device-only overrides -fsycl.
bool HasValidSYCLRuntime = (C.getInputArgs().hasFlag(options::OPT_fsycl,
options::OPT_fno_sycl, false) &&
!C.getInputArgs().hasArg(options::OPT_fsycl_device_only));
bool HasValidSYCLRuntime =
(C.getInputArgs().hasFlag(options::OPT_fsycl, options::OPT_fno_sycl,
false) ||
C.getInputArgs().hasArg(options::OPT_fsycl_device_only));

// A mechanism for retrieving SYCL-specific options, erroring out
// if SYCL offloading wasn't enabled prior to that
Expand Down Expand Up @@ -913,11 +914,18 @@ void Driver::CreateOffloadingDeviceToolChains(Compilation &C,
} else {
// If -fsycl is supplied without -fsycl-*targets we will assume SPIR-V
// unless -fintelfpga is supplied, which uses SPIR-V with fpga AOT.
if (HasValidSYCLRuntime) {
// For -fsycl-device-only, we also setup the implied triple as needed.
StringRef SYCLTargetArch;
if (C.getInputArgs().hasArg(options::OPT_fsycl_device_only))
if (C.getDefaultToolChain().getTriple().getArch() == llvm::Triple::x86)
SYCLTargetArch = "spir";
else
SYCLTargetArch = "spir64";
else if (HasValidSYCLRuntime)
// Triple for -fintelfpga is spir64_fpga-unknown-unknown-sycldevice.
const char *SYCLTargetArch = SYCLfpga ? "spir64_fpga" : "spir64";
SYCLTargetArch = SYCLfpga ? "spir64_fpga" : "spir64";
if (!SYCLTargetArch.empty())
UniqueSYCLTriplesVec.push_back(MakeSYCLDeviceTriple(SYCLTargetArch));
}
}
// We'll need to use the SYCL and host triples as the key into
// getOffloadingDeviceToolChain, because the device toolchains we're
Expand Down Expand Up @@ -3700,10 +3708,24 @@ class OffloadingActionBuilder final {
// The host depends on the generated integrated header from the device
// compilation.
if (CurPhase == phases::Compile) {
bool SYCLDeviceOnly = Args.hasArg(options::OPT_fsycl_device_only);
for (Action *&A : SYCLDeviceActions) {
DeviceCompilerInput =
C.MakeAction<CompileJobAction>(A, types::TY_SYCL_Header);
A = C.MakeAction<CompileJobAction>(A, types::TY_LLVM_BC);
types::ID OutputType = types::TY_LLVM_BC;
if (SYCLDeviceOnly) {
if (Args.hasArg(options::OPT_S))
OutputType = types::TY_LLVM_IR;
if (Args.hasFlag(options::OPT_fno_sycl_use_bitcode,
options::OPT_fsycl_use_bitcode, false)) {
auto *CompileAction =
C.MakeAction<CompileJobAction>(A, types::TY_LLVM_BC);
A = C.MakeAction<SPIRVTranslatorJobAction>(CompileAction,
types::TY_SPIRV);
continue;
}
}
A = C.MakeAction<CompileJobAction>(A, OutputType);
}
const auto *TC = ToolChains.front();
const char *BoundArch = nullptr;
Expand All @@ -3713,7 +3735,7 @@ class OffloadingActionBuilder final {
// Clear the input file, it is already a dependence to a host
// action.
DeviceCompilerInput = nullptr;
return ABRT_Success;
return SYCLDeviceOnly ? ABRT_Ignore_Host : ABRT_Success;
}

// Backend/Assemble actions are obsolete for the SYCL device side
Expand Down Expand Up @@ -4351,8 +4373,8 @@ class OffloadingActionBuilder final {
Arg *SYCLTargets =
C.getInputArgs().getLastArg(options::OPT_fsycl_targets_EQ);
Arg *SYCLAddTargets = Args.getLastArg(options::OPT_fsycl_add_targets_EQ);
bool HasValidSYCLRuntime = C.getInputArgs().hasFlag(options::OPT_fsycl,
options::OPT_fno_sycl, false);
bool HasValidSYCLRuntime = C.getInputArgs().hasFlag(
options::OPT_fsycl, options::OPT_fno_sycl, false);
bool SYCLfpgaTriple = false;
if (SYCLTargets || SYCLAddTargets) {
if (SYCLTargets) {
Expand Down Expand Up @@ -5411,19 +5433,6 @@ Action *Driver::ConstructPhaseAction(
Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
return C.MakeAction<BackendJobAction>(Input, Output);
}
if (Args.hasArg(options::OPT_fsycl_device_only)) {
types::ID OutputType =
Args.hasArg(options::OPT_S) ? types::TY_LLVM_IR : types::TY_LLVM_BC;
if (Args.hasFlag(options::OPT_fsycl_use_bitcode,
options::OPT_fno_sycl_use_bitcode, true))
return C.MakeAction<BackendJobAction>(Input, OutputType);
// Use of -fsycl-device-only creates a bitcode file, we need to translate
// that to a SPIR-V file with -fno-sycl-use-bitcode
auto *BackendAction =
C.MakeAction<BackendJobAction>(Input, types::TY_LLVM_BC);
return C.MakeAction<SPIRVTranslatorJobAction>(BackendAction,
types::TY_SPIRV);
}
return C.MakeAction<BackendJobAction>(Input, types::TY_PP_Asm);
}
case phases::Assemble:
Expand Down
33 changes: 6 additions & 27 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1232,8 +1232,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
if (JA.isOffloading(Action::OFK_HIP))
getToolChain().AddHIPIncludeArgs(Args, CmdArgs);

if (JA.isOffloading(Action::OFK_SYCL) ||
Args.hasArg(options::OPT_fsycl_device_only))
if (JA.isOffloading(Action::OFK_SYCL))
toolchains::SYCLToolChain::AddSYCLIncludeArgs(D, Args, CmdArgs);

// If we are offloading to a target via OpenMP we need to include the
Expand Down Expand Up @@ -4029,24 +4028,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
const ArgList &Args, const char *LinkingOutput) const {
const auto &TC = getToolChain();
const llvm::Triple &RawTriple = TC.getTriple();
llvm::Triple Triple = TC.getEffectiveTriple();
const llvm::Triple &Triple = TC.getEffectiveTriple();
const std::string &TripleStr = Triple.getTriple();

bool KernelOrKext =
Args.hasArg(options::OPT_mkernel, options::OPT_fapple_kext);
const Driver &D = TC.getDriver();
ArgStringList CmdArgs;

// -fsycl-device-only implies a SPIRV arch triple. Do not set if current
// effective triple is SYCLDevice
if (Args.hasArg(options::OPT_fsycl_device_only) &&
Triple.getEnvironment() != llvm::Triple::SYCLDevice) {
const char *SYCLTargetArch = "spir64";
if (C.getDefaultToolChain().getTriple().getArch() == llvm::Triple::x86)
SYCLTargetArch = "spir";
Triple = C.getDriver().MakeSYCLDeviceTriple(SYCLTargetArch);
}
const std::string &TripleStr = Triple.getTriple();

// Check number of inputs for sanity. We need at least one input.
assert(Inputs.size() >= 1 && "Must have at least one input.");
// CUDA/HIP compilation may have multiple inputs (source file + results of
Expand All @@ -4061,8 +4050,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
bool IsHIP = JA.isOffloading(Action::OFK_HIP);
bool IsOpenMPDevice = JA.isDeviceOffloading(Action::OFK_OpenMP);
bool IsSYCLOffloadDevice = JA.isDeviceOffloading(Action::OFK_SYCL);
bool IsSYCL = JA.isOffloading(Action::OFK_SYCL) ||
Args.hasArg(options::OPT_fsycl_device_only);
bool IsSYCL = JA.isOffloading(Action::OFK_SYCL);
bool IsHeaderModulePrecompile = isa<HeaderModulePrecompileJobAction>(JA);
assert((IsCuda || IsHIP || (IsOpenMPDevice && Inputs.size() == 2) || IsSYCL ||
IsHeaderModulePrecompile || Inputs.size() == 1) &&
Expand Down Expand Up @@ -4118,9 +4106,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// step, and being part of the SYCL tool chain causes the incorrect target.
// FIXME - Is it possible to retain host environment when on a target
// device toolchain.
bool UseSYCLTriple =
IsSYCLDevice && (!IsSYCL || IsSYCLOffloadDevice ||
Args.hasArg(options::OPT_fsycl_device_only));
bool UseSYCLTriple = IsSYCLDevice && (!IsSYCL || IsSYCLOffloadDevice);

// Adjust IsWindowsXYZ for CUDA/HIP/SYCL compilations. Even when compiling in
// device mode (i.e., getToolchain().getTriple() is NVPTX/AMDGCN, not
Expand Down Expand Up @@ -4222,9 +4208,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,

// Pass the triple of host when doing SYCL
llvm::Triple AuxT = C.getDefaultToolChain().getTriple();
if (Args.hasArg(options::OPT_fsycl_device_only) &&
RawTriple.getEnvironment() == llvm::Triple::SYCLDevice)
AuxT = llvm::Triple(llvm::sys::getProcessTriple());
std::string NormalizedTriple = AuxT.normalize();
CmdArgs.push_back("-aux-triple");
CmdArgs.push_back(Args.MakeArgString(NormalizedTriple));
Expand Down Expand Up @@ -6365,8 +6348,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
// SYCL library is guaranteed to work correctly only with dynamic
// MSVC runtime.
llvm::Triple AuxT = C.getDefaultToolChain().getTriple();
if (Args.hasFlag(options::OPT_fsycl_device_only, OptSpecifier(), false))
AuxT = llvm::Triple(llvm::sys::getProcessTriple());
if (AuxT.isWindowsMSVCEnvironment()) {
CmdArgs.push_back("-D_MT");
CmdArgs.push_back("-D_DLL");
Expand Down Expand Up @@ -6891,7 +6872,6 @@ void Clang::AddClangCLArgs(const ArgList &Args, types::ID InputType,
unsigned RTOptionID = options::OPT__SLASH_MT;
bool isNVPTX = getToolChain().getTriple().isNVPTX();
bool isSYCLDevice =
Args.hasArg(options::OPT_fsycl_device_only) ||
getToolChain().getTriple().getEnvironment() == llvm::Triple::SYCLDevice;
bool isSYCL = Args.hasArg(options::OPT_fsycl) || isSYCLDevice;
// For SYCL Windows, /MD is the default.
Expand Down Expand Up @@ -7906,8 +7886,7 @@ void SPIRVTranslator::ConstructJob(Compilation &C, const JobAction &JA,

TranslatorArgs.push_back("-o");
TranslatorArgs.push_back(Output.getFilename());
if (getToolChain().getTriple().isSYCLDeviceEnvironment() ||
TCArgs.hasArg(options::OPT_fsycl_device_only)) {
if (getToolChain().getTriple().isSYCLDeviceEnvironment()) {
TranslatorArgs.push_back("-spirv-max-version=1.1");
TranslatorArgs.push_back("-spirv-debug-info-version=legacy");
// Prevent crash in the translator if input IR contains DIExpression
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Driver/ToolChains/Cuda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,8 @@ void CudaToolChain::addClangTargetOptions(
CC1Args);
}

auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv);
auto NoLibSpirv = DriverArgs.hasArg(options::OPT_fno_sycl_libspirv,
options::OPT_fsycl_device_only);
if (DeviceOffloadingKind == Action::OFK_SYCL && !NoLibSpirv) {
std::string LibSpirvFile;

Expand Down
8 changes: 4 additions & 4 deletions clang/test/Driver/sycl.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@

/// Verify -fsycl-device-only phases
// RUN: %clang -### -ccc-print-phases -fsycl-device-only %s 2>&1 | FileCheck %s --check-prefix=DEFAULT-PHASES
// DEFAULT-PHASES: 0: input, "{{.*}}", c++
// DEFAULT-PHASES: 1: preprocessor, {0}, c++-cpp-output
// DEFAULT-PHASES: 2: compiler, {1}, ir
// DEFAULT-PHASES: 3: backend, {2}, ir
// DEFAULT-PHASES: 0: input, "{{.*}}", c++, (device-sycl)
// DEFAULT-PHASES: 1: preprocessor, {0}, c++-cpp-output, (device-sycl)
// DEFAULT-PHASES: 2: compiler, {1}, ir, (device-sycl)
// DEFAULT-PHASES: 3: offload, "device-sycl (spir64-unknown-unknown-sycldevice)" {2}, ir
// DEFAULT-PHASES-NOT: linker

// -fsycl-help tests
Expand Down
2 changes: 1 addition & 1 deletion libclc/cmake/modules/AddLibclc.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ function(add_libclc_sycl_binding OUT_LIST)
COMMAND ${CMAKE_COMMAND} -E make_directory
${CMAKE_CURRENT_BINARY_DIR}/sycldevice-binding-${ARG_TRIPLE}
COMMAND ${LLVM_CLANG}
-target ${ARG_TRIPLE}-sycldevice
-fsycl-targets=${ARG_TRIPLE}-sycldevice
-fsycl
-fsycl-device-only
-Dcl_khr_fp64
Expand Down