From 5798be84580be233e4cf34c08ceec8f79e80502e Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 17 Jun 2021 23:38:17 -0700 Subject: [PATCH] Revert D103717 "[InstrProfiling] Make __profd_ unconditionally private for ELF" This reverts commit 76d0747e0807307780ba84cbd7e5c80b20c26bd7. If a group has `__llvm_prf_vals` due to static value profiler counters (`NS!=0`), we cannot make `__llvm_prf_data` private, because a prevailing text section may reference `__llvm_prf_data` and will cause a `relocation refers to a discarded section` linker error. Note: while a `__profc_` group is non-prevailing, it may be referenced by a prevailing text section due to inlining. ``` group section [ 66] `.group' [__profc__ZN5clang20EmitClangDeclContextERN4llvm12RecordKeeperERNS0_11raw_ostreamE] contains 4 sections: [Index] Name [ 67] __llvm_prf_cnts [ 68] __llvm_prf_vals [ 69] __llvm_prf_data [ 70] .rela__llvm_prf_data ``` --- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp | 10 ++++------ .../Transforms/PGOProfile/indirect_call_profile.ll | 2 +- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp index 6b5ca896e358..4a5511d375fd 100644 --- a/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -929,12 +929,10 @@ InstrProfiling::getOrCreateRegionCounters(InstrProfIncrementInst *Inc) { #define INSTR_PROF_DATA(Type, LLVMType, Name, Init) Init, #include "llvm/ProfileData/InstrProfData.inc" }; - // If linker GC cannot discard data variables while the text section is - // retained, data variables can be private. This optimization applies on ELF. - // On COFF, when DataReferencedByCode is false, __profd_ is never a comdat - // leader, this is applicable as well. - if (TT.isOSBinFormatELF() || - (!DataReferencedByCode && TT.isOSBinFormatCOFF())) { + // If code never references data variables (the symbol is unneeded), and + // linker GC cannot discard data variables while the text section is retained, + // data variables can be private. This optimization applies on COFF and ELF. + if (!DataReferencedByCode && !TT.isOSBinFormatMachO()) { Linkage = GlobalValue::PrivateLinkage; Visibility = GlobalValue::DefaultVisibility; } diff --git a/llvm/test/Transforms/PGOProfile/indirect_call_profile.ll b/llvm/test/Transforms/PGOProfile/indirect_call_profile.ll index 0fa6c5258b55..a3c05e5b4701 100644 --- a/llvm/test/Transforms/PGOProfile/indirect_call_profile.ll +++ b/llvm/test/Transforms/PGOProfile/indirect_call_profile.ll @@ -54,7 +54,7 @@ bb11: ; preds = %bb2 } ; Test that comdat function's address is recorded. -; LOWER: @__profd_foo3.[[FOO3_HASH:[0-9]+]] = private {{.*}} @__profc_foo3.[[FOO3_HASH]] +; LOWER: @__profd_foo3.[[FOO3_HASH:[0-9]+]] = linkonce_odr{{.*}}@__profc_foo3.[[FOO3_HASH]] ; Function Attrs: nounwind uwtable define linkonce_odr i32 @foo3() comdat { ret i32 1