24
24
25
25
using namespace llvm ;
26
26
27
- // / Get the __llvm_prf_cnts section.
28
- Expected<object::SectionRef> getCountersSection (const object::ObjectFile &Obj) {
27
+ // / Get profile section.
28
+ Expected<object::SectionRef> getInstrProfSection (const object::ObjectFile &Obj,
29
+ InstrProfSectKind IPSK) {
30
+ Triple::ObjectFormatType ObjFormat = Obj.getTripleObjectFormat ();
31
+ std::string ExpectedSectionName =
32
+ getInstrProfSectionName (IPSK, ObjFormat,
33
+ /* AddSegmentInfo=*/ false );
29
34
for (auto &Section : Obj.sections ())
30
35
if (auto SectionName = Section.getName ())
31
- if (SectionName.get () == INSTR_PROF_CNTS_SECT_NAME )
36
+ if (SectionName.get () == ExpectedSectionName )
32
37
return Section;
33
38
return make_error<InstrProfError>(
34
39
instrprof_error::unable_to_correlate_profile,
35
- " could not find counter section (" INSTR_PROF_CNTS_SECT_NAME " )" );
40
+ " could not find section (" + Twine (ExpectedSectionName) + " )" );
36
41
}
37
42
38
43
const char *InstrProfCorrelator::FunctionNameAttributeName = " Function Name" ;
@@ -42,7 +47,7 @@ const char *InstrProfCorrelator::NumCountersAttributeName = "Num Counters";
42
47
llvm::Expected<std::unique_ptr<InstrProfCorrelator::Context>>
43
48
InstrProfCorrelator::Context::get (std::unique_ptr<MemoryBuffer> Buffer,
44
49
const object::ObjectFile &Obj) {
45
- auto CountersSection = getCountersSection (Obj);
50
+ auto CountersSection = getInstrProfSection (Obj, IPSK_cnts );
46
51
if (auto Err = CountersSection.takeError ())
47
52
return std::move (Err);
48
53
auto C = std::make_unique<Context>();
@@ -54,30 +59,32 @@ InstrProfCorrelator::Context::get(std::unique_ptr<MemoryBuffer> Buffer,
54
59
}
55
60
56
61
llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
57
- InstrProfCorrelator::get (StringRef DebugInfoFilename) {
58
- auto DsymObjectsOrErr =
59
- object::MachOObjectFile::findDsymObjectMembers (DebugInfoFilename);
60
- if (auto Err = DsymObjectsOrErr.takeError ())
61
- return std::move (Err);
62
- if (!DsymObjectsOrErr->empty ()) {
63
- // TODO: Enable profile correlation when there are multiple objects in a
64
- // dSYM bundle.
65
- if (DsymObjectsOrErr->size () > 1 )
66
- return make_error<InstrProfError>(
67
- instrprof_error::unable_to_correlate_profile,
68
- " using multiple objects is not yet supported" );
69
- DebugInfoFilename = *DsymObjectsOrErr->begin ();
62
+ InstrProfCorrelator::get (StringRef Filename, ProfCorrelatorKind FileKind) {
63
+ if (FileKind == DEBUG_INFO) {
64
+ auto DsymObjectsOrErr =
65
+ object::MachOObjectFile::findDsymObjectMembers (Filename);
66
+ if (auto Err = DsymObjectsOrErr.takeError ())
67
+ return std::move (Err);
68
+ if (!DsymObjectsOrErr->empty ()) {
69
+ // TODO: Enable profile correlation when there are multiple objects in a
70
+ // dSYM bundle.
71
+ if (DsymObjectsOrErr->size () > 1 )
72
+ return make_error<InstrProfError>(
73
+ instrprof_error::unable_to_correlate_profile,
74
+ " using multiple objects is not yet supported" );
75
+ Filename = *DsymObjectsOrErr->begin ();
76
+ }
70
77
}
71
- auto BufferOrErr =
72
- errorOrToExpected (MemoryBuffer::getFile (DebugInfoFilename));
78
+ auto BufferOrErr = errorOrToExpected (MemoryBuffer::getFile (Filename));
73
79
if (auto Err = BufferOrErr.takeError ())
74
80
return std::move (Err);
75
81
76
- return get (std::move (*BufferOrErr));
82
+ return get (std::move (*BufferOrErr), FileKind );
77
83
}
78
84
79
85
llvm::Expected<std::unique_ptr<InstrProfCorrelator>>
80
- InstrProfCorrelator::get (std::unique_ptr<MemoryBuffer> Buffer) {
86
+ InstrProfCorrelator::get (std::unique_ptr<MemoryBuffer> Buffer,
87
+ ProfCorrelatorKind FileKind) {
81
88
auto BinOrErr = object::createBinary (*Buffer);
82
89
if (auto Err = BinOrErr.takeError ())
83
90
return std::move (Err);
@@ -88,9 +95,11 @@ InstrProfCorrelator::get(std::unique_ptr<MemoryBuffer> Buffer) {
88
95
return std::move (Err);
89
96
auto T = Obj->makeTriple ();
90
97
if (T.isArch64Bit ())
91
- return InstrProfCorrelatorImpl<uint64_t >::get (std::move (*CtxOrErr), *Obj);
98
+ return InstrProfCorrelatorImpl<uint64_t >::get (std::move (*CtxOrErr), *Obj,
99
+ FileKind);
92
100
if (T.isArch32Bit ())
93
- return InstrProfCorrelatorImpl<uint32_t >::get (std::move (*CtxOrErr), *Obj);
101
+ return InstrProfCorrelatorImpl<uint32_t >::get (std::move (*CtxOrErr), *Obj,
102
+ FileKind);
94
103
}
95
104
return make_error<InstrProfError>(
96
105
instrprof_error::unable_to_correlate_profile, " not an object file" );
@@ -132,29 +141,33 @@ template <class IntPtrT>
132
141
llvm::Expected<std::unique_ptr<InstrProfCorrelatorImpl<IntPtrT>>>
133
142
InstrProfCorrelatorImpl<IntPtrT>::get(
134
143
std::unique_ptr<InstrProfCorrelator::Context> Ctx,
135
- const object::ObjectFile &Obj) {
136
- if (Obj.isELF () || Obj.isMachO ()) {
137
- auto DICtx = DWARFContext::create (Obj);
138
- return std::make_unique<DwarfInstrProfCorrelator<IntPtrT>>(std::move (DICtx),
139
- std::move (Ctx));
144
+ const object::ObjectFile &Obj, ProfCorrelatorKind FileKind) {
145
+ if (FileKind == DEBUG_INFO) {
146
+ if (Obj.isELF () || Obj.isMachO ()) {
147
+ auto DICtx = DWARFContext::create (Obj);
148
+ return std::make_unique<DwarfInstrProfCorrelator<IntPtrT>>(
149
+ std::move (DICtx), std::move (Ctx));
150
+ }
151
+ return make_error<InstrProfError>(
152
+ instrprof_error::unable_to_correlate_profile,
153
+ " unsupported debug info format (only DWARF is supported)" );
140
154
}
141
155
return make_error<InstrProfError>(
142
156
instrprof_error::unable_to_correlate_profile,
143
- " unsupported debug info format (only DWARF is supported)" );
157
+ " unsupported correlation file type (only DWARF is supported)" );
144
158
}
145
159
146
160
template <class IntPtrT >
147
161
Error InstrProfCorrelatorImpl<IntPtrT>::correlateProfileData(int MaxWarnings) {
148
162
assert (Data.empty () && Names.empty () && NamesVec.empty ());
149
163
correlateProfileDataImpl (MaxWarnings);
150
- if (Data. empty () || NamesVec .empty ())
164
+ if (this -> Data .empty ())
151
165
return make_error<InstrProfError>(
152
166
instrprof_error::unable_to_correlate_profile,
153
- " could not find any profile metadata in debug info" );
154
- auto Result =
155
- collectGlobalObjectNameStrings (NamesVec, /* doCompression=*/ false , Names);
156
- CounterOffsets.clear ();
157
- NamesVec.clear ();
167
+ " could not find any profile data metadata in correlated file" );
168
+ Error Result = correlateProfileNameImpl ();
169
+ this ->CounterOffsets .clear ();
170
+ this ->NamesVec .clear ();
158
171
return Result;
159
172
}
160
173
@@ -189,7 +202,7 @@ Error InstrProfCorrelatorImpl<IntPtrT>::dumpYaml(int MaxWarnings,
189
202
if (Data.Probes .empty ())
190
203
return make_error<InstrProfError>(
191
204
instrprof_error::unable_to_correlate_profile,
192
- " could not find any profile metadata in debug info" );
205
+ " could not find any profile data metadata in debug info" );
193
206
yaml::Output YamlOS (OS);
194
207
YamlOS << Data;
195
208
return Error::success ();
@@ -365,3 +378,16 @@ void DwarfInstrProfCorrelator<IntPtrT>::correlateProfileDataImpl(
365
378
WithColor::warning () << format (" Suppressed %d additional warnings\n " ,
366
379
NumSuppressedWarnings);
367
380
}
381
+
382
+ template <class IntPtrT >
383
+ Error DwarfInstrProfCorrelator<IntPtrT>::correlateProfileNameImpl() {
384
+ if (this ->NamesVec .empty ()) {
385
+ return make_error<InstrProfError>(
386
+ instrprof_error::unable_to_correlate_profile,
387
+ " could not find any profile name metadata in debug info" );
388
+ }
389
+ auto Result =
390
+ collectGlobalObjectNameStrings (this ->NamesVec ,
391
+ /* doCompression=*/ false , this ->Names );
392
+ return Result;
393
+ }
0 commit comments