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 8 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
35 changes: 31 additions & 4 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1340,9 +1340,18 @@ 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.
bool NonSYCLCompilation = !JA.isOffloading(Action::OFK_SYCL);
bool SYCLHostCompilation = JA.isOffloading(Action::OFK_SYCL) &&
!JA.isDeviceOffloading(Action::OFK_SYCL);
srividya-sundaram marked this conversation as resolved.
Show resolved Hide resolved

if (!isa<PrecompileJobAction>(JA) &&
(NonSYCLCompilation || SYCLHostCompilation)) {
CmdArgs.push_back("-include-pch");
CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
C, !ThroughHeader.empty()
Expand All @@ -1361,9 +1370,13 @@ 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()) {
// Emit an error when PCH file is used in SYCL device mode.
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 @@ -1383,8 +1396,15 @@ 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.
bool SYCLHostComp = JA.isOffloading(Action::OFK_SYCL) &&
!JA.isDeviceOffloading(Action::OFK_SYCL);

bool NonSYCLComp = !JA.isOffloading(Action::OFK_SYCL);

if (FoundPCH) {
if (FoundPCH && (SYCLHostComp || NonSYCLComp)) {
if (IsFirstImplicitInclude) {
A->claim();
CmdArgs.push_back("-include-pch");
Expand All @@ -1396,6 +1416,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.
// The PCH file is created without the -fsycl option.
srividya-sundaram marked this conversation as resolved.
Show resolved Hide resolved

// RUN: touch %t.h

// RUN: %clang -c -x c++-header %t.h
srividya-sundaram marked this conversation as resolved.
Show resolved Hide resolved

// 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