Skip to content

[Driver][SYCL] Set target macro for host compilation #7108

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 1 commit into from
Oct 24, 2022
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
16 changes: 16 additions & 0 deletions clang/include/clang/Driver/Driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,11 @@ class Driver {
/// targets.
mutable llvm::StringMap<StringRef> SYCLUniqueIDList;

/// Vector of Macros that need to be added to the Host compilation in a
/// SYCL based offloading scenario. These macros are gathered during
/// construction of the device compilations.
mutable std::vector<std::string> SYCLTargetMacroArgs;

/// Return the typical executable name for the specified driver \p Mode.
static const char *getExecutableForDriverMode(DriverMode Mode);

Expand Down Expand Up @@ -867,6 +872,17 @@ class Driver {
void createAppendedFooterInput(Action *&Input, Compilation &C,
const llvm::opt::ArgList &Args) const;

/// addSYCLTargetMacroArg - Add the given macro to the vector of args to be
/// added to the host compilation step.
void addSYCLTargetMacroArg(const llvm::opt::ArgList &Args,
StringRef Macro) const {
SYCLTargetMacroArgs.push_back(Args.MakeArgString(Macro));
}
/// getSYCLTargetMacroArgs - return the previously gathered macro target args.
llvm::ArrayRef<std::string> getSYCLTargetMacroArgs() const {
return SYCLTargetMacroArgs;
}

/// setSYCLUniqueID - set the Unique ID that is used for all FE invocations
/// when performing compilations for SYCL.
void addSYCLUniqueID(StringRef UniqueID, StringRef FileName) const {
Expand Down
42 changes: 29 additions & 13 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5094,19 +5094,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,

// Forward -fsycl-default-sub-group-size if in SYCL mode.
Args.AddLastArg(CmdArgs, options::OPT_fsycl_default_sub_group_size);

// Add any predefined macros associated with intel_gpu* type targets
// passed in with -fsycl-targets
if (RawTriple.isSPIR() &&
RawTriple.getSubArch() == llvm::Triple::SPIRSubArch_gen) {
StringRef Device = JA.getOffloadingArch();
if (!Device.empty())
CmdArgs.push_back(Args.MakeArgString(
Twine("-D") + SYCL::gen::getGenDeviceMacro(Device)));
}
if (RawTriple.isSPIR() &&
RawTriple.getSubArch() == llvm::Triple::SPIRSubArch_x86_64)
CmdArgs.push_back("-D__SYCL_TARGET_INTEL_X86_64__");
}

if (IsSYCL) {
Expand Down Expand Up @@ -5192,6 +5179,35 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
}
}
}
// Add any predefined macros associated with intel_gpu* type targets
// passed in with -fsycl-targets
// TODO: Macros are populated during device compilations and saved for
// addition to the host compilation. There is no dependence connection
// between device and host where we should be able to use the offloading
// arch to add the macro to the host compile.
auto addTargetMacros = [&](const llvm::Triple &Triple) {
if (!Triple.isSPIR())
return;
SmallString<64> Macro;
if (Triple.getSubArch() == llvm::Triple::SPIRSubArch_gen) {
StringRef Device = JA.getOffloadingArch();
if (!Device.empty()) {
Macro = "-D";
Macro += SYCL::gen::getGenDeviceMacro(Device);
}
} else if (Triple.getSubArch() == llvm::Triple::SPIRSubArch_x86_64)
Macro = "-D__SYCL_TARGET_INTEL_X86_64__";
if (Macro.size()) {
CmdArgs.push_back(Args.MakeArgString(Macro));
D.addSYCLTargetMacroArg(Args, Macro);
}
};
if (IsSYCLOffloadDevice)
addTargetMacros(RawTriple);
else {
for (auto &Macro : D.getSYCLTargetMacroArgs())
CmdArgs.push_back(Args.MakeArgString(Macro));
}
}

if (IsOpenMPDevice) {
Expand Down
4 changes: 4 additions & 0 deletions clang/test/Driver/sycl-intel-gpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
// MACRO: clang{{.*}} "-triple" "spir64_gen-unknown-unknown"
// MACRO: "-D__SYCL_TARGET_INTEL_GPU_[[MAC_STR]]__"
// DEVICE: ocloc{{.*}} "-device" "[[DEV_STR]]"
// MACRO: clang{{.*}} "-fsycl-is-host"
// MACRO: "-D__SYCL_TARGET_INTEL_GPU_[[MAC_STR]]__"

/// -fsycl-targets=spir64_x86_64 should set a specific macro
// RUN: %clangxx -c -fsycl -fsycl-targets=spir64_x86_64 -### %s 2>&1 | \
Expand All @@ -92,6 +94,8 @@
// RUN: FileCheck %s --check-prefix=MACRO_X86_64
// MACRO_X86_64: clang{{.*}} "-triple" "spir64_x86_64-unknown-unknown"
// MACRO_X86_64: "-D__SYCL_TARGET_INTEL_X86_64__"
// MACRO_X86_64: clang{{.*}} "-fsycl-is-host"
// MACRO_X86_64: "-D__SYCL_TARGET_INTEL_X86_64__"

/// test for invalid arch
// RUN: %clangxx -c -fsycl -fsycl-targets=intel_gpu_bad -### %s 2>&1 | \
Expand Down