[clang] [Clang][Driver][LTO] Fix empty stats filename when in LTO mode (PR #71197)

Min-Yih Hsu via cfe-commits cfe-commits at lists.llvm.org
Sun Nov 5 11:30:25 PST 2023


https://github.com/mshockwave updated https://github.com/llvm/llvm-project/pull/71197

>From 8d5acb56b364648d1abd6bfff6815af71e131d6e Mon Sep 17 00:00:00 2001
From: Min Hsu <min.hsu at sifive.com>
Date: Thu, 2 Nov 2023 17:26:17 -0700
Subject: [PATCH 1/2] [Clang][Driver][LTO] Fix empty stats filename when in LTO
 mode

Previously, if a linker flag (i.e.g -Wl) is presented before any input
filenames, Gnu driver would use the InputInfo object of that flag to
generate stats filename for LTO backend, causing an empty filename.
This patch fixes such issue.
---
 clang/lib/Driver/ToolChains/Gnu.cpp | 10 +++++++++-
 clang/test/Driver/save-stats.c      |  2 ++
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 5237951f84cce03..8448d4bda13c434 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -535,7 +535,15 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
   if (D.isUsingLTO()) {
     assert(!Inputs.empty() && "Must have at least one input.");
-    addLTOOptions(ToolChain, Args, CmdArgs, Output, Inputs[0],
+    // Find the first filename InputInfo object.
+    auto Input = llvm::find_if(
+        Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
+    if (Input == Inputs.end())
+      // For a very rare case, all of the inputs to the linker are
+      // flags. If that happens, just use the first InputInfo.
+      Input = Inputs.begin();
+
+    addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
                   D.getLTOMode() == LTOK_Thin);
   }
 
diff --git a/clang/test/Driver/save-stats.c b/clang/test/Driver/save-stats.c
index ca8f2a457d4488c..d6ad4e0097f3432 100644
--- a/clang/test/Driver/save-stats.c
+++ b/clang/test/Driver/save-stats.c
@@ -20,6 +20,8 @@
 // CHECK-INVALID: invalid value 'bla' in '-save-stats=bla'
 
 // RUN: %clang -target x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
+// Previously `-plugin-opt=stats-file` would use empty filename if a linker flag (i.e. -Wl) is presented before any input filename.
+// RUN: %clang -target x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
 // CHECK-LTO: "-stats-file=save-stats.stats"
 // CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
 // CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"

>From a235c39323e7e46e81a474fb6eb45a74ea4adcc5 Mon Sep 17 00:00:00 2001
From: Min Hsu <min.hsu at sifive.com>
Date: Sun, 5 Nov 2023 11:29:42 -0800
Subject: [PATCH 2/2] fixup! [Clang][Driver][LTO] Fix empty stats filename when
 in LTO mode

---
 clang/lib/Driver/ToolChains/Gnu.cpp | 2 +-
 clang/test/Driver/save-stats.c      | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Gnu.cpp b/clang/lib/Driver/ToolChains/Gnu.cpp
index 8448d4bda13c434..3276590729e47ea 100644
--- a/clang/lib/Driver/ToolChains/Gnu.cpp
+++ b/clang/lib/Driver/ToolChains/Gnu.cpp
@@ -540,7 +540,7 @@ void tools::gnutools::Linker::ConstructJob(Compilation &C, const JobAction &JA,
         Inputs, [](const InputInfo &II) -> bool { return II.isFilename(); });
     if (Input == Inputs.end())
       // For a very rare case, all of the inputs to the linker are
-      // flags. If that happens, just use the first InputInfo.
+      // InputArg. If that happens, just use the first InputInfo.
       Input = Inputs.begin();
 
     addLTOOptions(ToolChain, Args, CmdArgs, Output, *Input,
diff --git a/clang/test/Driver/save-stats.c b/clang/test/Driver/save-stats.c
index d6ad4e0097f3432..2208c229b91e56c 100644
--- a/clang/test/Driver/save-stats.c
+++ b/clang/test/Driver/save-stats.c
@@ -21,7 +21,7 @@
 
 // RUN: %clang -target x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
 // Previously `-plugin-opt=stats-file` would use empty filename if a linker flag (i.e. -Wl) is presented before any input filename.
-// RUN: %clang -target x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
+// RUN: %clang --target=x86_64-linux-unknown -save-stats -flto -o obj/dir/save-stats.exe -Wl,-plugin-opt=-dummy %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO
 // CHECK-LTO: "-stats-file=save-stats.stats"
 // CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
 // CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"



More information about the cfe-commits mailing list