@@ -265,8 +265,8 @@ static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
265
265
return PathNameStr.substr (LastPos);
266
266
}
267
267
268
- static StringRef getStrippedSourceFileName (const Function &F ) {
269
- StringRef FileName (F .getParent ()->getSourceFileName ());
268
+ static StringRef getStrippedSourceFileName (const GlobalObject &GO ) {
269
+ StringRef FileName (GO .getParent ()->getSourceFileName ());
270
270
uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t )-1 ;
271
271
if (StripLevel < StaticFuncStripDirNamePrefix)
272
272
StripLevel = StaticFuncStripDirNamePrefix;
@@ -289,64 +289,75 @@ static StringRef getStrippedSourceFileName(const Function &F) {
289
289
// mangled, they cannot be passed to Mach-O linkers via -order_file. We still
290
290
// need to compute this name to lookup functions from profiles built by older
291
291
// compilers.
292
- static std::string getIRPGOFuncName (const Function &F,
293
- GlobalValue::LinkageTypes Linkage,
294
- StringRef FileName) {
292
+ static std::string
293
+ getIRPGONameForGlobalObject (const GlobalObject &GO,
294
+ GlobalValue::LinkageTypes Linkage,
295
+ StringRef FileName) {
295
296
SmallString<64 > Name;
296
297
if (llvm::GlobalValue::isLocalLinkage (Linkage)) {
297
298
Name.append (FileName.empty () ? " <unknown>" : FileName);
298
299
Name.append (" ;" );
299
300
}
300
- Mangler ().getNameWithPrefix (Name, &F , /* CannotUsePrivateLabel=*/ true );
301
+ Mangler ().getNameWithPrefix (Name, &GO , /* CannotUsePrivateLabel=*/ true );
301
302
return Name.str ().str ();
302
303
}
303
304
304
- static std::optional<std::string> lookupPGOFuncName ( const Function &F ) {
305
- if (MDNode * MD = getPGOFuncNameMetadata (F) ) {
305
+ static std::optional<std::string> lookupPGONameFromMetadata (MDNode *MD ) {
306
+ if (MD != nullptr ) {
306
307
StringRef S = cast<MDString>(MD->getOperand (0 ))->getString ();
307
308
return S.str ();
308
309
}
309
310
return {};
310
311
}
311
312
312
- // See getPGOFuncName()
313
- std::string getIRPGOFuncName (const Function &F, bool InLTO) {
313
+ // Returns the PGO object name. This function has some special handling
314
+ // when called in LTO optimization. The following only applies when calling in
315
+ // LTO passes (when \c InLTO is true): LTO's internalization privatizes many
316
+ // global linkage symbols. This happens after value profile annotation, but
317
+ // those internal linkage functions should not have a source prefix.
318
+ // Additionally, for ThinLTO mode, exported internal functions are promoted
319
+ // and renamed. We need to ensure that the original internal PGO name is
320
+ // used when computing the GUID that is compared against the profiled GUIDs.
321
+ // To differentiate compiler generated internal symbols from original ones,
322
+ // PGOFuncName meta data are created and attached to the original internal
323
+ // symbols in the value profile annotation step
324
+ // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
325
+ // data, its original linkage must be non-internal.
326
+ static std::string getIRPGOObjectName (const GlobalObject &GO, bool InLTO,
327
+ MDNode *PGONameMetadata) {
314
328
if (!InLTO) {
315
- auto FileName = getStrippedSourceFileName (F );
316
- return getIRPGOFuncName (F, F .getLinkage (), FileName);
329
+ auto FileName = getStrippedSourceFileName (GO );
330
+ return getIRPGONameForGlobalObject (GO, GO .getLinkage (), FileName);
317
331
}
318
332
319
333
// In LTO mode (when InLTO is true), first check if there is a meta data.
320
- if (auto IRPGOFuncName = lookupPGOFuncName (F ))
334
+ if (auto IRPGOFuncName = lookupPGONameFromMetadata (PGONameMetadata ))
321
335
return *IRPGOFuncName;
322
336
323
337
// If there is no meta data, the function must be a global before the value
324
338
// profile annotation pass. Its current linkage may be internal if it is
325
339
// internalized in LTO mode.
326
- return getIRPGOFuncName (F , GlobalValue::ExternalLinkage, " " );
340
+ return getIRPGONameForGlobalObject (GO , GlobalValue::ExternalLinkage, " " );
327
341
}
328
342
329
- // Return the PGOFuncName. This function has some special handling when called
330
- // in LTO optimization. The following only applies when calling in LTO passes
331
- // (when \c InLTO is true): LTO's internalization privatizes many global linkage
332
- // symbols. This happens after value profile annotation, but those internal
333
- // linkage functions should not have a source prefix.
334
- // Additionally, for ThinLTO mode, exported internal functions are promoted
335
- // and renamed. We need to ensure that the original internal PGO name is
336
- // used when computing the GUID that is compared against the profiled GUIDs.
337
- // To differentiate compiler generated internal symbols from original ones,
338
- // PGOFuncName meta data are created and attached to the original internal
339
- // symbols in the value profile annotation step
340
- // (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
341
- // data, its original linkage must be non-internal.
343
+ // Returns the IRPGO function name and does special handling when called
344
+ // in LTO optimization. See the comments of `getIRPGOObjectName` for details.
345
+ std::string getIRPGOFuncName (const Function &F, bool InLTO) {
346
+ return getIRPGOObjectName (F, InLTO, getPGOFuncNameMetadata (F));
347
+ }
348
+
349
+ // This is similar to `getIRPGOFuncName` except that this function calls
350
+ // 'getPGOFuncName' to get a name and `getIRPGOFuncName` calls
351
+ // 'getIRPGONameForGlobalObject'. See the difference between two callees in the
352
+ // comments of `getIRPGONameForGlobalObject`.
342
353
std::string getPGOFuncName (const Function &F, bool InLTO, uint64_t Version) {
343
354
if (!InLTO) {
344
355
auto FileName = getStrippedSourceFileName (F);
345
356
return getPGOFuncName (F.getName (), F.getLinkage (), FileName, Version);
346
357
}
347
358
348
359
// In LTO mode (when InLTO is true), first check if there is a meta data.
349
- if (auto PGOFuncName = lookupPGOFuncName (F ))
360
+ if (auto PGOFuncName = lookupPGONameFromMetadata ( getPGOFuncNameMetadata (F) ))
350
361
return *PGOFuncName;
351
362
352
363
// If there is no meta data, the function must be a global before the value
@@ -494,8 +505,8 @@ void InstrProfSymtab::dumpNames(raw_ostream &OS) const {
494
505
OS << S << ' \n ' ;
495
506
}
496
507
497
- Error collectPGOFuncNameStrings (ArrayRef<std::string> NameStrs,
498
- bool doCompression, std::string &Result) {
508
+ Error collectGlobalObjectNameStrings (ArrayRef<std::string> NameStrs,
509
+ bool doCompression, std::string &Result) {
499
510
assert (!NameStrs.empty () && " No name data to emit" );
500
511
501
512
uint8_t Header[20 ], *P = Header;
@@ -545,7 +556,7 @@ Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
545
556
for (auto *NameVar : NameVars) {
546
557
NameStrs.push_back (std::string (getPGOFuncNameVarInitializer (NameVar)));
547
558
}
548
- return collectPGOFuncNameStrings (
559
+ return collectGlobalObjectNameStrings (
549
560
NameStrs, compression::zlib::isAvailable () && doCompression, Result);
550
561
}
551
562
0 commit comments