https://github.com/bd1976bris updated https://github.com/llvm/llvm-project/pull/147265
>From 737bb8c03620d668299a9b85d34eb659a30eebfb Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 5 Jun 2025 17:10:31 +0100 Subject: [PATCH 01/13] [DTLTO][Clang] Add support for Integrated Distributed ThinLTO This patch introduces support for Integrated Distributed ThinLTO (DTLTO) in Clang. DTLTO enables the distribution of ThinLTO backend compilations via external distribution systems, such as Incredibuild, during the traditional link step: https://llvm.org/docs/DTLTO.html. Testing: - `lit` test coverage has been added to Clang's Driver tests. - The DTLTO cross-project tests will use this Clang support. For the design discussion of the DTLTO feature, see: https://github.com/llvm/llvm-project/pull/126654 Note that I have removed the forwarding of -mllvm options to the backend compilations which was discussed in the design review from this patch. LTO configuration for DTLTO will be addressed in a follow-up patch. In the meantime -mllvm options can be forwarded manually if required. --- clang/docs/ThinLTO.rst | 32 ++++++++++++++++++++ clang/include/clang/Driver/Options.td | 14 ++++++++- clang/lib/Driver/ToolChains/Gnu.cpp | 15 ++++++++++ clang/test/Driver/DTLTO/dtlto.c | 43 +++++++++++++++++++++++++++ cross-project-tests/dtlto/ld-dtlto.c | 31 ++++++++----------- 5 files changed, 116 insertions(+), 19 deletions(-) create mode 100644 clang/test/Driver/DTLTO/dtlto.c diff --git a/clang/docs/ThinLTO.rst b/clang/docs/ThinLTO.rst index c042547678919..687795ac655a7 100644 --- a/clang/docs/ThinLTO.rst +++ b/clang/docs/ThinLTO.rst @@ -240,6 +240,38 @@ The ``BOOTSTRAP_LLVM_ENABLE_LTO=Thin`` will enable ThinLTO for stage 2 and stage 3 in case the compiler used for stage 1 does not support the ThinLTO option. +Integrated Distributed ThinLTO (DTLTO) +-------------------------------------- + +Integrated Distributed ThinLTO (DTLTO) enables the distribution of backend +ThinLTO compilations via external distribution systems, such as Incredibuild, +during the traditional link step. + +The implementation is documented here: https://llvm.org/docs/DTLTO.html. + +DTLTO requires the LLD linker (``-fuse-ld=lld``). + +``-fthinlto-distributor=<path>`` + - Specifies the ``<path>`` to the distributor process executable for DTLTO. + - If specified, ThinLTO backend compilations will be distributed by LLD. + +``-Xthinlto-distributor=<arg>`` + - Passes ``<arg>`` to the distributor process (see ``-fthinlto-distributor=``). + - Can be specified multiple times to pass multiple options. + - Multiple options can also be specified by separating them with commas. + +Examples: + - ``clang -flto=thin -fthinlto-distributor=incredibuild.exe -Xthinlto-distributor=--verbose,--j10 -fuse-ld=lld`` + - ``clang -flto=thin -fthinlto-distributor=$(which python) -Xthinlto-distributor=incredibuild.py -fuse-ld=lld`` + +If ``-fthinlto-distributor=`` is specified, Clang supplies the path to a +compiler to be executed remotely to perform the ThinLTO backend +compilations. Currently, this is Clang itself. + +Note that currently, DTLTO is only supported in some LLD flavors. Support will +be added to other LLD flavours in the future. +See `DTLTO <https://lld.llvm.org/dtlto.html>`_ for more information. + More Information ================ diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td index 0c8a219b19bf4..9c6f77af97be0 100644 --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -990,6 +990,13 @@ def Xlinker : Separate<["-"], "Xlinker">, Flags<[LinkerInput, RenderAsInput]>, Visibility<[ClangOption, CLOption, FlangOption]>, HelpText<"Pass <arg> to the linker">, MetaVarName<"<arg>">, Group<Link_Group>; +def Xthinlto_distributor_EQ : CommaJoined<["-"], "Xthinlto-distributor=">, + Flags<[LinkOption]>, + Visibility<[ClangOption, CLOption]>, + HelpText<"Pass <arg> to the ThinLTO distributor process. Can be specified " + "multiple times or with comma-separated values.">, + MetaVarName<"<arg>">, + Group<Link_Group>; def Xoffload_linker : JoinedAndSeparate<["-"], "Xoffload-linker">, Visibility<[ClangOption, FlangOption]>, HelpText<"Pass <arg> to the offload linkers or the ones identified by -<triple>">, @@ -4233,7 +4240,12 @@ def ffinite_loops: Flag<["-"], "ffinite-loops">, Group<f_Group>, def fno_finite_loops: Flag<["-"], "fno-finite-loops">, Group<f_Group>, HelpText<"Do not assume that any loop is finite.">, Visibility<[ClangOption, CC1Option]>; - +def fthinlto_distributor_EQ : Joined<["-"], "fthinlto-distributor=">, + Group<f_Group>, + HelpText<"Path to the ThinLTO distributor process. If specified, " + "ThinLTO backend compilations will be distributed by LLD">, + MetaVarName<"<path>">, + Visibility<[ClangOption, CLOption]>; def ftrigraphs : Flag<["-"], "ftrigraphs">, Group<f_Group>, HelpText<"Process trigraph sequences">, Visibility<[ClangOption, CC1Option]>; def fno_trigraphs : Flag<["-"], "fno-trigraphs">, Group<f_Group>, diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index f5e2655857432..3e9ca8f79d160 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -455,6 +455,21 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs, D.getLTOMode() == LTOK_Thin); + // Forward the DTLTO options to the linker. We add these unconditionally, + // rather than in addLTOOptions() as it is the linker that decides whether to + // do LTO or not dependent upon whether there are any bitcode input files in + // the link. + if (Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) { + CmdArgs.push_back( + Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue()))); + CmdArgs.push_back( + Args.MakeArgString("--thinlto-remote-compiler=" + + Twine(ToolChain.getDriver().getClangProgramPath()))); + + for (auto A : Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ)) + CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A)); + } + if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) CmdArgs.push_back("--no-demangle"); diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c new file mode 100644 index 0000000000000..a23a10fdfb055 --- /dev/null +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -0,0 +1,43 @@ +// REQUIRES: lld + +/// Check DTLTO options are forwarded to the linker. + +// RUN: echo "--target=x86_64-linux-gnu \ +// RUN: -Xthinlto-distributor=distarg1 \ +// RUN: -Xthinlto-distributor=distarg2,distarg3 \ +// RUN: -fuse-ld=lld" > %t.rsp + +/// Check that options are forwarded as expected with --thinlto-distributor=. +// RUN: %clang -### @%t.rsp -fthinlto-distributor=dist.exe %s 2>&1 | \ +// RUN: FileCheck %s --implicit-check-not=warning + +// CHECK: ld.lld +// CHECK-SAME: "--thinlto-distributor=dist.exe" +// CHECK-SAME: "--thinlto-remote-compiler={{.*}}clang +// CHECK-SAME: "--thinlto-distributor-arg=distarg1" +// CHECK-SAME: "--thinlto-distributor-arg=distarg2" +// CHECK-SAME: "--thinlto-distributor-arg=distarg3" + + +/// Check that options are not added without --thinlto-distributor= and +/// that there is an unused option warning issued for -Xthinlto-distributor= +/// options. We specify -flto here as these options should be unaffected by it. +// RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ +// RUN: FileCheck %s --check-prefixes=NONE,NOMORE --implicit-check-not=warning + +// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=distarg1' +// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=distarg2,distarg3' +// NONE: ld.lld +// NOMORE-NOT: distributor +// NOMORE-NOT: remote-compiler + + +/// Check the expected arguments are forwarded by default with only +/// --thinlto-distributor=. +// RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=dist.exe \ +// RUN: -fuse-ld=lld -Werror -### %s 2>&1 | \ +// RUN: FileCheck %s --check-prefixes=DEFAULT,NOMORE + +// DEFAULT: ld.lld +// DEFAULT-SAME: "--thinlto-distributor=dist.exe" +// DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang diff --git a/cross-project-tests/dtlto/ld-dtlto.c b/cross-project-tests/dtlto/ld-dtlto.c index 3ee962346bd4a..7dffe2e015bcb 100644 --- a/cross-project-tests/dtlto/ld-dtlto.c +++ b/cross-project-tests/dtlto/ld-dtlto.c @@ -5,13 +5,11 @@ // RUN: rm -rf %t && mkdir %t && cd %t -// RUN: %clang --target=x86_64-linux-gnu -c -flto=thin %s -o dtlto.o - -// RUN: ld.lld dtlto.o \ -// RUN: --thinlto-distributor=%python \ -// RUN: --thinlto-distributor-arg=%llvm_src_root/utils/dtlto/local.py \ -// RUN: --thinlto-remote-compiler=%clang \ -// RUN: --thinlto-remote-compiler-arg=--save-temps +// RUN: %clang --target=x86_64-linux-gnu %s -flto=thin -fuse-ld=lld \ +// RUN: -fthinlto-distributor=%python \ +// RUN: -Xthinlto-distributor=%llvm_src_root/utils/dtlto/local.py \ +// RUN: -Wl,--thinlto-remote-compiler-arg=--save-temps \ +// RUN: -nostdlib -Werror /// Check that the required output files have been created. // RUN: ls | sort | FileCheck %s @@ -22,18 +20,15 @@ /// Linked ELF. // CHECK: {{^}}a.out{{$}} -/// Produced by the bitcode compilation. -// CHECK-NEXT: {{^}}dtlto.o{{$}} - /// --save-temps output for the backend compilation. -// CHECK-NEXT: {{^}}dtlto.s{{$}} -// CHECK-NEXT: {{^}}dtlto.s.0.preopt.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.1.promote.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.2.internalize.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.3.import.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.4.opt.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.5.precodegen.bc{{$}} -// CHECK-NEXT: {{^}}dtlto.s.resolution.txt{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP:[a-zA-Z0-9_]+]].s{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.0.preopt.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.1.promote.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.2.internalize.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.3.import.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.4.opt.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.5.precodegen.bc{{$}} +// CHECK-NEXT: {{^}}ld-dtlto-[[TMP]].s.resolution.txt{{$}} /// No files are expected after. // CHECK-NOT: {{.}} >From af6691025dad397f4e9d1d8af329f113d21c6bed Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 16:45:52 +0100 Subject: [PATCH 02/13] =?UTF-8?q?Avoid=20committing=20to=20any=20work=20on?= =?UTF-8?q?=20Sony=E2=80=99s=20behalf=20that=E2=80=99s=20beyond=20my=20rem?= =?UTF-8?q?it.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clang/docs/ThinLTO.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/docs/ThinLTO.rst b/clang/docs/ThinLTO.rst index 687795ac655a7..569405ff5b2b7 100644 --- a/clang/docs/ThinLTO.rst +++ b/clang/docs/ThinLTO.rst @@ -268,7 +268,7 @@ If ``-fthinlto-distributor=`` is specified, Clang supplies the path to a compiler to be executed remotely to perform the ThinLTO backend compilations. Currently, this is Clang itself. -Note that currently, DTLTO is only supported in some LLD flavors. Support will +Note that currently, DTLTO is only supported in some LLD flavors. Support can be added to other LLD flavours in the future. See `DTLTO <https://lld.llvm.org/dtlto.html>`_ for more information. >From fd51379778e4667a27c1ddfcc55ea42b1db12c54 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 16:52:05 +0100 Subject: [PATCH 03/13] shorten argument strings for readability --- clang/test/Driver/DTLTO/dtlto.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index a23a10fdfb055..b2047d017cff2 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -3,20 +3,20 @@ /// Check DTLTO options are forwarded to the linker. // RUN: echo "--target=x86_64-linux-gnu \ -// RUN: -Xthinlto-distributor=distarg1 \ -// RUN: -Xthinlto-distributor=distarg2,distarg3 \ +// RUN: -Xthinlto-distributor=a1 \ +// RUN: -Xthinlto-distributor=a2,a3 \ // RUN: -fuse-ld=lld" > %t.rsp /// Check that options are forwarded as expected with --thinlto-distributor=. -// RUN: %clang -### @%t.rsp -fthinlto-distributor=dist.exe %s 2>&1 | \ +// RUN: %clang -### @%t.rsp -fthinlto-distributor=d.exe %s 2>&1 | \ // RUN: FileCheck %s --implicit-check-not=warning // CHECK: ld.lld -// CHECK-SAME: "--thinlto-distributor=dist.exe" +// CHECK-SAME: "--thinlto-distributor=d.exe" // CHECK-SAME: "--thinlto-remote-compiler={{.*}}clang -// CHECK-SAME: "--thinlto-distributor-arg=distarg1" -// CHECK-SAME: "--thinlto-distributor-arg=distarg2" -// CHECK-SAME: "--thinlto-distributor-arg=distarg3" +// CHECK-SAME: "--thinlto-distributor-arg=a1" +// CHECK-SAME: "--thinlto-distributor-arg=a2" +// CHECK-SAME: "--thinlto-distributor-arg=a3" /// Check that options are not added without --thinlto-distributor= and @@ -25,8 +25,8 @@ // RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ // RUN: FileCheck %s --check-prefixes=NONE,NOMORE --implicit-check-not=warning -// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=distarg1' -// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=distarg2,distarg3' +// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=a1' +// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' // NONE: ld.lld // NOMORE-NOT: distributor // NOMORE-NOT: remote-compiler @@ -34,10 +34,10 @@ /// Check the expected arguments are forwarded by default with only /// --thinlto-distributor=. -// RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=dist.exe \ +// RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=d.exe \ // RUN: -fuse-ld=lld -Werror -### %s 2>&1 | \ // RUN: FileCheck %s --check-prefixes=DEFAULT,NOMORE // DEFAULT: ld.lld -// DEFAULT-SAME: "--thinlto-distributor=dist.exe" +// DEFAULT-SAME: "--thinlto-distributor=d.exe" // DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang >From f2c6b8d5fd11a241aaabcbd230b55b9252ab2102 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 16:54:32 +0100 Subject: [PATCH 04/13] Use meaningful prefixes for all check lines. --- clang/test/Driver/DTLTO/dtlto.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index b2047d017cff2..18c53e1ce760a 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -9,25 +9,25 @@ /// Check that options are forwarded as expected with --thinlto-distributor=. // RUN: %clang -### @%t.rsp -fthinlto-distributor=d.exe %s 2>&1 | \ -// RUN: FileCheck %s --implicit-check-not=warning +// RUN: FileCheck %s --implicit-check-not=warning --check-prefix=FORWARD -// CHECK: ld.lld -// CHECK-SAME: "--thinlto-distributor=d.exe" -// CHECK-SAME: "--thinlto-remote-compiler={{.*}}clang -// CHECK-SAME: "--thinlto-distributor-arg=a1" -// CHECK-SAME: "--thinlto-distributor-arg=a2" -// CHECK-SAME: "--thinlto-distributor-arg=a3" +// FORWARD: ld.lld +// FORWARD-SAME: "--thinlto-distributor=d.exe" +// FORWARD-SAME: "--thinlto-remote-compiler={{.*}}clang +// FORWARD-SAME: "--thinlto-distributor-arg=a1" +// FORWARD-SAME: "--thinlto-distributor-arg=a2" +// FORWARD-SAME: "--thinlto-distributor-arg=a3" /// Check that options are not added without --thinlto-distributor= and /// that there is an unused option warning issued for -Xthinlto-distributor= /// options. We specify -flto here as these options should be unaffected by it. // RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ -// RUN: FileCheck %s --check-prefixes=NONE,NOMORE --implicit-check-not=warning +// RUN: FileCheck %s --check-prefixes=NODIST,NOMORE --implicit-check-not=warning -// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=a1' -// NONE: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' -// NONE: ld.lld +// NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a1' +// NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' +// NODIST: ld.lld // NOMORE-NOT: distributor // NOMORE-NOT: remote-compiler >From cdce7ffdee915ac9b9723106f35e872df62b0aa7 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 16:57:42 +0100 Subject: [PATCH 05/13] NOMORE -> --implicit-check-not --- clang/test/Driver/DTLTO/dtlto.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 18c53e1ce760a..86bd540134f8d 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -9,7 +9,9 @@ /// Check that options are forwarded as expected with --thinlto-distributor=. // RUN: %clang -### @%t.rsp -fthinlto-distributor=d.exe %s 2>&1 | \ -// RUN: FileCheck %s --implicit-check-not=warning --check-prefix=FORWARD +// RUN: FileCheck %s --implicit-check-not=distributor \ +// RUN: --implicit-check-not=remote-compiler \ +// RUN: --implicit-check-not=warning: --check-prefix=FORWARD // FORWARD: ld.lld // FORWARD-SAME: "--thinlto-distributor=d.exe" @@ -23,20 +25,22 @@ /// that there is an unused option warning issued for -Xthinlto-distributor= /// options. We specify -flto here as these options should be unaffected by it. // RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ -// RUN: FileCheck %s --check-prefixes=NODIST,NOMORE --implicit-check-not=warning +// RUN: FileCheck %s --check-prefix=NODIST --implicit-check-not=distributor \ +// RUN: --implicit-check-not=remote-compiler \ +// RUN: --implicit-check-not=warning: // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a1' // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' // NODIST: ld.lld -// NOMORE-NOT: distributor -// NOMORE-NOT: remote-compiler /// Check the expected arguments are forwarded by default with only /// --thinlto-distributor=. // RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=d.exe \ // RUN: -fuse-ld=lld -Werror -### %s 2>&1 | \ -// RUN: FileCheck %s --check-prefixes=DEFAULT,NOMORE +// RUN: FileCheck %s --check-prefix=DEFAULT --implicit-check-not=distributor \ +// RUN: --implicit-check-not=remote-compiler \ +// RUN: --implicit-check-not=warning: // DEFAULT: ld.lld // DEFAULT-SAME: "--thinlto-distributor=d.exe" >From 75289e703e02bf9005736e095d26ba23a16c3189 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:00:00 +0100 Subject: [PATCH 06/13] Use shared response files for all FileCheck invocations. --- clang/test/Driver/DTLTO/dtlto.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 86bd540134f8d..e3ccd019135c9 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -2,6 +2,13 @@ /// Check DTLTO options are forwarded to the linker. +/// Create a response file for all FileCheck invocations to share. These implicit +/// checks ensure that all lines which mention DTLTO options are checked, +/// and that no unexpected warnings appear. +// RUN: echo " \"%s\" --implicit-check-not=distributor \ +// RUN: --implicit-check-not=remote-compiler \ +// RUN: --implicit-check-not=warning:" > %t_f.rsp + // RUN: echo "--target=x86_64-linux-gnu \ // RUN: -Xthinlto-distributor=a1 \ // RUN: -Xthinlto-distributor=a2,a3 \ @@ -9,9 +16,7 @@ /// Check that options are forwarded as expected with --thinlto-distributor=. // RUN: %clang -### @%t.rsp -fthinlto-distributor=d.exe %s 2>&1 | \ -// RUN: FileCheck %s --implicit-check-not=distributor \ -// RUN: --implicit-check-not=remote-compiler \ -// RUN: --implicit-check-not=warning: --check-prefix=FORWARD +// RUN: FileCheck @%t_f.rsp --check-prefix=FORWARD // FORWARD: ld.lld // FORWARD-SAME: "--thinlto-distributor=d.exe" @@ -25,9 +30,7 @@ /// that there is an unused option warning issued for -Xthinlto-distributor= /// options. We specify -flto here as these options should be unaffected by it. // RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ -// RUN: FileCheck %s --check-prefix=NODIST --implicit-check-not=distributor \ -// RUN: --implicit-check-not=remote-compiler \ -// RUN: --implicit-check-not=warning: +// RUN: FileCheck @%t_f.rsp --check-prefix=NODIST // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a1' // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' @@ -38,9 +41,7 @@ /// --thinlto-distributor=. // RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=d.exe \ // RUN: -fuse-ld=lld -Werror -### %s 2>&1 | \ -// RUN: FileCheck %s --check-prefix=DEFAULT --implicit-check-not=distributor \ -// RUN: --implicit-check-not=remote-compiler \ -// RUN: --implicit-check-not=warning: +// RUN: FileCheck @%t_f.rsp --check-prefix=DEFAULT // DEFAULT: ld.lld // DEFAULT-SAME: "--thinlto-distributor=d.exe" >From bb2f7d3655365caf5ee1eb401b2e9a7485e19e1d Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:03:00 +0100 Subject: [PATCH 07/13] Simplify test by including more arguments in linker shared response file --- clang/test/Driver/DTLTO/dtlto.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index e3ccd019135c9..8bbecf75ae533 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -9,13 +9,14 @@ // RUN: --implicit-check-not=remote-compiler \ // RUN: --implicit-check-not=warning:" > %t_f.rsp -// RUN: echo "--target=x86_64-linux-gnu \ +/// Create a response file to check that explicitly specified -Xthinlto-distributor +/// options are forwarded correctly. +// RUN: echo "-flto=thin \"%/s\" -### -fuse-ld=lld --target=x86_64-linux-gnu \ // RUN: -Xthinlto-distributor=a1 \ -// RUN: -Xthinlto-distributor=a2,a3 \ -// RUN: -fuse-ld=lld" > %t.rsp +// RUN: -Xthinlto-distributor=a2,a3" > %t_l1.rsp /// Check that options are forwarded as expected with --thinlto-distributor=. -// RUN: %clang -### @%t.rsp -fthinlto-distributor=d.exe %s 2>&1 | \ +// RUN: %clang @%t_l1.rsp -fthinlto-distributor=d.exe 2>&1 | \ // RUN: FileCheck @%t_f.rsp --check-prefix=FORWARD // FORWARD: ld.lld @@ -29,7 +30,7 @@ /// Check that options are not added without --thinlto-distributor= and /// that there is an unused option warning issued for -Xthinlto-distributor= /// options. We specify -flto here as these options should be unaffected by it. -// RUN: %clang -### @%t.rsp -flto=thin %s 2>&1 | \ +// RUN: %clang @%t_l1.rsp 2>&1 | \ // RUN: FileCheck @%t_f.rsp --check-prefix=NODIST // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a1' >From 00321cf8cf8ae4a87d0cbbc7728d06f01030443f Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:04:41 +0100 Subject: [PATCH 08/13] Improve match for clang executable --- clang/test/Driver/DTLTO/dtlto.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 8bbecf75ae533..0af9be4223dc1 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -21,12 +21,11 @@ // FORWARD: ld.lld // FORWARD-SAME: "--thinlto-distributor=d.exe" -// FORWARD-SAME: "--thinlto-remote-compiler={{.*}}clang +// FORWARD-SAME: "--thinlto-remote-compiler={{.*}}clang{{(.exe)?}}" // FORWARD-SAME: "--thinlto-distributor-arg=a1" // FORWARD-SAME: "--thinlto-distributor-arg=a2" // FORWARD-SAME: "--thinlto-distributor-arg=a3" - /// Check that options are not added without --thinlto-distributor= and /// that there is an unused option warning issued for -Xthinlto-distributor= /// options. We specify -flto here as these options should be unaffected by it. @@ -46,4 +45,4 @@ // DEFAULT: ld.lld // DEFAULT-SAME: "--thinlto-distributor=d.exe" -// DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang +// DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang{{(.exe)?}}" >From fb25cda6f61321d44e348b213e9fecf2aa1491b3 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:07:00 +0100 Subject: [PATCH 09/13] Removed unneeded -Werror as we can rely on the implicit check not check --- clang/test/Driver/DTLTO/dtlto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 0af9be4223dc1..6055c31e0e522 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -40,7 +40,7 @@ /// Check the expected arguments are forwarded by default with only /// --thinlto-distributor=. // RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=d.exe \ -// RUN: -fuse-ld=lld -Werror -### %s 2>&1 | \ +// RUN: -fuse-ld=lld -### %s 2>&1 | \ // RUN: FileCheck @%t_f.rsp --check-prefix=DEFAULT // DEFAULT: ld.lld >From 44abe764c8757d64c31f9d625dbf202de74012e9 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:08:39 +0100 Subject: [PATCH 10/13] Introduce a second linker response file which will simplify the test when additional test cases are added. --- clang/test/Driver/DTLTO/dtlto.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 6055c31e0e522..81682436bf44e 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -36,11 +36,13 @@ // NODIST: warning: argument unused during compilation: '-Xthinlto-distributor=a2,a3' // NODIST: ld.lld +/// Create a response file to check the default behavior. +// RUN: echo " \"%/s\" -### -fuse-ld=lld --target=x86_64-linux-gnu \ +// RUN: -fthinlto-distributor=d.exe" > %t_l2.rsp /// Check the expected arguments are forwarded by default with only /// --thinlto-distributor=. -// RUN: %clang --target=x86_64-linux-gnu -fthinlto-distributor=d.exe \ -// RUN: -fuse-ld=lld -### %s 2>&1 | \ +// RUN: %clang -flto=thin @%t_l2.rsp 2>&1 | \ // RUN: FileCheck @%t_f.rsp --check-prefix=DEFAULT // DEFAULT: ld.lld >From 820a50d1d77faab32af8664b2ab8834cb87e47a3 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:11:45 +0100 Subject: [PATCH 11/13] Move option handling into addLTOOptions for consistency. --- clang/lib/Driver/ToolChains/CommonArgs.cpp | 11 +++++++++++ clang/lib/Driver/ToolChains/Gnu.cpp | 15 --------------- clang/test/Driver/DTLTO/dtlto.c | 11 +++++++++-- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp index 070901f037823..8d56488777ad5 100644 --- a/clang/lib/Driver/ToolChains/CommonArgs.cpp +++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp @@ -1309,6 +1309,17 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args, if (Args.hasArg(options::OPT_ftime_report)) CmdArgs.push_back( Args.MakeArgString(Twine(PluginOptPrefix) + "-time-passes")); + + if (Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) { + CmdArgs.push_back( + Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue()))); + CmdArgs.push_back( + Args.MakeArgString("--thinlto-remote-compiler=" + + Twine(ToolChain.getDriver().getClangProgramPath()))); + + for (auto A : Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ)) + CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A)); + } } void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC, diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp index 3e9ca8f79d160..f5e2655857432 100644 --- a/clang/lib/Driver/ToolChains/Gnu.cpp +++ b/clang/lib/Driver/ToolChains/Gnu.cpp @@ -455,21 +455,6 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA, addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs, D.getLTOMode() == LTOK_Thin); - // Forward the DTLTO options to the linker. We add these unconditionally, - // rather than in addLTOOptions() as it is the linker that decides whether to - // do LTO or not dependent upon whether there are any bitcode input files in - // the link. - if (Arg *A = Args.getLastArg(options::OPT_fthinlto_distributor_EQ)) { - CmdArgs.push_back( - Args.MakeArgString("--thinlto-distributor=" + Twine(A->getValue()))); - CmdArgs.push_back( - Args.MakeArgString("--thinlto-remote-compiler=" + - Twine(ToolChain.getDriver().getClangProgramPath()))); - - for (auto A : Args.getAllArgValues(options::OPT_Xthinlto_distributor_EQ)) - CmdArgs.push_back(Args.MakeArgString("--thinlto-distributor-arg=" + A)); - } - if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle)) CmdArgs.push_back("--no-demangle"); diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 81682436bf44e..5e0aed97a4da0 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -27,8 +27,7 @@ // FORWARD-SAME: "--thinlto-distributor-arg=a3" /// Check that options are not added without --thinlto-distributor= and -/// that there is an unused option warning issued for -Xthinlto-distributor= -/// options. We specify -flto here as these options should be unaffected by it. +/// that a warning is issued for unused -Xthinlto-distributor options. // RUN: %clang @%t_l1.rsp 2>&1 | \ // RUN: FileCheck @%t_f.rsp --check-prefix=NODIST @@ -48,3 +47,11 @@ // DEFAULT: ld.lld // DEFAULT-SAME: "--thinlto-distributor=d.exe" // DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang{{(.exe)?}}" + +/// Check that nothing is forwarded when the compiler is not in LTO mode, and that +/// appropriate unused option warnings are issued. +// RUN: %clang @%t_l2.rsp 2>&1 | \ +// RUN: FileCheck @%t_f.rsp --check-prefix=NOFLTO + +// NOFLTO: warning: argument unused during compilation: '-fthinlto-distributor=d.exe' +// NOFLTO: ld.lld >From 60e87c9e3cd3cc61f047881183d0f47de56f1dce Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Thu, 10 Jul 2025 17:56:36 +0100 Subject: [PATCH 12/13] On linux the clang exectuable might be clang-20. Fix match. --- clang/test/Driver/DTLTO/dtlto.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 5e0aed97a4da0..78b8816c953e7 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -21,7 +21,7 @@ // FORWARD: ld.lld // FORWARD-SAME: "--thinlto-distributor=d.exe" -// FORWARD-SAME: "--thinlto-remote-compiler={{.*}}clang{{(.exe)?}}" +// FORWARD-SAME: "--thinlto-remote-compiler={{.*}}clang{{[^\"]*}}" // FORWARD-SAME: "--thinlto-distributor-arg=a1" // FORWARD-SAME: "--thinlto-distributor-arg=a2" // FORWARD-SAME: "--thinlto-distributor-arg=a3" @@ -46,7 +46,7 @@ // DEFAULT: ld.lld // DEFAULT-SAME: "--thinlto-distributor=d.exe" -// DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang{{(.exe)?}}" +// DEFAULT-SAME: "--thinlto-remote-compiler={{.*}}clang{{[^\"]*}}" /// Check that nothing is forwarded when the compiler is not in LTO mode, and that /// appropriate unused option warnings are issued. >From c961cc0dc5f4d613a374ba037d6c01c40f103040 Mon Sep 17 00:00:00 2001 From: Dunbobbin <ben.dunbob...@sony.com> Date: Fri, 11 Jul 2025 02:02:38 +0100 Subject: [PATCH 13/13] Use %/s consistently in response files. --- clang/test/Driver/DTLTO/dtlto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/clang/test/Driver/DTLTO/dtlto.c b/clang/test/Driver/DTLTO/dtlto.c index 78b8816c953e7..29a777d8cf866 100644 --- a/clang/test/Driver/DTLTO/dtlto.c +++ b/clang/test/Driver/DTLTO/dtlto.c @@ -5,7 +5,7 @@ /// Create a response file for all FileCheck invocations to share. These implicit /// checks ensure that all lines which mention DTLTO options are checked, /// and that no unexpected warnings appear. -// RUN: echo " \"%s\" --implicit-check-not=distributor \ +// RUN: echo " \"%/s\" --implicit-check-not=distributor \ // RUN: --implicit-check-not=remote-compiler \ // RUN: --implicit-check-not=warning:" > %t_f.rsp _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits