[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-30 Thread Ellis Hoag via cfe-commits

https://github.com/ellishg approved this pull request.


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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-26 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 6ddc03d97c0de96691ed27f05c9b30869051ce06 
39cb3204ad4eea8c8b91ad1225e8dd681156237a -- clang/lib/CodeGen/BackendUtil.cpp 
compiler-rt/lib/profile/InstrProfiling.c 
compiler-rt/lib/profile/InstrProfiling.h 
compiler-rt/lib/profile/InstrProfilingBuffer.c 
compiler-rt/lib/profile/InstrProfilingMerge.c 
compiler-rt/lib/profile/InstrProfilingWriter.c 
compiler-rt/test/profile/Darwin/instrprof-debug-info-correlate.c 
compiler-rt/test/profile/Linux/instrprof-debug-info-correlate-warnings.c 
compiler-rt/test/profile/Linux/instrprof-debug-info-correlate.c 
compiler-rt/test/profile/Linux/instrprof-show-debug-info-correlation.c 
llvm/include/llvm/ProfileData/InstrProfCorrelator.h 
llvm/include/llvm/ProfileData/InstrProfReader.h 
llvm/include/llvm/Transforms/Instrumentation/PGOInstrumentation.h 
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp 
llvm/lib/ProfileData/InstrProfCorrelator.cpp 
llvm/lib/ProfileData/InstrProfReader.cpp 
llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp 
llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp 
llvm/tools/llvm-profdata/llvm-profdata.cpp
``





View the diff from clang-format here.


``diff
diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp 
b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
index a5110829a1c5..7c49bbaa7045 100644
--- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -964,8 +964,8 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfInstBase 
*Inc) {
   DebugInfoCorrelate || ProfileCorrelate == 
InstrProfCorrelator::DEBUG_INFO;
   // Use internal rather than private linkage so the counter variable shows up
   // in the symbol table when using debug info for correlation.
-  if (EnableDebugCorrelate &&
-  TT.isOSBinFormatMachO() && Linkage == GlobalValue::PrivateLinkage)
+  if (EnableDebugCorrelate && TT.isOSBinFormatMachO() &&
+  Linkage == GlobalValue::PrivateLinkage)
 Linkage = GlobalValue::InternalLinkage;
 
   // Due to the limitation of binder as of 2021/09/28, the duplicate weak

``




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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ZequanWu wrote:

> I guess the root of the problem is that `Instrumentation` depends on 
> `ProfileData`, so we can't have `InstrProfCorrelator.cpp` use a symbol 
> defined in `Instrumentation`. 

Probably I didn't make it clear. We can keep `-debug-info-correlate` in 
`InstrProfiling.cpp`. 

In my 
[PR](https://github.com/llvm/llvm-project/pull/69493/files#diff-db16d50e8b3efb345f2adca0c4071e8bc1e06dfea64386454586a436d887ecefR484),
 I added a check for `-profile-correlate=binary` in 
`TargetLoweringObjectFileImpl.cpp`: , to determine if we should mark profile 
name & data sections as strippable, which requires `-profile-correlate` flag 
being defined in `TargetLoweringObjectFileImpl.cpp` in order to linked 
successfully with other targets that doesn't use `InstrProfiling.cpp`. That 
makes two flags separated in two files, so I moved them together to 
`InstrProfCorrelator.cpp`.

> That is unfortunate because `-debug-info-correlate` 
> (`-profile-correlate=debug-info`) is a codegen flag while 
> `InstrProfCorrelator.cpp` is primarily used by `llvm-prodata` which will not 
> use `-debug-info-correlate`.
> I really think `-debug-info-correlate` does not belong in 
> `InstrProfCorrelator.cpp`. I'm fine with keeping 
> `InstrProfCorrelator::ProfCorrelatorKind` in this file, but the the flags 
> should stay in `InstrProfiling.cpp` and if needed we can pass 
> `ProfCorrelatorKind` as a parameter.

I didn't thought about that before. I agree it makes less sense to put codegen 
flags in `InstrProfCorrelator.cpp`.  I think this can be avoided by moving this 
flag to llvm/lib/CodeGen/CommandFlags.cpp. I'll take a look later.



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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-25 Thread Ellis Hoag via cfe-commits


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ellishg wrote:

I guess the root of the problem is that `Instrumentation` depends on 
`ProfileData`, so we can't have `InstrProfCorrelator.cpp` use a symbol defined 
in `Instrumentation`. That is unfortunate because `-debug-info-correlate` 
(`-profile-correlate=debug-info`) is a codegen flag while 
`InstrProfCorrelator.cpp` is primarily used by `llvm-prodata` which will not 
use `-debug-info-correlate`.

I really think `-debug-info-correlate` does not belong in 
`InstrProfCorrelator.cpp`. I'm fine with keeping 
`InstrProfCorrelator::ProfCorrelatorKind` in this file, but the the flags 
should stay in `InstrProfiling.cpp` and if needed we can pass 
`ProfCorrelatorKind` as a parameter.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ZequanWu wrote:

> so keeping the option in InstrProfCorrelator.cpp works fine without linker 
> error? That works for me.
Yes, PowerPCTests will linked successfully.

> What if we move it to `PGOInstrumentation.cpp`?
It still doesn't work. I guess the flags needs to be in one of those files: 
https://github.com/llvm/llvm-project/blob/7e34ee5291b935a1e37cd1b132552187d5c784d0/llvm/lib/ProfileData/CMakeLists.txt#L1


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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-25 Thread David Li via cfe-commits


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

david-xl wrote:

so  keeping the option in InstrProfCorrelator.cpp works fine without linker 
error? That works for me.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ZequanWu wrote:

Using extern decl of `ProfileCorrelate` in `TargetLoweringObjectFileImpl.cpp` 
doesn't work for binary correlation when linking 
`unittests/Target/PowerPC/PowerPCTests` because `InstrProfiling.o` which 
contains the definition is not linked for PowerPCTests.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-25 Thread David Li via cfe-commits


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

david-xl wrote:

Use extern decl and add some comments.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ZequanWu wrote:

Because `-profile-correlate=binary` (though not added in this patch yet) will 
be used by `TargetLoweringObjectFileImpl.cpp`, if keeping those flags 
definitions in `InstrProfiling.cpp`, I got a linker error:
```
ld.lld: error: undefined symbol: llvm::ProfileCorrelate
>>> referenced by TargetLoweringObjectFileImpl.cpp
>>>   
>>> TargetLoweringObjectFileImpl.cpp.o:(selectExplicitSectionGlobal(llvm::GlobalObject
>>>  const*, llvm::SectionKind, llvm::TargetMachine const&, llvm::MCContext&, 
>>> llvm::Mangler&, unsigned int&, bool, bool)) in archive lib/libLLVMCodeGen.a
```

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

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


@@ -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"

ZequanWu wrote:

It's not added. It's reordered by git-clang-format.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-24 Thread Ellis Hoag via cfe-commits


@@ -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"

ellishg wrote:

Is this include used?

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-24 Thread Ellis Hoag via cfe-commits


@@ -24,15 +24,38 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt DebugInfoCorrelate(
+"debug-info-correlate",
+cl::desc("Use debug info to correlate profiles (Deprecated). Use "
+ "-profile-correlate=debug-info instead."),
+cl::init(false));
+
+cl::opt ProfileCorrelate(
+"profile-correlate",
+cl::desc("Use debug info or binary file to correlate profiles."),
+cl::init(InstrProfCorrelator::NONE),
+cl::values(clEnumValN(InstrProfCorrelator::NONE, "",
+  "No profile correlation"),
+   clEnumValN(InstrProfCorrelator::DEBUG_INFO, "debug-info",
+  "Use debug info to correlate")));

ellishg wrote:

This patch moves these options from `InstrProfiling.cpp` to 
`InstrProfCorrelator.cpp`, but this file doesn't actually use these flags. Is 
there any reason why we can't keep the flags in `InstrProfiling.cpp`?

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-23 Thread Ellis Hoag via cfe-commits


@@ -24,15 +24,37 @@
 
 using namespace llvm;
 
-/// Get the __llvm_prf_cnts section.
-Expected getCountersSection(const object::ObjectFile ) 
{
+namespace llvm {
+// Deprecated. Use -profile-correlate=debug-info.
+cl::opt
+DebugInfoCorrelate("profile-correlate=debug-info",

ellishg wrote:

```suggestion
DebugInfoCorrelate("debug-info-correlate",
```
I think you meant to keep `-debug-info-correlate` unchanged. Also, please add 
the deprecated comment to the help message.

Maybe after the next LLVM release we can remove this flag. This will allow us 
and others to easily migrate to the new flag.

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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-19 Thread David Li via cfe-commits

https://github.com/david-xl approved this pull request.


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


[clang] [Profile] Refactor profile correlation. (PR #69656)

2023-10-19 Thread via cfe-commits

llvmbot wrote:




@llvm/pr-subscribers-clang-codegen

Author: Zequan Wu (ZequanWu)


Changes

Refactor some code from #69493. 

---

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


21 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/ProfileData/InstrProfCorrelator.cpp (+80-38) 
- (modified) llvm/lib/ProfileData/InstrProfReader.cpp (+2-2) 
- (modified) llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp (+10-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
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 0dd5ff5ae6331cb..e3bb8329216363e 100644
--- a/compiler-rt/lib/profile/InstrProfiling.c
+++ b/compiler-rt/lib/profile/InstrProfiling.c
@@ -85,3 +85,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 4433d7bd48871fc..c3203d51d5ca75c 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -259,6 +259,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 

[clang] [Profile] Refactor profile correlation. (PR #69656)

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

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

Refactor some code from #69493. 

>From 89f041c62da8c0924ee45a6ed8d9ab7afb560935 Mon Sep 17 00:00:00 2001
From: Zequan Wu 
Date: Thu, 19 Oct 2023 18:55:08 -0400
Subject: [PATCH] [Profile] 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 -
 llvm/lib/ProfileData/InstrProfCorrelator.cpp  | 118 --
 llvm/lib/ProfileData/InstrProfReader.cpp  |   4 +-
 .../Instrumentation/InstrProfiling.cpp|  19 +--
 .../Instrumentation/PGOInstrumentation.cpp|   6 +-
 .../debug-info-correlate-coverage.ll  |   2 +-
 .../InstrProfiling/debug-info-correlate.ll|   2 +-
 llvm/tools/llvm-profdata/llvm-profdata.cpp|   9 +-
 21 files changed, 175 insertions(+), 91 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 0dd5ff5ae6331cb..e3bb8329216363e 100644
--- a/compiler-rt/lib/profile/InstrProfiling.c
+++ b/compiler-rt/lib/profile/InstrProfiling.c
@@ -85,3 +85,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 4433d7bd48871fc..c3203d51d5ca75c 100644
--- a/compiler-rt/lib/profile/InstrProfiling.h
+++ b/compiler-rt/lib/profile/InstrProfiling.h
@@ -259,6 +259,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);
@@ -276,6 +279,9 @@ uint64_t __llvm_profile_get_num_counters(const char *Begin, 
const char *End);
 /*!