Skip to content

Commit a1e9777

Browse files
[NFC] In InstrProf, generalize helper functions to take 'GlobalObject'. They currently take 'Functions' as function parameters or have 'Func' in the name. (llvm#70287)
- For instance, `collectPGOFuncNameStrings` is reused a lot in llvm#66825 to get the compressed vtable names; and in some added callsites it's just confusing to see 'func' since context clearly shows it's not. This function currently just takes a list of strings as input so name it to `collectGlobalObjectNameStrings` - Do the rename in a standalone patch since the method is used in non-llvm codebase. It's easier to rollback this NFC in case rename in that codebase takes longer.
1 parent c2a1249 commit a1e9777

File tree

4 files changed

+56
-42
lines changed

4 files changed

+56
-42
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,23 @@ StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar);
220220
StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName,
221221
StringRef FileName = "<unknown>");
222222

223-
/// Given a vector of strings (function PGO names) \c NameStrs, the
224-
/// method generates a combined string \c Result that is ready to be
225-
/// serialized. The \c Result string is comprised of three fields:
226-
/// The first field is the length of the uncompressed strings, and the
227-
/// the second field is the length of the zlib-compressed string.
228-
/// Both fields are encoded in ULEB128. If \c doCompress is false, the
223+
/// Given a vector of strings (names of global objects like functions or,
224+
/// virtual tables) \c NameStrs, the method generates a combined string \c
225+
/// Result that is ready to be serialized. The \c Result string is comprised of
226+
/// three fields: The first field is the length of the uncompressed strings, and
227+
/// the the second field is the length of the zlib-compressed string. Both
228+
/// fields are encoded in ULEB128. If \c doCompress is false, the
229229
/// third field is the uncompressed strings; otherwise it is the
230230
/// compressed string. When the string compression is off, the
231231
/// second field will have value zero.
232-
Error collectPGOFuncNameStrings(ArrayRef<std::string> NameStrs,
233-
bool doCompression, std::string &Result);
232+
Error collectGlobalObjectNameStrings(ArrayRef<std::string> NameStrs,
233+
bool doCompression, std::string &Result);
234234

235235
/// Produce \c Result string with the same format described above. The input
236236
/// is vector of PGO function name variables that are referenced.
237+
/// The global variable element in 'NameVars' is a string containing the pgo
238+
/// name of a function. See `createPGOFuncNameVar` that creates these global
239+
/// variables.
237240
Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
238241
std::string &Result, bool doCompression = true);
239242

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ static StringRef stripDirPrefix(StringRef PathNameStr, uint32_t NumPrefix) {
265265
return PathNameStr.substr(LastPos);
266266
}
267267

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());
270270
uint32_t StripLevel = StaticFuncFullModulePrefix ? 0 : (uint32_t)-1;
271271
if (StripLevel < StaticFuncStripDirNamePrefix)
272272
StripLevel = StaticFuncStripDirNamePrefix;
@@ -289,64 +289,75 @@ static StringRef getStrippedSourceFileName(const Function &F) {
289289
// mangled, they cannot be passed to Mach-O linkers via -order_file. We still
290290
// need to compute this name to lookup functions from profiles built by older
291291
// 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) {
295296
SmallString<64> Name;
296297
if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
297298
Name.append(FileName.empty() ? "<unknown>" : FileName);
298299
Name.append(";");
299300
}
300-
Mangler().getNameWithPrefix(Name, &F, /*CannotUsePrivateLabel=*/true);
301+
Mangler().getNameWithPrefix(Name, &GO, /*CannotUsePrivateLabel=*/true);
301302
return Name.str().str();
302303
}
303304

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) {
306307
StringRef S = cast<MDString>(MD->getOperand(0))->getString();
307308
return S.str();
308309
}
309310
return {};
310311
}
311312

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) {
314328
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);
317331
}
318332

319333
// 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))
321335
return *IRPGOFuncName;
322336

323337
// If there is no meta data, the function must be a global before the value
324338
// profile annotation pass. Its current linkage may be internal if it is
325339
// internalized in LTO mode.
326-
return getIRPGOFuncName(F, GlobalValue::ExternalLinkage, "");
340+
return getIRPGONameForGlobalObject(GO, GlobalValue::ExternalLinkage, "");
327341
}
328342

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`.
342353
std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
343354
if (!InLTO) {
344355
auto FileName = getStrippedSourceFileName(F);
345356
return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
346357
}
347358

348359
// 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)))
350361
return *PGOFuncName;
351362

352363
// 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 {
494505
OS << S << '\n';
495506
}
496507

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) {
499510
assert(!NameStrs.empty() && "No name data to emit");
500511

501512
uint8_t Header[20], *P = Header;
@@ -545,7 +556,7 @@ Error collectPGOFuncNameStrings(ArrayRef<GlobalVariable *> NameVars,
545556
for (auto *NameVar : NameVars) {
546557
NameStrs.push_back(std::string(getPGOFuncNameVarInitializer(NameVar)));
547558
}
548-
return collectPGOFuncNameStrings(
559+
return collectGlobalObjectNameStrings(
549560
NameStrs, compression::zlib::isAvailable() && doCompression, Result);
550561
}
551562

llvm/lib/ProfileData/InstrProfCorrelator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Error InstrProfCorrelatorImpl<IntPtrT>::correlateProfileData(int MaxWarnings) {
152152
instrprof_error::unable_to_correlate_profile,
153153
"could not find any profile metadata in debug info");
154154
auto Result =
155-
collectPGOFuncNameStrings(NamesVec, /*doCompression=*/false, Names);
155+
collectGlobalObjectNameStrings(NamesVec, /*doCompression=*/false, Names);
156156
CounterOffsets.clear();
157157
NamesVec.clear();
158158
return Result;

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1368,15 +1368,15 @@ TEST_P(MaybeSparseInstrProfTest, instr_prof_symtab_compression_test) {
13681368
for (bool DoCompression : {false, true}) {
13691369
// Compressing:
13701370
std::string FuncNameStrings1;
1371-
EXPECT_THAT_ERROR(collectPGOFuncNameStrings(
1371+
EXPECT_THAT_ERROR(collectGlobalObjectNameStrings(
13721372
FuncNames1,
13731373
(DoCompression && compression::zlib::isAvailable()),
13741374
FuncNameStrings1),
13751375
Succeeded());
13761376

13771377
// Compressing:
13781378
std::string FuncNameStrings2;
1379-
EXPECT_THAT_ERROR(collectPGOFuncNameStrings(
1379+
EXPECT_THAT_ERROR(collectGlobalObjectNameStrings(
13801380
FuncNames2,
13811381
(DoCompression && compression::zlib::isAvailable()),
13821382
FuncNameStrings2),

0 commit comments

Comments
 (0)