Skip to content
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

[SYCL][Driver] Enable PCH inclusion while performing host compilation with -fsycl #9864

Merged
merged 14 commits into from
Jul 10, 2023
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
28 changes: 24 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
const InputInfoList &Inputs) const {
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
const bool IsIntelFPGA = Args.hasArg(options::OPT_fintelfpga);
bool SYCLDeviceCompilation = JA.isOffloading(Action::OFK_SYCL) &&
JA.isDeviceOffloading(Action::OFK_SYCL);

CheckPreprocessingOptions(D, Args);

Expand Down Expand Up @@ -1335,9 +1337,14 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
options::OPT_fno_pch_instantiate_templates, true))
CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates"));
}

if (YcArg || YuArg) {
StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
if (!isa<PrecompileJobAction>(JA)) {
// If PCH file is available, include it while performing
// host compilation (-fsycl-is-host) in SYCL mode (-fsycl).
// as well as in non-sycl mode.

if (!isa<PrecompileJobAction>(JA) && !SYCLDeviceCompilation) {
CmdArgs.push_back("-include-pch");
CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
C, !ThroughHeader.empty()
Expand All @@ -1356,9 +1363,12 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
}

bool RenderedImplicitInclude = false;

for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
if (A->getOption().matches(options::OPT_include) &&
D.getProbePrecompiled()) {
if ((A->getOption().matches(options::OPT_include) &&
D.getProbePrecompiled()) ||
A->getOption().matches(options::OPT_include_pch)) {

// Handling of gcc-style gch precompiled headers.
bool IsFirstImplicitInclude = !RenderedImplicitInclude;
RenderedImplicitInclude = true;
Expand All @@ -1378,8 +1388,11 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
FoundPCH = true;
}
}
// If PCH file is available, include it while performing
// host compilation (-fsycl-is-host) in SYCL mode (-fsycl).
// as well as in non-sycl mode.

if (FoundPCH) {
if (FoundPCH && !SYCLDeviceCompilation) {
if (IsFirstImplicitInclude) {
A->claim();
CmdArgs.push_back("-include-pch");
Expand All @@ -1391,6 +1404,13 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
<< A->getAsString(Args);
}
}
// No PCH file, but we still want to include the header file
// (-include dummy.h) in device compilation mode.
else if (JA.isDeviceOffloading(Action::OFK_SYCL) &&
A->getOption().matches(options::OPT_include_pch)) {
continue;
}

} else if (A->getOption().matches(options::OPT_isystem_after)) {
// Handling of paths which must come late. These entries are handled by
// the toolchain itself after the resource dir is inserted in the right
Expand Down
33 changes: 33 additions & 0 deletions clang/test/Driver/pch-include-sycl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This test checks that a PCH(Precompiled Header) file is
// included while performing host compilation in -fsycl mode.

// RUN: touch %t.h

// PCH file.
// RUN: touch %t.h.gch

// Linux
// -fsycl and -include
// RUN: %clang -fsycl -include %t.h -### %s 2>&1 \
// RUN: | FileCheck -check-prefixes=CHECK-SYCL-HOST,CHECK-SYCL-DEVICE %s
// CHECK-SYCL-DEVICE: -fsycl-is-device
// CHECK-SYCL-DEVICE-NOT: -include-pch
// CHECK-SYCL-HOST: -fsycl-is-host
// CHECK-SYCL-HOST-SAME: -include-pch


// -fsycl and -include-pch
// RUN: %clang -fsycl -include-pch %t.h.gch -### %s 2>&1 \
// RUN: | FileCheck -check-prefixes=CHECK-PCH-HOST,CHECK-PCH-DEVICE %s
// CHECK-PCH-DEVICE: -fsycl-is-device
// CHECK-PCH-DEVICE-NOT: -include-pch
// CHECK-PCH-HOST: -fsycl-is-host
// CHECK-PCH-HOST-SAME: -include-pch

// Windows
// RUN: %clang_cl -fsycl --target=x86_64-unknown-linux-gnu /Yupchfile.h /FIpchfile.h -### %s 2>&1 \
// RUN: | FileCheck -check-prefixes=CHECK-YU-HOST,CHECK-YU-DEVICE %s
// CHECK-YU-DEVICE: -fsycl-is-device
// CHECK-YU-DEVICE-NOT: -include-pch
// CHECK-YU-HOST: -fsycl-is-host
// CHECK-YU-HOST-SAME: -include-pch