[PATCH] D12248: Put profile variables of COMDAT functions to it's own COMDAT group

Rong Xu via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 11:44:44 PDT 2015


xur created this revision.
xur added reviewers: bogner, dnovillo.
xur added a subscriber: llvm-commits.

In -fprofile-instr-generate compilation, to remove the redundant profile variables for the COMDAT functions, these variables are placed in the same COMDAT group as its associated function.
This way when the COMDAT function is not picked by the linker, those profile variables will also not be output in the final binary.

One problem here is that we lose the ability to link the objects built w and w/o -fprofile-instr-generate. The linker may choose one version of COMDAT that not expanded (i.e. not built with -fprofile-instr-generate). All the references to the variables
become dangling. The result binary will segfalut.  If ld.gold is used, we usually see a warning like:
foo.o:foo.bc:function __llvm_profile_register_functions: warning: relocation refers to discarded section

This patch puts the profile variables for COMDAT functions to its
own COMDAT group to avoid the problem.

http://reviews.llvm.org/D12248

Files:
  InstrProfiling.cpp

Index: InstrProfiling.cpp
===================================================================
--- InstrProfiling.cpp
+++ InstrProfiling.cpp
@@ -196,13 +196,17 @@
   if (It != RegionCounters.end())
     return It->second;
 
-  // Move the name variable to the right section. Make sure it is placed in the
-  // same comdat as its associated function. Otherwise, we may get multiple
-  // counters for the same function in certain cases.
+  // Move the name variable to the right section. Make them in a COMDAT group
+  // if the associated function is a COMDAT. This will make sure that
+  // only one copy of counters of the COMDAT function will be emitted after
+  // linking.
   Function *Fn = Inc->getParent()->getParent();
+  Comdat *ProfileVarsComdat = nullptr;
+  if (Fn->hasComdat())
+    ProfileVarsComdat = M->getOrInsertComdat(StringRef(getVarName(Inc, "var")));
   Name->setSection(getNameSection());
   Name->setAlignment(1);
-  Name->setComdat(Fn->getComdat());
+  Name->setComdat(ProfileVarsComdat);
 
   uint64_t NumCounters = Inc->getNumCounters()->getZExtValue();
   LLVMContext &Ctx = M->getContext();
@@ -215,7 +219,7 @@
   Counters->setVisibility(Name->getVisibility());
   Counters->setSection(getCountersSection());
   Counters->setAlignment(8);
-  Counters->setComdat(Fn->getComdat());
+  Counters->setComdat(ProfileVarsComdat);
 
   RegionCounters[Inc->getName()] = Counters;
 
@@ -240,7 +244,7 @@
   Data->setVisibility(Name->getVisibility());
   Data->setSection(getDataSection());
   Data->setAlignment(8);
-  Data->setComdat(Fn->getComdat());
+  Data->setComdat(ProfileVarsComdat);
 
   // Mark the data variable as used so that it isn't stripped out.
   UsedVars.push_back(Data);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12248.32850.patch
Type: text/x-patch
Size: 1709 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20150821/8de9c955/attachment.bin>


More information about the llvm-commits mailing list