From e6ada38bbbbebea8d5b0988a526370219ad3ee42 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Thu, 22 Jul 2021 12:19:18 -0700 Subject: [PATCH 1/6] [Driver][SYCL] Enable way to emit int-footer source to a specific dir During an -fsycl enabled build, we use an integration footer during the host portion of the compilation. This requires to create a temporary file that is based on the original source file and contains the generated footer. Some build systems recognize this additional file that is being generated and is not part of any expected generated dependency. Introduce an option that allows the user to specify the location in which this new source file is created so the build system can properly track and recognize its creation. The new option is -fsycl-footer-path= --- clang/include/clang/Driver/Options.td | 3 +++ clang/lib/Driver/Driver.cpp | 12 ++++++++++++ clang/test/Driver/sycl-int-footer.cpp | 6 ++++++ 3 files changed, 21 insertions(+) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 05d6203453c20..4d34ce387f1cf 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2635,6 +2635,9 @@ def fsycl_host_compiler_options_EQ : Joined<["-"], "fsycl-host-compiler-options= def fno_sycl_use_footer : Flag<["-"], "fno-sycl-use-footer">, Flags<[CoreOption]>, HelpText<"Disable usage of the integration footer during SYCL enabled " "compilations.">; +def fsycl_footer_path_EQ : Joined<["-"], "fsycl-footer-path=">, + Flags<[CoreOption]>, HelpText<"Specify the location of the temporary " + "integration footer source file.">; def fsyntax_only : Flag<["-"], "fsyntax-only">, Flags<[NoXarchOption,CoreOption,CC1Option,FC1Option]>, Group; def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index fb2ec73acaf4a..4f8fad9cb29fb 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6753,6 +6753,18 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, &JA); } + // Redirect output for the generated source + integration footer. + if (isa(JA) && !isSaveTempsEnabled()) { + if (Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_footer_path_EQ)) { + SmallString<128> OutName(A->getValue()); + StringRef BaseName = llvm::sys::path::filename(BaseInput); + std::string TmpName = GetTemporaryPath(llvm::sys::path::stem(BaseName), + types::getTypeTempSuffix(JA.getType())); + llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); + return C.addTempFile(C.getArgs().MakeArgString(OutName)); + } + } + // Default to writing to stdout? if (AtTopLevel && !CCGenDiagnostics && HasPreprocessOutput(JA)) { return "-"; diff --git a/clang/test/Driver/sycl-int-footer.cpp b/clang/test/Driver/sycl-int-footer.cpp index 3b03febcfb438..264260fd1f5f9 100644 --- a/clang/test/Driver/sycl-int-footer.cpp +++ b/clang/test/Driver/sycl-int-footer.cpp @@ -60,3 +60,9 @@ // COMMON-PHASES: [[#OFFLOAD+9]]: file-table-tform, {[[#OFFLOAD+6]], [[#OFFLOAD+8]]}, tempfiletable, (device-sycl) // COMMON-PHASES: [[#OFFLOAD+10]]: clang-offload-wrapper, {[[#OFFLOAD+9]]}, object, (device-sycl) // COMMON-PHASES: [[#OFFLOAD+11]]: offload, "host-sycl (x86_64-{{.*}})" {[[#OFFLOAD+4]]}, "device-sycl (spir64-unknown-unknown-sycldevice)" {[[#OFFLOAD+10]]}, image + +/// Test for -fsycl-footer-path= +// RUN: %clangxx -fsycl -fsycl-footer-path=/dummy/path %s -### 2>&1 \ +// RUN: | FileCheck -check-prefix FOOTER_PATH %s +// FOOTER_PATH: append-file{{.*}} "--output=/dummy/path/[[APPENDEDSRC:.+\.cpp]]" +// FOOTER_PATH: clang{{.*}} "-x" "c++" "/dummy/path/[[APPENDEDSRC]]" From 44219209f2f7aa8e560c5414405fde300f1a7d3a Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Thu, 22 Jul 2021 12:24:30 -0700 Subject: [PATCH 2/6] clang format --- clang/lib/Driver/Driver.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 4f8fad9cb29fb..504f5d715c5fe 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6758,8 +6758,9 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, if (Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_footer_path_EQ)) { SmallString<128> OutName(A->getValue()); StringRef BaseName = llvm::sys::path::filename(BaseInput); - std::string TmpName = GetTemporaryPath(llvm::sys::path::stem(BaseName), - types::getTypeTempSuffix(JA.getType())); + std::string TmpName = + GetTemporaryPath(llvm::sys::path::stem(BaseName), + types::getTypeTempSuffix(JA.getType())); llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); return C.addTempFile(C.getArgs().MakeArgString(OutName)); } From ec9a2f91b07052871163fc03b9733d19da754d2f Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Thu, 22 Jul 2021 16:00:57 -0700 Subject: [PATCH 3/6] [NFC] Update test for Windows --- clang/test/Driver/sycl-int-footer.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/test/Driver/sycl-int-footer.cpp b/clang/test/Driver/sycl-int-footer.cpp index 264260fd1f5f9..e1e9e62199ff7 100644 --- a/clang/test/Driver/sycl-int-footer.cpp +++ b/clang/test/Driver/sycl-int-footer.cpp @@ -62,7 +62,7 @@ // COMMON-PHASES: [[#OFFLOAD+11]]: offload, "host-sycl (x86_64-{{.*}})" {[[#OFFLOAD+4]]}, "device-sycl (spir64-unknown-unknown-sycldevice)" {[[#OFFLOAD+10]]}, image /// Test for -fsycl-footer-path= -// RUN: %clangxx -fsycl -fsycl-footer-path=/dummy/path %s -### 2>&1 \ +// RUN: %clangxx -fsycl -fsycl-footer-path=dummy_dir %s -### 2>&1 \ // RUN: | FileCheck -check-prefix FOOTER_PATH %s -// FOOTER_PATH: append-file{{.*}} "--output=/dummy/path/[[APPENDEDSRC:.+\.cpp]]" -// FOOTER_PATH: clang{{.*}} "-x" "c++" "/dummy/path/[[APPENDEDSRC]]" +// FOOTER_PATH: append-file{{.*}} "--output=dummy_dir{{(/|\\\\)}}[[APPENDEDSRC:.+\.cpp]]" +// FOOTER_PATH: clang{{.*}} "-x" "c++" "dummy_dir{{(/|\\\\)}}[[APPENDEDSRC]]" From 02d811d41aa6825c345c19b4b992514ae46e54ea Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Fri, 23 Jul 2021 09:42:09 -0700 Subject: [PATCH 4/6] Update help and honor output dir with -save-temps --- clang/include/clang/Driver/Options.td | 2 +- clang/lib/Driver/Driver.cpp | 24 +++++++++++++++++++----- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 4d34ce387f1cf..7fd2b4f539347 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2637,7 +2637,7 @@ def fno_sycl_use_footer : Flag<["-"], "fno-sycl-use-footer">, Flags<[CoreOption] "compilations.">; def fsycl_footer_path_EQ : Joined<["-"], "fsycl-footer-path=">, Flags<[CoreOption]>, HelpText<"Specify the location of the temporary " - "integration footer source file.">; + "source file with the included integration footer.">; def fsyntax_only : Flag<["-"], "fsyntax-only">, Flags<[NoXarchOption,CoreOption,CC1Option,FC1Option]>, Group; def ftabstop_EQ : Joined<["-"], "ftabstop=">, Group; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 504f5d715c5fe..75d220fd75756 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6754,14 +6754,28 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, } // Redirect output for the generated source + integration footer. - if (isa(JA) && !isSaveTempsEnabled()) { + if (isa(JA)) { if (Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_footer_path_EQ)) { SmallString<128> OutName(A->getValue()); StringRef BaseName = llvm::sys::path::filename(BaseInput); - std::string TmpName = - GetTemporaryPath(llvm::sys::path::stem(BaseName), - types::getTypeTempSuffix(JA.getType())); - llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); + std::string TmpName; + if (isSaveTempsEnabled()) { + // Retain the location specified by the user with -save-temps. + const char *Suffix = types::getTypeTempSuffix(JA.getType()); + std::string::size_type End = std::string::npos; + if (!types::appendSuffixForType(JA.getType())) + End = BaseName.rfind('.'); + SmallString<128> Suffixed(BaseName.substr(0, End)); + Suffixed += OffloadingPrefix; + Suffixed += '.'; + Suffixed += Suffix; + llvm::sys::path::append(OutName, Suffixed.c_str()); + } + else { + TmpName = GetTemporaryPath(llvm::sys::path::stem(BaseName), + types::getTypeTempSuffix(JA.getType())); + llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); + } return C.addTempFile(C.getArgs().MakeArgString(OutName)); } } From f733b3d1012599ce183ee27a2ffdf79ee72d099f Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Fri, 23 Jul 2021 09:59:01 -0700 Subject: [PATCH 5/6] clang format --- clang/lib/Driver/Driver.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 75d220fd75756..93257c5613437 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6770,8 +6770,7 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, Suffixed += '.'; Suffixed += Suffix; llvm::sys::path::append(OutName, Suffixed.c_str()); - } - else { + } else { TmpName = GetTemporaryPath(llvm::sys::path::stem(BaseName), types::getTypeTempSuffix(JA.getType())); llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); From d8d1fe5bf177ac46b9957aef1b73dc39d01997c9 Mon Sep 17 00:00:00 2001 From: Michael D Toguchi Date: Fri, 23 Jul 2021 10:20:42 -0700 Subject: [PATCH 6/6] Address review comment --- clang/lib/Driver/Driver.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp index 93257c5613437..64ad2b449032d 100644 --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -6758,7 +6758,6 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, if (Arg *A = C.getArgs().getLastArg(options::OPT_fsycl_footer_path_EQ)) { SmallString<128> OutName(A->getValue()); StringRef BaseName = llvm::sys::path::filename(BaseInput); - std::string TmpName; if (isSaveTempsEnabled()) { // Retain the location specified by the user with -save-temps. const char *Suffix = types::getTypeTempSuffix(JA.getType()); @@ -6771,8 +6770,9 @@ const char *Driver::GetNamedOutputPath(Compilation &C, const JobAction &JA, Suffixed += Suffix; llvm::sys::path::append(OutName, Suffixed.c_str()); } else { - TmpName = GetTemporaryPath(llvm::sys::path::stem(BaseName), - types::getTypeTempSuffix(JA.getType())); + std::string TmpName = + GetTemporaryPath(llvm::sys::path::stem(BaseName), + types::getTypeTempSuffix(JA.getType())); llvm::sys::path::append(OutName, llvm::sys::path::filename(TmpName)); } return C.addTempFile(C.getArgs().MakeArgString(OutName));