[clang] [Clang] [PGO] Provide default value for -fprofile-sample-use (PR #112750)

Mikołaj Piróg via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 18 04:51:23 PDT 2024


https://github.com/mikolaj-pirog updated https://github.com/llvm/llvm-project/pull/112750

>From 642f7ac9b1e999d8adb690b67c6303139794379d Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Thu, 17 Oct 2024 10:17:09 -0700
Subject: [PATCH 1/2] Provide default value for -fprofile-sample-use

---
 clang/lib/Driver/ToolChains/Clang.cpp | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 3fc39296f44281..2da8b604ebff62 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -594,6 +594,8 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
         << PGOGenerateArg->getSpelling() << ProfileGenerateArg->getSpelling();
 
   auto *ProfileUseArg = getLastProfileUseArg(Args);
+  auto *ProfileSampleUseArg = Args.getLastArg(
+      options::OPT_fprofile_sample_use, options::OPT_fprofile_sample_use_EQ);
 
   if (PGOGenerateArg && ProfileUseArg)
     D.Diag(diag::err_drv_argument_not_allowed_with)
@@ -677,6 +679,25 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
     }
   }
 
+  if (ProfileSampleUseArg) {
+    if ((ProfileSampleUseArg->getOption().matches(
+             options::OPT_fprofile_sample_use) ||
+         ProfileSampleUseArg->getOption().matches(
+             options::OPT_fprofile_sample_use_EQ))) {
+      SmallString<128> Path(ProfileSampleUseArg->getNumValues() == 0
+                                ? ""
+                                : ProfileSampleUseArg->getValue());
+      if (Path.empty() || llvm::sys::fs::is_directory(Path))
+        llvm::sys::path::append(Path, "default.profdata");
+
+      if (!llvm::sys::fs::exists(Path))
+        D.Diag(diag::err_drv_no_such_file) << Path;
+
+      CmdArgs.push_back(
+          Args.MakeArgString(Twine("-fprofile-sample-use=") + Path));
+    }
+  }
+
   bool EmitCovNotes = Args.hasFlag(options::OPT_ftest_coverage,
                                    options::OPT_fno_test_coverage, false) ||
                       Args.hasArg(options::OPT_coverage);

>From 571b9ddef3dcffdea76681fd2b3608a26dc3f1ef Mon Sep 17 00:00:00 2001
From: "Pirog, Mikolaj Maciej" <mikolaj.maciej.pirog at intel.com>
Date: Fri, 18 Oct 2024 04:51:11 -0700
Subject: [PATCH 2/2] Fix tests and correct behaviour on -fno.. options

---
 clang/lib/Driver/ToolChains/Clang.cpp | 10 +++++++++-
 clang/test/Driver/clang_f_opts.c      |  5 +++--
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 2da8b604ebff62..d588badcc03b14 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -595,7 +595,15 @@ static void addPGOAndCoverageFlags(const ToolChain &TC, Compilation &C,
 
   auto *ProfileUseArg = getLastProfileUseArg(Args);
   auto *ProfileSampleUseArg = Args.getLastArg(
-      options::OPT_fprofile_sample_use, options::OPT_fprofile_sample_use_EQ);
+      options::OPT_fprofile_sample_use, options::OPT_fprofile_sample_use_EQ,
+      options::OPT_fauto_profile, options::OPT_fauto_profile_EQ,
+      options::OPT_fno_profile_sample_use, options::OPT_fno_auto_profile);
+
+  if (ProfileSampleUseArg &&
+      (ProfileSampleUseArg->getOption().matches(
+           options::OPT_fno_profile_sample_use) ||
+       ProfileSampleUseArg->getOption().matches(options::OPT_fno_auto_profile)))
+    ProfileSampleUseArg = nullptr;
 
   if (PGOGenerateArg && ProfileUseArg)
     D.Diag(diag::err_drv_argument_not_allowed_with)
diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c
index fd15552715cb35..4d6f13038763ed 100644
--- a/clang/test/Driver/clang_f_opts.c
+++ b/clang/test/Driver/clang_f_opts.c
@@ -71,8 +71,9 @@
 // RUN: %clang -### -S -fauto-profile=%S/Inputs/file.prof -fno-auto-profile %s 2>&1 | FileCheck -check-prefix=CHECK-NO-AUTO-PROFILE %s
 // CHECK-NO-AUTO-PROFILE-NOT: "-fprofile-sample-use={{.*}}/file.prof"
 
-// RUN: %clang -### -S -fauto-profile=%S/Inputs/file.prof -fno-profile-sample-use -fauto-profile %s 2>&1 | FileCheck -check-prefix=CHECK-AUTO-PROFILE %s
-// RUN: %clang -### -S -fauto-profile=%S/Inputs/file.prof -fno-auto-profile -fprofile-sample-use %s 2>&1 | FileCheck -check-prefix=CHECK-AUTO-PROFILE %s
+// RUN: not %clang -### -S -fauto-profile=%S/Inputs/file.prof -fno-profile-sample-use -fauto-profile %s 2>&1 | FileCheck -check-prefix=CHECK-AUTO-PROFILE-NO-DEFAULT %s
+// RUN: not %clang -### -S -fauto-profile=%S/Inputs/file.prof -fno-auto-profile -fprofile-sample-use %s 2>&1 | FileCheck -check-prefix=CHECK-AUTO-PROFILE-NO-DEFAULT %s
+// CHECK-AUTO-PROFILE-NO-DEFAULT: error: no such file or directory: 'default.profdata'
 
 // RUN: %clang -### -S -fprofile-generate %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-GENERATE-LLVM %s
 // RUN: %clang -### -S -fprofile-instr-generate %s 2>&1 | FileCheck -check-prefix=CHECK-PROFILE-GENERATE %s



More information about the cfe-commits mailing list