[PATCH] D32688: [Coverage] Comdat section name should be same as the variable name in COFF format

Adam Folwarczny via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 30 14:47:57 PDT 2017


adamf created this revision.

COFF format requires to have Comdat section names same as the variable name.
For Instruction Profile Data Variable the Comdat name wasn't equal to the variable name. 
It caused linker error like:  "error LNK2005: ___profd_?compute@@YAHH at Z already defined in Source.obj" e.g. for functions included into multiple cpp files.


https://reviews.llvm.org/D32688

Files:
  lib/Transforms/Instrumentation/InstrProfiling.cpp


Index: lib/Transforms/Instrumentation/InstrProfiling.cpp
===================================================================
--- lib/Transforms/Instrumentation/InstrProfiling.cpp
+++ lib/Transforms/Instrumentation/InstrProfiling.cpp
@@ -362,7 +362,8 @@
 }
 
 static inline Comdat *getOrCreateProfileComdat(Module &M, Function &F,
-                                               InstrProfIncrementInst *Inc) {
+                                               InstrProfIncrementInst *Inc,
+                                               StringRef VarName) {
   if (!needsComdatForCounter(F, M))
     return nullptr;
 
@@ -370,10 +371,10 @@
   // name. The linker targeting COFF also requires that the COMDAT
   // a section is associated to must precede the associating section. For this
   // reason, we must choose the counter var's name as the name of the comdat.
-  StringRef ComdatPrefix = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
-                                ? getInstrProfCountersVarPrefix()
-                                : getInstrProfComdatPrefix());
-  return M.getOrInsertComdat(StringRef(getVarName(Inc, ComdatPrefix)));
+  StringRef ComdatName = (Triple(M.getTargetTriple()).isOSBinFormatCOFF()
+                                ? VarName
+                                : getVarName(Inc, getInstrProfComdatPrefix()));
+  return M.getOrInsertComdat(ComdatName);
 }
 
 static bool needsRuntimeRegistrationOfSectionRange(const Module &M) {
@@ -406,9 +407,6 @@
   // only one copy of counters of the COMDAT function will be emitted after
   // linking.
   Function *Fn = Inc->getParent()->getParent();
-  Comdat *ProfileVarsComdat = nullptr;
-  ProfileVarsComdat = getOrCreateProfileComdat(*M, *Fn, Inc);
-
   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
   LLVMContext &Ctx = M->getContext();
   ArrayType *CounterTy = ArrayType::get(Type::getInt64Ty(Ctx), NumCounters);
@@ -422,7 +420,8 @@
   CounterPtr->setSection(
       getInstrProfSectionName(IPSK_cnts, TT.getObjectFormat()));
   CounterPtr->setAlignment(8);
-  CounterPtr->setComdat(ProfileVarsComdat);
+  CounterPtr->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc,
+                                                 CounterPtr->getName()));
 
   auto *Int8PtrTy = Type::getInt8PtrTy(Ctx);
   // Allocate statically the array of pointers to value profile nodes for
@@ -443,7 +442,8 @@
       ValuesVar->setSection(
           getInstrProfSectionName(IPSK_vals, TT.getObjectFormat()));
       ValuesVar->setAlignment(8);
-      ValuesVar->setComdat(ProfileVarsComdat);
+      ValuesVar->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc,
+                                                    ValuesVar->getName()));
       ValuesPtrExpr =
           ConstantExpr::getBitCast(ValuesVar, Type::getInt8PtrTy(Ctx));
     }
@@ -476,7 +476,7 @@
   Data->setVisibility(NamePtr->getVisibility());
   Data->setSection(getInstrProfSectionName(IPSK_data, TT.getObjectFormat()));
   Data->setAlignment(INSTR_PROF_DATA_ALIGNMENT);
-  Data->setComdat(ProfileVarsComdat);
+  Data->setComdat(getOrCreateProfileComdat(*M, *Fn, Inc, Data->getName()));
 
   PD.RegionCounters = CounterPtr;
   PD.DataVar = Data;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32688.97244.patch
Type: text/x-patch
Size: 3187 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170430/216d7ccc/attachment.bin>


More information about the llvm-commits mailing list