Skip to content

[SYCL][Driver] Improve -save-temps for SYCL mode #4931

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

Merged
merged 7 commits into from
Dec 1, 2021
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
30 changes: 26 additions & 4 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5571,6 +5571,17 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
// When compiling for -fsycl, generate the integration header files and the
// Unique ID that will be used during the compilation.
if (Args.hasFlag(options::OPT_fsycl, options::OPT_fno_sycl, false)) {
const bool IsSaveTemps = isSaveTempsEnabled();
SmallString<128> OutFileDir;
if (IsSaveTemps) {
if (SaveTemps == SaveTempsObj) {
auto *OptO = C.getArgs().getLastArg(options::OPT_o);
OutFileDir = (OptO ? OptO->getValues()[0] : "");
llvm::sys::path::remove_filename(OutFileDir);
if (!OutFileDir.empty())
OutFileDir.append(llvm::sys::path::get_separator());
}
}
for (auto &I : Inputs) {
std::string SrcFileName(I.second->getAsString(Args));
if (I.first == types::TY_PP_C || I.first == types::TY_PP_CXX ||
Expand All @@ -5582,12 +5593,23 @@ void Driver::BuildActions(Compilation &C, DerivedArgList &Args,
}
if (!types::isSrcFile(I.first))
continue;
std::string TmpFileNameHeader = C.getDriver().GetTemporaryPath(
llvm::sys::path::stem(SrcFileName).str() + "-header", "h");

std::string TmpFileNameHeader;
std::string TmpFileNameFooter;
auto StemmedSrcFileName = llvm::sys::path::stem(SrcFileName).str();
if (IsSaveTemps) {
TmpFileNameHeader.append(C.getDriver().GetUniquePath(
OutFileDir.c_str() + StemmedSrcFileName + "-header", "h"));
TmpFileNameFooter.append(C.getDriver().GetUniquePath(
OutFileDir.c_str() + StemmedSrcFileName + "-footer", "h"));
} else {
TmpFileNameHeader.assign(C.getDriver().GetTemporaryPath(
StemmedSrcFileName + "-header", "h"));
TmpFileNameFooter =
C.getDriver().GetTemporaryPath(StemmedSrcFileName + "-footer", "h");
}
StringRef TmpFileHeader =
C.addTempFile(C.getArgs().MakeArgString(TmpFileNameHeader));
std::string TmpFileNameFooter = C.getDriver().GetTemporaryPath(
llvm::sys::path::stem(SrcFileName).str() + "-footer", "h");
StringRef TmpFileFooter =
C.addTempFile(C.getArgs().MakeArgString(TmpFileNameFooter));
// Use of -fsycl-footer-path puts the integration footer into that
Expand Down
19 changes: 19 additions & 0 deletions clang/lib/Driver/ToolChains/SYCL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,25 @@ void SYCL::constructLLVMForeachCommand(Compilation &C, const JobAction &JA,
if (!ParallelJobs.empty())
ForeachArgs.push_back(C.getArgs().MakeArgString("--jobs=" + ParallelJobs));

if (C.getDriver().isSaveTempsEnabled()) {
SmallString<128> OutputDirName;
if (C.getDriver().isSaveTempsObj()) {
OutputDirName =
T->getToolChain().GetFilePath(OutputFileName.c_str()).c_str();
llvm::sys::path::remove_filename(OutputDirName);
}
// Use the current dir if the `GetFilePath` returned en empty string, which
// is the case when the `OutputFileName` does not contain any directory
// information, or if in CWD mode. This is necessary for `llvm-foreach`, as
// it would disregard the parameter without it. Otherwise append separator.
if (OutputDirName.empty())
llvm::sys::path::native(OutputDirName = "./");
else
OutputDirName.append(llvm::sys::path::get_separator());
ForeachArgs.push_back(
C.getArgs().MakeArgString("--out-dir=" + OutputDirName));
}

ForeachArgs.push_back(C.getArgs().MakeArgString("--"));
ForeachArgs.push_back(
C.getArgs().MakeArgString(InputCommand->getExecutable()));
Expand Down
9 changes: 9 additions & 0 deletions clang/test/Driver/sycl-offload.c
Original file line number Diff line number Diff line change
Expand Up @@ -1143,3 +1143,12 @@
// RUN: | FileCheck -check-prefixes=CHK-FSYNTAX-ONLY %s
// CHK-FSYNTAX-ONLY-NOT: "-emit-llvm-bc"
// CHK-FSYNTAX-ONLY: "-fsyntax-only"

/// ###########################################################################
/// Verify that -save-temps puts header/footer in a correct place
// 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
// 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"

/// Verify that -save-temps=obj respects the -o dir
// 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
// 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