@@ -208,6 +208,7 @@ class InstrLowerer final {
208
208
std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
209
209
210
210
const bool DataReferencedByCode;
211
+
211
212
struct PerFunctionProfileData {
212
213
uint32_t NumValueSites[IPVK_Last + 1 ] = {};
213
214
GlobalVariable *RegionCounters = nullptr ;
@@ -1193,18 +1194,41 @@ static bool needsRuntimeRegistrationOfSectionRange(const Triple &TT) {
1193
1194
}
1194
1195
1195
1196
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.
1197
1202
bool NeedComdat = needsComdatForCounter (*Fn, M);
1198
1203
bool UseComdat = (NeedComdat || TT.isOSBinFormatELF ());
1199
1204
1200
1205
if (!UseComdat)
1201
1206
return ;
1202
1207
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;
1205
1221
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.
1207
1230
C->setSelectionKind (Comdat::NoDeduplicate);
1231
+ }
1208
1232
GV->setComdat (C);
1209
1233
// COFF doesn't allow the comdat group leader to have private linkage, so
1210
1234
// upgrade private linkage to internal linkage to produce a symbol table
@@ -1238,23 +1262,7 @@ GlobalVariable *InstrLowerer::setupProfileSection(InstrProfInstBase *Inc,
1238
1262
Linkage = GlobalValue::PrivateLinkage;
1239
1263
Visibility = GlobalValue::DefaultVisibility;
1240
1264
}
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.
1258
1266
bool Renamed;
1259
1267
GlobalVariable *Ptr;
1260
1268
StringRef VarPrefix;
0 commit comments