Skip to content

Commit 96012a4

Browse files
authored
[SYCL][Driver] Improve -save-temps for SYCL mode (#4931)
Make sure that `header` and `footer` files as well as the output of `llvm-foreach` (such as `asm` and `fatbin` for CUDA) stay in the output directory. As discussed here: #4752
1 parent a49e506 commit 96012a4

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

clang/lib/Driver/Driver.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5571,6 +5571,17 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
55715571
// When compiling for -fsycl, generate the integration header files and the
55725572
// Unique ID that will be used during the compilation.
55735573
if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
5574+
const bool IsSaveTemps = isSaveTempsEnabled();
5575+
SmallString<128> OutFileDir;
5576+
if (IsSaveTemps) {
5577+
if (SaveTemps == SaveTempsObj) {
5578+
auto *OptO = C.getArgs().getLastArg(options::OPT_o);
5579+
OutFileDir = (OptO ? OptO->getValues()[0] : "");
5580+
llvm::sys::path::remove_filename(OutFileDir);
5581+
if (!OutFileDir.empty())
5582+
OutFileDir.append(llvm::sys::path::get_separator());
5583+
}
5584+
}
55745585
for (auto &I : Inputs) {
55755586
std::string SrcFileName(I.second->getAsString(Args));
55765587
if (I.first == types::TY_PP_C || I.first == types::TY_PP_CXX ||
@@ -5582,12 +5593,23 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
55825593
}
55835594
if (!types::isSrcFile(I.first))
55845595
continue;
5585-
std::string TmpFileNameHeader = C.getDriver().GetTemporaryPath(
5586-
llvm::sys::path::stem(SrcFileName).str() + "-header", "h");
5596+
5597+
std::string TmpFileNameHeader;
5598+
std::string TmpFileNameFooter;
5599+
auto StemmedSrcFileName = llvm::sys::path::stem(SrcFileName).str();
5600+
if (IsSaveTemps) {
5601+
TmpFileNameHeader.append(C.getDriver().GetUniquePath(
5602+
OutFileDir.c_str() + StemmedSrcFileName + "-header", "h"));
5603+
TmpFileNameFooter.append(C.getDriver().GetUniquePath(
5604+
OutFileDir.c_str() + StemmedSrcFileName + "-footer", "h"));
5605+
} else {
5606+
TmpFileNameHeader.assign(C.getDriver().GetTemporaryPath(
5607+
StemmedSrcFileName + "-header", "h"));
5608+
TmpFileNameFooter =
5609+
C.getDriver().GetTemporaryPath(StemmedSrcFileName + "-footer", "h");
5610+
}
55875611
StringRef TmpFileHeader =
55885612
C.addTempFile(C.getArgs().MakeArgString(TmpFileNameHeader));
5589-
std::string TmpFileNameFooter = C.getDriver().GetTemporaryPath(
5590-
llvm::sys::path::stem(SrcFileName).str() + "-footer", "h");
55915613
StringRef TmpFileFooter =
55925614
C.addTempFile(C.getArgs().MakeArgString(TmpFileNameFooter));
55935615
// Use of -fsycl-footer-path puts the integration footer into that

clang/lib/Driver/ToolChains/SYCL.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,25 @@ void SYCL::constructLLVMForeachCommand(Compilation &C, const JobAction &JA,
123123
if (!ParallelJobs.empty())
124124
ForeachArgs.push_back(C.getArgs().MakeArgString("--jobs=" + ParallelJobs));
125125

126+
if (C.getDriver().isSaveTempsEnabled()) {
127+
SmallString<128> OutputDirName;
128+
if (C.getDriver().isSaveTempsObj()) {
129+
OutputDirName =
130+
T->getToolChain().GetFilePath(OutputFileName.c_str()).c_str();
131+
llvm::sys::path::remove_filename(OutputDirName);
132+
}
133+
// Use the current dir if the `GetFilePath` returned en empty string, which
134+
// is the case when the `OutputFileName` does not contain any directory
135+
// information, or if in CWD mode. This is necessary for `llvm-foreach`, as
136+
// it would disregard the parameter without it. Otherwise append separator.
137+
if (OutputDirName.empty())
138+
llvm::sys::path::native(OutputDirName = "./");
139+
else
140+
OutputDirName.append(llvm::sys::path::get_separator());
141+
ForeachArgs.push_back(
142+
C.getArgs().MakeArgString("--out-dir=" + OutputDirName));
143+
}
144+
126145
ForeachArgs.push_back(C.getArgs().MakeArgString("--"));
127146
ForeachArgs.push_back(
128147
C.getArgs().MakeArgString(InputCommand->getExecutable()));

clang/test/Driver/sycl-offload.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,3 +1143,12 @@
11431143
// RUN: | FileCheck -check-prefixes=CHK-FSYNTAX-ONLY %s
11441144
// CHK-FSYNTAX-ONLY-NOT: "-emit-llvm-bc"
11451145
// CHK-FSYNTAX-ONLY: "-fsyntax-only"
1146+
1147+
/// ###########################################################################
1148+
/// Verify that -save-temps puts header/footer in a correct place
1149+
// RUN: %clang -fsycl -fno-sycl-device-lib=all -fsycl-targets=spir64-unknown-unknown -target x86_64-unknown-linux-gnu -save-temps %s -### 2>&1 | FileCheck %s -check-prefixes=CHECK-SAVE-TEMPS-DIR
1150+
// CHECK-SAVE-TEMPS-DIR: clang{{.*}} "-fsycl-int-header=sycl-offload-header-{{[a-z0-9]*}}.h"{{.*}}"-fsycl-int-footer=sycl-offload-footer-{{[a-z0-9]*}}.h"
1151+
1152+
/// Verify that -save-temps=obj respects the -o dir
1153+
// RUN: %clang -fsycl -fno-sycl-device-lib=all -fsycl-targets=spir64-unknown-unknown -target x86_64-unknown-linux-gnu -save-temps=obj -o %S %s -### 2>&1 | FileCheck %s -check-prefixes=CHECK-SAVE-TEMPS-OBJ-DIR
1154+
// CHECK-SAVE-TEMPS-OBJ-DIR: clang{{.*}}-fsycl-int-header={{.*[/\\]+clang[/\\]+test[/\\]+sycl-offload-header-[a-z0-9]*}}.h{{.*}}-fsycl-int-footer={{.*[/\\]+clang[/\\]+test[/\\]+sycl-offload-footer-[a-z0-9]*}}.h

0 commit comments

Comments
 (0)