14
14
#include " bolt/Rewrite/MetadataRewriter.h"
15
15
#include " bolt/Rewrite/MetadataRewriters.h"
16
16
#include " bolt/Utils/CommandLineOpts.h"
17
+ #include " bolt/Utils/Utils.h"
17
18
#include " llvm/IR/Function.h"
18
19
#include " llvm/MC/MCPseudoProbe.h"
19
20
#include " llvm/Support/CommandLine.h"
@@ -49,7 +50,7 @@ static cl::opt<PrintPseudoProbesOptions> PrintPseudoProbes(
49
50
clEnumValN(PPP_All, " all" , " enable all debugging printout" )),
50
51
cl::Hidden, cl::cat(BoltCategory));
51
52
52
- extern cl::opt<bool > ProfileUsePseudoProbes ;
53
+ extern cl::opt<bool > ProfileWritePseudoProbes ;
53
54
} // namespace opts
54
55
55
56
namespace {
@@ -71,7 +72,8 @@ class PseudoProbeRewriter final : public MetadataRewriter {
71
72
72
73
// / Parse .pseudo_probe_desc section and .pseudo_probe section
73
74
// / Setup Pseudo probe decoder
74
- void parsePseudoProbe ();
75
+ // / If \p ProfiledOnly is set, only parse records for functions with profile.
76
+ void parsePseudoProbe (bool ProfiledOnly = false );
75
77
76
78
// / PseudoProbe decoder
77
79
std::shared_ptr<MCPseudoProbeDecoder> ProbeDecoderPtr;
@@ -90,21 +92,21 @@ class PseudoProbeRewriter final : public MetadataRewriter {
90
92
};
91
93
92
94
Error PseudoProbeRewriter::preCFGInitializer () {
93
- if (opts::ProfileUsePseudoProbes )
94
- parsePseudoProbe ();
95
+ if (opts::ProfileWritePseudoProbes )
96
+ parsePseudoProbe (true );
95
97
96
98
return Error::success ();
97
99
}
98
100
99
101
Error PseudoProbeRewriter::postEmitFinalizer () {
100
- if (!opts::ProfileUsePseudoProbes )
102
+ if (!opts::ProfileWritePseudoProbes )
101
103
parsePseudoProbe ();
102
104
updatePseudoProbes ();
103
105
104
106
return Error::success ();
105
107
}
106
108
107
- void PseudoProbeRewriter::parsePseudoProbe () {
109
+ void PseudoProbeRewriter::parsePseudoProbe (bool ProfiledOnly ) {
108
110
MCPseudoProbeDecoder &ProbeDecoder (*ProbeDecoderPtr);
109
111
PseudoProbeDescSection = BC.getUniqueSectionByName (" .pseudo_probe_desc" );
110
112
PseudoProbeSection = BC.getUniqueSectionByName (" .pseudo_probe" );
@@ -133,10 +135,22 @@ void PseudoProbeRewriter::parsePseudoProbe() {
133
135
134
136
MCPseudoProbeDecoder::Uint64Set GuidFilter;
135
137
MCPseudoProbeDecoder::Uint64Map FuncStartAddrs;
138
+ SmallVector<StringRef, 0 > Suffixes (
139
+ {" .destroy" , " .resume" , " .llvm." , " .cold" , " .warm" });
136
140
for (const BinaryFunction *F : BC.getAllBinaryFunctions ()) {
141
+ bool HasProfile = F->hasProfileAvailable ();
137
142
for (const MCSymbol *Sym : F->getSymbols ()) {
138
- FuncStartAddrs[Function::getGUID (NameResolver::restore (Sym->getName ()))] =
139
- F->getAddress ();
143
+ StringRef SymName = Sym->getName ();
144
+ for (auto Name : {std::optional (NameResolver::restore (SymName)),
145
+ getCommonName (SymName, false , Suffixes)}) {
146
+ if (!Name)
147
+ continue ;
148
+ SymName = *Name;
149
+ uint64_t GUID = Function::getGUID (SymName);
150
+ FuncStartAddrs[GUID] = F->getAddress ();
151
+ if (ProfiledOnly && HasProfile)
152
+ GuidFilter.insert (GUID);
153
+ }
140
154
}
141
155
}
142
156
Contents = PseudoProbeSection->getContents ();
@@ -155,13 +169,25 @@ void PseudoProbeRewriter::parsePseudoProbe() {
155
169
ProbeDecoder.printProbesForAllAddresses (outs ());
156
170
}
157
171
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);
172
+ const GUIDProbeFunctionMap &GUID2Func = ProbeDecoder.getGUID2FuncDescMap ();
173
+ // Checks GUID in GUID2Func and returns it if it's present or null otherwise.
174
+ auto checkGUID = [&](StringRef SymName) -> uint64_t {
175
+ uint64_t GUID = Function::getGUID (SymName);
176
+ if (GUID2Func.find (GUID) == GUID2Func.end ())
177
+ return 0 ;
178
+ return GUID;
179
+ };
180
+ for (BinaryFunction *F : BC.getAllBinaryFunctions ()) {
181
+ for (const MCSymbol *Sym : F->getSymbols ()) {
182
+ StringRef SymName = NameResolver::restore (Sym->getName ());
183
+ uint64_t GUID = checkGUID (SymName);
184
+ std::optional<StringRef> CommonName =
185
+ getCommonName (SymName, false , Suffixes);
186
+ if (!GUID && CommonName)
187
+ GUID = checkGUID (*CommonName);
188
+ if (GUID)
189
+ F->setGUID (GUID);
190
+ }
165
191
}
166
192
}
167
193
0 commit comments