Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Profile] Refactor profile correlation. #70712

Merged
merged 1 commit into from
Oct 31, 2023

Conversation

ZequanWu
Copy link
Contributor

Refactor some code from #69493.

Rebase of #69656 on top of main as it was messed up.

@llvmbot llvmbot added clang Clang issues not falling into any other category compiler-rt clang:codegen PGO Profile Guided Optimizations llvm:transforms llvm:binary-utilities labels Oct 30, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Oct 30, 2023

@llvm/pr-subscribers-clang
@llvm/pr-subscribers-llvm-binary-utilities
@llvm/pr-subscribers-pgo
@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-clang-codegen

Author: Zequan Wu (ZequanWu)

Changes

Refactor some code from #69493.

Rebase of #69656 on top of main as it was messed up.


Patch is 38.04 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/70712.diff

22 Files Affected:

  • (modified) clang/lib/CodeGen/BackendUtil.cpp (+10-4)
  • (modified) compiler-rt/lib/profile/InstrProfiling.c (+4)
  • (modified) compiler-rt/lib/profile/InstrProfiling.h (+6)
  • (modified) compiler-rt/lib/profile/InstrProfilingBuffer.c (+11)
  • (modified) compiler-rt/lib/profile/InstrProfilingMerge.c (+6-5)
  • (modified) compiler-rt/lib/profile/InstrProfilingWriter.c (+10-11)
  • (modified) compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c (+2-2)
  • (modified) compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c (+1-1)
  • (modified) compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c (+3-3)
  • (modified) compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c (+3-3)
  • (modified) llvm/docs/CommandGuide/llvm-profdata.rst (+2-2)
  • (modified) llvm/include/llvm/ProfileData/InstrProfCorrelator.h (+10-3)
  • (modified) llvm/include/llvm/ProfileData/InstrProfReader.h (+2)
  • (modified) llvm/include/llvm/Transforms/Instrumentation/PGOInstrumentation.h (-2)
  • (modified) llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp (+19)
  • (modified) llvm/lib/ProfileData/InstrProfCorrelator.cpp (+63-37)
  • (modified) llvm/lib/ProfileData/InstrProfReader.cpp (+2-2)
  • (modified) llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp (+9-9)
  • (modified) llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp (+5-1)
  • (modified) llvm/test/Instrumentation/InstrProfiling/debug-info-correlate-coverage.ll (+1-1)
  • (modified) llvm/test/Instrumentation/InstrProfiling/debug-info-correlate.ll (+1-1)
  • (modified) llvm/tools/llvm-profdata/llvm-profdata.cpp (+6-3)
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 70accce456d3c07..83b81a38a768523 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -42,6 +42,7 @@
 #include "llvm/Passes/PassBuilder.h"
 #include "llvm/Passes/PassPlugin.h"
 #include "llvm/Passes/StandardInstrumentations.h"
+#include "llvm/ProfileData/InstrProfCorrelator.h"
 #include "llvm/Support/BuryPointer.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/MemoryBuffer.h"
@@ -55,6 +56,7 @@
 #include "llvm/Target/TargetOptions.h"
 #include "llvm/TargetParser/SubtargetFeature.h"
 #include "llvm/TargetParser/Triple.h"
+#include "llvm/Transforms/HipStdPar/HipStdPar.h"
 #include "llvm/Transforms/IPO/EmbedBitcodePass.h"
 #include "llvm/Transforms/IPO/LowerTypeTests.h"
 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
@@ -78,7 +80,6 @@
 #include "llvm/Transforms/Scalar/EarlyCSE.h"
 #include "llvm/Transforms/Scalar/GVN.h"
 #include "llvm/Transforms/Scalar/JumpThreading.h"
-#include "llvm/Transforms/HipStdPar/HipStdPar.h"
 #include "llvm/Transforms/Utils/Debugify.h"
 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
 #include "llvm/Transforms/Utils/ModuleUtils.h"
@@ -98,13 +99,18 @@ extern cl::opt<bool> PrintPipelinePasses;
 static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
     "sanitizer-early-opt-ep", cl::Optional,
     cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
-}
+
+extern cl::opt<bool> DebugInfoCorrelate;
+extern cl::opt<InstrProfCorrelator::ProfCorrelatorKind> ProfileCorrelate;
+} // namespace llvm
 
 namespace {
 
 // Default filename used for profile generation.
 std::string getDefaultProfileGenName() {
-  return DebugInfoCorrelate ? "default_%m.proflite" : "default_%m.profraw";
+  return DebugInfoCorrelate || ProfileCorrelate != InstrProfCorrelator::NONE
+             ? "default_%m.proflite"
+             : "default_%m.profraw";
 }
 
 class EmitAssemblyHelper {
@@ -197,7 +203,7 @@ class EmitAssemblyHelper {
   void EmitAssembly(BackendAction Action,
                     std::unique_ptr<raw_pwrite_stream> OS);
 };
-}
+} // namespace
 
 static SanitizerCoverageOptions
 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
diff --git a/compiler-rt/lib/profile/InstrProfiling.c b/compiler-rt/lib/profile/InstrProfiling.c
index da04d8ebdec95bb..7d69e37815c948f 100644
--- a/compiler-rt/lib/profile/InstrProfiling.c
+++ b/compiler-rt/lib/profile/InstrProfiling.c
@@ -89,3 +89,7 @@ COMPILER_RT_VISIBILITY void __llvm_profile_reset_counters(void) {
   }
   lprofSetProfileDumped(0);
 }
+
+inline int hasCorrelation() {
+  return (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) != 0ULL;
+}
diff --git a/compiler-rt/lib/profile/InstrProfiling.h b/compiler-rt/lib/profile/InstrProfiling.h
index e143149fca82707..b8104af5f12b910 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -261,6 +261,9 @@ uint64_t __llvm_profile_get_magic(void);
 /*! \brief Get the version of the file format. */
 uint64_t __llvm_profile_get_version(void);
 
+/*! \brief If the binary is compiled with profile correlation. */
+int hasCorrelation();
+
 /*! \brief Get the number of entries in the profile data section. */
 uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
                                      const __llvm_profile_data *End);
@@ -282,6 +285,9 @@ uint64_t __llvm_profile_get_counters_size(const char *Begin, const char *End);
 uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
                                              const char *End);
 
+/*! \brief Get the size of the profile name section in bytes. */
+uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End);
+
 /* ! \brief Given the sizes of the data and counter information, return the
  * number of padding bytes before and after the counters, and after the names,
  * in the raw profile.
diff --git a/compiler-rt/lib/profile/InstrProfilingBuffer.c b/compiler-rt/lib/profile/InstrProfilingBuffer.c
index c7217b2dfef8a97..69965142d978746 100644
--- a/compiler-rt/lib/profile/InstrProfilingBuffer.c
+++ b/compiler-rt/lib/profile/InstrProfilingBuffer.c
@@ -56,6 +56,8 @@ uint64_t __llvm_profile_get_size_for_buffer(void) {
 COMPILER_RT_VISIBILITY
 uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
                                      const __llvm_profile_data *End) {
+  if (hasCorrelation())
+    return 0;
   intptr_t BeginI = (intptr_t)Begin, EndI = (intptr_t)End;
   return ((EndI + sizeof(__llvm_profile_data) - 1) - BeginI) /
          sizeof(__llvm_profile_data);
@@ -64,6 +66,8 @@ uint64_t __llvm_profile_get_num_data(const __llvm_profile_data *Begin,
 COMPILER_RT_VISIBILITY
 uint64_t __llvm_profile_get_data_size(const __llvm_profile_data *Begin,
                                       const __llvm_profile_data *End) {
+  if (hasCorrelation())
+    return 0;
   return __llvm_profile_get_num_data(Begin, End) * sizeof(__llvm_profile_data);
 }
 
@@ -92,6 +96,13 @@ uint64_t __llvm_profile_get_num_bitmap_bytes(const char *Begin,
   return (End - Begin);
 }
 
+COMPILER_RT_VISIBILITY
+uint64_t __llvm_profile_get_name_size(const char *Begin, const char *End) {
+  if (hasCorrelation())
+    return 0;
+  return End - Begin;
+}
+
 /// Calculate the number of padding bytes needed to add to \p Offset in order
 /// for (\p Offset + Padding) to be page-aligned.
 static uint64_t calculateBytesNeededToPageAlign(uint64_t Offset) {
diff --git a/compiler-rt/lib/profile/InstrProfilingMerge.c b/compiler-rt/lib/profile/InstrProfilingMerge.c
index c5f168bf7517718..b08973debda94f3 100644
--- a/compiler-rt/lib/profile/InstrProfilingMerge.c
+++ b/compiler-rt/lib/profile/InstrProfilingMerge.c
@@ -69,8 +69,9 @@ int __llvm_profile_check_compatibility(const char *ProfileData,
       Header->NumBitmapBytes !=
           __llvm_profile_get_num_bitmap_bytes(__llvm_profile_begin_bitmap(),
                                               __llvm_profile_end_bitmap()) ||
-      Header->NamesSize != (uint64_t)(__llvm_profile_end_names() -
-                                      __llvm_profile_begin_names()) ||
+      Header->NamesSize !=
+          __llvm_profile_get_name_size(__llvm_profile_begin_names(),
+                                       __llvm_profile_end_names()) ||
       Header->ValueKindLast != IPVK_Last)
     return 1;
 
@@ -138,9 +139,9 @@ int __llvm_profile_merge_from_buffer(const char *ProfileData,
   if (SrcNameStart < SrcCountersStart || SrcNameStart < SrcBitmapStart)
     return 1;
 
-  // Merge counters by iterating the entire counter section when debug info
-  // correlation is enabled.
-  if (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) {
+  // Merge counters by iterating the entire counter section when correlation is
+  // enabled.
+  if (hasCorrelation()) {
     for (SrcCounter = SrcCountersStart,
         DstCounter = __llvm_profile_begin_counters();
          SrcCounter < SrcCountersEnd;) {
diff --git a/compiler-rt/lib/profile/InstrProfilingWriter.c b/compiler-rt/lib/profile/InstrProfilingWriter.c
index 3b61f3def9f6ef0..d35ee6b20504f3e 100644
--- a/compiler-rt/lib/profile/InstrProfilingWriter.c
+++ b/compiler-rt/lib/profile/InstrProfilingWriter.c
@@ -262,21 +262,19 @@ lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
                    const char *BitmapBegin, const char *BitmapEnd,
                    VPDataReaderType *VPDataReader, const char *NamesBegin,
                    const char *NamesEnd, int SkipNameDataWrite) {
-  int DebugInfoCorrelate =
-      (__llvm_profile_get_version() & VARIANT_MASK_DBG_CORRELATE) != 0ULL;
+  int ProfileCorrelation = hasCorrelation();
 
   /* Calculate size of sections. */
   const uint64_t DataSectionSize =
-      DebugInfoCorrelate ? 0 : __llvm_profile_get_data_size(DataBegin, DataEnd);
-  const uint64_t NumData =
-      DebugInfoCorrelate ? 0 : __llvm_profile_get_num_data(DataBegin, DataEnd);
+      __llvm_profile_get_data_size(DataBegin, DataEnd);
+  const uint64_t NumData = __llvm_profile_get_num_data(DataBegin, DataEnd);
   const uint64_t CountersSectionSize =
       __llvm_profile_get_counters_size(CountersBegin, CountersEnd);
   const uint64_t NumCounters =
       __llvm_profile_get_num_counters(CountersBegin, CountersEnd);
   const uint64_t NumBitmapBytes =
       __llvm_profile_get_num_bitmap_bytes(BitmapBegin, BitmapEnd);
-  const uint64_t NamesSize = DebugInfoCorrelate ? 0 : NamesEnd - NamesBegin;
+  const uint64_t NamesSize = __llvm_profile_get_name_size(NamesBegin, NamesEnd);
 
   /* Create the header. */
   __llvm_profile_header Header;
@@ -304,7 +302,7 @@ lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
 #endif
 
   /* The data and names sections are omitted in lightweight mode. */
-  if (DebugInfoCorrelate) {
+  if (ProfileCorrelation) {
     Header.CountersDelta = 0;
     Header.NamesDelta = 0;
   }
@@ -320,21 +318,22 @@ lprofWriteDataImpl(ProfDataWriter *Writer, const __llvm_profile_data *DataBegin,
 
   /* Write the profile data. */
   ProfDataIOVec IOVecData[] = {
-      {DebugInfoCorrelate ? NULL : DataBegin, sizeof(uint8_t), DataSectionSize,
+      {ProfileCorrelation ? NULL : DataBegin, sizeof(uint8_t), DataSectionSize,
        0},
       {NULL, sizeof(uint8_t), PaddingBytesBeforeCounters, 1},
       {CountersBegin, sizeof(uint8_t), CountersSectionSize, 0},
       {NULL, sizeof(uint8_t), PaddingBytesAfterCounters, 1},
       {BitmapBegin, sizeof(uint8_t), NumBitmapBytes, 0},
       {NULL, sizeof(uint8_t), PaddingBytesAfterBitmapBytes, 1},
-      {(SkipNameDataWrite || DebugInfoCorrelate) ? NULL : NamesBegin,
+      {(SkipNameDataWrite || ProfileCorrelation) ? NULL : NamesBegin,
        sizeof(uint8_t), NamesSize, 0},
       {NULL, sizeof(uint8_t), PaddingBytesAfterNames, 1}};
   if (Writer->Write(Writer, IOVecData, sizeof(IOVecData) / sizeof(*IOVecData)))
     return -1;
 
-  /* Value profiling is not yet supported in continuous mode. */
-  if (__llvm_profile_is_continuous_mode_enabled())
+  /* Value profiling is not yet supported in continuous mode and profile
+   * correlation mode. */
+  if (__llvm_profile_is_continuous_mode_enabled() || ProfileCorrelation)
     return 0;
 
   return writeValueProfData(Writer, VPDataReader, DataBegin, DataEnd);
diff --git a/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c b/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
index f347d439e2e0671..46d25a4e386dc3a 100644
--- a/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
+++ b/compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c
@@ -1,5 +1,5 @@
 // Value profiling is currently not supported in lightweight mode.
-// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t -g -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t.dSYM %t.proflite
 
@@ -9,7 +9,7 @@
 
 // RUN: diff <(llvm-profdata show --all-functions --counts %t.normal.profdata) <(llvm-profdata show --all-functions --counts %t.profdata)
 
-// RUN: %clang_pgogen -o %t.cov -g -mllvm --debug-info-correlate -mllvm -pgo-function-entry-coverage -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t.cov -g -mllvm --profile-correlate=debug-info -mllvm -pgo-function-entry-coverage -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.cov.proflite %run %t.cov
 // RUN: llvm-profdata merge -o %t.cov.profdata --debug-info=%t.cov.dSYM %t.cov.proflite
 
diff --git a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c
index 5069c6340b64fd2..25022f241a6d281 100644
--- a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c
+++ b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c
@@ -1,6 +1,6 @@
 // Disable full debug info and verify that we get warnings during merging
 
-// RUN: %clang_pgogen -o %t -gline-tables-only -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t -gline-tables-only -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite --max-debug-info-correlation-warnings=2 2>&1 >/dev/null | FileCheck %s --check-prefixes=CHECK,LIMIT --implicit-check-not=warning
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite --max-debug-info-correlation-warnings=0 2>&1 >/dev/null | FileCheck %s --check-prefixes=CHECK,NOLIMIT --implicit-check-not=warning
diff --git a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
index a918d7b6299005e..ccfd65b6f4c4b16 100644
--- a/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
+++ b/compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c
@@ -3,19 +3,19 @@
 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t.normal
 // RUN: llvm-profdata merge -o %t.normal.profdata %t.profraw
 
-// RUN: %clang_pgogen -o %t.d4 -g -gdwarf-4 -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t.d4 -g -gdwarf-4 -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.d4.proflite %run %t.d4
 // RUN: llvm-profdata merge -o %t.d4.profdata --debug-info=%t.d4 %t.d4.proflite
 
 // RUN: diff <(llvm-profdata show --all-functions --counts %t.normal.profdata) <(llvm-profdata show --all-functions --counts %t.d4.profdata)
 
-// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t -g -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.proflite %run %t
 // RUN: llvm-profdata merge -o %t.profdata --debug-info=%t %t.proflite
 
 // RUN: diff <(llvm-profdata show --all-functions --counts %t.normal.profdata) <(llvm-profdata show --all-functions --counts %t.profdata)
 
-// RUN: %clang_pgogen -o %t.cov -g -mllvm --debug-info-correlate -mllvm -pgo-function-entry-coverage -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
+// RUN: %clang_pgogen -o %t.cov -g -mllvm --profile-correlate=debug-info -mllvm -pgo-function-entry-coverage -mllvm --disable-vp=true %S/../Inputs/instrprof-debug-info-correlate-main.cpp %S/../Inputs/instrprof-debug-info-correlate-foo.cpp
 // RUN: env LLVM_PROFILE_FILE=%t.cov.proflite %run %t.cov
 // RUN: llvm-profdata merge -o %t.cov.profdata --debug-info=%t.cov %t.cov.proflite
 
diff --git a/compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c b/compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c
index 226d678aca347a4..93bf40f98d3ab62 100644
--- a/compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c
+++ b/compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c
@@ -1,10 +1,10 @@
-// RUN: %clang_pgogen -o %t -g -mllvm --debug-info-correlate -mllvm --disable-vp=true %s
+// RUN: %clang_pgogen -o %t -g -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %s
 // RUN: llvm-profdata show --debug-info=%t --detailed-summary --show-prof-sym-list | FileCheck %s
 // RUN: llvm-profdata show --debug-info=%t --show-format=yaml | FileCheck %s --match-full-lines --check-prefix YAML
 
-// RUN: %clang_pgogen -o %t.no.dbg -mllvm --debug-info-correlate -mllvm --disable-vp=true %s
+// RUN: %clang_pgogen -o %t.no.dbg -mllvm --profile-correlate=debug-info -mllvm --disable-vp=true %s
 // RUN: not llvm-profdata show --debug-info=%t.no.dbg 2>&1 | FileCheck %s --check-prefix NO-DBG
-// NO-DBG: unable to correlate profile: could not find any profile metadata in debug info
+// NO-DBG: unable to correlate profile: could not find any profile data metadata in correlated file
 
 // YAML: Probes:
 // YAML:   - Function Name:   a
diff --git a/llvm/docs/CommandGuide/llvm-profdata.rst b/llvm/docs/CommandGuide/llvm-profdata.rst
index be42733ca140567..73759f14b49a9d8 100644
--- a/llvm/docs/CommandGuide/llvm-profdata.rst
+++ b/llvm/docs/CommandGuide/llvm-profdata.rst
@@ -195,7 +195,7 @@ OPTIONS
 .. option:: --debug-info=<path>
 
  Specify the executable or ``.dSYM`` that contains debug info for the raw profile.
- When ``-debug-info-correlate`` was used for instrumentation, use this option
+ When ``-profile-correlate=debug-info`` was used for instrumentation, use this option
  to correlate the raw profile.
 
 .. option:: --temporal-profile-trace-reservoir-size
@@ -346,7 +346,7 @@ OPTIONS
 .. option:: --debug-info=<path>
 
  Specify the executable or ``.dSYM`` that contains debug info for the raw profile.
- When ``-debug-info-correlate`` was used for instrumentation, use this option
+ When ``-profile-correlate=debug-info`` was used for instrumentation, use this option
  to show the correlated functions from the raw profile.
 
 .. option:: --covered
diff --git a/llvm/include/llvm/ProfileData/InstrProfCorrelator.h b/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
index dd062951f277c1c..a3a0805a294a20c 100644
--- a/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
+++ b/llvm/include/llvm/ProfileData/InstrProfCorrelator.h
@@ -31,8 +31,11 @@ class ObjectFile;
 /// to their functions.
 class InstrProfCorrelator {
 public:
+  /// Indicate which kind correlator to use.
+  enum ProfCorrelatorKind { NONE, DEBUG_INFO };
+
   static llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
-  get(StringRef DebugInfoFilename);
+  get(StringRef Filename, ProfCorrelatorKind FileKind);
 
   /// Construct a ProfileData vector used to correlate raw instrumentation data
   /// to their functions.
@@ -104,7 +107,7 @@ class InstrProfCorrelator {
 
 private:
   static llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
-  get(std::unique_ptr<MemoryBuffer> Buffer);
+  get(std::unique_ptr<MemoryBuffer> Buffer, ProfCorrelatorKind FileKind);
 
   const InstrProfCorrelatorKind Kind;
 };
@@ -128,7 +131,7 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
 
   static llvm::Expected<std::unique_ptr<InstrProfCorrelatorImpl<IntPtrT>>>
   get(std::unique_ptr<InstrProfCorrelator::Context> Ctx,
-      const object::ObjectFile &Obj);
+      const object::ObjectFile &Obj, ProfCorrelatorKind FileKind);
 
 protected:
   std::vector<RawInstrProf::ProfileData<IntPtrT>> Data;
@@ -138,6 +141,8 @@ class InstrProfCorrelatorImpl : public InstrProfCorrelator {
       int MaxWarnings,
       InstrProfCorrelator::CorrelationData *Data = nullptr) = 0;
 
+  virtual Error correlateProfileNameImpl() = 0;
+
   Error dumpYaml(int MaxWarnings, raw_ostream &OS) override;
 
   void addProbe(StringRef FunctionName, uint64_t CFGHash, IntPtrT CounterOffset,
@@ -205,6 +210,8 @@ class DwarfInstrProfCorrelator : public InstrProfCorrelatorImpl<IntPtrT> {
   void correlateProfileDataImpl(
       int MaxWarnings,
       InstrProfCorrelator::CorrelationData *Data = nullptr) override;
+
+  Error correlateProfileNameImpl() override;
 };
 
 } // end namespace llvm
diff --git a/llvm/include/llvm/ProfileData/InstrProfReader.h b/llvm/include/llvm/ProfileData/InstrProfReader.h
index cf6429a324d36b3.....
[truncated]

@ZequanWu
Copy link
Contributor Author

I think it's fine to land as this is already reviewed & approved at #69656

@ZequanWu ZequanWu merged commit 4b383d0 into llvm:main Oct 31, 2023
10 checks passed
@ZequanWu ZequanWu deleted the refactor-profile-cor branch October 31, 2023 14:41
ZequanWu added a commit that referenced this pull request Oct 31, 2023
@ZequanWu ZequanWu restored the refactor-profile-cor branch October 31, 2023 14:54
@ZequanWu
Copy link
Contributor Author

Reverted at db7a1ed.

The build shows bunch linking failures, but I can build those failed targets locally.

@ZequanWu
Copy link
Contributor Author

ZequanWu commented Oct 31, 2023

I can repro the linking failures when using the same cmake invocation.

Here is the full command:

$ /usr/bin/c++ -std=c++11 -Wno-documentation-deprecated-sync -fPIC -fno-semantic-interposition -fvisibility-inlines-hidden -Werror=date-time -fno-lifetime-dse -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wno-missing-field-initializers -pedantic -Wno-long-long -Wimplicit-fallthrough -Wno-maybe-uninitialized -Wno-nonnull -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wdelete-non-virtual-dtor -Wsuggest-override -Wno-comment -Wno-misleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG -Wl,--gc-sections unittests/IR/CMakeFiles/IRTests.dir/AbstractCallSiteTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/AsmWriterTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/AttributesTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/BasicBlockTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/CFGBuilder.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ConstantRangeTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ConstantsTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DataLayoutTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DebugInfoTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DebugTypeODRUniquingTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DemandedBitsTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DominatorTreeTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/DominatorTreeBatchUpdatesTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/FunctionTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/PassBuilderCallbacksTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/IRBuilderTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/InstructionsTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/IntrinsicsTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/LegacyPassManagerTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/MDBuilderTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ManglerTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/MetadataTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ModuleTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ModuleSummaryIndexTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/PassManagerTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/PatternMatch.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ShuffleVectorInstTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/StructuralHashTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/TimePassesTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/TypesTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/UseTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/UserTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ValueHandleTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ValueMapTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/ValueTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/VectorBuilderTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/VectorTypesTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/VerifierTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/VPIntrinsicTest.cpp.o unittests/IR/CMakeFiles/IRTests.dir/CoreBindings.cpp.o -o unittests/IR/IRTests  lib/libLLVMAnalysis.a  lib/libLLVMAsmParser.a  lib/libLLVMCore.a  lib/libLLVMSupport.a  lib/libLLVMPasses.a  lib/libLLVMTargetParser.a  lib/libLLVMTransformUtils.a  lib/libLLVMScalarOpts.a  lib/libLLVMSupport.a  lib/libllvm_gtest_main.a  lib/libllvm_gtest.a  lib/libLLVMTestingSupport.a  lib/libLLVMCodeGen.a  lib/libLLVMCodeGenTypes.a  lib/libLLVMCoroutines.a  lib/libLLVMHipStdPar.a  lib/libLLVMipo.a  lib/libLLVMBitWriter.a  lib/libLLVMFrontendOpenMP.a  lib/libLLVMScalarOpts.a  lib/libLLVMAggressiveInstCombine.a  lib/libLLVMFrontendOffloading.a  lib/libLLVMLinker.a  lib/libLLVMInstCombine.a  lib/libLLVMIRPrinter.a  lib/libLLVMObjCARCOpts.a  lib/libLLVMTarget.a  lib/libLLVMVectorize.a  lib/libLLVMInstrumentation.a  lib/libLLVMTransformUtils.a  lib/libLLVMAnalysis.a  lib/libLLVMProfileData.a  lib/libLLVMSymbolize.a  lib/libLLVMDebugInfoPDB.a  lib/libLLVMDebugInfoMSF.a  lib/libLLVMDebugInfoBTF.a  lib/libLLVMDebugInfoDWARF.a  lib/libLLVMObject.a  lib/libLLVMIRReader.a  lib/libLLVMAsmParser.a  lib/libLLVMBitReader.a  lib/libLLVMCore.a  lib/libLLVMRemarks.a  lib/libLLVMBitstreamReader.a  lib/libLLVMMCParser.a  lib/libLLVMTextAPI.a  lib/libLLVMMC.a  lib/libLLVMBinaryFormat.a  lib/libLLVMTargetParser.a  lib/libLLVMDebugInfoCodeView.a  lib/libllvm_gtest.a  lib/libLLVMSupport.a  lib/libLLVMDemangle.a  -lrt  -ldl  -lm  /usr/lib/x86_64-linux-gnu/libz.so  /usr/lib/x86_64-linux-gnu/libzstd.so  /usr/lib/x86_64-linux-gnu/libtinfo.so  -lpthread

/usr/bin/ld: lib/libLLVMInstrumentation.a(InstrProfiling.cpp.o): in function `llvm::InstrProfiling::lowerValueProfileInst(llvm::InstrProfValueProfileInst*) [clone .localalias]':
InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling21lowerValueProfileInstEPNS_25InstrProfValueProfileInstE+0x1a): undefined reference to `llvm::DebugInfoCorrelate'
/usr/bin/ld: InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling21lowerValueProfileInstEPNS_25InstrProfValueProfileInstE+0x2a): undefined reference to `llvm::ProfileCorrelate'
/usr/bin/ld: lib/libLLVMInstrumentation.a(InstrProfiling.cpp.o): in function `llvm::InstrProfiling::setupProfileSection(llvm::InstrProfInstBase*, llvm::InstrProfSectKind) [clone .localalias]':
InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling19setupProfileSectionEPNS_17InstrProfInstBaseENS_17InstrProfSectKindE+0x68): undefined reference to `llvm::DebugInfoCorrelate'
/usr/bin/ld: InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling19setupProfileSectionEPNS_17InstrProfInstBaseENS_17InstrProfSectKindE+0x243): undefined reference to `llvm::ProfileCorrelate'
/usr/bin/ld: lib/libLLVMInstrumentation.a(InstrProfiling.cpp.o): in function `llvm::InstrProfiling::getOrCreateRegionCounters(llvm::InstrProfCntrInstBase*) [clone .localalias]':
InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling25getOrCreateRegionCountersEPNS_21InstrProfCntrInstBaseE+0x12a): undefined reference to `llvm::DebugInfoCorrelate'
/usr/bin/ld: InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling25getOrCreateRegionCountersEPNS_21InstrProfCntrInstBaseE+0x13a): undefined reference to `llvm::ProfileCorrelate'
/usr/bin/ld: lib/libLLVMInstrumentation.a(InstrProfiling.cpp.o): in function `llvm::InstrProfiling::run(llvm::Module&, std::function<llvm::TargetLibraryInfo const& (llvm::Function&)>) [clone .localalias]':
InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling3runERNS_6ModuleESt8functionIFRKNS_17TargetLibraryInfoERNS_8FunctionEEE+0x486): undefined reference to `llvm::DebugInfoCorrelate'
/usr/bin/ld: InstrProfiling.cpp:(.text._ZN4llvm14InstrProfiling3runERNS_6ModuleESt8functionIFRKNS_17TargetLibraryInfoERNS_8FunctionEEE+0x49b): undefined reference to `llvm::ProfileCorrelate'
/usr/bin/ld: lib/libLLVMInstrumentation.a(PGOInstrumentation.cpp.o): in function `createIRLevelProfileFlagVar(llvm::Module&, bool)':
PGOInstrumentation.cpp:(.text._ZL27createIRLevelProfileFlagVarRN4llvm6ModuleEb+0x5f): undefined reference to `llvm::DebugInfoCorrelate'
/usr/bin/ld: PGOInstrumentation.cpp:(.text._ZL27createIRLevelProfileFlagVarRN4llvm6ModuleEb+0x73): undefined reference to `llvm::ProfileCorrelate'
collect2: error: ld returned 1 exit status

Note that we have File: lib/libLLVMCodeGen.a(TargetLoweringObjectFileImpl.cpp.o) where llvm::ProfileCorrelate and llvm::DebugInfoCorrelate are defined and File: lib/libLLVMInstrumentation.a(InstrProfiling.cpp.o) where those symbols are referenced so they should be able to linked together. And if I switch to clang and use -fuse-ld=lld removing -fno-lifetime-dse, this links successfully. So, it is a ld problem causing the linking error, but I have to work around this.

I'll just simply remove the flag -profile-correlate and keep -debug-info-correlate.

ZequanWu added a commit that referenced this pull request Nov 1, 2023
Refactor some code from #69493.

#70712 was reverted due to linking failures. So, `-debug-info-correlate` remains unchanged and no new flag added.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen clang Clang issues not falling into any other category compiler-rt llvm:binary-utilities llvm:transforms PGO Profile Guided Optimizations
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants