Skip to content

Commit 87e7140

Browse files
[nfc][InstrProfiling]For comdat setting helper function, move comment closer to the code (#83757)
1 parent bd7bce2 commit 87e7140

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

llvm/lib/Transforms/Instrumentation/InstrProfiling.cpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ class InstrLowerer final {
208208
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
209209

210210
const bool DataReferencedByCode;
211+
211212
struct PerFunctionProfileData {
212213
uint32_t NumValueSites[IPVK_Last + 1] = {};
213214
GlobalVariable *RegionCounters = nullptr;
@@ -1193,18 +1194,41 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
11931194
}
11941195

11951196
void InstrLowerer::maybeSetComdat(GlobalVariable *GV, Function *Fn,
1196-
StringRef VarName) {
1197+
StringRef CounterGroupName) {
1198+
// Place lowered global variables in a comdat group if the associated function
1199+
// is a COMDAT. This will make sure that only one copy of global variable
1200+
// (e.g. function counters) of the COMDAT function will be emitted after
1201+
// linking.
11971202
bool NeedComdat = needsComdatForCounter(*Fn, M);
11981203
bool UseComdat = (NeedComdat || TT.isOSBinFormatELF());
11991204

12001205
if (!UseComdat)
12011206
return;
12021207

1203-
StringRef GroupName =
1204-
TT.isOSBinFormatCOFF() && DataReferencedByCode ? GV->getName() : VarName;
1208+
// Keep in mind that this pass may run before the inliner, so we need to
1209+
// create a new comdat group (for counters, profiling data, etc). If we use
1210+
// the comdat of the parent function, that will result in relocations against
1211+
// discarded sections.
1212+
//
1213+
// If the data variable is referenced by code, non-counter variables (notably
1214+
// profiling data) and counters have to be in different comdats for COFF
1215+
// because the Visual C++ linker will report duplicate symbol errors if there
1216+
// are multiple external symbols with the same name marked
1217+
// IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1218+
StringRef GroupName = TT.isOSBinFormatCOFF() && DataReferencedByCode
1219+
? GV->getName()
1220+
: CounterGroupName;
12051221
Comdat *C = M.getOrInsertComdat(GroupName);
1206-
if (!NeedComdat)
1222+
1223+
if (!NeedComdat) {
1224+
// Object file format must be ELF since `UseComdat && !NeedComdat` is true.
1225+
//
1226+
// For ELF, when not using COMDAT, put counters, data and values into a
1227+
// nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1228+
// allows -z start-stop-gc to discard the entire group when the function is
1229+
// discarded.
12071230
C->setSelectionKind(Comdat::NoDeduplicate);
1231+
}
12081232
GV->setComdat(C);
12091233
// COFF doesn't allow the comdat group leader to have private linkage, so
12101234
// upgrade private linkage to internal linkage to produce a symbol table
@@ -1238,23 +1262,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
12381262
Linkage = GlobalValue::PrivateLinkage;
12391263
Visibility = GlobalValue::DefaultVisibility;
12401264
}
1241-
// Move the name variable to the right section. Place them in a COMDAT group
1242-
// if the associated function is a COMDAT. This will make sure that only one
1243-
// copy of counters of the COMDAT function will be emitted after linking. Keep
1244-
// in mind that this pass may run before the inliner, so we need to create a
1245-
// new comdat group for the counters and profiling data. If we use the comdat
1246-
// of the parent function, that will result in relocations against discarded
1247-
// sections.
1248-
//
1249-
// If the data variable is referenced by code, counters and data have to be
1250-
// in different comdats for COFF because the Visual C++ linker will report
1251-
// duplicate symbol errors if there are multiple external symbols with the
1252-
// same name marked IMAGE_COMDAT_SELECT_ASSOCIATIVE.
1253-
//
1254-
// For ELF, when not using COMDAT, put counters, data and values into a
1255-
// nodeduplicate COMDAT which is lowered to a zero-flag section group. This
1256-
// allows -z start-stop-gc to discard the entire group when the function is
1257-
// discarded.
1265+
// Move the name variable to the right section.
12581266
bool Renamed;
12591267
GlobalVariable *Ptr;
12601268
StringRef VarPrefix;

0 commit comments

Comments
 (0)