Skip to content

Commit b59d93c

Browse files
authored
[Driver][SYCL] Set target macro for host compilation (#7108)
When building with -fsycl-targets=intel_gpu*, we add pre-defined macros to the device compilation. We need to extend this as the same macros need to be passed to the host compilation.
1 parent 6c9a380 commit b59d93c

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

clang/include/clang/Driver/Driver.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -797,6 +797,11 @@ class Driver {
797797
/// targets.
798798
mutable llvm::StringMap<StringRef> SYCLUniqueIDList;
799799

800+
/// Vector of Macros that need to be added to the Host compilation in a
801+
/// SYCL based offloading scenario. These macros are gathered during
802+
/// construction of the device compilations.
803+
mutable std::vector<std::string> SYCLTargetMacroArgs;
804+
800805
/// Return the typical executable name for the specified driver \p Mode.
801806
static const char *getExecutableForDriverMode(DriverMode Mode);
802807

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

875+
/// addSYCLTargetMacroArg - Add the given macro to the vector of args to be
876+
/// added to the host compilation step.
877+
void addSYCLTargetMacroArg(const llvm::opt::ArgList &Args,
878+
StringRef Macro) const {
879+
SYCLTargetMacroArgs.push_back(Args.MakeArgString(Macro));
880+
}
881+
/// getSYCLTargetMacroArgs - return the previously gathered macro target args.
882+
llvm::ArrayRef<std::string> getSYCLTargetMacroArgs() const {
883+
return SYCLTargetMacroArgs;
884+
}
885+
870886
/// setSYCLUniqueID - set the Unique ID that is used for all FE invocations
871887
/// when performing compilations for SYCL.
872888
void addSYCLUniqueID(StringRef UniqueID, StringRef FileName) const {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5094,19 +5094,6 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
50945094

50955095
// Forward -fsycl-default-sub-group-size if in SYCL mode.
50965096
Args.AddLastArg(CmdArgs, options::OPT_fsycl_default_sub_group_size);
5097-
5098-
// Add any predefined macros associated with intel_gpu* type targets
5099-
// passed in with -fsycl-targets
5100-
if (RawTriple.isSPIR() &&
5101-
RawTriple.getSubArch() == llvm::Triple::SPIRSubArch_gen) {
5102-
StringRef Device = JA.getOffloadingArch();
5103-
if (!Device.empty())
5104-
CmdArgs.push_back(Args.MakeArgString(
5105-
Twine("-D") + SYCL::gen::getGenDeviceMacro(Device)));
5106-
}
5107-
if (RawTriple.isSPIR() &&
5108-
RawTriple.getSubArch() == llvm::Triple::SPIRSubArch_x86_64)
5109-
CmdArgs.push_back("-D__SYCL_TARGET_INTEL_X86_64__");
51105097
}
51115098

51125099
if (IsSYCL) {
@@ -5192,6 +5179,35 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51925179
}
51935180
}
51945181
}
5182+
// Add any predefined macros associated with intel_gpu* type targets
5183+
// passed in with -fsycl-targets
5184+
// TODO: Macros are populated during device compilations and saved for
5185+
// addition to the host compilation. There is no dependence connection
5186+
// between device and host where we should be able to use the offloading
5187+
// arch to add the macro to the host compile.
5188+
auto addTargetMacros = [&](const llvm::Triple &Triple) {
5189+
if (!Triple.isSPIR())
5190+
return;
5191+
SmallString<64> Macro;
5192+
if (Triple.getSubArch() == llvm::Triple::SPIRSubArch_gen) {
5193+
StringRef Device = JA.getOffloadingArch();
5194+
if (!Device.empty()) {
5195+
Macro = "-D";
5196+
Macro += SYCL::gen::getGenDeviceMacro(Device);
5197+
}
5198+
} else if (Triple.getSubArch() == llvm::Triple::SPIRSubArch_x86_64)
5199+
Macro = "-D__SYCL_TARGET_INTEL_X86_64__";
5200+
if (Macro.size()) {
5201+
CmdArgs.push_back(Args.MakeArgString(Macro));
5202+
D.addSYCLTargetMacroArg(Args, Macro);
5203+
}
5204+
};
5205+
if (IsSYCLOffloadDevice)
5206+
addTargetMacros(RawTriple);
5207+
else {
5208+
for (auto &Macro : D.getSYCLTargetMacroArgs())
5209+
CmdArgs.push_back(Args.MakeArgString(Macro));
5210+
}
51955211
}
51965212

51975213
if (IsOpenMPDevice) {

clang/test/Driver/sycl-intel-gpu.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@
8484
// MACRO: clang{{.*}} "-triple" "spir64_gen-unknown-unknown"
8585
// MACRO: "-D__SYCL_TARGET_INTEL_GPU_[[MAC_STR]]__"
8686
// DEVICE: ocloc{{.*}} "-device" "[[DEV_STR]]"
87+
// MACRO: clang{{.*}} "-fsycl-is-host"
88+
// MACRO: "-D__SYCL_TARGET_INTEL_GPU_[[MAC_STR]]__"
8789

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

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

0 commit comments

Comments
 (0)