[PATCH] D105176: [InstrProfiling] Use external linkage for bias variable

Petr Hosek via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 30 00:23:38 PDT 2021


phosek created this revision.
phosek added reviewers: davidxl, vsk, mcgrathr.
Herald added a subscriber: hiraditya.
phosek requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We need this variable to override the weak symbol of the same name
inside the profile runtime, but using LinkOnceODRLinkage results in
weak symbol being emitted which leads to an issue where the linker
might choose either of the weak symbols potentially disabling the
runtime counter relocation. This change modified the variable to use
external linkage to ensure that a strong symbol is emitted.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D105176

Files:
  llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
  llvm/test/Instrumentation/InstrProfiling/runtime-counter-relocation.ll


Index: llvm/test/Instrumentation/InstrProfiling/runtime-counter-relocation.ll
===================================================================
--- llvm/test/Instrumentation/InstrProfiling/runtime-counter-relocation.ll
+++ llvm/test/Instrumentation/InstrProfiling/runtime-counter-relocation.ll
@@ -4,7 +4,8 @@
 target triple = "x86_64-unknown-linux-gnu"
 
 @__profn_foo = private constant [3 x i8] c"foo"
-; RELOC: @__llvm_profile_counter_bias = linkonce_odr hidden global i64 0
+; RELOC: $__llvm_profile_counter_bias = comdat any
+; RELOC: @__llvm_profile_counter_bias = hidden global i64 0, comdat
 
 ; CHECK-LABEL: define void @foo
 ; CHECK-NEXT: %pgocount = load i64, i64* getelementptr inbounds ([1 x i64], [1 x i64]* @__profc_foo, i64 0, i64 0)
Index: llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -690,10 +690,12 @@
       Type *Int64Ty = Type::getInt64Ty(M->getContext());
       GlobalVariable *Bias = M->getGlobalVariable(getInstrProfCounterBiasVarName());
       if (!Bias) {
-        Bias = new GlobalVariable(*M, Int64Ty, false, GlobalValue::LinkOnceODRLinkage,
+        Bias = new GlobalVariable(*M, Int64Ty, false, GlobalValue::ExternalLinkage,
                                   Constant::getNullValue(Int64Ty),
                                   getInstrProfCounterBiasVarName());
         Bias->setVisibility(GlobalVariable::HiddenVisibility);
+        if (TT.supportsCOMDAT())
+            Bias->setComdat(M->getOrInsertComdat(Bias->getName()));
       }
       LI = Builder.CreateLoad(Int64Ty, Bias);
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105176.355457.patch
Type: text/x-patch
Size: 1728 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210630/cfb11cc3/attachment.bin>


More information about the llvm-commits mailing list