[clang] [Clang][Driver][LTO] Change the filename format for LTO'd stats file (PR #70242)

Min-Yih Hsu via cfe-commits cfe-commits at lists.llvm.org
Wed Oct 25 11:57:00 PDT 2023


https://github.com/mshockwave created https://github.com/llvm/llvm-project/pull/70242

Use "<output filename>.ld.stats" instead of "<base input filename>.stats" for stats file generated by LTO backend. The new extension makes it easier to search for LTO'd stats file and be consistent with LTO'd optimization remarks files' naming convention (i.e. *.opt.ld.yaml, as opposed to *.opt.yaml for normal opt remarks files).

>From 5f2504eb4393bbd8948b85c4dd05f2c9c244c9f7 Mon Sep 17 00:00:00 2001
From: Min Hsu <min.hsu at sifive.com>
Date: Wed, 25 Oct 2023 11:42:14 -0700
Subject: [PATCH] [Clang][Driver][LTO] Change the filename format for LTO'd
 stats file

Use "<output filename>.ld.stats" instead of "<base input filename>.stats"
for stats file generated by LTO backend. The new extension makes it
easier to search for LTO'd stats file and be consistent with LTO'd
optimization remarks files' naming convention (i.e. *.opt.ld.yaml,
as opposed to *.opt.yaml for normal opt remarks files).
---
 clang/lib/Driver/ToolChains/CommonArgs.cpp | 19 +++++++++++++++----
 clang/lib/Driver/ToolChains/CommonArgs.h   |  3 ++-
 clang/test/Driver/save-stats.c             |  5 +++--
 3 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index ad012d3d0d4b46f..98e20abc015c5fd 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -852,7 +852,8 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
         Args.MakeArgString(Twine(PluginOptPrefix) + "-stack-size-section"));
 
   // Setup statistics file output.
-  SmallString<128> StatsFile = getStatsFileName(Args, Output, Input, D);
+  SmallString<128> StatsFile =
+      getStatsFileName(Args, Output, Input, D, /*IsLTO=*/true);
   if (!StatsFile.empty())
     CmdArgs.push_back(
         Args.MakeArgString(Twine(PluginOptPrefix) + "stats-file=" + StatsFile));
@@ -1941,7 +1942,7 @@ void tools::AddRunTimeLibs(const ToolChain &TC, const Driver &D,
 SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args,
                                          const InputInfo &Output,
                                          const InputInfo &Input,
-                                         const Driver &D) {
+                                         const Driver &D, bool IsLTO) {
   const Arg *A = Args.getLastArg(options::OPT_save_stats_EQ);
   if (!A && !D.CCPrintInternalStats)
     return {};
@@ -1957,9 +1958,19 @@ SmallString<128> tools::getStatsFileName(const llvm::opt::ArgList &Args,
       return {};
     }
 
-    StringRef BaseName = llvm::sys::path::filename(Input.getBaseInput());
+    StringRef BaseName;
+    // For stats files generated by the LTO backend, we're using "<output
+    // filename>.ld.stats" for its name. Note that we don't query
+    // `options::OPT_flto_EQ` and `options::OPT_fno_lto` here to decide `IsLTO`
+    // because it also changes the stats filenames for the pre-link object (LLVM
+    // bitcode) files, which we want to keep the same.
+    if (IsLTO && Output.isFilename())
+      BaseName = llvm::sys::path::filename(Output.getFilename());
+    else
+      BaseName = llvm::sys::path::filename(Input.getBaseInput());
+
     llvm::sys::path::append(StatsFile, BaseName);
-    llvm::sys::path::replace_extension(StatsFile, "stats");
+    llvm::sys::path::replace_extension(StatsFile, IsLTO ? "ld.stats" : "stats");
   } else {
     assert(D.CCPrintInternalStats);
     StatsFile.assign(D.CCPrintInternalStatReportFilename.empty()
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.h b/clang/lib/Driver/ToolChains/CommonArgs.h
index f364c9793c9be62..bab80af5fb2f67a 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.h
+++ b/clang/lib/Driver/ToolChains/CommonArgs.h
@@ -184,7 +184,8 @@ SmallVector<StringRef> unifyTargetFeatures(ArrayRef<StringRef> Features);
 /// to.
 SmallString<128> getStatsFileName(const llvm::opt::ArgList &Args,
                                   const InputInfo &Output,
-                                  const InputInfo &Input, const Driver &D);
+                                  const InputInfo &Input, const Driver &D,
+                                  bool IsLTO = false);
 
 /// \p Flag must be a flag accepted by the driver.
 void addMultilibFlag(bool Enabled, const StringRef Flag,
diff --git a/clang/test/Driver/save-stats.c b/clang/test/Driver/save-stats.c
index ca8f2a457d4488c..44a6ddabde2a2e9 100644
--- a/clang/test/Driver/save-stats.c
+++ b/clang/test/Driver/save-stats.c
@@ -22,10 +22,11 @@
 // 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
 // CHECK-LTO: "-stats-file=save-stats.stats"
 // CHECK-LTO: "-o" "obj/dir{{/|\\\\}}save-stats.exe"
-// CHECK-LTO: "-plugin-opt=stats-file=save-stats.stats"
+// CHECK-LTO: "-plugin-opt=stats-file=save-stats.ld.stats"
 
 // RUN: %clang -target x86_64-linux-unknown -save-stats=obj -flto -o obj/dir/save-stats.exe %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-LTO-OBJ
-// CHECK-LTO-OBJ: "-plugin-opt=stats-file=obj/dir{{/|\\\\}}save-stats.stats"
+// CHECK-LTO-OBJ: "-stats-file={{.*}}save-stats.stats"
+// CHECK-LTO-OBJ: "-plugin-opt=stats-file=obj/dir{{/|\\\\}}save-stats.ld.stats"
 
 // RUN: env CC_PRINT_INTERNAL_STAT=1 \
 // RUN: %clang -target x86_64-apple-darwin %s -### 2>&1 | FileCheck %s -check-prefix=CHECK-ENV



More information about the cfe-commits mailing list