Skip to content

Commit 7f12b28

Browse files
[SYCL][Driver] Enable PCH inclusion while performing host compilation with -fsycl (#9864)
This PR allows users to include a Pre-Compiled header (PCH) file while performing the host compilation step with `-fsycl`. **// Create PCH** `clang++ -x c-header test.h` **// Use PCH** ``` clang++ -c -fsycl -include test.h test.cpp clang++ -c -fsycl -include-pch test.h.gch test.cpp ```
1 parent 85d8411 commit 7f12b28

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,6 +1119,8 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
11191119
const InputInfoList &Inputs) const {
11201120
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();
11211121
const bool IsIntelFPGA = Args.hasArg(options::OPT_fintelfpga);
1122+
bool SYCLDeviceCompilation = JA.isOffloading(Action::OFK_SYCL) &&
1123+
JA.isDeviceOffloading(Action::OFK_SYCL);
11221124

11231125
CheckPreprocessingOptions(D, Args);
11241126

@@ -1335,9 +1337,14 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13351337
options::OPT_fno_pch_instantiate_templates, true))
13361338
CmdArgs.push_back(Args.MakeArgString("-fpch-instantiate-templates"));
13371339
}
1340+
13381341
if (YcArg || YuArg) {
13391342
StringRef ThroughHeader = YcArg ? YcArg->getValue() : YuArg->getValue();
1340-
if (!isa<PrecompileJobAction>(JA)) {
1343+
// If PCH file is available, include it while performing
1344+
// host compilation (-fsycl-is-host) in SYCL mode (-fsycl).
1345+
// as well as in non-sycl mode.
1346+
1347+
if (!isa<PrecompileJobAction>(JA) && !SYCLDeviceCompilation) {
13411348
CmdArgs.push_back("-include-pch");
13421349
CmdArgs.push_back(Args.MakeArgString(D.GetClPchPath(
13431350
C, !ThroughHeader.empty()
@@ -1356,9 +1363,12 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13561363
}
13571364

13581365
bool RenderedImplicitInclude = false;
1366+
13591367
for (const Arg *A : Args.filtered(options::OPT_clang_i_Group)) {
1360-
if (A->getOption().matches(options::OPT_include) &&
1361-
D.getProbePrecompiled()) {
1368+
if ((A->getOption().matches(options::OPT_include) &&
1369+
D.getProbePrecompiled()) ||
1370+
A->getOption().matches(options::OPT_include_pch)) {
1371+
13621372
// Handling of gcc-style gch precompiled headers.
13631373
bool IsFirstImplicitInclude = !RenderedImplicitInclude;
13641374
RenderedImplicitInclude = true;
@@ -1378,8 +1388,11 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13781388
FoundPCH = true;
13791389
}
13801390
}
1391+
// If PCH file is available, include it while performing
1392+
// host compilation (-fsycl-is-host) in SYCL mode (-fsycl).
1393+
// as well as in non-sycl mode.
13811394

1382-
if (FoundPCH) {
1395+
if (FoundPCH && !SYCLDeviceCompilation) {
13831396
if (IsFirstImplicitInclude) {
13841397
A->claim();
13851398
CmdArgs.push_back("-include-pch");
@@ -1391,6 +1404,13 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
13911404
<< A->getAsString(Args);
13921405
}
13931406
}
1407+
// No PCH file, but we still want to include the header file
1408+
// (-include dummy.h) in device compilation mode.
1409+
else if (JA.isDeviceOffloading(Action::OFK_SYCL) &&
1410+
A->getOption().matches(options::OPT_include_pch)) {
1411+
continue;
1412+
}
1413+
13941414
} else if (A->getOption().matches(options::OPT_isystem_after)) {
13951415
// Handling of paths which must come late. These entries are handled by
13961416
// the toolchain itself after the resource dir is inserted in the right
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// This test checks that a PCH(Precompiled Header) file is
2+
// included while performing host compilation in -fsycl mode.
3+
4+
// RUN: touch %t.h
5+
6+
// PCH file.
7+
// RUN: touch %t.h.gch
8+
9+
// Linux
10+
// -fsycl and -include
11+
// RUN: %clang -fsycl -include %t.h -### %s 2>&1 \
12+
// RUN: | FileCheck -check-prefixes=CHECK-SYCL-HOST,CHECK-SYCL-DEVICE %s
13+
// CHECK-SYCL-DEVICE: -fsycl-is-device
14+
// CHECK-SYCL-DEVICE-NOT: -include-pch
15+
// CHECK-SYCL-HOST: -fsycl-is-host
16+
// CHECK-SYCL-HOST-SAME: -include-pch
17+
18+
19+
// -fsycl and -include-pch
20+
// RUN: %clang -fsycl -include-pch %t.h.gch -### %s 2>&1 \
21+
// RUN: | FileCheck -check-prefixes=CHECK-PCH-HOST,CHECK-PCH-DEVICE %s
22+
// CHECK-PCH-DEVICE: -fsycl-is-device
23+
// CHECK-PCH-DEVICE-NOT: -include-pch
24+
// CHECK-PCH-HOST: -fsycl-is-host
25+
// CHECK-PCH-HOST-SAME: -include-pch
26+
27+
// Windows
28+
// RUN: %clang_cl -fsycl --target=x86_64-unknown-linux-gnu /Yupchfile.h /FIpchfile.h -### %s 2>&1 \
29+
// RUN: | FileCheck -check-prefixes=CHECK-YU-HOST,CHECK-YU-DEVICE %s
30+
// CHECK-YU-DEVICE: -fsycl-is-device
31+
// CHECK-YU-DEVICE-NOT: -include-pch
32+
// CHECK-YU-HOST: -fsycl-is-host
33+
// CHECK-YU-HOST-SAME: -include-pch

0 commit comments

Comments
 (0)