Skip to content

Commit f7f4699

Browse files
author
Artem Gindinson
authored
[SYCL][Driver] Fix -save-temps for SYCL compilations (#1114)
The following issues have been blocking the correct functioning of -save-temps in the presence of -fsycl: 1. LLVM BC -> host assembly transformation naturally crashes upon encountering a SYCL device triple. Without -save-temps, this was hidden due to compile/backend/assemble actions collapsing, however the need to save all intermediate results uncovers the issue. Solution: remove backend/assemble actions from the SYCL device flow altogether, as these do not carry practical value anyway. 2. The integration header name being emptied by -save-temps enabling. This is due to a bug in the llvm::sys::fs::real_path() function - the Unix implementation doesn't guarantee a valid path for non-existent files. Solution: work around the issue by placing the Windows-specific caller code under an OS preprocessor check. Signed-off-by: Artem Gindinson <[email protected]>
1 parent c81c1c5 commit f7f4699

File tree

7 files changed

+194
-256
lines changed

7 files changed

+194
-256
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,14 +3270,20 @@ class OffloadingActionBuilder final {
32703270
for (Action *&A : SYCLDeviceActions) {
32713271
DeviceCompilerInput =
32723272
C.MakeAction<CompileJobAction>(A, types::TY_SYCL_Header);
3273+
A = C.MakeAction<CompileJobAction>(A, types::TY_LLVM_BC);
32733274
}
32743275
DA.add(*DeviceCompilerInput, *ToolChains.front(), /*BoundArch=*/nullptr,
32753276
Action::OFK_SYCL);
32763277
// Clear the input file, it is already a dependence to a host
32773278
// action.
32783279
DeviceCompilerInput = nullptr;
3280+
return ABRT_Success;
32793281
}
32803282

3283+
// Backend/Assemble actions are obsolete for the SYCL device side
3284+
if (CurPhase == phases::Backend || CurPhase == phases::Assemble)
3285+
return ABRT_Inactive;
3286+
32813287
// The host only depends on device action in the linking phase, when all
32823288
// the device images have to be embedded in the host image.
32833289
if (CurPhase == phases::Link) {

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5978,16 +5978,29 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
59785978
// Inputs[1]. Include the header with -include
59795979
if (!IsSYCLOffloadDevice && SYCLDeviceInput) {
59805980
SmallString<128> RealPath;
5981+
#if defined(_WIN32)
59815982
// Fixup the header path name in case there are discrepancies in the
59825983
// string used for the temporary directory environment variable and
59835984
// actual path expectations.
5985+
//
5986+
// While generating the driver commands, we're most often working
5987+
// with non-existing files. The Unix implementation of LLVM's real_path
5988+
// returns an empty path for a non-existing file if it's expected to be
5989+
// placed in the current directory. This becomes a problem when we're
5990+
// saving intermediate compilation results via -save-temps.
5991+
// Since the header file path fix-up is Windows-specific, the real_path
5992+
// call is not necessary for a Unix-based OS (case-sensitive filesystem).
59845993
llvm::sys::fs::real_path(SYCLDeviceInput->getFilename(), RealPath);
5994+
#else // _WIN32
5995+
RealPath.assign(StringRef(SYCLDeviceInput->getFilename()));
5996+
#endif // _WIN32
5997+
const char *IntHeaderPath = Args.MakeArgString(RealPath);
59855998
CmdArgs.push_back("-include");
5986-
CmdArgs.push_back(Args.MakeArgString(RealPath));
5999+
CmdArgs.push_back(IntHeaderPath);
59876000
// When creating dependency information, filter out the generated
59886001
// header file.
59896002
CmdArgs.push_back("-dependency-filter");
5990-
CmdArgs.push_back(Args.MakeArgString(RealPath));
6003+
CmdArgs.push_back(IntHeaderPath);
59916004
// Let the FE know we are doing a SYCL offload compilation, but we are
59926005
// doing the host pass.
59936006
CmdArgs.push_back("-fsycl-is-host");

clang/test/Driver/sycl-offload-intelfpga.cpp

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,12 @@
137137
// CHK-FPGA-LINK-SRC: 8: assembler, {7}, object, (host-sycl)
138138
// CHK-FPGA-LINK-SRC: 9: linker, {8}, archive, (host-sycl)
139139
// CHK-FPGA-LINK-SRC: 10: compiler, {3}, ir, (device-sycl)
140-
// CHK-FPGA-LINK-SRC: 11: backend, {10}, assembler, (device-sycl)
141-
// CHK-FPGA-LINK-SRC: 12: assembler, {11}, object, (device-sycl)
142-
// CHK-FPGA-LINK-SRC: 13: linker, {12}, ir, (device-sycl)
143-
// CHK-FPGA-LINK-SRC: 14: llvm-spirv, {13}, spirv, (device-sycl)
144-
// CHK-FPGA-LINK-SRC: 15: backend-compiler, {14}, fpga_aocr, (device-sycl)
145-
// CHK-FPGA-LINK-SRC: 16: clang-offload-wrapper, {15}, object, (device-sycl)
146-
// CHK-FPGA-LINK-SRC-DEFAULT: 17: offload, "host-sycl (x86_64-unknown-linux-gnu)" {9}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {16}, archive
147-
// CHK-FPGA-LINK-SRC-CL: 17: offload, "host-sycl (x86_64-pc-windows-msvc)" {9}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice-coff)" {16}, archive
140+
// CHK-FPGA-LINK-SRC: 11: linker, {10}, ir, (device-sycl)
141+
// CHK-FPGA-LINK-SRC: 12: llvm-spirv, {11}, spirv, (device-sycl)
142+
// CHK-FPGA-LINK-SRC: 13: backend-compiler, {12}, fpga_aocr, (device-sycl)
143+
// CHK-FPGA-LINK-SRC: 14: clang-offload-wrapper, {13}, object, (device-sycl)
144+
// CHK-FPGA-LINK-SRC-DEFAULT: 15: offload, "host-sycl (x86_64-unknown-linux-gnu)" {9}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {14}, archive
145+
// CHK-FPGA-LINK-SRC-CL: 15: offload, "host-sycl (x86_64-pc-windows-msvc)" {9}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice-coff)" {14}, archive
148146

149147
/// -fintelfpga with -reuse-exe=
150148
// RUN: touch %t.cpp
@@ -197,17 +195,15 @@
197195
// CHK-FPGA-AOCO-PHASES: 9: assembler, {8}, object, (host-sycl)
198196
// CHK-FPGA-AOCO-PHASES: 10: linker, {0, 9}, image, (host-sycl)
199197
// CHK-FPGA-AOCO-PHASES: 11: compiler, {4}, ir, (device-sycl)
200-
// CHK-FPGA-AOCO-PHASES: 12: backend, {11}, assembler, (device-sycl)
201-
// CHK-FPGA-AOCO-PHASES: 13: assembler, {12}, object, (device-sycl)
202-
// CHK-FPGA-AOCO-PHASES: 14: input, "[[INPUTA]]", archive
203-
// CHK-FPGA-AOCO-PHASES-LIN: 15: clang-offload-unbundler, {14}, object
204-
// CHK-FPGA-AOCO-PHASES-WIN: 15: clang-offload-unbundler, {14}, archive
205-
// CHK-FPGA-AOCO-PHASES: 16: linker, {13, 15}, ir, (device-sycl)
206-
// CHK-FPGA-AOCO-PHASES: 17: llvm-spirv, {16}, spirv, (device-sycl)
207-
// CHK-FPGA-AOCO-PHASES: 18: backend-compiler, {17}, fpga_aocx, (device-sycl)
208-
// CHK-FPGA-AOCO-PHASES: 19: clang-offload-wrapper, {18}, object, (device-sycl)
209-
// CHK-FPGA-AOCO-PHASES-LIN: 20: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {19}, image
210-
// CHK-FPGA-AOCO-PHASES-WIN: 20: offload, "host-sycl (x86_64-pc-windows-msvc)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice-coff)" {19}, image
198+
// CHK-FPGA-AOCO-PHASES: 12: input, "[[INPUTA]]", archive
199+
// CHK-FPGA-AOCO-PHASES-LIN: 13: clang-offload-unbundler, {12}, object
200+
// CHK-FPGA-AOCO-PHASES-WIN: 13: clang-offload-unbundler, {12}, archive
201+
// CHK-FPGA-AOCO-PHASES: 14: linker, {11, 13}, ir, (device-sycl)
202+
// CHK-FPGA-AOCO-PHASES: 15: llvm-spirv, {14}, spirv, (device-sycl)
203+
// CHK-FPGA-AOCO-PHASES: 16: backend-compiler, {15}, fpga_aocx, (device-sycl)
204+
// CHK-FPGA-AOCO-PHASES: 17: clang-offload-wrapper, {16}, object, (device-sycl)
205+
// CHK-FPGA-AOCO-PHASES-LIN: 18: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice)" {17}, image
206+
// CHK-FPGA-AOCO-PHASES-WIN: 18: offload, "host-sycl (x86_64-pc-windows-msvc)" {10}, "device-sycl (spir64_fpga-unknown-unknown-sycldevice-coff)" {17}, image
211207

212208
/// aoco test, checking tools
213209
// RUN: %clangxx -target x86_64-unknown-linux-gnu -fsycl -fintelfpga -foffload-static-lib=%t_aoco.a -### %s 2>&1 \

clang/test/Driver/sycl-offload-static-lib.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,12 @@
4747
// FOFFLOAD_STATIC_LIB_SRC: 9: assembler, {8}, object, (host-sycl)
4848
// FOFFLOAD_STATIC_LIB_SRC: 10: linker, {0, 9}, image, (host-sycl)
4949
// FOFFLOAD_STATIC_LIB_SRC: 11: compiler, {4}, ir, (device-sycl)
50-
// FOFFLOAD_STATIC_LIB_SRC: 12: backend, {11}, assembler, (device-sycl)
51-
// FOFFLOAD_STATIC_LIB_SRC: 13: assembler, {12}, object, (device-sycl)
52-
// FOFFLOAD_STATIC_LIB_SRC: 14: input, "[[INPUTA]]", archive
53-
// FOFFLOAD_STATIC_LIB_SRC: 15: clang-offload-unbundler, {14}, object
54-
// FOFFLOAD_STATIC_LIB_SRC: 16: linker, {13, 15}, ir, (device-sycl)
55-
// FOFFLOAD_STATIC_LIB_SRC: 17: llvm-spirv, {16}, spirv, (device-sycl)
56-
// FOFFLOAD_STATIC_LIB_SRC: 18: clang-offload-wrapper, {17}, object, (device-sycl)
57-
// FOFFLOAD_STATIC_LIB_SRC: 19: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64-unknown-unknown-sycldevice)" {18}, image
50+
// FOFFLOAD_STATIC_LIB_SRC: 12: input, "[[INPUTA]]", archive
51+
// FOFFLOAD_STATIC_LIB_SRC: 13: clang-offload-unbundler, {12}, object
52+
// FOFFLOAD_STATIC_LIB_SRC: 14: linker, {11, 13}, ir, (device-sycl)
53+
// FOFFLOAD_STATIC_LIB_SRC: 15: llvm-spirv, {14}, spirv, (device-sycl)
54+
// FOFFLOAD_STATIC_LIB_SRC: 16: clang-offload-wrapper, {15}, object, (device-sycl)
55+
// FOFFLOAD_STATIC_LIB_SRC: 17: offload, "host-sycl (x86_64-unknown-linux-gnu)" {10}, "device-sycl (spir64-unknown-unknown-sycldevice)" {16}, image
5856

5957
/// ###########################################################################
6058

clang/test/Driver/sycl-offload-win.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,12 @@
7676
// FOFFLOAD_STATIC_LIB_SRC: 9: assembler, {8}, object, (host-sycl)
7777
// FOFFLOAD_STATIC_LIB_SRC: 10: linker, {0, 9}, image, (host-sycl)
7878
// FOFFLOAD_STATIC_LIB_SRC: 11: compiler, {4}, ir, (device-sycl)
79-
// FOFFLOAD_STATIC_LIB_SRC: 12: backend, {11}, assembler, (device-sycl)
80-
// FOFFLOAD_STATIC_LIB_SRC: 13: assembler, {12}, object, (device-sycl)
81-
// FOFFLOAD_STATIC_LIB_SRC: 14: input, "[[INPUTLIB]]", archive
82-
// FOFFLOAD_STATIC_LIB_SRC: 15: clang-offload-unbundler, {14}, archive
83-
// FOFFLOAD_STATIC_LIB_SRC: 16: linker, {13, 15}, ir, (device-sycl)
84-
// FOFFLOAD_STATIC_LIB_SRC: 17: llvm-spirv, {16}, spirv, (device-sycl)
85-
// FOFFLOAD_STATIC_LIB_SRC: 18: clang-offload-wrapper, {17}, object, (device-sycl)
86-
// FOFFLOAD_STATIC_LIB_SRC: 19: offload, "host-sycl (x86_64-pc-windows-msvc)" {10}, "device-sycl (spir64-unknown-unknown-sycldevice{{(-coff)?}})" {18}, image
79+
// FOFFLOAD_STATIC_LIB_SRC: 12: input, "[[INPUTLIB]]", archive
80+
// FOFFLOAD_STATIC_LIB_SRC: 13: clang-offload-unbundler, {12}, archive
81+
// FOFFLOAD_STATIC_LIB_SRC: 14: linker, {11, 13}, ir, (device-sycl)
82+
// FOFFLOAD_STATIC_LIB_SRC: 15: llvm-spirv, {14}, spirv, (device-sycl)
83+
// FOFFLOAD_STATIC_LIB_SRC: 16: clang-offload-wrapper, {15}, object, (device-sycl)
84+
// FOFFLOAD_STATIC_LIB_SRC: 17: offload, "host-sycl (x86_64-pc-windows-msvc)" {10}, "device-sycl (spir64-unknown-unknown-sycldevice{{(-coff)?}})" {16}, image
8785

8886
/// ###########################################################################
8987

0 commit comments

Comments
 (0)