[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-11-02 Thread Zequan Wu via cfe-commits


@@ -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();

ZequanWu wrote:

Renamed to `__llvm_profile_has_correlation` at 
https://github.com/llvm/llvm-project/commit/7fa9930847bbef4319c2d2e9c782eb5c8e6b1892

https://github.com/llvm/llvm-project/pull/70856
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-11-02 Thread Zequan Wu via cfe-commits

ZequanWu wrote:

> > Sounds fine to me, but I guess I don't understand why `-profile-correlate=` 
> > doesn't work. Do you still plan to add the flag later?
> 
> I haven't found a way to share information (whether of not binary correlation 
> is enabled) between CodeGen(TargetLoweringObjectFileImpl.cpp) and 
> Instrumentation(InstrProfiling.cpp) components. The explanation is here: 
> https://discourse.llvm.org/t/rfc-add-binary-profile-correlation-to-not-load-profile-metadata-sections-into-memory-at-runtime/74565#use-temporary-section-names-6.

Oh, actually, we can have -profile-correlate flag just need to define it at 
InstrProfCorrelator.cpp (ProfileData component), which is less desired but 
working as discussed at 
https://github.com/llvm/llvm-project/pull/69656#discussion_r1372179620. 

https://github.com/llvm/llvm-project/pull/70856
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-11-02 Thread Petr Hosek via cfe-commits


@@ -89,3 +89,7 @@ COMPILER_RT_VISIBILITY void 
__llvm_profile_reset_counters(void) {
   }
   lprofSetProfileDumped(0);
 }
+
+inline int hasCorrelation() {

petrhosek wrote:

The function should have a hidden visibility (the `COMPILER_RT_VISIBILITY` 
macro) so it's hidden from ABI.

https://github.com/llvm/llvm-project/pull/70856
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-11-02 Thread Petr Hosek via cfe-commits


@@ -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();

petrhosek wrote:

The name of this function doesn't follow the existing convention, it should be 
named `__llvm_profile_has_correlation`.

https://github.com/llvm/llvm-project/pull/70856
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-10-31 Thread Zequan Wu via cfe-commits

ZequanWu wrote:

Sorry for so many noise regarding this change.

https://github.com/llvm/llvm-project/pull/70856
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [llvm] [compiler-rt] [Profile] Refactor profile correlation. (PR #70856)

2023-10-31 Thread Zequan Wu via cfe-commits

https://github.com/ZequanWu created 
https://github.com/llvm/llvm-project/pull/70856

Refactor some code from https://github.com/llvm/llvm-project/pull/69493.

#70712 was reverted due to linking failures. So, I removed 
`-profile-correlate=` flag and kept `-debug-info-correlate` in this change. 

>From e33d4e7d8324e8efb6f0c0ff5f3426a2e0a51ee1 Mon Sep 17 00:00:00 2001
From: Zequan Wu 
Date: Mon, 30 Oct 2023 15:45:08 -0400
Subject: [PATCH 1/4] Refactor profile correlation.

---
 clang/lib/CodeGen/BackendUtil.cpp |  14 ++-
 compiler-rt/lib/profile/InstrProfiling.c  |   4 +
 compiler-rt/lib/profile/InstrProfiling.h  |   6 ++
 .../lib/profile/InstrProfilingBuffer.c|  11 ++
 compiler-rt/lib/profile/InstrProfilingMerge.c |  11 +-
 .../lib/profile/InstrProfilingWriter.c|  21 ++--
 .../Darwin/instrprof-debug-info-correlate.c   |   4 +-
 .../instrprof-debug-info-correlate-warnings.c |   2 +-
 .../Linux/instrprof-debug-info-correlate.c|   6 +-
 .../instrprof-show-debug-info-correlation.c   |   6 +-
 llvm/docs/CommandGuide/llvm-profdata.rst  |   4 +-
 .../llvm/ProfileData/InstrProfCorrelator.h|  13 ++-
 .../llvm/ProfileData/InstrProfReader.h|   2 +
 .../Instrumentation/PGOInstrumentation.h  |   2 -
 .../CodeGen/TargetLoweringObjectFileImpl.cpp  |  19 
 llvm/lib/ProfileData/InstrProfCorrelator.cpp  | 100 +++---
 llvm/lib/ProfileData/InstrProfReader.cpp  |   4 +-
 .../Instrumentation/InstrProfiling.cpp|  18 ++--
 .../Instrumentation/PGOInstrumentation.cpp|   6 +-
 .../debug-info-correlate-coverage.ll  |   2 +-
 .../InstrProfiling/debug-info-correlate.ll|   2 +-
 llvm/tools/llvm-profdata/llvm-profdata.cpp|   9 +-
 22 files changed, 176 insertions(+), 90 deletions(-)

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 PrintPipelinePasses;
 static cl::opt ClSanitizeOnOptimizerEarlyEP(
 "sanitizer-early-opt-ep", cl::Optional,
 cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
-}
+
+extern cl::opt DebugInfoCorrelate;
+extern cl::opt 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 OS);
 };
-}
+} // namespace
 
 static SanitizerCoverageOptions
 getSancovOptsFromCGOpts(const CodeGenOptions ) {
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