Skip to content

Commit 97f8101

Browse files
committed
[BOLT][NFCI] Strip suffix in getLTOCommonName
Also provide a mechanism to override the list of suffixes to consider. Override LTOSuffixes for getGUID in pseudo probe parsing. Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D61857819 Pull Request: #106243
1 parent 1c469cf commit 97f8101

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

bolt/include/bolt/Utils/Utils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ std::string getEscapedName(const StringRef &Name);
4141
/// Return the unescaped name
4242
std::string getUnescapedName(const StringRef &Name);
4343

44+
/// Return a common part for a given \p Name wrt a given \p Suffixes list.
45+
/// Preserve the suffix if \p KeepSuffix is set, only dropping characters
46+
/// following it, otherwise drop the suffix as well.
47+
std::optional<StringRef> getCommonName(const StringRef Name, bool KeepSuffix,
48+
ArrayRef<StringRef> Suffixes);
4449
/// LTO-generated function names take a form:
4550
///
4651
/// <function_name>.lto_priv.<decimal_number>/...

bolt/lib/Rewrite/PseudoProbeRewriter.cpp

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "bolt/Rewrite/MetadataRewriter.h"
1515
#include "bolt/Rewrite/MetadataRewriters.h"
1616
#include "bolt/Utils/CommandLineOpts.h"
17+
#include "bolt/Utils/Utils.h"
1718
#include "llvm/IR/Function.h"
1819
#include "llvm/MC/MCPseudoProbe.h"
1920
#include "llvm/Support/CommandLine.h"
@@ -133,10 +134,16 @@ void PseudoProbeRewriter::parsePseudoProbe() {
133134

134135
MCPseudoProbeDecoder::Uint64Set GuidFilter;
135136
MCPseudoProbeDecoder::Uint64Map FuncStartAddrs;
137+
SmallVector<StringRef, 3> Suffixes({".destroy", ".resume", ".llvm."});
136138
for (const BinaryFunction *F : BC.getAllBinaryFunctions()) {
137139
for (const MCSymbol *Sym : F->getSymbols()) {
138-
FuncStartAddrs[Function::getGUID(NameResolver::restore(Sym->getName()))] =
139-
F->getAddress();
140+
StringRef SymName = NameResolver::restore(Sym->getName());
141+
if (std::optional<StringRef> CommonName =
142+
getCommonName(SymName, false, Suffixes)) {
143+
SymName = *CommonName;
144+
}
145+
uint64_t GUID = Function::getGUID(SymName);
146+
FuncStartAddrs[GUID] = F->getAddress();
140147
}
141148
}
142149
Contents = PseudoProbeSection->getContents();
@@ -155,13 +162,25 @@ void PseudoProbeRewriter::parsePseudoProbe() {
155162
ProbeDecoder.printProbesForAllAddresses(outs());
156163
}
157164

158-
for (const auto &FuncDesc : ProbeDecoder.getGUID2FuncDescMap()) {
159-
uint64_t GUID = FuncDesc.FuncGUID;
160-
if (!FuncStartAddrs.contains(GUID))
161-
continue;
162-
BinaryFunction *BF = BC.getBinaryFunctionAtAddress(FuncStartAddrs[GUID]);
163-
assert(BF);
164-
BF->setGUID(GUID);
165+
const GUIDProbeFunctionMap &GUID2Func = ProbeDecoder.getGUID2FuncDescMap();
166+
// Checks GUID in GUID2Func and returns it if it's present or null otherwise.
167+
auto checkGUID = [&](StringRef SymName) {
168+
uint64_t GUID = Function::getGUID(SymName);
169+
if (GUID2Func.find(GUID) == GUID2Func.end())
170+
return 0ull;
171+
return GUID;
172+
};
173+
for (BinaryFunction *F : BC.getAllBinaryFunctions()) {
174+
for (const MCSymbol *Sym : F->getSymbols()) {
175+
StringRef SymName = NameResolver::restore(Sym->getName());
176+
uint64_t GUID = checkGUID(SymName);
177+
std::optional<StringRef> CommonName =
178+
getCommonName(SymName, false, Suffixes);
179+
if (!GUID && CommonName)
180+
GUID = checkGUID(*CommonName);
181+
if (GUID)
182+
F->setGUID(GUID);
183+
}
165184
}
166185
}
167186

bolt/lib/Utils/Utils.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,21 @@ std::string getUnescapedName(const StringRef &Name) {
6666
return Output;
6767
}
6868

69-
std::optional<StringRef> getLTOCommonName(const StringRef Name) {
70-
for (StringRef Suffix : {".__uniq.", ".lto_priv.", ".constprop.", ".llvm."}) {
69+
std::optional<StringRef> getCommonName(const StringRef Name, bool KeepSuffix,
70+
ArrayRef<StringRef> Suffixes) {
71+
for (StringRef Suffix : Suffixes) {
7172
size_t LTOSuffixPos = Name.find(Suffix);
7273
if (LTOSuffixPos != StringRef::npos)
73-
return Name.substr(0, LTOSuffixPos + Suffix.size());
74+
return Name.substr(0, LTOSuffixPos + (KeepSuffix ? Suffix.size() : 0));
7475
}
7576
return std::nullopt;
7677
}
7778

79+
std::optional<StringRef> getLTOCommonName(const StringRef Name) {
80+
return getCommonName(Name, true,
81+
{".__uniq.", ".lto_priv.", ".constprop.", ".llvm."});
82+
}
83+
7884
std::optional<uint8_t> readDWARFExpressionTargetReg(StringRef ExprBytes) {
7985
uint8_t Opcode = ExprBytes[0];
8086
if (Opcode == dwarf::DW_CFA_def_cfa_expression)

0 commit comments

Comments
 (0)