llvm.org GIT mirror llvm / 515ee3e
Retry^2 "[ProfileData] (llvm) Use Error in InstrProf and Coverage, NFC" Transition InstrProf and Coverage over to the stricter Error/Expected interface. Changes since the initial commit: - Address undefined-var-template warning. - Fix error message printing in llvm-profdata. - Check errors in loadTestingFormat() + annotateAllFunctions(). - Defer error handling in InstrProfIterator to InstrProfReader. Differential Revision: http://reviews.llvm.org/D19901 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269694 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 4 years ago
18 changed file(s) with 657 addition(s) and 549 deletion(s). Raw diff Collapse all Expand all
2222 #include "llvm/ProfileData/InstrProf.h"
2323 #include "llvm/Support/Debug.h"
2424 #include "llvm/Support/Endian.h"
25 #include "llvm/Support/ErrorOr.h"
2625 #include "llvm/Support/raw_ostream.h"
2726 #include
2827 #include
2928
3029 namespace llvm {
3130 namespace coverage {
31
3232 enum class coveragemap_error {
3333 success = 0,
3434 eof,
3737 truncated,
3838 malformed
3939 };
40
41 class CoverageMapError : public ProfErrorInfoBase {
42 public:
43 CoverageMapError(coveragemap_error Err)
44 : ProfErrorInfoBase(Err) {}
45
46 std::string message() const override;
47 };
48
4049 } // end of coverage namespace.
41 }
42
43 namespace std {
44 template <>
45 struct is_error_code_enum : std::true_type {
46 };
47 }
50
51 template <> char ProfErrorInfoBase::ID;
52
53 } // end of llvm namespace
4854
4955 namespace llvm {
5056 class IndexedInstrProfReader;
264270
265271 /// \brief Return the number of times that a region of code associated with
266272 /// this counter was executed.
267 ErrorOr evaluate(const Counter &C) const;
273 Expected evaluate(const Counter &C) const;
268274 };
269275
270276 /// \brief Code coverage information for a single function.
414420
415421 public:
416422 /// \brief Load the coverage mapping using the given readers.
417 static ErrorOr>
423 static Expected>
418424 load(CoverageMappingReader &CoverageReader,
419425 IndexedInstrProfReader &ProfileReader);
420426
421427 /// \brief Load the coverage mapping from the given files.
422 static ErrorOr>
428 static Expected>
423429 load(StringRef ObjectFilename, StringRef ProfileFilename,
424430 StringRef Arch = StringRef());
425431
500506 }
501507 // Return the PGO name of the function */
502508 template
503 std::error_code getFuncName(InstrProfSymtab &ProfileNames,
504 StringRef &FuncName) const {
509 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
505510 IntPtrT NameRef = getFuncNameRef();
506511 uint32_t NameS = support::endian::byte_swap(NameSize);
507512 FuncName = ProfileNames.getFuncName(NameRef, NameS);
508513 if (NameS && FuncName.empty())
509 return coveragemap_error::malformed;
510 return std::error_code();
514 return make_error(coveragemap_error::malformed);
515 return Error::success();
511516 }
512517 };
513518
529534 }
530535 // Return the PGO name of the function */
531536 template
532 std::error_code getFuncName(InstrProfSymtab &ProfileNames,
533 StringRef &FuncName) const {
537 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
534538 uint64_t NameRef = getFuncNameRef();
535539 FuncName = ProfileNames.getFuncName(NameRef);
536 return std::error_code();
540 return Error::success();
537541 }
538542 };
539543
6868
6969 class CoverageMappingReader {
7070 public:
71 virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0;
71 virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
7272 CoverageMappingIterator begin() { return CoverageMappingIterator(this); }
7373 CoverageMappingIterator end() { return CoverageMappingIterator(); }
7474 virtual ~CoverageMappingReader() {}
8181
8282 RawCoverageReader(StringRef Data) : Data(Data) {}
8383
84 std::error_code readULEB128(uint64_t &Result);
85 std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1);
86 std::error_code readSize(uint64_t &Result);
87 std::error_code readString(StringRef &Result);
84 Error readULEB128(uint64_t &Result);
85 Error readIntMax(uint64_t &Result, uint64_t MaxPlus1);
86 Error readSize(uint64_t &Result);
87 Error readString(StringRef &Result);
8888 };
8989
9090 /// \brief Reader for the raw coverage filenames.
9999 RawCoverageFilenamesReader(StringRef Data, std::vector &Filenames)
100100 : RawCoverageReader(Data), Filenames(Filenames) {}
101101
102 std::error_code read();
102 Error read();
103103 };
104104
105105 /// \brief Reader for the raw coverage mapping data.
124124 Filenames(Filenames), Expressions(Expressions),
125125 MappingRegions(MappingRegions) {}
126126
127 std::error_code read();
127 Error read();
128128
129129 private:
130 std::error_code decodeCounter(unsigned Value, Counter &C);
131 std::error_code readCounter(Counter &C);
132 std::error_code
130 Error decodeCounter(unsigned Value, Counter &C);
131 Error readCounter(Counter &C);
132 Error
133133 readMappingRegionsSubArray(std::vector &MappingRegions,
134134 unsigned InferredFileID, size_t NumFileIDs);
135135 };
169169 BinaryCoverageReader() : CurrentRecord(0) {}
170170
171171 public:
172 static ErrorOr>
172 static Expected>
173173 create(std::unique_ptr &ObjectBuffer,
174174 StringRef Arch);
175175
176 std::error_code readNextRecord(CoverageMappingRecord &Record) override;
176 Error readNextRecord(CoverageMappingRecord &Record) override;
177177 };
178178
179179 } // end namespace coverage
2424 #include "llvm/ProfileData/ProfileCommon.h"
2525 #include "llvm/Support/Endian.h"
2626 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/ErrorOr.h"
2827 #include "llvm/Support/MD5.h"
2928 #include "llvm/Support/MathExtras.h"
3029 #include
203202 /// third field is the uncompressed strings; otherwise it is the
204203 /// compressed string. When the string compression is off, the
205204 /// second field will have value zero.
206 std::error_code
207 collectPGOFuncNameStrings(const std::vector &NameStrs,
208 bool doCompression, std::string &Result);
205 Error collectPGOFuncNameStrings(const std::vector &NameStrs,
206 bool doCompression, std::string &Result);
209207 /// Produce \c Result string with the same format described above. The input
210208 /// is vector of PGO function name variables that are referenced.
211 std::error_code
212 collectPGOFuncNameStrings(const std::vector &NameVars,
213 std::string &Result, bool doCompression = true);
209 Error collectPGOFuncNameStrings(const std::vector &NameVars,
210 std::string &Result, bool doCompression = true);
214211 class InstrProfSymtab;
215212 /// \c NameStrings is a string composed of one of more sub-strings encoded in
216213 /// the format described above. The substrings are seperated by 0 or more zero
217214 /// bytes. This method decodes the string and populates the \c Symtab.
218 std::error_code readPGOFuncNameStrings(StringRef NameStrings,
219 InstrProfSymtab &Symtab);
215 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
220216
221217 enum InstrProfValueKind : uint32_t {
222218 #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
283279 return std::error_code(static_cast(E), instrprof_category());
284280 }
285281
282 class InstrProfError : public ProfErrorInfoBase {
283 public:
284 InstrProfError(instrprof_error Err)
285 : ProfErrorInfoBase(Err) {}
286
287 std::string message() const override;
288
289 /// Consume an Error and return the raw enum value contained within it. The
290 /// Error must either be a success value, or contain a single InstrProfError.
291 static instrprof_error take(Error E) {
292 auto Err = instrprof_error::success;
293 handleAllErrors(std::move(E), [&Err](const InstrProfError &IPE) {
294 assert(Err == instrprof_error::success && "Multiple errors encountered");
295 Err = IPE.get();
296 });
297 return Err;
298 }
299 };
300
301 template <> char ProfErrorInfoBase::ID;
302
286303 class SoftInstrProfErrors {
287304 /// Count the number of soft instrprof_errors encountered and keep track of
288305 /// the first such error for reporting purposes.
308325 NumCountMismatches(0), NumCounterOverflows(0),
309326 NumValueSiteCountMismatches(0) {}
310327
328 ~SoftInstrProfErrors() {
329 assert(FirstError == instrprof_error::success &&
330 "Unchecked soft error encountered");
331 }
332
311333 /// Track a soft error (\p IE) and increment its associated counter.
312334 void addError(instrprof_error IE);
313335
325347 return NumValueSiteCountMismatches;
326348 }
327349
328 /// Return an error code for the first encountered error.
329 std::error_code getError() const { return make_error_code(FirstError); }
350 /// Return the first encountered error and reset FirstError to a success
351 /// value.
352 Error takeError() {
353 if (FirstError == instrprof_error::success)
354 return Error::success();
355 auto E = make_error(FirstError);
356 FirstError = instrprof_error::success;
357 return E;
358 }
330359 };
331360
332361 namespace object {
371400 /// only initialize the symtab with reference to the data and
372401 /// the section base address. The decompression will be delayed
373402 /// until before it is used. See also \c create(StringRef) method.
374 std::error_code create(object::SectionRef &Section);
403 Error create(object::SectionRef &Section);
375404 /// This interface is used by reader of CoverageMapping test
376405 /// format.
377 inline std::error_code create(StringRef D, uint64_t BaseAddr);
406 inline Error create(StringRef D, uint64_t BaseAddr);
378407 /// \c NameStrings is a string composed of one of more sub-strings
379408 /// encoded in the format described in \c collectPGOFuncNameStrings.
380409 /// This method is a wrapper to \c readPGOFuncNameStrings method.
381 inline std::error_code create(StringRef NameStrings);
410 inline Error create(StringRef NameStrings);
382411 /// A wrapper interface to populate the PGO symtab with functions
383412 /// decls from module \c M. This interface is used by transformation
384413 /// passes such as indirect function call promotion. Variable \c InLTO
423452 inline StringRef getNameData() const { return Data; }
424453 };
425454
426 std::error_code InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
455 Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
427456 Data = D;
428457 Address = BaseAddr;
429 return std::error_code();
430 }
431
432 std::error_code InstrProfSymtab::create(StringRef NameStrings) {
458 return Error::success();
459 }
460
461 Error InstrProfSymtab::create(StringRef NameStrings) {
433462 return readPGOFuncNameStrings(NameStrings, *this);
434463 }
435464
571600 }
572601
573602 /// Get the error contained within the record's soft error counter.
574 std::error_code getError() const { return SIPE.getError(); }
603 Error takeError() { return SIPE.takeError(); }
575604
576605 private:
577606 std::vector IndirectCallSites;
868897
869898 } // end namespace llvm
870899
871 namespace std {
872 template <>
873 struct is_error_code_enum : std::true_type {};
874 }
875
876900 #endif // LLVM_PROFILEDATA_INSTRPROF_H
321321 static std::unique_ptr
322322 serializeFrom(const InstrProfRecord &Record);
323323 /*!
324 * Check the integrity of the record. Return the error code when
325 * an error is detected, otherwise return instrprof_error::success.
326 */
327 instrprof_error checkIntegrity();
324 * Check the integrity of the record.
325 */
326 Error checkIntegrity();
328327 /*!
329328 * Return a pointer to \c ValueProfileData instance ready to be read.
330329 * All data in the instance are properly byte swapped. The input
331330 * data is assumed to be in little endian order.
332331 */
333 static ErrorOr>
332 static Expected>
334333 getValueProfData(const unsigned char *SrcBuffer,
335334 const unsigned char *const SrcBufferEnd,
336335 support::endianness SrcDataEndianness);
1818 #include "llvm/ADT/StringExtras.h"
1919 #include "llvm/ProfileData/InstrProf.h"
2020 #include "llvm/Support/EndianStream.h"
21 #include "llvm/Support/ErrorOr.h"
2221 #include "llvm/Support/LineIterator.h"
2322 #include "llvm/Support/MemoryBuffer.h"
2423 #include "llvm/Support/OnDiskHashTable.h"
5049 /// Base class and interface for reading profiling data of any known instrprof
5150 /// format. Provides an iterator over InstrProfRecords.
5251 class InstrProfReader {
53 std::error_code LastError;
52 instrprof_error LastError;
5453
5554 public:
5655 InstrProfReader() : LastError(instrprof_error::success), Symtab() {}
5756 virtual ~InstrProfReader() {}
5857
5958 /// Read the header. Required before reading first record.
60 virtual std::error_code readHeader() = 0;
59 virtual Error readHeader() = 0;
6160 /// Read a single record.
62 virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0;
61 virtual Error readNextRecord(InstrProfRecord &Record) = 0;
6362 /// Iterator over profile data.
6463 InstrProfIterator begin() { return InstrProfIterator(this); }
6564 InstrProfIterator end() { return InstrProfIterator(); }
7978
8079 protected:
8180 std::unique_ptr Symtab;
82 /// Set the current std::error_code and return same.
83 std::error_code error(std::error_code EC) {
84 LastError = EC;
85 return EC;
86 }
87
88 /// Clear the current error code and return a successful one.
89 std::error_code success() { return error(instrprof_error::success); }
81 /// Set the current error and return same.
82 Error error(instrprof_error Err) {
83 LastError = Err;
84 if (Err == instrprof_error::success)
85 return Error::success();
86 return make_error(Err);
87 }
88 Error error(Error E) { return error(InstrProfError::take(std::move(E))); }
89
90 /// Clear the current error and return a successful one.
91 Error success() { return error(instrprof_error::success); }
9092
9193 public:
9294 /// Return true if the reader has finished reading the profile data.
9395 bool isEOF() { return LastError == instrprof_error::eof; }
9496 /// Return true if the reader encountered an error reading profiling data.
95 bool hasError() { return LastError && !isEOF(); }
96 /// Get the current error code.
97 std::error_code getError() { return LastError; }
97 bool hasError() { return LastError != instrprof_error::success && !isEOF(); }
98 /// Get the current error.
99 Error getError() {
100 if (hasError())
101 return make_error(LastError);
102 return Error::success();
103 }
98104
99105 /// Factory method to create an appropriately typed reader for the given
100106 /// instrprof file.
101 static ErrorOr> create(std::string Path);
102
103 static ErrorOr>
107 static Expected> create(std::string Path);
108
109 static Expected>
104110 create(std::unique_ptr Buffer);
105111 };
106112
122128
123129 TextInstrProfReader(const TextInstrProfReader &) = delete;
124130 TextInstrProfReader &operator=(const TextInstrProfReader &) = delete;
125 std::error_code readValueProfileData(InstrProfRecord &Record);
131 Error readValueProfileData(InstrProfRecord &Record);
126132
127133 public:
128134 TextInstrProfReader(std::unique_ptr DataBuffer_)
135141 bool isIRLevelProfile() const override { return IsIRLevelProfile; }
136142
137143 /// Read the header.
138 std::error_code readHeader() override;
144 Error readHeader() override;
139145 /// Read a single record.
140 std::error_code readNextRecord(InstrProfRecord &Record) override;
146 Error readNextRecord(InstrProfRecord &Record) override;
141147
142148 InstrProfSymtab &getSymtab() override {
143149 assert(Symtab.get());
184190 : DataBuffer(std::move(DataBuffer)) { }
185191
186192 static bool hasFormat(const MemoryBuffer &DataBuffer);
187 std::error_code readHeader() override;
188 std::error_code readNextRecord(InstrProfRecord &Record) override;
193 Error readHeader() override;
194 Error readNextRecord(InstrProfRecord &Record) override;
189195 bool isIRLevelProfile() const override {
190196 return (Version & VARIANT_MASK_IR_PROF) != 0;
191197 }
196202 }
197203
198204 private:
199 std::error_code createSymtab(InstrProfSymtab &Symtab);
200 std::error_code readNextHeader(const char *CurrentPos);
201 std::error_code readHeader(const RawInstrProf::Header &Header);
205 Error createSymtab(InstrProfSymtab &Symtab);
206 Error readNextHeader(const char *CurrentPos);
207 Error readHeader(const RawInstrProf::Header &Header);
202208 template IntT swap(IntT Int) const {
203209 return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
204210 }
215221 inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) {
216222 return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
217223 }
218 std::error_code readName(InstrProfRecord &Record);
219 std::error_code readFuncHash(InstrProfRecord &Record);
220 std::error_code readRawCounts(InstrProfRecord &Record);
221 std::error_code readValueProfilingData(InstrProfRecord &Record);
224 Error readName(InstrProfRecord &Record);
225 Error readFuncHash(InstrProfRecord &Record);
226 Error readRawCounts(InstrProfRecord &Record);
227 Error readValueProfilingData(InstrProfRecord &Record);
222228 bool atEnd() const { return Data == DataEnd; }
223229 void advanceData() {
224230 Data++;
299305 struct InstrProfReaderIndexBase {
300306 // Read all the profile records with the same key pointed to the current
301307 // iterator.
302 virtual std::error_code getRecords(ArrayRef &Data) = 0;
308 virtual Error getRecords(ArrayRef &Data) = 0;
303309 // Read all the profile records with the key equal to FuncName
304 virtual std::error_code getRecords(StringRef FuncName,
310 virtual Error getRecords(StringRef FuncName,
305311 ArrayRef &Data) = 0;
306312 virtual void advanceToNextKey() = 0;
307313 virtual bool atEnd() const = 0;
329335 const unsigned char *const Base,
330336 IndexedInstrProf::HashT HashType, uint64_t Version);
331337
332 std::error_code getRecords(ArrayRef &Data) override;
333 std::error_code getRecords(StringRef FuncName,
334 ArrayRef &Data) override;
338 Error getRecords(ArrayRef &Data) override;
339 Error getRecords(StringRef FuncName,
340 ArrayRef &Data) override;
335341 void advanceToNextKey() override { RecordIterator++; }
336342 bool atEnd() const override {
337343 return RecordIterator == HashTable->data_end();
378384 static bool hasFormat(const MemoryBuffer &DataBuffer);
379385
380386 /// Read the file header.
381 std::error_code readHeader() override;
387 Error readHeader() override;
382388 /// Read a single record.
383 std::error_code readNextRecord(InstrProfRecord &Record) override;
389 Error readNextRecord(InstrProfRecord &Record) override;
384390
385391 /// Return the pointer to InstrProfRecord associated with FuncName
386392 /// and FuncHash
387 ErrorOr getInstrProfRecord(StringRef FuncName,
388 uint64_t FuncHash);
393 Expected getInstrProfRecord(StringRef FuncName,
394 uint64_t FuncHash);
389395
390396 /// Fill Counts with the profile data for the given function name.
391 std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
392 std::vector &Counts);
397 Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
398 std::vector &Counts);
393399
394400 /// Return the maximum of all known function counts.
395401 uint64_t getMaximumFunctionCount() { return Summary->getMaxFunctionCount(); }
396402
397403 /// Factory method to create an indexed reader.
398 static ErrorOr>
404 static Expected>
399405 create(std::string Path);
400406
401 static ErrorOr>
407 static Expected>
402408 create(std::unique_ptr Buffer);
403409
404410 // Used for testing purpose only.
4545 /// Add function counts for the given function. If there are already counts
4646 /// for this function and the hash and number of counts match, each counter is
4747 /// summed. Optionally scale counts by \p Weight.
48 std::error_code addRecord(InstrProfRecord &&I, uint64_t Weight = 1);
48 Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1);
4949 /// Write the profile to \c OS
5050 void write(raw_fd_ostream &OS);
5151 /// Write the profile in text format to \c OS
5757 std::unique_ptr writeBuffer();
5858
5959 /// Set the ProfileKind. Report error if mixing FE and IR level profiles.
60 std::error_code setIsIRLevelProfile(bool IsIRLevel) {
60 Error setIsIRLevelProfile(bool IsIRLevel) {
6161 if (ProfileKind == PF_Unknown) {
6262 ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE;
63 return instrprof_error::success;
63 return Error::success();
6464 }
65 return (IsIRLevel == (ProfileKind == PF_IRLevel)) ?
66 instrprof_error::success : instrprof_error::unsupported_version;
65 return (IsIRLevel == (ProfileKind == PF_IRLevel))
66 ? Error::success()
67 : make_error(
68 instrprof_error::unsupported_version);
6769 }
6870
6971 // Internal interface for testing purpose only.
2020 #include
2121
2222 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Error.h"
2324
2425 namespace llvm {
2526 class Function;
3435 class Metadata;
3536 class MDTuple;
3637 class MDNode;
38
39 template
40 class ProfErrorInfoBase : public ErrorInfo> {
41 public:
42 ProfErrorInfoBase(ErrT Err) : Err(Err) {
43 assert(Err != ErrT::success && "Not an error");
44 }
45
46 virtual std::string message() const override = 0;
47
48 void log(raw_ostream &OS) const override { OS << message(); }
49
50 std::error_code convertToErrorCode() const override {
51 return make_error_code(Err);
52 }
53
54 ErrT get() const { return Err; }
55
56 static char ID;
57
58 protected:
59 ErrT Err;
60 };
3761
3862 inline const char *getHotSectionPrefix() { return ".hot"; }
3963 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
142142 }
143143 if (CounterValues.empty())
144144 return;
145 ErrorOr Value = evaluate(C);
146 if (!Value)
145 Expected Value = evaluate(C);
146 if (auto E = Value.takeError()) {
147 llvm::consumeError(std::move(E));
147148 return;
149 }
148150 OS << '[' << *Value << ']';
149151 }
150152
151 ErrorOr CounterMappingContext::evaluate(const Counter &C) const {
153 Expected CounterMappingContext::evaluate(const Counter &C) const {
152154 switch (C.getKind()) {
153155 case Counter::Zero:
154156 return 0;
155157 case Counter::CounterValueReference:
156158 if (C.getCounterID() >= CounterValues.size())
157 return make_error_code(errc::argument_out_of_domain);
159 return errorCodeToError(errc::argument_out_of_domain);
158160 return CounterValues[C.getCounterID()];
159161 case Counter::Expression: {
160162 if (C.getExpressionID() >= Expressions.size())
161 return make_error_code(errc::argument_out_of_domain);
163 return errorCodeToError(errc::argument_out_of_domain);
162164 const auto &E = Expressions[C.getExpressionID()];
163 ErrorOr LHS = evaluate(E.LHS);
165 Expected LHS = evaluate(E.LHS);
164166 if (!LHS)
165167 return LHS;
166 ErrorOr RHS = evaluate(E.RHS);
168 Expected RHS = evaluate(E.RHS);
167169 if (!RHS)
168170 return RHS;
169171 return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
180182 *this = FunctionRecordIterator();
181183 }
182184
183 ErrorOr>
185 Expected>
184186 CoverageMapping::load(CoverageMappingReader &CoverageReader,
185187 IndexedInstrProfReader &ProfileReader) {
186188 auto Coverage = std::unique_ptr(new CoverageMapping());
190192 CounterMappingContext Ctx(Record.Expressions);
191193
192194 Counts.clear();
193 if (std::error_code EC = ProfileReader.getFunctionCounts(
195 if (Error E = ProfileReader.getFunctionCounts(
194196 Record.FunctionName, Record.FunctionHash, Counts)) {
195 if (EC == instrprof_error::hash_mismatch) {
197 instrprof_error IPE = InstrProfError::take(std::move(E));
198 if (IPE == instrprof_error::hash_mismatch) {
196199 Coverage->MismatchedFunctionCount++;
197200 continue;
198 } else if (EC != instrprof_error::unknown_function)
199 return EC;
201 } else if (IPE != instrprof_error::unknown_function)
202 return make_error(IPE);
200203 Counts.assign(Record.MappingRegions.size(), 0);
201204 }
202205 Ctx.setCounts(Counts);
211214 getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
212215 FunctionRecord Function(OrigFuncName, Record.Filenames);
213216 for (const auto &Region : Record.MappingRegions) {
214 ErrorOr ExecutionCount = Ctx.evaluate(Region.Count);
215 if (!ExecutionCount)
217 Expected ExecutionCount = Ctx.evaluate(Region.Count);
218 if (auto E = ExecutionCount.takeError()) {
219 llvm::consumeError(std::move(E));
216220 break;
221 }
217222 Function.pushRegion(Region, *ExecutionCount);
218223 }
219224 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
227232 return std::move(Coverage);
228233 }
229234
230 ErrorOr>
235 Expected>
231236 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
232237 StringRef Arch) {
233238 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
234239 if (std::error_code EC = CounterMappingBuff.getError())
235 return EC;
240 return errorCodeToError(EC);
236241 auto CoverageReaderOrErr =
237242 BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
238 if (std::error_code EC = CoverageReaderOrErr.getError())
239 return EC;
243 if (Error E = CoverageReaderOrErr.takeError())
244 return std::move(E);
240245 auto CoverageReader = std::move(CoverageReaderOrErr.get());
241246 auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
242 if (auto EC = ProfileReaderOrErr.getError())
243 return EC;
247 if (Error E = ProfileReaderOrErr.takeError())
248 return std::move(E);
244249 auto ProfileReader = std::move(ProfileReaderOrErr.get());
245250 return load(*CoverageReader, *ProfileReader);
246251 }
532537 }
533538
534539 namespace {
540 std::string getCoverageMapErrString(coveragemap_error Err) {
541 switch (Err) {
542 case coveragemap_error::success:
543 return "Success";
544 case coveragemap_error::eof:
545 return "End of File";
546 case coveragemap_error::no_data_found:
547 return "No coverage data found";
548 case coveragemap_error::unsupported_version:
549 return "Unsupported coverage format version";
550 case coveragemap_error::truncated:
551 return "Truncated coverage data";
552 case coveragemap_error::malformed:
553 return "Malformed coverage data";
554 }
555 llvm_unreachable("A value of coveragemap_error has no message.");
556 }
557
535558 class CoverageMappingErrorCategoryType : public std::error_category {
536559 const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
537560 std::string message(int IE) const override {
538 auto E = static_cast(IE);
539 switch (E) {
540 case coveragemap_error::success:
541 return "Success";
542 case coveragemap_error::eof:
543 return "End of File";
544 case coveragemap_error::no_data_found:
545 return "No coverage data found";
546 case coveragemap_error::unsupported_version:
547 return "Unsupported coverage format version";
548 case coveragemap_error::truncated:
549 return "Truncated coverage data";
550 case coveragemap_error::malformed:
551 return "Malformed coverage data";
552 }
553 llvm_unreachable("A value of coveragemap_error has no message.");
561 return getCoverageMapErrString(static_cast(IE));
554562 }
555563 };
564 } // end anonymous namespace
565
566 std::string CoverageMapError::message() const {
567 return getCoverageMapErrString(Err);
556568 }
557569
558570 static ManagedStatic ErrorCategory;
560572 const std::error_category &llvm::coverage::coveragemap_category() {
561573 return *ErrorCategory;
562574 }
575
576 template <> char ProfErrorInfoBase::ID = 0;
3030 void CoverageMappingIterator::increment() {
3131 // Check if all the records were read or if an error occurred while reading
3232 // the next record.
33 if (Reader->readNextRecord(Record))
34 *this = CoverageMappingIterator();
35 }
36
37 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
33 if (auto E = Reader->readNextRecord(Record)) {
34 handleAllErrors(std::move(E), [&](const CoverageMapError &CME) {
35 if (CME.get() == coveragemap_error::eof)
36 *this = CoverageMappingIterator();
37 else
38 llvm_unreachable("Unexpected error in coverage mapping iterator");
39 });
40 }
41 }
42
43 Error RawCoverageReader::readULEB128(uint64_t &Result) {
3844 if (Data.size() < 1)
39 return coveragemap_error::truncated;
45 return make_error(coveragemap_error::truncated);
4046 unsigned N = 0;
4147 Result = decodeULEB128(reinterpret_cast(Data.data()), &N);
4248 if (N > Data.size())
43 return coveragemap_error::malformed;
49 return make_error(coveragemap_error::malformed);
4450 Data = Data.substr(N);
45 return std::error_code();
46 }
47
48 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
49 uint64_t MaxPlus1) {
51 return Error::success();
52 }
53
54 Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
5055 if (auto Err = readULEB128(Result))
5156 return Err;
5257 if (Result >= MaxPlus1)
53 return coveragemap_error::malformed;
54 return std::error_code();
55 }
56
57 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
58 return make_error(coveragemap_error::malformed);
59 return Error::success();
60 }
61
62 Error RawCoverageReader::readSize(uint64_t &Result) {
5863 if (auto Err = readULEB128(Result))
5964 return Err;
6065 // Sanity check the number.
6166 if (Result > Data.size())
62 return coveragemap_error::malformed;
63 return std::error_code();
64 }
65
66 std::error_code RawCoverageReader::readString(StringRef &Result) {
67 return make_error(coveragemap_error::malformed);
68 return Error::success();
69 }
70
71 Error RawCoverageReader::readString(StringRef &Result) {
6772 uint64_t Length;
6873 if (auto Err = readSize(Length))
6974 return Err;
7075 Result = Data.substr(0, Length);
7176 Data = Data.substr(Length);
72 return std::error_code();
73 }
74
75 std::error_code RawCoverageFilenamesReader::read() {
77 return Error::success();
78 }
79
80 Error RawCoverageFilenamesReader::read() {
7681 uint64_t NumFilenames;
7782 if (auto Err = readSize(NumFilenames))
7883 return Err;
8287 return Err;
8388 Filenames.push_back(Filename);
8489 }
85 return std::error_code();
86 }
87
88 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
89 Counter &C) {
90 return Error::success();
91 }
92
93 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
9094 auto Tag = Value & Counter::EncodingTagMask;
9195 switch (Tag) {
9296 case Counter::Zero:
9397 C = Counter::getZero();
94 return std::error_code();
98 return Error::success();
9599 case Counter::CounterValueReference:
96100 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
97 return std::error_code();
101 return Error::success();
98102 default:
99103 break;
100104 }
104108 case CounterExpression::Add: {
105109 auto ID = Value >> Counter::EncodingTagBits;
106110 if (ID >= Expressions.size())
107 return coveragemap_error::malformed;
111 return make_error(coveragemap_error::malformed);
108112 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
109113 C = Counter::getExpression(ID);
110114 break;
111115 }
112116 default:
113 return coveragemap_error::malformed;
114 }
115 return std::error_code();
116 }
117
118 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
117 return make_error(coveragemap_error::malformed);
118 }
119 return Error::success();
120 }
121
122 Error RawCoverageMappingReader::readCounter(Counter &C) {
119123 uint64_t EncodedCounter;
120124 if (auto Err =
121125 readIntMax(EncodedCounter, std::numeric_limits::max()))
122126 return Err;
123127 if (auto Err = decodeCounter(EncodedCounter, C))
124128 return Err;
125 return std::error_code();
129 return Error::success();
126130 }
127131
128132 static const unsigned EncodingExpansionRegionBit = 1
131135 /// \brief Read the sub-array of regions for the given inferred file id.
132136 /// \param NumFileIDs the number of file ids that are defined for this
133137 /// function.
134 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
138 Error RawCoverageMappingReader::readMappingRegionsSubArray(
135139 std::vector &MappingRegions, unsigned InferredFileID,
136140 size_t NumFileIDs) {
137141 uint64_t NumRegions;
159163 ExpandedFileID = EncodedCounterAndRegion >>
160164 Counter::EncodingCounterTagAndExpansionRegionTagBits;
161165 if (ExpandedFileID >= NumFileIDs)
162 return coveragemap_error::malformed;
166 return make_error(coveragemap_error::malformed);
163167 } else {
164168 switch (EncodedCounterAndRegion >>
165169 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
170174 Kind = CounterMappingRegion::SkippedRegion;
171175 break;
172176 default:
173 return coveragemap_error::malformed;
177 return make_error(coveragemap_error::malformed);
174178 }
175179 }
176180 }
183187 if (auto Err = readULEB128(ColumnStart))
184188 return Err;
185189 if (ColumnStart > std::numeric_limits::max())
186 return coveragemap_error::malformed;
190 return make_error(coveragemap_error::malformed);
187191 if (auto Err = readIntMax(NumLines, std::numeric_limits::max()))
188192 return Err;
189193 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits::max()))
217221 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
218222 LineStart + NumLines, ColumnEnd, Kind));
219223 }
220 return std::error_code();
221 }
222
223 std::error_code RawCoverageMappingReader::read() {
224 return Error::success();
225 }
226
227 Error RawCoverageMappingReader::read() {
224228
225229 // Read the virtual file mapping.
226230 llvm::SmallVector VirtualFileMapping;
286290 }
287291 }
288292
289 return std::error_code();
290 }
291
292 std::error_code InstrProfSymtab::create(SectionRef &Section) {
293 if (auto Err = Section.getContents(Data))
294 return Err;
293 return Error::success();
294 }
295
296 Error InstrProfSymtab::create(SectionRef &Section) {
297 if (auto EC = Section.getContents(Data))
298 return errorCodeToError(EC);
295299 Address = Section.getAddress();
296 return std::error_code();
300 return Error::success();
297301 }
298302
299303 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
311315 // a module. \p Buf is a reference to the buffer pointer pointing
312316 // to the \c CovHeader of coverage mapping data associated with
313317 // the module.
314 virtual std::error_code readFunctionRecords(const char *&Buf,
315 const char *End) = 0;
318 virtual Error readFunctionRecords(const char *&Buf, const char *End) = 0;
316319 virtual ~CovMapFuncRecordReader() {}
317320 template
318 static ErrorOr>
321 static Expected>
319322 get(coverage::CovMapVersion Version, InstrProfSymtab &P,
320323 std::vector &R,
321324 std::vector &F);
343346 : ProfileNames(P), Filenames(F), Records(R) {}
344347 ~VersionedCovMapFuncRecordReader() override {}
345348
346 std::error_code readFunctionRecords(const char *&Buf,
347 const char *End) override {
349 Error readFunctionRecords(const char *&Buf, const char *End) override {
348350 using namespace support;
349351 if (Buf + sizeof(CovMapHeader) > End)
350 return coveragemap_error::malformed;
352 return make_error(coveragemap_error::malformed);
351353 auto CovHeader = reinterpret_cast(Buf);
352354 uint32_t NRecords = CovHeader->getNRecords();
353355 uint32_t FilenamesSize = CovHeader->getFilenamesSize();
362364
363365 // Get the filenames.
364366 if (Buf + FilenamesSize > End)
365 return coveragemap_error::malformed;
367 return make_error(coveragemap_error::malformed);
366368 size_t FilenamesBegin = Filenames.size();
367369 RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
368370 if (auto Err = Reader.read())
375377 const char *CovEnd = Buf;
376378
377379 if (Buf > End)
378 return coveragemap_error::malformed;
380 return make_error(coveragemap_error::malformed);
379381 // Each coverage map has an alignment of 8, so we need to adjust alignment
380382 // before reading the next map.
381383 Buf += alignmentAdjustment(Buf, 8);
388390
389391 // Now use that to read the coverage data.
390392 if (CovBuf + DataSize > CovEnd)
391 return coveragemap_error::malformed;
393 return make_error(coveragemap_error::malformed);
392394 auto Mapping = StringRef(CovBuf, DataSize);
393395 CovBuf += DataSize;
394396
402404 }
403405
404406 StringRef FuncName;
405 if (std::error_code EC =
406 CFR->template getFuncName(ProfileNames, FuncName))
407 return EC;
407 if (Error E = CFR->template getFuncName(ProfileNames, FuncName))
408 return E;
408409 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
409410 Version, FuncName, FuncHash, Mapping, FilenamesBegin,
410411 Filenames.size() - FilenamesBegin));
411412 CFR++;
412413 }
413 return std::error_code();
414 return Error::success();
414415 }
415416 };
416417 } // end anonymous namespace
417418
418419 template
419 ErrorOr> CovMapFuncRecordReader::get(
420 Expected> CovMapFuncRecordReader::get(
420421 coverage::CovMapVersion Version, InstrProfSymtab &P,
421422 std::vector &R,
422423 std::vector &F) {
427428 CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F);
428429 case CovMapVersion::Version2:
429430 // Decompress the name data.
430 if (auto EC = P.create(P.getNameData()))
431 return EC;
431 if (Error E = P.create(P.getNameData()))
432 return std::move(E);
432433 return llvm::make_unique
433434 CovMapVersion::Version2, IntPtrT, Endian>>(P, R, F);
434435 }
436437 }
437438
438439 template
439 static std::error_code readCoverageMappingData(
440 static Error readCoverageMappingData(
440441 InstrProfSymtab &ProfileNames, StringRef Data,
441442 std::vector &Records,
442443 std::vector &Filenames) {
446447 reinterpret_cast(Data.data());
447448 CovMapVersion Version = (CovMapVersion)CovHeader->getVersion();
448449 if (Version > coverage::CovMapVersion::CurrentVersion)
449 return coveragemap_error::unsupported_version;
450 ErrorOr> ReaderErrorOr =
450 return make_error(coveragemap_error::unsupported_version);
451 Expected> ReaderExpected =
451452 CovMapFuncRecordReader::get(Version, ProfileNames, Records,
452453 Filenames);
453 if (auto EC = ReaderErrorOr.getError())
454 return EC;
455 auto Reader = std::move(ReaderErrorOr.get());
454 if (Error E = ReaderExpected.takeError())
455 return E;
456 auto Reader = std::move(ReaderExpected.get());
456457 for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
457 if (std::error_code EC = Reader->readFunctionRecords(Buf, End))
458 return EC;
459 }
460 return std::error_code();
458 if (Error E = Reader->readFunctionRecords(Buf, End))
459 return E;
460 }
461 return Error::success();
461462 }
462463 static const char *TestingFormatMagic = "llvmcovmtestdata";
463464
464 static std::error_code loadTestingFormat(StringRef Data,
465 InstrProfSymtab &ProfileNames,
466 StringRef &CoverageMapping,
467 uint8_t &BytesInAddress,
468 support::endianness &Endian) {
465 static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames,
466 StringRef &CoverageMapping,
467 uint8_t &BytesInAddress,
468 support::endianness &Endian) {
469469 BytesInAddress = 8;
470470 Endian = support::endianness::little;
471471
472472 Data = Data.substr(StringRef(TestingFormatMagic).size());
473473 if (Data.size() < 1)
474 return coveragemap_error::truncated;
474 return make_error(coveragemap_error::truncated);
475475 unsigned N = 0;
476476 auto ProfileNamesSize =
477477 decodeULEB128(reinterpret_cast(Data.data()), &N);
478478 if (N > Data.size())
479 return coveragemap_error::malformed;
479 return make_error(coveragemap_error::malformed);
480480 Data = Data.substr(N);
481481 if (Data.size() < 1)
482 return coveragemap_error::truncated;
482 return make_error(coveragemap_error::truncated);
483483 N = 0;
484484 uint64_t Address =
485485 decodeULEB128(reinterpret_cast(Data.data()), &N);
486486 if (N > Data.size())
487 return coveragemap_error::malformed;
487 return make_error(coveragemap_error::malformed);
488488 Data = Data.substr(N);
489489 if (Data.size() < ProfileNamesSize)
490 return coveragemap_error::malformed;
491 ProfileNames.create(Data.substr(0, ProfileNamesSize), Address);
490 return make_error(coveragemap_error::malformed);
491 if (Error E = ProfileNames.create(Data.substr(0, ProfileNamesSize), Address))
492 return E;
492493 CoverageMapping = Data.substr(ProfileNamesSize);
493 return std::error_code();
494 }
495
496 static ErrorOr lookupSection(ObjectFile &OF, StringRef Name) {
494 return Error::success();
495 }
496
497 static Expected lookupSection(ObjectFile &OF, StringRef Name) {
497498 StringRef FoundName;
498499 for (const auto &Section : OF.sections()) {
499500 if (auto EC = Section.getName(FoundName))
500 return EC;
501 return errorCodeToError(EC);
501502 if (FoundName == Name)
502503 return Section;
503504 }
504 return coveragemap_error::no_data_found;
505 }
506
507 static std::error_code
508 loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames,
509 StringRef &CoverageMapping, uint8_t &BytesInAddress,
510 support::endianness &Endian, StringRef Arch) {
505 return make_error(coveragemap_error::no_data_found);
506 }
507
508 static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer,
509 InstrProfSymtab &ProfileNames,
510 StringRef &CoverageMapping,
511 uint8_t &BytesInAddress,
512 support::endianness &Endian, StringRef Arch) {
511513 auto BinOrErr = object::createBinary(ObjectBuffer);
512514 if (!BinOrErr)
513 return errorToErrorCode(BinOrErr.takeError());
515 return BinOrErr.takeError();
514516 auto Bin = std::move(BinOrErr.get());
515517 std::unique_ptr OF;
516518 if (auto *Universal = dyn_cast(Bin.get())) {
517519 // If we have a universal binary, try to look up the object for the
518520 // appropriate architecture.
519521 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
520 if (std::error_code EC = ObjectFileOrErr.getError())
521 return EC;
522 if (auto EC = ObjectFileOrErr.getError())
523 return errorCodeToError(EC);
522524 OF = std::move(ObjectFileOrErr.get());
523525 } else if (isa(Bin.get())) {
524526 // For any other object file, upcast and take ownership.
525527 OF.reset(cast(Bin.release()));
526528 // If we've asked for a particular arch, make sure they match.
527529 if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
528 return object_error::arch_not_found;
530 return errorCodeToError(object_error::arch_not_found);
529531 } else
530532 // We can only handle object files.
531 return coveragemap_error::malformed;
533 return make_error(coveragemap_error::malformed);
532534
533535 // The coverage uses native pointer sizes for the object it's written in.
534536 BytesInAddress = OF->getBytesInAddress();
537539
538540 // Look for the sections that we are interested in.
539541 auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false));
540 if (auto EC = NamesSection.getError())
541 return EC;
542 if (auto E = NamesSection.takeError())
543 return E;
542544 auto CoverageSection =
543545 lookupSection(*OF, getInstrProfCoverageSectionName(false));
544 if (auto EC = CoverageSection.getError())
545 return EC;
546 if (auto E = CoverageSection.takeError())
547 return E;
546548
547549 // Get the contents of the given sections.
548 if (std::error_code EC = CoverageSection->getContents(CoverageMapping))
549 return EC;
550 if (std::error_code EC = ProfileNames.create(*NamesSection))
551 return EC;
552
553 return std::error_code();
554 }
555
556 ErrorOr>
550 if (auto EC = CoverageSection->getContents(CoverageMapping))
551 return errorCodeToError(EC);
552 if (Error E = ProfileNames.create(*NamesSection))
553 return E;
554
555 return Error::success();
556 }
557
558 Expected>
557559 BinaryCoverageReader::create(std::unique_ptr &ObjectBuffer,
558560 StringRef Arch) {
559561 std::unique_ptr Reader(new BinaryCoverageReader());
561563 StringRef Coverage;
562564 uint8_t BytesInAddress;
563565 support::endianness Endian;
564 std::error_code EC;
566 Error E;
567 consumeError(std::move(E));
565568 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
566569 // This is a special format used for testing.
567 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
568 Coverage, BytesInAddress, Endian);
570 E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
571 Coverage, BytesInAddress, Endian);
569572 else
570 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
571 Coverage, BytesInAddress, Endian, Arch);
572 if (EC)
573 return EC;
573 E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
574 Coverage, BytesInAddress, Endian, Arch);
575 if (E)
576 return std::move(E);
574577
575578 if (BytesInAddress == 4 && Endian == support::endianness::little)
576 EC = readCoverageMappingData(
579 E = readCoverageMappingData(
577580 Reader->ProfileNames, Coverage, Reader->MappingRecords,
578581 Reader->Filenames);
579582 else if (BytesInAddress == 4 && Endian == support::endianness::big)
580 EC = readCoverageMappingData(
583 E = readCoverageMappingData(
581584 Reader->ProfileNames, Coverage, Reader->MappingRecords,
582585 Reader->Filenames);
583586 else if (BytesInAddress == 8 && Endian == support::endianness::little)
584 EC = readCoverageMappingData(
587 E = readCoverageMappingData(
585588 Reader->ProfileNames, Coverage, Reader->MappingRecords,
586589 Reader->Filenames);
587590 else if (BytesInAddress == 8 && Endian == support::endianness::big)
588 EC = readCoverageMappingData(
591 E = readCoverageMappingData(
589592 Reader->ProfileNames, Coverage, Reader->MappingRecords,
590593 Reader->Filenames);
591594 else
592 return coveragemap_error::malformed;
593 if (EC)
594 return EC;
595 return make_error(coveragemap_error::malformed);
596 if (E)
597 return std::move(E);
595598 return std::move(Reader);
596599 }
597600
598 std::error_code
599 BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
601 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
600602 if (CurrentRecord >= MappingRecords.size())
601 return coveragemap_error::eof;
603 return make_error(coveragemap_error::eof);
602604
603605 FunctionsFilenames.clear();
604606 Expressions.clear();
618620 Record.MappingRegions = MappingRegions;
619621
620622 ++CurrentRecord;
621 return std::error_code();
622 }
623 return Error::success();
624 }
2626 using namespace llvm;
2727
2828 namespace {
29 std::string getInstrProfErrString(instrprof_error Err) {
30 switch (Err) {
31 case instrprof_error::success:
32 return "Success";
33 case instrprof_error::eof:
34 return "End of File";
35 case instrprof_error::unrecognized_format:
36 return "Unrecognized instrumentation profile encoding format";
37 case instrprof_error::bad_magic:
38 return "Invalid instrumentation profile data (bad magic)";
39 case instrprof_error::bad_header:
40 return "Invalid instrumentation profile data (file header is corrupt)";
41 case instrprof_error::unsupported_version:
42 return "Unsupported instrumentation profile format version";
43 case instrprof_error::unsupported_hash_type:
44 return "Unsupported instrumentation profile hash type";
45 case instrprof_error::too_large:
46 return "Too much profile data";
47 case instrprof_error::truncated:
48 return "Truncated profile data";
49 case instrprof_error::malformed:
50 return "Malformed instrumentation profile data";
51 case instrprof_error::unknown_function:
52 return "No profile data available for function";
53 case instrprof_error::hash_mismatch:
54 return "Function control flow change detected (hash mismatch)";
55 case instrprof_error::count_mismatch:
56 return "Function basic block count change detected (counter mismatch)";
57 case instrprof_error::counter_overflow:
58 return "Counter overflow";
59 case instrprof_error::value_site_count_mismatch:
60 return "Function value site count change detected (counter mismatch)";
61 case instrprof_error::compress_failed:
62 return "Failed to compress data (zlib)";
63 case instrprof_error::uncompress_failed:
64 return "Failed to uncompress data (zlib)";
65 }
66 llvm_unreachable("A value of instrprof_error has no message.");
67 }
68
2969 class InstrProfErrorCategoryType : public std::error_category {
3070 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
3171 std::string message(int IE) const override {
32 instrprof_error E = static_cast(IE);
33 switch (E) {
34 case instrprof_error::success:
35 return "Success";
36 case instrprof_error::eof:
37 return "End of File";
38 case instrprof_error::unrecognized_format:
39 return "Unrecognized instrumentation profile encoding format";
40 case instrprof_error::bad_magic:
41 return "Invalid instrumentation profile data (bad magic)";
42 case instrprof_error::bad_header:
43 return "Invalid instrumentation profile data (file header is corrupt)";
44 case instrprof_error::unsupported_version:
45 return "Unsupported instrumentation profile format version";
46 case instrprof_error::unsupported_hash_type:
47 return "Unsupported instrumentation profile hash type";
48 case instrprof_error::too_large:
49 return "Too much profile data";
50 case instrprof_error::truncated:
51 return "Truncated profile data";
52 case instrprof_error::malformed:
53 return "Malformed instrumentation profile data";
54 case instrprof_error::unknown_function:
55 return "No profile data available for function";
56 case instrprof_error::hash_mismatch:
57 return "Function control flow change detected (hash mismatch)";
58 case instrprof_error::count_mismatch:
59 return "Function basic block count change detected (counter mismatch)";
60 case instrprof_error::counter_overflow:
61 return "Counter overflow";
62 case instrprof_error::value_site_count_mismatch:
63 return "Function value site count change detected (counter mismatch)";
64 case instrprof_error::compress_failed:
65 return "Failed to compress data (zlib)";
66 case instrprof_error::uncompress_failed:
67 return "Failed to uncompress data (zlib)";
68 }
69 llvm_unreachable("A value of instrprof_error has no message.");
72 return getInstrProfErrString(static_cast(IE));
7073 }
7174 };
7275 } // end anonymous namespace
104107 }
105108 }
106109
110 std::string InstrProfError::message() const {
111 return getInstrProfErrString(Err);
112 }
113
114 template <> char ProfErrorInfoBase::ID = 0;
115
107116 std::string getPGOFuncName(StringRef RawFuncName,
108117 GlobalValue::LinkageTypes Linkage,
109118 StringRef FileName,
213222 finalizeSymtab();
214223 }
215224
216 std::error_code
217 collectPGOFuncNameStrings(const std::vector &NameStrs,
218 bool doCompression, std::string &Result) {
225 Error collectPGOFuncNameStrings(const std::vector &NameStrs,
226 bool doCompression, std::string &Result) {
219227 assert(NameStrs.size() && "No name data to emit");
220228
221229 uint8_t Header[16], *P = Header;
237245 unsigned HeaderLen = P - &Header[0];
238246 Result.append(HeaderStr, HeaderLen);
239247 Result += InputStr;
240 return make_error_code(instrprof_error::success);
248 return Error::success();
241249 };
242250
243 if (!doCompression)
251 if (!doCompression) {
244252 return WriteStringToResult(0, UncompressedNameStrings);
253 }
245254
246255 SmallVector CompressedNameStrings;
247256 zlib::Status Success =
249258 zlib::BestSizeCompression);
250259
251260 if (Success != zlib::StatusOK)
252 return make_error_code(instrprof_error::compress_failed);
261 return make_error(instrprof_error::compress_failed);
253262
254263 return WriteStringToResult(
255264 CompressedNameStrings.size(),
263272 return NameStr;
264273 }
265274
266 std::error_code
267 collectPGOFuncNameStrings(const std::vector &NameVars,
268 std::string &Result, bool doCompression) {
275 Error collectPGOFuncNameStrings(const std::vector &NameVars,
276 std::string &Result, bool doCompression) {
269277 std::vector NameStrs;
270278 for (auto *NameVar : NameVars) {
271279 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
274282 NameStrs, zlib::isAvailable() && doCompression, Result);
275283 }
276284
277 std::error_code readPGOFuncNameStrings(StringRef NameStrings,
278 InstrProfSymtab &Symtab) {
285 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
279286 const uint8_t *P = reinterpret_cast(NameStrings.data());
280287 const uint8_t *EndP = reinterpret_cast(NameStrings.data() +
281288 NameStrings.size());
293300 CompressedSize);
294301 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
295302 UncompressedSize) != zlib::StatusOK)
296 return make_error_code(instrprof_error::uncompress_failed);
303 return make_error(instrprof_error::uncompress_failed);
297304 P += CompressedSize;
298305 NameStrings = StringRef(UncompressedNameStrings.data(),
299306 UncompressedNameStrings.size());
312319 P++;
313320 }
314321 Symtab.finalizeSymtab();
315 return make_error_code(instrprof_error::success);
322 return Error::success();
316323 }
317324
318325 void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
576583 ValueProfData());
577584 }
578585
579 instrprof_error ValueProfData::checkIntegrity() {
586 Error ValueProfData::checkIntegrity() {
580587 if (NumValueKinds > IPVK_Last + 1)
581 return instrprof_error::malformed;
588 return make_error(instrprof_error::malformed);
582589 // Total size needs to be mulltiple of quadword size.
583590 if (TotalSize % sizeof(uint64_t))
584 return instrprof_error::malformed;
591 return make_error(instrprof_error::malformed);
585592
586593 ValueProfRecord *VR = getFirstValueProfRecord(this);
587594 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
588595 if (VR->Kind > IPVK_Last)
589 return instrprof_error::malformed;
596 return make_error(instrprof_error::malformed);
590597 VR = getValueProfRecordNext(VR);
591598 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
592 return instrprof_error::malformed;
593 }
594 return instrprof_error::success;
595 }
596
597 ErrorOr>
599 return make_error(instrprof_error::malformed);
600 }
601 return Error::success();
602 }
603
604 Expected>
598605 ValueProfData::getValueProfData(const unsigned char *D,
599606 const unsigned char *const BufferEnd,
600607 support::endianness Endianness) {
601608 using namespace support;
602609 if (D + sizeof(ValueProfData) > BufferEnd)
603 return instrprof_error::truncated;
610 return make_error(instrprof_error::truncated);
604611
605612 const unsigned char *Header = D;
606613 uint32_t TotalSize = swapToHostOrder(Header, Endianness);
607614 if (D + TotalSize > BufferEnd)
608 return instrprof_error::too_large;
615 return make_error(instrprof_error::too_large);
609616
610617 std::unique_ptr VPD = allocValueProfData(TotalSize);
611618 memcpy(VPD.get(), D, TotalSize);
612619 // Byte swap.
613620 VPD->swapBytesToHost(Endianness);
614621
615 instrprof_error EC = VPD->checkIntegrity();
616 if (EC != instrprof_error::success)
617 return EC;
622 Error E = VPD->checkIntegrity();
623 if (E)
624 return std::move(E);
618625
619626 return std::move(VPD);
620627 }
1717
1818 using namespace llvm;
1919
20 static ErrorOr>
20 static Expected>
2121 setupMemoryBuffer(std::string Path) {
2222 ErrorOr> BufferOrErr =
2323 MemoryBuffer::getFileOrSTDIN(Path);
2424 if (std::error_code EC = BufferOrErr.getError())
25 return EC;
25 return errorCodeToError(EC);
2626 return std::move(BufferOrErr.get());
2727 }
2828
29 static std::error_code initializeReader(InstrProfReader &Reader) {
29 static Error initializeReader(InstrProfReader &Reader) {
3030 return Reader.readHeader();
3131 }
3232
33 ErrorOr>
33 Expected>
3434 InstrProfReader::create(std::string Path) {
3535 // Set up the buffer to read.
3636 auto BufferOrError = setupMemoryBuffer(Path);
37 if (std::error_code EC = BufferOrError.getError())
38 return EC;
37 if (Error E = BufferOrError.takeError())
38 return std::move(E);
3939 return InstrProfReader::create(std::move(BufferOrError.get()));
4040 }
4141
42 ErrorOr>
42 Expected>
4343 InstrProfReader::create(std::unique_ptr Buffer) {
4444 // Sanity check the buffer.
4545 if (Buffer->getBufferSize() > std::numeric_limits::max())
46 return instrprof_error::too_large;
46 return make_error(instrprof_error::too_large);
4747
4848 std::unique_ptr Result;
4949 // Create the reader.
5656 else if (TextInstrProfReader::hasFormat(*Buffer))
5757 Result.reset(new TextInstrProfReader(std::move(Buffer)));
5858 else
59 return instrprof_error::unrecognized_format;
59 return make_error(instrprof_error::unrecognized_format);
6060
6161 // Initialize the reader and return the result.
62 if (std::error_code EC = initializeReader(*Result))
63 return EC;
62 if (Error E = initializeReader(*Result))
63 return std::move(E);
6464
6565 return std::move(Result);
6666 }
6767
68 ErrorOr>
68 Expected>
6969 IndexedInstrProfReader::create(std::string Path) {
7070 // Set up the buffer to read.
7171 auto BufferOrError = setupMemoryBuffer(Path);
72 if (std::error_code EC = BufferOrError.getError())
73 return EC;
72 if (Error E = BufferOrError.takeError())
73 return std::move(E);
7474 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
7575 }
7676
7777
78 ErrorOr>
78 Expected>
7979 IndexedInstrProfReader::create(std::unique_ptr Buffer) {
8080 // Sanity check the buffer.
8181 if (Buffer->getBufferSize() > std::numeric_limits::max())
82 return instrprof_error::too_large;
82 return make_error(instrprof_error::too_large);
8383
8484 // Create the reader.
8585 if (!IndexedInstrProfReader::hasFormat(*Buffer))
86 return instrprof_error::bad_magic;
86 return make_error(instrprof_error::bad_magic);
8787 auto Result = llvm::make_unique(std::move(Buffer));
8888
8989 // Initialize the reader and return the result.
90 if (std::error_code EC = initializeReader(*Result))
91 return EC;
90 if (Error E = initializeReader(*Result))
91 return std::move(E);
9292
9393 return std::move(Result);
9494 }
9595
9696 void InstrProfIterator::Increment() {
97 if (Reader->readNextRecord(Record))
97 if (auto E = Reader->readNextRecord(Record)) {
98 // Handle errors in the reader.
99 InstrProfError::take(std::move(E));
98100 *this = InstrProfIterator();
101 }
99102 }
100103
101104 bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
111114 // Read the profile variant flag from the header: ":FE" means this is a FE
112115 // generated profile. ":IR" means this is an IR level profile. Other strings
113116 // with a leading ':' will be reported an error format.
114 std::error_code TextInstrProfReader::readHeader() {
117 Error TextInstrProfReader::readHeader() {
115118 Symtab.reset(new InstrProfSymtab());
116119 bool IsIRInstr = false;
117120 if (!Line->startswith(":")) {
124127 else if (Str.equals_lower("fe"))
125128 IsIRInstr = false;
126129 else
127 return instrprof_error::bad_header;
130 return error(instrprof_error::bad_header);
128131
129132 ++Line;
130133 IsIRLevelProfile = IsIRInstr;
131134 return success();
132135 }
133136
134 std::error_code
137 Error
135138 TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
136139
137140 #define CHECK_LINE_END(Line) \
195198 #undef VP_READ_ADVANCE
196199 }
197200
198 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
201 Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
199202 // Skip empty lines and comments.
200203 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
201204 ++Line;
237240 }
238241
239242 // Check if value profile data exists and read it if so.
240 if (std::error_code EC = readValueProfileData(Record))
241 return EC;
243 if (Error E = readValueProfileData(Record))
244 return E;
242245
243246 // This is needed to avoid two pass parsing because llvm-profdata
244247 // does dumping while reading.
257260 }
258261
259262 template
260 std::error_code RawInstrProfReader::readHeader() {
263 Error RawInstrProfReader::readHeader() {
261264 if (!hasFormat(*DataBuffer))
262265 return error(instrprof_error::bad_magic);
263266 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
269272 }
270273
271274 template
272 std::error_code
273 RawInstrProfReader::readNextHeader(const char *CurrentPos) {
275 Error RawInstrProfReader::readNextHeader(const char *CurrentPos) {
274276 const char *End = DataBuffer->getBufferEnd();
275277 // Skip zero padding between profiles.
276278 while (CurrentPos != End && *CurrentPos == 0)
277279 ++CurrentPos;
278280 // If there's nothing left, we're done.
279281 if (CurrentPos == End)
280 return instrprof_error::eof;
282 return make_error(instrprof_error::eof);
281283 // If there isn't enough space for another header, this is probably just
282284 // garbage at the end of the file.
283285 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
284 return instrprof_error::malformed;
286 return make_error(instrprof_error::malformed);
285287 // The writer ensures each profile is padded to start at an aligned address.
286288 if (reinterpret_cast(CurrentPos) % alignOf())
287 return instrprof_error::malformed;
289 return make_error(instrprof_error::malformed);
288290 // The magic should have the same byte order as in the previous header.
289291 uint64_t Magic = *reinterpret_cast(CurrentPos);
290292 if (Magic != swap(RawInstrProf::getMagic()))
291 return instrprof_error::bad_magic;
293 return make_error(instrprof_error::bad_magic);
292294
293295 // There's another profile to read, so we need to process the header.
294296 auto *Header = reinterpret_cast(CurrentPos);
296298 }
297299
298300 template
299 std::error_code
300 RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) {
301 std::error_code EC = Symtab.create(StringRef(NamesStart, NamesSize));
302 if (EC)
303 return EC;
301 Error RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) {
302 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
303 return error(std::move(E));
304304 for (const RawInstrProf::ProfileData *I = Data; I != DataEnd; ++I) {
305305 const IntPtrT FPtr = swap(I->FunctionPointer);
306306 if (!FPtr)
312312 }
313313
314314 template
315 std::error_code
316 RawInstrProfReader::readHeader(const RawInstrProf::Header &Header) {
315 Error RawInstrProfReader::readHeader(
316 const RawInstrProf::Header &Header) {
317317 Version = swap(Header.Version);
318318 if (GET_VERSION(Version) != RawInstrProf::Version)
319319 return error(instrprof_error::unsupported_version);
345345 ValueDataStart = reinterpret_cast(Start + ValueDataOffset);
346346
347347 std::unique_ptr NewSymtab = make_unique();
348 if (auto EC = createSymtab(*NewSymtab.get()))
349 return EC;
348 if (Error E = createSymtab(*NewSymtab.get()))
349 return E;
350350
351351 Symtab = std::move(NewSymtab);
352352 return success();
353353 }
354354
355355 template
356 std::error_code RawInstrProfReader::readName(InstrProfRecord &Record) {
356 Error RawInstrProfReader::readName(InstrProfRecord &Record) {
357357 Record.Name = getName(Data->NameRef);
358358 return success();
359359 }
360360
361361 template
362 std::error_code RawInstrProfReader::readFuncHash(
363 InstrProfRecord &Record) {
362 Error RawInstrProfReader::readFuncHash(InstrProfRecord &Record) {
364363 Record.Hash = swap(Data->FuncHash);
365364 return success();
366365 }
367366
368367 template
369 std::error_code RawInstrProfReader::readRawCounts(
368 Error RawInstrProfReader::readRawCounts(
370369 InstrProfRecord &Record) {
371370 uint32_t NumCounters = swap(Data->NumCounters);
372371 IntPtrT CounterPtr = Data->CounterPtr;
393392 }
394393
395394 template
396 std::error_code
397 RawInstrProfReader::readValueProfilingData(InstrProfRecord &Record) {
395 Error RawInstrProfReader::readValueProfilingData(
396 InstrProfRecord &Record) {
398397
399398 Record.clearValueData();
400399 CurValueDataSize = 0;
406405 if (!NumValueKinds)
407406 return success();
408407
409 ErrorOr> VDataPtrOrErr =
408 Expected> VDataPtrOrErr =
410409 ValueProfData::getValueProfData(
411410 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
412411 getDataEndianness());
413412
414 if (VDataPtrOrErr.getError())
415 return VDataPtrOrErr.getError();
413 if (Error E = VDataPtrOrErr.takeError())
414 return E;
416415
417416 // Note that besides deserialization, this also performs the conversion for
418417 // indirect call targets. The function pointers from the raw profile are
423422 }
424423
425424 template
426 std::error_code
427 RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
425 Error RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
428426 if (atEnd())
429427 // At this point, ValueDataStart field points to the next header.
430 if (std::error_code EC = readNextHeader(getNextHeaderPos()))
431 return EC;
428 if (Error E = readNextHeader(getNextHeaderPos()))
429 return E;
432430
433431 // Read name ad set it in Record.
434 if (std::error_code EC = readName(Record))
435 return EC;
432 if (Error E = readName(Record))
433 return E;
436434
437435 // Read FuncHash and set it in Record.
438 if (std::error_code EC = readFuncHash(Record))
439 return EC;
436 if (Error E = readFuncHash(Record))
437 return E;
440438
441439 // Read raw counts and set Record.
442 if (std::error_code EC = readRawCounts(Record))
443 return EC;
440 if (Error E = readRawCounts(Record))
441 return E;
444442
445443 // Read value data and set Record.
446 if (std::error_code EC = readValueProfilingData(Record))
447 return EC;
444 if (Error E = readValueProfilingData(Record))
445 return E;
448446
449447 // Iterate.
450448 advanceData();
466464
467465 bool InstrProfLookupTrait::readValueProfilingData(
468466 const unsigned char *&D, const unsigned char *const End) {
469 ErrorOr> VDataPtrOrErr =
467 Expected> VDataPtrOrErr =
470468 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
471469
472 if (VDataPtrOrErr.getError())
470 if (VDataPtrOrErr.takeError())
473471 return false;
474472
475473 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
525523 }
526524
527525 template
528 std::error_code InstrProfReaderIndex::getRecords(
526 Error InstrProfReaderIndex::getRecords(
529527 StringRef FuncName, ArrayRef &Data) {
530528 auto Iter = HashTable->find(FuncName);
531529 if (Iter == HashTable->end())
532 return instrprof_error::unknown_function;
530 return make_error(instrprof_error::unknown_function);
533531
534532 Data = (*Iter);
535533 if (Data.empty())
536 return instrprof_error::malformed;
537
538 return instrprof_error::success;
534 return make_error(instrprof_error::malformed);
535
536 return Error::success();
539537 }
540538
541539 template
542 std::error_code InstrProfReaderIndex::getRecords(
540 Error InstrProfReaderIndex::getRecords(
543541 ArrayRef &Data) {
544542 if (atEnd())
545 return instrprof_error::eof;
543 return make_error(instrprof_error::eof);
546544
547545 Data = *RecordIterator;
548546
549547 if (Data.empty())
550 return instrprof_error::malformed;
551
552 return instrprof_error::success;
548 return make_error(instrprof_error::malformed);
549
550 return Error::success();
553551 }
554552
555553 template
608606 }
609607 }
610608
611 std::error_code IndexedInstrProfReader::readHeader() {
609 Error IndexedInstrProfReader::readHeader() {
612610 const unsigned char *Start =
613611 (const unsigned char *)DataBuffer->getBufferStart();
614612 const unsigned char *Cur = Start;
660658 return *Symtab.get();
661659 }
662660
663 ErrorOr
661 Expected
664662 IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
665663 uint64_t FuncHash) {
666664 ArrayRef Data;
667 std::error_code EC = Index->getRecords(FuncName, Data);
668 if (EC != instrprof_error::success)
669 return EC;
665 Error Err = Index->getRecords(FuncName, Data);
666 if (Err)
667 return std::move(Err);
670668 // Found it. Look for counters with the right hash.
671669 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
672670 // Check for a match and fill the vector if there is one.
677675 return error(instrprof_error::hash_mismatch);
678676 }
679677
680 std::error_code
681 IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
682 std::vector &Counts) {
683 ErrorOr Record = getInstrProfRecord(FuncName, FuncHash);
684 if (std::error_code EC = Record.getError())
685 return EC;
678 Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
679 uint64_t FuncHash,
680 std::vector &Counts) {
681 Expected Record = getInstrProfRecord(FuncName, FuncHash);
682 if (Error E = Record.takeError())
683 return error(std::move(E));
686684
687685 Counts = Record.get().Counts;
688686 return success();
689687 }
690688
691 std::error_code IndexedInstrProfReader::readNextRecord(
692 InstrProfRecord &Record) {
689 Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
693690 static unsigned RecordIndex = 0;
694691
695692 ArrayRef Data;
696693
697 std::error_code EC = Index->getRecords(Data);
698 if (EC != instrprof_error::success)
699 return error(EC);
694 Error E = Index->getRecords(Data);
695 if (E)
696 return error(std::move(E));
700697
701698 Record = Data[RecordIndex++];
702699 if (RecordIndex >= Data.size()) {
155155 this->Sparse = Sparse;
156156 }
157157
158 std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
159 uint64_t Weight) {
158 Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) {
160159 auto &ProfileDataMap = FunctionData[I.Name];
161160
162161 bool NewFunc;
179178
180179 Dest.sortValueData();
181180
182 return Dest.getError();
181 return Dest.takeError();
183182 }
184183
185184 bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
373373 return;
374374
375375 std::string CompressedNameStr;
376 if (auto EC = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
376 if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
377377 DoNameCompression)) {
378 llvm::report_fatal_error(EC.message(), false);
378 llvm::report_fatal_error(toString(std::move(E)), false);
379379 }
380380
381381 auto &Ctx = M->getContext();
581581 // Return true if the profile are successfully read, and false on errors.
582582 bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader) {
583583 auto &Ctx = M->getContext();
584 ErrorOr Result =
584 Expected Result =
585585 PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);
586 if (std::error_code EC = Result.getError()) {
587 if (EC == instrprof_error::unknown_function) {
588 NumOfPGOMissing++;
589 if (NoPGOWarnMissing)
590 return false;
591 } else if (EC == instrprof_error::hash_mismatch ||
592 EC == llvm::instrprof_error::malformed) {
593 NumOfPGOMismatch++;
594 if (NoPGOWarnMismatch)
595 return false;
596 }
597
598 std::string Msg = EC.message() + std::string(" ") + F.getName().str();
599 Ctx.diagnose(
600 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
586 if (Error E = Result.takeError()) {
587 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
588 auto Err = IPE.get();
589 bool SkipWarning = false;
590 if (Err == instrprof_error::unknown_function) {
591 NumOfPGOMissing++;
592 SkipWarning = NoPGOWarnMissing;
593 } else if (Err == instrprof_error::hash_mismatch ||
594 Err == instrprof_error::malformed) {
595 NumOfPGOMismatch++;
596 SkipWarning = NoPGOWarnMismatch;
597 }
598
599 if (SkipWarning)
600 return;
601
602 std::string Msg = IPE.message() + std::string(" ") + F.getName().str();
603 Ctx.diagnose(
604 DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning));
605 });
601606 return false;
602607 }
603608 ProfileRecord = std::move(Result.get());
853858 auto &Ctx = M.getContext();
854859 // Read the counter array from file.
855860 auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName);
856 if (std::error_code EC = ReaderOrErr.getError()) {
857 Ctx.diagnose(
858 DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message()));
861 if (Error E = ReaderOrErr.takeError()) {
862 handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
863 Ctx.diagnose(
864 DiagnosticInfoPGOProfile(ProfileFileName.data(), EI.message()));
865 });
859866 return false;
860867 }
861868
209209 errs() << "warning: profile data may be out of date - object is newer\n";
210210 auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename,
211211 CoverageArch);
212 if (std::error_code EC = CoverageOrErr.getError()) {
212 if (Error E = CoverageOrErr.takeError()) {
213213 colored_ostream(errs(), raw_ostream::RED)
214 << "error: Failed to load coverage: " << EC.message();
215 errs() << "\n";
214 << "error: Failed to load coverage: " << toString(std::move(E)) << "\n";
216215 return nullptr;
217216 }
218217 auto Coverage = std::move(CoverageOrErr.get());
4646 ::exit(1);
4747 }
4848
49 static void exitWithErrorCode(const std::error_code &Error,
50 StringRef Whence = "") {
51 if (Error.category() == instrprof_category()) {
52 instrprof_error instrError = static_cast(Error.value());
53 if (instrError == instrprof_error::unrecognized_format) {
54 // Hint for common error of forgetting -sample for sample profiles.
55 exitWithError(Error.message(), Whence,
56 "Perhaps you forgot to use the -sample option?");
57 }
58 }
59 exitWithError(Error.message(), Whence);
49 static void exitWithError(Error E, StringRef Whence = "") {
50 if (E.isA()) {
51 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
52 instrprof_error instrError = IPE.get();
53 StringRef Hint = "";
54 if (instrError == instrprof_error::unrecognized_format) {
55 // Hint for common error of forgetting -sample for sample profiles.
56 Hint = "Perhaps you forgot to use the -sample option?";
57 }
58 exitWithError(IPE.message(), Whence, Hint);
59 });
60 }
61
62 exitWithError(toString(std::move(E)), Whence);
63 }
64
65 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
66 exitWithError(EC.message(), Whence);
6067 }
6168
6269 namespace {
6370 enum ProfileKinds { instr, sample };
6471 }
6572
66 static void handleMergeWriterError(std::error_code &Error,
67 StringRef WhenceFile = "",
73 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
6874 StringRef WhenceFunction = "",
6975 bool ShowHint = true) {
7076 if (!WhenceFile.empty())
7177 errs() << WhenceFile << ": ";
7278 if (!WhenceFunction.empty())
7379 errs() << WhenceFunction << ": ";
74 errs() << Error.message() << "\n";
80
81 auto IPE = instrprof_error::success;
82 E = handleErrors(std::move(E),
83 [&IPE](std::unique_ptr E) -> Error {
84 IPE = E->get();
85 return Error(std::move(E));
86 });
87 errs() << toString(std::move(E)) << "\n";
7588
7689 if (ShowHint) {
7790 StringRef Hint = "";
78 if (Error.category() == instrprof_category()) {
79 instrprof_error instrError = static_cast(Error.value());
80 switch (instrError) {
91 if (IPE != instrprof_error::success) {
92 switch (IPE) {
8193 case instrprof_error::hash_mismatch:
8294 case instrprof_error::count_mismatch:
8395 case instrprof_error::value_site_count_mismatch:
119131 exitWithErrorCode(EC, OutputFilename);
120132
121133 InstrProfWriter Writer(OutputSparse);
122 SmallSet<std::error_code, 4> WriterErrorCodes;
134 SmallSet<instrprof_error, 4> WriterErrorCodes;
123135 for (const auto &Input : Inputs) {
124136 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
125 if (std::error_code ec = ReaderOrErr.getError())
126 exitWithErrorCode(ec, Input.Filename);
137 if (Error E = ReaderOrErr.takeError())
138 exitWithError(std::move(E), Input.Filename);
127139
128140 auto Reader = std::move(ReaderOrErr.get());
129141 bool IsIRProfile = Reader->isIRLevelProfile();
131143 exitWithError("Merge IR generated profile with Clang generated profile.");
132144
133145 for (auto &I : *Reader) {
134 if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
146 if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
135147 // Only show hint the first time an error occurs.
136 bool firstTime = WriterErrorCodes.insert(EC).second;
137 handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
148 instrprof_error IPE = InstrProfError::take(std::move(E));
149 bool firstTime = WriterErrorCodes.insert(IPE).second;
150 handleMergeWriterError(make_error(IPE), Input.Filename,
151 I.Name, firstTime);
138152 }
139153 }
140154 if (Reader->hasError())
141 exitWithErrorCode(Reader->getError(), Input.Filename);
155 exitWithError(Reader->getError(), Input.Filename);
142156 }
143157 if (OutputFormat == PF_Text)
144158 Writer.writeText(Output);
186200 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
187201 if (Result != sampleprof_error::success) {
188202 std::error_code EC = make_error_code(Result);
189 handleMergeWriterError(EC, Input.Filename, FName);
203 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
190204 }
191205 }
192206 }
267281 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
268282 }
269283 InstrProfSummary PS(Cutoffs);
270 if (std::error_code EC = ReaderOrErr.getError())
271 exitWithErrorCode(EC, Filename);
284 if (Error E = ReaderOrErr.takeError())
285 exitWithError(std::move(E), Filename);
272286
273287 auto Reader = std::move(ReaderOrErr.get());
274288 bool IsIRInstr = Reader->isIRLevelProfile();
334348 }
335349
336350 if (Reader->hasError())
337 exitWithErrorCode(Reader->getError(), Filename);
351 exitWithError(Reader->getError(), Filename);
338352
339353 if (ShowCounts && TextFormat)
340354 return 0;
1919 using namespace llvm;
2020 using namespace coverage;
2121
22 static ::testing::AssertionResult NoError(std::error_code EC) {
23 if (!EC)
22 static ::testing::AssertionResult NoError(Error E) {
23 if (!E)
2424 return ::testing::AssertionSuccess();
25 return ::testing::AssertionFailure() << "error " << EC.value()
26 << ": " << EC.message();
25 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
26 << "\n";
2727 }
2828
2929 namespace llvm {
6969 CoverageMappingReaderMock(ArrayRef Functions)
7070 : Functions(Functions) {}
7171
72 std::error_code readNextRecord(CoverageMappingRecord &Record) override {
72 Error readNextRecord(CoverageMappingRecord &Record) override {
7373 if (Functions.empty())
74 return coveragemap_error::eof;
74 return make_error(coveragemap_error::eof);
7575
7676 Functions.front().fillCoverageMappingRecord(Record);
7777 Functions = Functions.slice(1);
7878
79 return coveragemap_error::success;
79 return Error::success();
8080 }
8181 };
8282
189189 void readProfCounts() {
190190 auto Profile = ProfileWriter.writeBuffer();
191191 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
192 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
192 ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
193193 ProfileReader = std::move(ReaderOrErr.get());
194194 }
195195
199199
200200 CoverageMappingReaderMock CovReader(OutputFunctions);
201201 auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader);
202 ASSERT_TRUE(NoError(CoverageOrErr.getError()));
202 ASSERT_TRUE(NoError(CoverageOrErr.takeError()));
203203 LoadedCoverage = std::move(CoverageOrErr.get());
204204 }
205205 };
1818
1919 using namespace llvm;
2020
21 static ::testing::AssertionResult NoError(std::error_code EC) {
22 if (!EC)
21 static ::testing::AssertionResult NoError(Error E) {
22 if (!E)
2323 return ::testing::AssertionSuccess();
24 return ::testing::AssertionFailure() << "error " << EC.value()
25 << ": " << EC.message();
26 }
27
28 static ::testing::AssertionResult ErrorEquals(std::error_code Expected,
29 std::error_code Found) {
24 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
25 << "\n";
26 }
27
28 static ::testing::AssertionResult ErrorEquals(instrprof_error Expected,
29 Error E) {
30 instrprof_error Found;
31 std::string FoundMsg;
32 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
33 Found = IPE.get();
34 FoundMsg = IPE.message();
35 });
3036 if (Expected == Found)
3137 return ::testing::AssertionSuccess();
32 return ::testing::AssertionFailure() << "error " << Found.value()
33 << ": " << Found.message();
38 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
3439 }
3540
3641 namespace {
4348
4449 void readProfile(std::unique_ptr Profile) {
4550 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
46 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
51 ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
4752 Reader = std::move(ReaderOrErr.get());
4853 }
4954 };
8994 auto Profile = Writer.writeBuffer();
9095 readProfile(std::move(Profile));
9196
92 ErrorOr R = Reader->getInstrProfRecord("foo", 0x1234);
93 ASSERT_TRUE(NoError(R.getError()));
97 Expected R = Reader->getInstrProfRecord("foo", 0x1234);
98 ASSERT_TRUE(NoError(R.takeError()));
9499 ASSERT_EQ(2U, R->Counts.size());
95100 ASSERT_EQ(1U, R->Counts[0]);
96101 ASSERT_EQ(2U, R->Counts[1]);
97102
98103 R = Reader->getInstrProfRecord("foo", 0x1235);
99 ASSERT_TRUE(NoError(R.getError()));
104 ASSERT_TRUE(NoError(R.takeError()));
100105 ASSERT_EQ(2U, R->Counts.size());
101106 ASSERT_EQ(3U, R->Counts[0]);
102107 ASSERT_EQ(4U, R->Counts[1]);
103108
104109 R = Reader->getInstrProfRecord("foo", 0x5678);
105 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError()));
110 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError()));
106111
107112 R = Reader->getInstrProfRecord("bar", 0x1234);
108 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError()));
113 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError()));
109114 }
110115
111116 TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
127132 ASSERT_EQ(3U, Counts[0]);
128133 ASSERT_EQ(4U, Counts[1]);
129134
130 std::error_code EC;
131 EC = Reader->getFunctionCounts("foo", 0x5678, Counts);
132 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC));
133
134 EC = Reader->getFunctionCounts("bar", 0x1234, Counts);
135 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC));
135 Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts);
136 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1)));
137
138 Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts);
139 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2)));
136140 }
137141
138142 // Profile data is copied from general.proftext
234238 auto Profile = Writer.writeBuffer();
235239 readProfile(std::move(Profile));
236240
237 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
238 ASSERT_TRUE(NoError(R.getError()));
241 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
242 ASSERT_TRUE(NoError(R.takeError()));
239243 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
240244 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
241245 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
265269 NoError(Writer.addRecord(std::move(Record)));
266270 auto Profile = Writer.writeBuffer();
267271 readProfile(std::move(Profile));
268 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
269 ASSERT_TRUE(NoError(R.getError()));
272 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
273 ASSERT_TRUE(NoError(R.takeError()));
270274
271275 LLVMContext Ctx;
272276 std::unique_ptr M(new Module("MyModule", Ctx));
377381 auto Profile = Writer.writeBuffer();
378382 readProfile(std::move(Profile));
379383
380 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
381 ASSERT_TRUE(NoError(R.getError()));
384 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
385 ASSERT_TRUE(NoError(R.takeError()));
382386 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
383387 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
384388 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
430434 // Set big endian input.
431435 Reader->setValueProfDataEndianness(support::big);
432436
433 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
434 ASSERT_TRUE(NoError(R.getError()));
437 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
438 ASSERT_TRUE(NoError(R.takeError()));
435439 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
436440 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
437441 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
512516 auto Profile = Writer.writeBuffer();
513517 readProfile(std::move(Profile));
514518
515 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
516 ASSERT_TRUE(NoError(R.getError()));
519 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
520 ASSERT_TRUE(NoError(R.takeError()));
517521 ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget));
518522 ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
519523 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
565569
566570 InstrProfRecord Record1("foo", 0x1234, {1});
567571 auto Result1 = Writer.addRecord(std::move(Record1));
568 ASSERT_EQ(Result1, instrprof_error::success);
572 ASSERT_EQ(InstrProfError::take(std::move(Result1)),
573 instrprof_error::success);
569574
570575 // Verify counter overflow.
571576 InstrProfRecord Record2("foo", 0x1234, {Max});
572577 auto Result2 = Writer.addRecord(std::move(Record2));
573 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
578 ASSERT_EQ(InstrProfError::take(std::move(Result2)),
579 instrprof_error::counter_overflow);
574580
575581 InstrProfRecord Record3(bar, 0x9012, {8});
576582 auto Result3 = Writer.addRecord(std::move(Record3));
577 ASSERT_EQ(Result3, instrprof_error::success);
583 ASSERT_EQ(InstrProfError::take(std::move(Result3)),
584 instrprof_error::success);
578585
579586 InstrProfRecord Record4("baz", 0x5678, {3, 4});
580587 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
581588 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
582589 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
583590 auto Result4 = Writer.addRecord(std::move(Record4));
584 ASSERT_EQ(Result4, instrprof_error::success);
591 ASSERT_EQ(InstrProfError::take(std::move(Result4)),
592 instrprof_error::success);
585593
586594 // Verify value data counter overflow.
587595 InstrProfRecord Record5("baz", 0x5678, {5, 6});
589597 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
590598 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
591599 auto Result5 = Writer.addRecord(std::move(Record5));
592 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
600 ASSERT_EQ(InstrProfError::take(std::move(Result5)),
601 instrprof_error::counter_overflow);
593602
594603 auto Profile = Writer.writeBuffer();
595604 readProfile(std::move(Profile));
596605
597606 // Verify saturation of counts.
598 ErrorOr ReadRecord1 =
607 Expected ReadRecord1 =
599608 Reader->getInstrProfRecord("foo", 0x1234);
600 ASSERT_TRUE(NoError(ReadRecord1.getError()));
609 ASSERT_TRUE(NoError(ReadRecord1.takeError()));
601610 ASSERT_EQ(Max, ReadRecord1->Counts[0]);
602611
603 ErrorOr ReadRecord2 =
612 Expected ReadRecord2 =
604613 Reader->getInstrProfRecord("baz", 0x5678);
614 ASSERT_TRUE(bool(ReadRecord2));
605615 ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget));
606616 std::unique_ptr VD =
607617 ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0);
646656 auto Profile = Writer.writeBuffer();
647657 readProfile(std::move(Profile));
648658
649 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
650 ASSERT_TRUE(NoError(R.getError()));
659 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
660 ASSERT_TRUE(NoError(R.takeError()));
651661 std::unique_ptr VD(
652662 R->getValueForSite(IPVK_IndirectCallTarget, 0));
653663 ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));