llvm.org GIT mirror llvm / 8007bdd
Revert "(HEAD -> master, origin/master, origin/HEAD) [ProfileData] (llvm) Use Error in InstrProf and Coverage, NFC" This reverts commit r269462. It fails two llvm-profdata tests. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269466 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 4 years ago
18 changed file(s) with 547 addition(s) and 645 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"
2526 #include "llvm/Support/raw_ostream.h"
2627 #include
2728 #include
2829
2930 namespace llvm {
3031 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
4940 } // end of coverage namespace.
41 }
42
43 namespace std {
44 template <>
45 struct is_error_code_enum : std::true_type {
46 };
5047 }
5148
5249 namespace llvm {
267264
268265 /// \brief Return the number of times that a region of code associated with
269266 /// this counter was executed.
270 Expected evaluate(const Counter &C) const;
267 ErrorOr evaluate(const Counter &C) const;
271268 };
272269
273270 /// \brief Code coverage information for a single function.
417414
418415 public:
419416 /// \brief Load the coverage mapping using the given readers.
420 static Expected>
417 static ErrorOr>
421418 load(CoverageMappingReader &CoverageReader,
422419 IndexedInstrProfReader &ProfileReader);
423420
424421 /// \brief Load the coverage mapping from the given files.
425 static Expected>
422 static ErrorOr>
426423 load(StringRef ObjectFilename, StringRef ProfileFilename,
427424 StringRef Arch = StringRef());
428425
503500 }
504501 // Return the PGO name of the function */
505502 template
506 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
503 std::error_code getFuncName(InstrProfSymtab &ProfileNames,
504 StringRef &FuncName) const {
507505 IntPtrT NameRef = getFuncNameRef();
508506 uint32_t NameS = support::endian::byte_swap(NameSize);
509507 FuncName = ProfileNames.getFuncName(NameRef, NameS);
510508 if (NameS && FuncName.empty())
511 return make_error(coveragemap_error::malformed);
512 return Error::success();
509 return coveragemap_error::malformed;
510 return std::error_code();
513511 }
514512 };
515513
531529 }
532530 // Return the PGO name of the function */
533531 template
534 Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const {
532 std::error_code getFuncName(InstrProfSymtab &ProfileNames,
533 StringRef &FuncName) const {
535534 uint64_t NameRef = getFuncNameRef();
536535 FuncName = ProfileNames.getFuncName(NameRef);
537 return Error::success();
536 return std::error_code();
538537 }
539538 };
540539
6868
6969 class CoverageMappingReader {
7070 public:
71 virtual Error readNextRecord(CoverageMappingRecord &Record) = 0;
71 virtual std::error_code 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 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);
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);
8888 };
8989
9090 /// \brief Reader for the raw coverage filenames.
9999 RawCoverageFilenamesReader(StringRef Data, std::vector &Filenames)
100100 : RawCoverageReader(Data), Filenames(Filenames) {}
101101
102 Error read();
102 std::error_code read();
103103 };
104104
105105 /// \brief Reader for the raw coverage mapping data.
124124 Filenames(Filenames), Expressions(Expressions),
125125 MappingRegions(MappingRegions) {}
126126
127 Error read();
127 std::error_code read();
128128
129129 private:
130 Error decodeCounter(unsigned Value, Counter &C);
131 Error readCounter(Counter &C);
132 Error
130 std::error_code decodeCounter(unsigned Value, Counter &C);
131 std::error_code readCounter(Counter &C);
132 std::error_code
133133 readMappingRegionsSubArray(std::vector &MappingRegions,
134134 unsigned InferredFileID, size_t NumFileIDs);
135135 };
169169 BinaryCoverageReader() : CurrentRecord(0) {}
170170
171171 public:
172 static Expected>
172 static ErrorOr>
173173 create(std::unique_ptr &ObjectBuffer,
174174 StringRef Arch);
175175
176 Error readNextRecord(CoverageMappingRecord &Record) override;
176 std::error_code 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"
2728 #include "llvm/Support/MD5.h"
2829 #include "llvm/Support/MathExtras.h"
2930 #include
202203 /// third field is the uncompressed strings; otherwise it is the
203204 /// compressed string. When the string compression is off, the
204205 /// second field will have value zero.
205 Error collectPGOFuncNameStrings(const std::vector &NameStrs,
206 bool doCompression, std::string &Result);
206 std::error_code
207 collectPGOFuncNameStrings(const std::vector &NameStrs,
208 bool doCompression, std::string &Result);
207209 /// Produce \c Result string with the same format described above. The input
208210 /// is vector of PGO function name variables that are referenced.
209 Error collectPGOFuncNameStrings(const std::vector &NameVars,
210 std::string &Result, bool doCompression = true);
211 std::error_code
212 collectPGOFuncNameStrings(const std::vector &NameVars,
213 std::string &Result, bool doCompression = true);
211214 class InstrProfSymtab;
212215 /// \c NameStrings is a string composed of one of more sub-strings encoded in
213216 /// the format described above. The substrings are seperated by 0 or more zero
214217 /// bytes. This method decodes the string and populates the \c Symtab.
215 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab);
218 std::error_code readPGOFuncNameStrings(StringRef NameStrings,
219 InstrProfSymtab &Symtab);
216220
217221 enum InstrProfValueKind : uint32_t {
218222 #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value,
279283 return std::error_code(static_cast(E), instrprof_category());
280284 }
281285
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
301286 class SoftInstrProfErrors {
302287 /// Count the number of soft instrprof_errors encountered and keep track of
303288 /// the first such error for reporting purposes.
323308 NumCountMismatches(0), NumCounterOverflows(0),
324309 NumValueSiteCountMismatches(0) {}
325310
326 ~SoftInstrProfErrors() {
327 assert(FirstError == instrprof_error::success &&
328 "Unchecked soft error encountered");
329 }
330
331311 /// Track a soft error (\p IE) and increment its associated counter.
332312 void addError(instrprof_error IE);
333313
345325 return NumValueSiteCountMismatches;
346326 }
347327
348 /// Return the first encountered error and reset FirstError to a success
349 /// value.
350 Error takeError() {
351 if (FirstError == instrprof_error::success)
352 return Error::success();
353 auto E = make_error(FirstError);
354 FirstError = instrprof_error::success;
355 return E;
356 }
328 /// Return an error code for the first encountered error.
329 std::error_code getError() const { return make_error_code(FirstError); }
357330 };
358331
359332 namespace object {
398371 /// only initialize the symtab with reference to the data and
399372 /// the section base address. The decompression will be delayed
400373 /// until before it is used. See also \c create(StringRef) method.
401 Error create(object::SectionRef &Section);
374 std::error_code create(object::SectionRef &Section);
402375 /// This interface is used by reader of CoverageMapping test
403376 /// format.
404 inline Error create(StringRef D, uint64_t BaseAddr);
377 inline std::error_code create(StringRef D, uint64_t BaseAddr);
405378 /// \c NameStrings is a string composed of one of more sub-strings
406379 /// encoded in the format described in \c collectPGOFuncNameStrings.
407380 /// This method is a wrapper to \c readPGOFuncNameStrings method.
408 inline Error create(StringRef NameStrings);
381 inline std::error_code create(StringRef NameStrings);
409382 /// A wrapper interface to populate the PGO symtab with functions
410383 /// decls from module \c M. This interface is used by transformation
411384 /// passes such as indirect function call promotion. Variable \c InLTO
450423 inline StringRef getNameData() const { return Data; }
451424 };
452425
453 Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
426 std::error_code InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) {
454427 Data = D;
455428 Address = BaseAddr;
456 return Error::success();
457 }
458
459 Error InstrProfSymtab::create(StringRef NameStrings) {
429 return std::error_code();
430 }
431
432 std::error_code InstrProfSymtab::create(StringRef NameStrings) {
460433 return readPGOFuncNameStrings(NameStrings, *this);
461434 }
462435
598571 }
599572
600573 /// Get the error contained within the record's soft error counter.
601 Error takeError() { return SIPE.takeError(); }
574 std::error_code getError() const { return SIPE.getError(); }
602575
603576 private:
604577 std::vector IndirectCallSites;
916889
917890 } // end namespace llvm
918891
892 namespace std {
893 template <>
894 struct is_error_code_enum : std::true_type {};
895 }
896
919897 #endif // LLVM_PROFILEDATA_INSTRPROF_H
294294 static std::unique_ptr
295295 serializeFrom(const InstrProfRecord &Record);
296296 /*!
297 * Check the integrity of the record.
298 */
299 Error checkIntegrity();
297 * Check the integrity of the record. Return the error code when
298 * an error is detected, otherwise return instrprof_error::success.
299 */
300 instrprof_error checkIntegrity();
300301 /*!
301302 * Return a pointer to \c ValueProfileData instance ready to be read.
302303 * All data in the instance are properly byte swapped. The input
303304 * data is assumed to be in little endian order.
304305 */
305 static Expected>
306 static ErrorOr>
306307 getValueProfData(const unsigned char *SrcBuffer,
307308 const unsigned char *const SrcBufferEnd,
308309 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"
2122 #include "llvm/Support/LineIterator.h"
2223 #include "llvm/Support/MemoryBuffer.h"
2324 #include "llvm/Support/OnDiskHashTable.h"
4950 /// Base class and interface for reading profiling data of any known instrprof
5051 /// format. Provides an iterator over InstrProfRecords.
5152 class InstrProfReader {
52 instrprof_error LastError;
53 std::error_code LastError;
5354
5455 public:
5556 InstrProfReader() : LastError(instrprof_error::success), Symtab() {}
5657 virtual ~InstrProfReader() {}
5758
5859 /// Read the header. Required before reading first record.
59 virtual Error readHeader() = 0;
60 virtual std::error_code readHeader() = 0;
6061 /// Read a single record.
61 virtual Error readNextRecord(InstrProfRecord &Record) = 0;
62 virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0;
6263 /// Iterator over profile data.
6364 InstrProfIterator begin() { return InstrProfIterator(this); }
6465 InstrProfIterator end() { return InstrProfIterator(); }
7879
7980 protected:
8081 std::unique_ptr Symtab;
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); }
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); }
9290
9391 public:
9492 /// Return true if the reader has finished reading the profile data.
9593 bool isEOF() { return LastError == instrprof_error::eof; }
9694 /// Return true if the reader encountered an error reading profiling data.
97 bool hasError() { return LastError != instrprof_error::success && !isEOF(); }
98 /// Get the current error.
99 Error getError() { return make_error(LastError); }
95 bool hasError() { return LastError && !isEOF(); }
96 /// Get the current error code.
97 std::error_code getError() { return LastError; }
10098
10199 /// Factory method to create an appropriately typed reader for the given
102100 /// instrprof file.
103 static Expected> create(std::string Path);
104
105 static Expected>
101 static ErrorOr> create(std::string Path);
102
103 static ErrorOr>
106104 create(std::unique_ptr Buffer);
107105 };
108106
124122
125123 TextInstrProfReader(const TextInstrProfReader &) = delete;
126124 TextInstrProfReader &operator=(const TextInstrProfReader &) = delete;
127 Error readValueProfileData(InstrProfRecord &Record);
125 std::error_code readValueProfileData(InstrProfRecord &Record);
128126
129127 public:
130128 TextInstrProfReader(std::unique_ptr DataBuffer_)
137135 bool isIRLevelProfile() const override { return IsIRLevelProfile; }
138136
139137 /// Read the header.
140 Error readHeader() override;
138 std::error_code readHeader() override;
141139 /// Read a single record.
142 Error readNextRecord(InstrProfRecord &Record) override;
140 std::error_code readNextRecord(InstrProfRecord &Record) override;
143141
144142 InstrProfSymtab &getSymtab() override {
145143 assert(Symtab.get());
186184 : DataBuffer(std::move(DataBuffer)) { }
187185
188186 static bool hasFormat(const MemoryBuffer &DataBuffer);
189 Error readHeader() override;
190 Error readNextRecord(InstrProfRecord &Record) override;
187 std::error_code readHeader() override;
188 std::error_code readNextRecord(InstrProfRecord &Record) override;
191189 bool isIRLevelProfile() const override {
192190 return (Version & VARIANT_MASK_IR_PROF) != 0;
193191 }
198196 }
199197
200198 private:
201 Error createSymtab(InstrProfSymtab &Symtab);
202 Error readNextHeader(const char *CurrentPos);
203 Error readHeader(const RawInstrProf::Header &Header);
199 std::error_code createSymtab(InstrProfSymtab &Symtab);
200 std::error_code readNextHeader(const char *CurrentPos);
201 std::error_code readHeader(const RawInstrProf::Header &Header);
204202 template IntT swap(IntT Int) const {
205203 return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int;
206204 }
217215 inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) {
218216 return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t));
219217 }
220 Error readName(InstrProfRecord &Record);
221 Error readFuncHash(InstrProfRecord &Record);
222 Error readRawCounts(InstrProfRecord &Record);
223 Error readValueProfilingData(InstrProfRecord &Record);
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);
224222 bool atEnd() const { return Data == DataEnd; }
225223 void advanceData() {
226224 Data++;
301299 struct InstrProfReaderIndexBase {
302300 // Read all the profile records with the same key pointed to the current
303301 // iterator.
304 virtual Error getRecords(ArrayRef &Data) = 0;
302 virtual std::error_code getRecords(ArrayRef &Data) = 0;
305303 // Read all the profile records with the key equal to FuncName
306 virtual Error getRecords(StringRef FuncName,
304 virtual std::error_code getRecords(StringRef FuncName,
307305 ArrayRef &Data) = 0;
308306 virtual void advanceToNextKey() = 0;
309307 virtual bool atEnd() const = 0;
331329 const unsigned char *const Base,
332330 IndexedInstrProf::HashT HashType, uint64_t Version);
333331
334 Error getRecords(ArrayRef &Data) override;
335 Error getRecords(StringRef FuncName,
336 ArrayRef &Data) override;
332 std::error_code getRecords(ArrayRef &Data) override;
333 std::error_code getRecords(StringRef FuncName,
334 ArrayRef &Data) override;
337335 void advanceToNextKey() override { RecordIterator++; }
338336 bool atEnd() const override {
339337 return RecordIterator == HashTable->data_end();
380378 static bool hasFormat(const MemoryBuffer &DataBuffer);
381379
382380 /// Read the file header.
383 Error readHeader() override;
381 std::error_code readHeader() override;
384382 /// Read a single record.
385 Error readNextRecord(InstrProfRecord &Record) override;
383 std::error_code readNextRecord(InstrProfRecord &Record) override;
386384
387385 /// Return the pointer to InstrProfRecord associated with FuncName
388386 /// and FuncHash
389 Expected getInstrProfRecord(StringRef FuncName,
390 uint64_t FuncHash);
387 ErrorOr getInstrProfRecord(StringRef FuncName,
388 uint64_t FuncHash);
391389
392390 /// Fill Counts with the profile data for the given function name.
393 Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
394 std::vector &Counts);
391 std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash,
392 std::vector &Counts);
395393
396394 /// Return the maximum of all known function counts.
397395 uint64_t getMaximumFunctionCount() { return Summary->getMaxFunctionCount(); }
398396
399397 /// Factory method to create an indexed reader.
400 static Expected>
398 static ErrorOr>
401399 create(std::string Path);
402400
403 static Expected>
401 static ErrorOr>
404402 create(std::unique_ptr Buffer);
405403
406404 // 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 Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1);
48 std::error_code 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 Error setIsIRLevelProfile(bool IsIRLevel) {
60 std::error_code setIsIRLevelProfile(bool IsIRLevel) {
6161 if (ProfileKind == PF_Unknown) {
6262 ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE;
63 return Error::success();
63 return instrprof_error::success;
6464 }
65 return (IsIRLevel == (ProfileKind == PF_IRLevel))
66 ? Error::success()
67 : make_error(
68 instrprof_error::unsupported_version);
65 return (IsIRLevel == (ProfileKind == PF_IRLevel)) ?
66 instrprof_error::success : instrprof_error::unsupported_version;
6967 }
7068
7169 // Internal interface for testing purpose only.
2020 #include
2121
2222 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Error.h"
2423
2524 namespace llvm {
2625 class Function;
3534 class Metadata;
3635 class MDTuple;
3736 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 };
6137
6238 inline const char *getHotSectionPrefix() { return ".hot"; }
6339 inline const char *getUnlikelySectionPrefix() { return ".unlikely"; }
142142 }
143143 if (CounterValues.empty())
144144 return;
145 Expected Value = evaluate(C);
146 if (auto E = Value.takeError()) {
147 llvm::consumeError(std::move(E));
145 ErrorOr Value = evaluate(C);
146 if (!Value)
148147 return;
149 }
150148 OS << '[' << *Value << ']';
151149 }
152150
153 Expected CounterMappingContext::evaluate(const Counter &C) const {
151 ErrorOr CounterMappingContext::evaluate(const Counter &C) const {
154152 switch (C.getKind()) {
155153 case Counter::Zero:
156154 return 0;
157155 case Counter::CounterValueReference:
158156 if (C.getCounterID() >= CounterValues.size())
159 return errorCodeToError(errc::argument_out_of_domain);
157 return make_error_code(errc::argument_out_of_domain);
160158 return CounterValues[C.getCounterID()];
161159 case Counter::Expression: {
162160 if (C.getExpressionID() >= Expressions.size())
163 return errorCodeToError(errc::argument_out_of_domain);
161 return make_error_code(errc::argument_out_of_domain);
164162 const auto &E = Expressions[C.getExpressionID()];
165 Expected LHS = evaluate(E.LHS);
163 ErrorOr LHS = evaluate(E.LHS);
166164 if (!LHS)
167165 return LHS;
168 Expected RHS = evaluate(E.RHS);
166 ErrorOr RHS = evaluate(E.RHS);
169167 if (!RHS)
170168 return RHS;
171169 return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS;
182180 *this = FunctionRecordIterator();
183181 }
184182
185 Expected>
183 ErrorOr>
186184 CoverageMapping::load(CoverageMappingReader &CoverageReader,
187185 IndexedInstrProfReader &ProfileReader) {
188186 auto Coverage = std::unique_ptr(new CoverageMapping());
192190 CounterMappingContext Ctx(Record.Expressions);
193191
194192 Counts.clear();
195 if (Error E = ProfileReader.getFunctionCounts(
193 if (std::error_code EC = ProfileReader.getFunctionCounts(
196194 Record.FunctionName, Record.FunctionHash, Counts)) {
197 instrprof_error IPE = InstrProfError::take(std::move(E));
198 if (IPE == instrprof_error::hash_mismatch) {
195 if (EC == instrprof_error::hash_mismatch) {
199196 Coverage->MismatchedFunctionCount++;
200197 continue;
201 } else if (IPE != instrprof_error::unknown_function)
202 return make_error(IPE);
198 } else if (EC != instrprof_error::unknown_function)
199 return EC;
203200 Counts.assign(Record.MappingRegions.size(), 0);
204201 }
205202 Ctx.setCounts(Counts);
214211 getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]);
215212 FunctionRecord Function(OrigFuncName, Record.Filenames);
216213 for (const auto &Region : Record.MappingRegions) {
217 Expected ExecutionCount = Ctx.evaluate(Region.Count);
218 if (auto E = ExecutionCount.takeError()) {
219 llvm::consumeError(std::move(E));
214 ErrorOr ExecutionCount = Ctx.evaluate(Region.Count);
215 if (!ExecutionCount)
220216 break;
221 }
222217 Function.pushRegion(Region, *ExecutionCount);
223218 }
224219 if (Function.CountedRegions.size() != Record.MappingRegions.size()) {
232227 return std::move(Coverage);
233228 }
234229
235 Expected>
230 ErrorOr>
236231 CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename,
237232 StringRef Arch) {
238233 auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename);
239234 if (std::error_code EC = CounterMappingBuff.getError())
240 return errorCodeToError(EC);
235 return EC;
241236 auto CoverageReaderOrErr =
242237 BinaryCoverageReader::create(CounterMappingBuff.get(), Arch);
243 if (Error E = CoverageReaderOrErr.takeError())
244 return std::move(E);
238 if (std::error_code EC = CoverageReaderOrErr.getError())
239 return EC;
245240 auto CoverageReader = std::move(CoverageReaderOrErr.get());
246241 auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename);
247 if (Error E = ProfileReaderOrErr.takeError())
248 return std::move(E);
242 if (auto EC = ProfileReaderOrErr.getError())
243 return EC;
249244 auto ProfileReader = std::move(ProfileReaderOrErr.get());
250245 return load(*CoverageReader, *ProfileReader);
251246 }
537532 }
538533
539534 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
558535 class CoverageMappingErrorCategoryType : public std::error_category {
559536 const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; }
560537 std::string message(int IE) const override {
561 return getCoverageMapErrString(static_cast(IE));
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.");
562554 }
563555 };
564 } // end anonymous namespace
565
566 std::string CoverageMapError::message() const {
567 return getCoverageMapErrString(Err);
568556 }
569557
570558 static ManagedStatic ErrorCategory;
572560 const std::error_category &llvm::coverage::coveragemap_category() {
573561 return *ErrorCategory;
574562 }
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 (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) {
33 if (Reader->readNextRecord(Record))
34 *this = CoverageMappingIterator();
35 }
36
37 std::error_code RawCoverageReader::readULEB128(uint64_t &Result) {
4438 if (Data.size() < 1)
45 return make_error(coveragemap_error::truncated);
39 return coveragemap_error::truncated;
4640 unsigned N = 0;
4741 Result = decodeULEB128(reinterpret_cast(Data.data()), &N);
4842 if (N > Data.size())
49 return make_error(coveragemap_error::malformed);
43 return coveragemap_error::malformed;
5044 Data = Data.substr(N);
51 return Error::success();
52 }
53
54 Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) {
45 return std::error_code();
46 }
47
48 std::error_code RawCoverageReader::readIntMax(uint64_t &Result,
49 uint64_t MaxPlus1) {
5550 if (auto Err = readULEB128(Result))
5651 return Err;
5752 if (Result >= MaxPlus1)
58 return make_error(coveragemap_error::malformed);
59 return Error::success();
60 }
61
62 Error RawCoverageReader::readSize(uint64_t &Result) {
53 return coveragemap_error::malformed;
54 return std::error_code();
55 }
56
57 std::error_code RawCoverageReader::readSize(uint64_t &Result) {
6358 if (auto Err = readULEB128(Result))
6459 return Err;
6560 // Sanity check the number.
6661 if (Result > Data.size())
67 return make_error(coveragemap_error::malformed);
68 return Error::success();
69 }
70
71 Error RawCoverageReader::readString(StringRef &Result) {
62 return coveragemap_error::malformed;
63 return std::error_code();
64 }
65
66 std::error_code RawCoverageReader::readString(StringRef &Result) {
7267 uint64_t Length;
7368 if (auto Err = readSize(Length))
7469 return Err;
7570 Result = Data.substr(0, Length);
7671 Data = Data.substr(Length);
77 return Error::success();
78 }
79
80 Error RawCoverageFilenamesReader::read() {
72 return std::error_code();
73 }
74
75 std::error_code RawCoverageFilenamesReader::read() {
8176 uint64_t NumFilenames;
8277 if (auto Err = readSize(NumFilenames))
8378 return Err;
8782 return Err;
8883 Filenames.push_back(Filename);
8984 }
90 return Error::success();
91 }
92
93 Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) {
85 return std::error_code();
86 }
87
88 std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value,
89 Counter &C) {
9490 auto Tag = Value & Counter::EncodingTagMask;
9591 switch (Tag) {
9692 case Counter::Zero:
9793 C = Counter::getZero();
98 return Error::success();
94 return std::error_code();
9995 case Counter::CounterValueReference:
10096 C = Counter::getCounter(Value >> Counter::EncodingTagBits);
101 return Error::success();
97 return std::error_code();
10298 default:
10399 break;
104100 }
108104 case CounterExpression::Add: {
109105 auto ID = Value >> Counter::EncodingTagBits;
110106 if (ID >= Expressions.size())
111 return make_error(coveragemap_error::malformed);
107 return coveragemap_error::malformed;
112108 Expressions[ID].Kind = CounterExpression::ExprKind(Tag);
113109 C = Counter::getExpression(ID);
114110 break;
115111 }
116112 default:
117 return make_error(coveragemap_error::malformed);
118 }
119 return Error::success();
120 }
121
122 Error RawCoverageMappingReader::readCounter(Counter &C) {
113 return coveragemap_error::malformed;
114 }
115 return std::error_code();
116 }
117
118 std::error_code RawCoverageMappingReader::readCounter(Counter &C) {
123119 uint64_t EncodedCounter;
124120 if (auto Err =
125121 readIntMax(EncodedCounter, std::numeric_limits::max()))
126122 return Err;
127123 if (auto Err = decodeCounter(EncodedCounter, C))
128124 return Err;
129 return Error::success();
125 return std::error_code();
130126 }
131127
132128 static const unsigned EncodingExpansionRegionBit = 1
135131 /// \brief Read the sub-array of regions for the given inferred file id.
136132 /// \param NumFileIDs the number of file ids that are defined for this
137133 /// function.
138 Error RawCoverageMappingReader::readMappingRegionsSubArray(
134 std::error_code RawCoverageMappingReader::readMappingRegionsSubArray(
139135 std::vector &MappingRegions, unsigned InferredFileID,
140136 size_t NumFileIDs) {
141137 uint64_t NumRegions;
163159 ExpandedFileID = EncodedCounterAndRegion >>
164160 Counter::EncodingCounterTagAndExpansionRegionTagBits;
165161 if (ExpandedFileID >= NumFileIDs)
166 return make_error(coveragemap_error::malformed);
162 return coveragemap_error::malformed;
167163 } else {
168164 switch (EncodedCounterAndRegion >>
169165 Counter::EncodingCounterTagAndExpansionRegionTagBits) {
174170 Kind = CounterMappingRegion::SkippedRegion;
175171 break;
176172 default:
177 return make_error(coveragemap_error::malformed);
173 return coveragemap_error::malformed;
178174 }
179175 }
180176 }
187183 if (auto Err = readULEB128(ColumnStart))
188184 return Err;
189185 if (ColumnStart > std::numeric_limits::max())
190 return make_error(coveragemap_error::malformed);
186 return coveragemap_error::malformed;
191187 if (auto Err = readIntMax(NumLines, std::numeric_limits::max()))
192188 return Err;
193189 if (auto Err = readIntMax(ColumnEnd, std::numeric_limits::max()))
221217 C, InferredFileID, ExpandedFileID, LineStart, ColumnStart,
222218 LineStart + NumLines, ColumnEnd, Kind));
223219 }
224 return Error::success();
225 }
226
227 Error RawCoverageMappingReader::read() {
220 return std::error_code();
221 }
222
223 std::error_code RawCoverageMappingReader::read() {
228224
229225 // Read the virtual file mapping.
230226 llvm::SmallVector VirtualFileMapping;
290286 }
291287 }
292288
293 return Error::success();
294 }
295
296 Error InstrProfSymtab::create(SectionRef &Section) {
297 if (auto EC = Section.getContents(Data))
298 return errorCodeToError(EC);
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;
299295 Address = Section.getAddress();
300 return Error::success();
296 return std::error_code();
301297 }
302298
303299 StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) {
315311 // a module. \p Buf is a reference to the buffer pointer pointing
316312 // to the \c CovHeader of coverage mapping data associated with
317313 // the module.
318 virtual Error readFunctionRecords(const char *&Buf, const char *End) = 0;
314 virtual std::error_code readFunctionRecords(const char *&Buf,
315 const char *End) = 0;
319316 virtual ~CovMapFuncRecordReader() {}
320317 template
321 static Expected>
318 static ErrorOr>
322319 get(coverage::CovMapVersion Version, InstrProfSymtab &P,
323320 std::vector &R,
324321 std::vector &F);
346343 : ProfileNames(P), Filenames(F), Records(R) {}
347344 ~VersionedCovMapFuncRecordReader() override {}
348345
349 Error readFunctionRecords(const char *&Buf, const char *End) override {
346 std::error_code readFunctionRecords(const char *&Buf,
347 const char *End) override {
350348 using namespace support;
351349 if (Buf + sizeof(CovMapHeader) > End)
352 return make_error(coveragemap_error::malformed);
350 return coveragemap_error::malformed;
353351 auto CovHeader = reinterpret_cast(Buf);
354352 uint32_t NRecords = CovHeader->getNRecords();
355353 uint32_t FilenamesSize = CovHeader->getFilenamesSize();
364362
365363 // Get the filenames.
366364 if (Buf + FilenamesSize > End)
367 return make_error(coveragemap_error::malformed);
365 return coveragemap_error::malformed;
368366 size_t FilenamesBegin = Filenames.size();
369367 RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames);
370368 if (auto Err = Reader.read())
377375 const char *CovEnd = Buf;
378376
379377 if (Buf > End)
380 return make_error(coveragemap_error::malformed);
378 return coveragemap_error::malformed;
381379 // Each coverage map has an alignment of 8, so we need to adjust alignment
382380 // before reading the next map.
383381 Buf += alignmentAdjustment(Buf, 8);
390388
391389 // Now use that to read the coverage data.
392390 if (CovBuf + DataSize > CovEnd)
393 return make_error(coveragemap_error::malformed);
391 return coveragemap_error::malformed;
394392 auto Mapping = StringRef(CovBuf, DataSize);
395393 CovBuf += DataSize;
396394
404402 }
405403
406404 StringRef FuncName;
407 if (Error E = CFR->template getFuncName(ProfileNames, FuncName))
408 return E;
405 if (std::error_code EC =
406 CFR->template getFuncName(ProfileNames, FuncName))
407 return EC;
409408 Records.push_back(BinaryCoverageReader::ProfileMappingRecord(
410409 Version, FuncName, FuncHash, Mapping, FilenamesBegin,
411410 Filenames.size() - FilenamesBegin));
412411 CFR++;
413412 }
414 return Error::success();
413 return std::error_code();
415414 }
416415 };
417416 } // end anonymous namespace
418417
419418 template
420 Expected> CovMapFuncRecordReader::get(
419 ErrorOr> CovMapFuncRecordReader::get(
421420 coverage::CovMapVersion Version, InstrProfSymtab &P,
422421 std::vector &R,
423422 std::vector &F) {
428427 CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F);
429428 case CovMapVersion::Version2:
430429 // Decompress the name data.
431 if (Error E = P.create(P.getNameData()))
432 return std::move(E);
430 if (auto EC = P.create(P.getNameData()))
431 return EC;
433432 return llvm::make_unique
434433 CovMapVersion::Version2, IntPtrT, Endian>>(P, R, F);
435434 }
437436 }
438437
439438 template
440 static Error readCoverageMappingData(
439 static std::error_code readCoverageMappingData(
441440 InstrProfSymtab &ProfileNames, StringRef Data,
442441 std::vector &Records,
443442 std::vector &Filenames) {
447446 reinterpret_cast(Data.data());
448447 CovMapVersion Version = (CovMapVersion)CovHeader->getVersion();
449448 if (Version > coverage::CovMapVersion::CurrentVersion)
450 return make_error(coveragemap_error::unsupported_version);
451 Expected> ReaderExpected =
449 return coveragemap_error::unsupported_version;
450 ErrorOr> ReaderErrorOr =
452451 CovMapFuncRecordReader::get(Version, ProfileNames, Records,
453452 Filenames);
454 if (Error E = ReaderExpected.takeError())
455 return E;
456 auto Reader = std::move(ReaderExpected.get());
453 if (auto EC = ReaderErrorOr.getError())
454 return EC;
455 auto Reader = std::move(ReaderErrorOr.get());
457456 for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) {
458 if (Error E = Reader->readFunctionRecords(Buf, End))
459 return E;
460 }
461 return Error::success();
457 if (std::error_code EC = Reader->readFunctionRecords(Buf, End))
458 return EC;
459 }
460 return std::error_code();
462461 }
463462 static const char *TestingFormatMagic = "llvmcovmtestdata";
464463
465 static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames,
466 StringRef &CoverageMapping,
467 uint8_t &BytesInAddress,
468 support::endianness &Endian) {
464 static std::error_code loadTestingFormat(StringRef Data,
465 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 make_error(coveragemap_error::truncated);
474 return coveragemap_error::truncated;
475475 unsigned N = 0;
476476 auto ProfileNamesSize =
477477 decodeULEB128(reinterpret_cast(Data.data()), &N);
478478 if (N > Data.size())
479 return make_error(coveragemap_error::malformed);
479 return coveragemap_error::malformed;
480480 Data = Data.substr(N);
481481 if (Data.size() < 1)
482 return make_error(coveragemap_error::truncated);
482 return coveragemap_error::truncated;
483483 N = 0;
484484 uint64_t Address =
485485 decodeULEB128(reinterpret_cast(Data.data()), &N);
486486 if (N > Data.size())
487 return make_error(coveragemap_error::malformed);
487 return coveragemap_error::malformed;
488488 Data = Data.substr(N);
489489 if (Data.size() < ProfileNamesSize)
490 return make_error(coveragemap_error::malformed);
490 return coveragemap_error::malformed;
491491 ProfileNames.create(Data.substr(0, ProfileNamesSize), Address);
492492 CoverageMapping = Data.substr(ProfileNamesSize);
493 return Error::success();
494 }
495
496 static Expected lookupSection(ObjectFile &OF, StringRef Name) {
493 return std::error_code();
494 }
495
496 static ErrorOr lookupSection(ObjectFile &OF, StringRef Name) {
497497 StringRef FoundName;
498498 for (const auto &Section : OF.sections()) {
499499 if (auto EC = Section.getName(FoundName))
500 return errorCodeToError(EC);
500 return EC;
501501 if (FoundName == Name)
502502 return Section;
503503 }
504 return make_error(coveragemap_error::no_data_found);
505 }
506
507 static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer,
508 InstrProfSymtab &ProfileNames,
509 StringRef &CoverageMapping,
510 uint8_t &BytesInAddress,
511 support::endianness &Endian, StringRef Arch) {
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) {
512511 auto BinOrErr = object::createBinary(ObjectBuffer);
513512 if (!BinOrErr)
514 return BinOrErr.takeError();
513 return errorToErrorCode(BinOrErr.takeError());
515514 auto Bin = std::move(BinOrErr.get());
516515 std::unique_ptr OF;
517516 if (auto *Universal = dyn_cast(Bin.get())) {
518517 // If we have a universal binary, try to look up the object for the
519518 // appropriate architecture.
520519 auto ObjectFileOrErr = Universal->getObjectForArch(Arch);
521 if (auto EC = ObjectFileOrErr.getError())
522 return errorCodeToError(EC);
520 if (std::error_code EC = ObjectFileOrErr.getError())
521 return EC;
523522 OF = std::move(ObjectFileOrErr.get());
524523 } else if (isa(Bin.get())) {
525524 // For any other object file, upcast and take ownership.
526525 OF.reset(cast(Bin.release()));
527526 // If we've asked for a particular arch, make sure they match.
528527 if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch())
529 return errorCodeToError(object_error::arch_not_found);
528 return object_error::arch_not_found;
530529 } else
531530 // We can only handle object files.
532 return make_error(coveragemap_error::malformed);
531 return coveragemap_error::malformed;
533532
534533 // The coverage uses native pointer sizes for the object it's written in.
535534 BytesInAddress = OF->getBytesInAddress();
538537
539538 // Look for the sections that we are interested in.
540539 auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false));
541 if (auto E = NamesSection.takeError())
542 return E;
540 if (auto EC = NamesSection.getError())
541 return EC;
543542 auto CoverageSection =
544543 lookupSection(*OF, getInstrProfCoverageSectionName(false));
545 if (auto E = CoverageSection.takeError())
546 return E;
544 if (auto EC = CoverageSection.getError())
545 return EC;
547546
548547 // Get the contents of the given sections.
549 if (auto EC = CoverageSection->getContents(CoverageMapping))
550 return errorCodeToError(EC);
551 if (Error E = ProfileNames.create(*NamesSection))
552 return E;
553
554 return Error::success();
555 }
556
557 Expected>
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>
558557 BinaryCoverageReader::create(std::unique_ptr &ObjectBuffer,
559558 StringRef Arch) {
560559 std::unique_ptr Reader(new BinaryCoverageReader());
562561 StringRef Coverage;
563562 uint8_t BytesInAddress;
564563 support::endianness Endian;
565 Error E;
566 consumeError(std::move(E));
564 std::error_code EC;
567565 if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic))
568566 // This is a special format used for testing.
569 E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
570 Coverage, BytesInAddress, Endian);
567 EC = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames,
568 Coverage, BytesInAddress, Endian);
571569 else
572 E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
573 Coverage, BytesInAddress, Endian, Arch);
574 if (E)
575 return std::move(E);
570 EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames,
571 Coverage, BytesInAddress, Endian, Arch);
572 if (EC)
573 return EC;
576574
577575 if (BytesInAddress == 4 && Endian == support::endianness::little)
578 E = readCoverageMappingData(
576 EC = readCoverageMappingData(
579577 Reader->ProfileNames, Coverage, Reader->MappingRecords,
580578 Reader->Filenames);
581579 else if (BytesInAddress == 4 && Endian == support::endianness::big)
582 E = readCoverageMappingData(
580 EC = readCoverageMappingData(
583581 Reader->ProfileNames, Coverage, Reader->MappingRecords,
584582 Reader->Filenames);
585583 else if (BytesInAddress == 8 && Endian == support::endianness::little)
586 E = readCoverageMappingData(
584 EC = readCoverageMappingData(
587585 Reader->ProfileNames, Coverage, Reader->MappingRecords,
588586 Reader->Filenames);
589587 else if (BytesInAddress == 8 && Endian == support::endianness::big)
590 E = readCoverageMappingData(
588 EC = readCoverageMappingData(
591589 Reader->ProfileNames, Coverage, Reader->MappingRecords,
592590 Reader->Filenames);
593591 else
594 return make_error(coveragemap_error::malformed);
595 if (E)
596 return std::move(E);
592 return coveragemap_error::malformed;
593 if (EC)
594 return EC;
597595 return std::move(Reader);
598596 }
599597
600 Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
598 std::error_code
599 BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) {
601600 if (CurrentRecord >= MappingRecords.size())
602 return make_error(coveragemap_error::eof);
601 return coveragemap_error::eof;
603602
604603 FunctionsFilenames.clear();
605604 Expressions.clear();
619618 Record.MappingRegions = MappingRegions;
620619
621620 ++CurrentRecord;
622 return Error::success();
623 }
621 return std::error_code();
622 }
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
6929 class InstrProfErrorCategoryType : public std::error_category {
7030 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
7131 std::string message(int IE) const override {
72 return getInstrProfErrString(static_cast(IE));
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.");
7370 }
7471 };
7572 } // end anonymous namespace
107104 }
108105 }
109106
110 std::string InstrProfError::message() const {
111 return getInstrProfErrString(Err);
112 }
113
114 template <> char ProfErrorInfoBase::ID = 0;
115
116107 std::string getPGOFuncName(StringRef RawFuncName,
117108 GlobalValue::LinkageTypes Linkage,
118109 StringRef FileName,
222213 finalizeSymtab();
223214 }
224215
225 Error collectPGOFuncNameStrings(const std::vector &NameStrs,
226 bool doCompression, std::string &Result) {
216 std::error_code
217 collectPGOFuncNameStrings(const std::vector &NameStrs,
218 bool doCompression, std::string &Result) {
227219 assert(NameStrs.size() && "No name data to emit");
228220
229221 uint8_t Header[16], *P = Header;
245237 unsigned HeaderLen = P - &Header[0];
246238 Result.append(HeaderStr, HeaderLen);
247239 Result += InputStr;
248 return Error::success();
240 return make_error_code(instrprof_error::success);
249241 };
250242
251 if (!doCompression) {
243 if (!doCompression)
252244 return WriteStringToResult(0, UncompressedNameStrings);
253 }
254245
255246 SmallVector CompressedNameStrings;
256247 zlib::Status Success =
258249 zlib::BestSizeCompression);
259250
260251 if (Success != zlib::StatusOK)
261 return make_error(instrprof_error::compress_failed);
252 return make_error_code(instrprof_error::compress_failed);
262253
263254 return WriteStringToResult(
264255 CompressedNameStrings.size(),
272263 return NameStr;
273264 }
274265
275 Error collectPGOFuncNameStrings(const std::vector &NameVars,
276 std::string &Result, bool doCompression) {
266 std::error_code
267 collectPGOFuncNameStrings(const std::vector &NameVars,
268 std::string &Result, bool doCompression) {
277269 std::vector NameStrs;
278270 for (auto *NameVar : NameVars) {
279271 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
282274 NameStrs, zlib::isAvailable() && doCompression, Result);
283275 }
284276
285 Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
277 std::error_code readPGOFuncNameStrings(StringRef NameStrings,
278 InstrProfSymtab &Symtab) {
286279 const uint8_t *P = reinterpret_cast(NameStrings.data());
287280 const uint8_t *EndP = reinterpret_cast(NameStrings.data() +
288281 NameStrings.size());
300293 CompressedSize);
301294 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
302295 UncompressedSize) != zlib::StatusOK)
303 return make_error(instrprof_error::uncompress_failed);
296 return make_error_code(instrprof_error::uncompress_failed);
304297 P += CompressedSize;
305298 NameStrings = StringRef(UncompressedNameStrings.data(),
306299 UncompressedNameStrings.size());
319312 P++;
320313 }
321314 Symtab.finalizeSymtab();
322 return Error::success();
315 return make_error_code(instrprof_error::success);
323316 }
324317
325318 void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
583576 ValueProfData());
584577 }
585578
586 Error ValueProfData::checkIntegrity() {
579 instrprof_error ValueProfData::checkIntegrity() {
587580 if (NumValueKinds > IPVK_Last + 1)
588 return make_error(instrprof_error::malformed);
581 return instrprof_error::malformed;
589582 // Total size needs to be mulltiple of quadword size.
590583 if (TotalSize % sizeof(uint64_t))
591 return make_error(instrprof_error::malformed);
584 return instrprof_error::malformed;
592585
593586 ValueProfRecord *VR = getFirstValueProfRecord(this);
594587 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
595588 if (VR->Kind > IPVK_Last)
596 return make_error(instrprof_error::malformed);
589 return instrprof_error::malformed;
597590 VR = getValueProfRecordNext(VR);
598591 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
599 return make_error(instrprof_error::malformed);
600 }
601 return Error::success();
602 }
603
604 Expected>
592 return instrprof_error::malformed;
593 }
594 return instrprof_error::success;
595 }
596
597 ErrorOr>
605598 ValueProfData::getValueProfData(const unsigned char *D,
606599 const unsigned char *const BufferEnd,
607600 support::endianness Endianness) {
608601 using namespace support;
609602 if (D + sizeof(ValueProfData) > BufferEnd)
610 return make_error(instrprof_error::truncated);
603 return instrprof_error::truncated;
611604
612605 const unsigned char *Header = D;
613606 uint32_t TotalSize = swapToHostOrder(Header, Endianness);
614607 if (D + TotalSize > BufferEnd)
615 return make_error(instrprof_error::too_large);
608 return instrprof_error::too_large;
616609
617610 std::unique_ptr VPD = allocValueProfData(TotalSize);
618611 memcpy(VPD.get(), D, TotalSize);
619612 // Byte swap.
620613 VPD->swapBytesToHost(Endianness);
621614
622 Error E = VPD->checkIntegrity();
623 if (E)
624 return std::move(E);
615 instrprof_error EC = VPD->checkIntegrity();
616 if (EC != instrprof_error::success)
617 return EC;
625618
626619 return std::move(VPD);
627620 }
1717
1818 using namespace llvm;
1919
20 static Expected>
20 static ErrorOr>
2121 setupMemoryBuffer(std::string Path) {
2222 ErrorOr> BufferOrErr =
2323 MemoryBuffer::getFileOrSTDIN(Path);
2424 if (std::error_code EC = BufferOrErr.getError())
25 return errorCodeToError(EC);
25 return EC;
2626 return std::move(BufferOrErr.get());
2727 }
2828
29 static Error initializeReader(InstrProfReader &Reader) {
29 static std::error_code initializeReader(InstrProfReader &Reader) {
3030 return Reader.readHeader();
3131 }
3232
33 Expected>
33 ErrorOr>
3434 InstrProfReader::create(std::string Path) {
3535 // Set up the buffer to read.
3636 auto BufferOrError = setupMemoryBuffer(Path);
37 if (Error E = BufferOrError.takeError())
38 return std::move(E);
37 if (std::error_code EC = BufferOrError.getError())
38 return EC;
3939 return InstrProfReader::create(std::move(BufferOrError.get()));
4040 }
4141
42 Expected>
42 ErrorOr>
4343 InstrProfReader::create(std::unique_ptr Buffer) {
4444 // Sanity check the buffer.
4545 if (Buffer->getBufferSize() > std::numeric_limits::max())
46 return make_error(instrprof_error::too_large);
46 return 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 make_error(instrprof_error::unrecognized_format);
59 return instrprof_error::unrecognized_format;
6060
6161 // Initialize the reader and return the result.
62 if (Error E = initializeReader(*Result))
63 return std::move(E);
62 if (std::error_code EC = initializeReader(*Result))
63 return EC;
6464
6565 return std::move(Result);
6666 }
6767
68 Expected>
68 ErrorOr>
6969 IndexedInstrProfReader::create(std::string Path) {
7070 // Set up the buffer to read.
7171 auto BufferOrError = setupMemoryBuffer(Path);
72 if (Error E = BufferOrError.takeError())
73 return std::move(E);
72 if (std::error_code EC = BufferOrError.getError())
73 return EC;
7474 return IndexedInstrProfReader::create(std::move(BufferOrError.get()));
7575 }
7676
7777
78 Expected>
78 ErrorOr>
7979 IndexedInstrProfReader::create(std::unique_ptr Buffer) {
8080 // Sanity check the buffer.
8181 if (Buffer->getBufferSize() > std::numeric_limits::max())
82 return make_error(instrprof_error::too_large);
82 return instrprof_error::too_large;
8383
8484 // Create the reader.
8585 if (!IndexedInstrProfReader::hasFormat(*Buffer))
86 return make_error(instrprof_error::bad_magic);
86 return instrprof_error::bad_magic;
8787 auto Result = llvm::make_unique(std::move(Buffer));
8888
8989 // Initialize the reader and return the result.
90 if (Error E = initializeReader(*Result))
91 return std::move(E);
90 if (std::error_code EC = initializeReader(*Result))
91 return EC;
9292
9393 return std::move(Result);
9494 }
9595
9696 void InstrProfIterator::Increment() {
97 if (auto E = Reader->readNextRecord(Record)) {
98 instrprof_error Err = InstrProfError::take(std::move(E));
99 if (Err != instrprof_error::eof)
100 llvm_unreachable("Unexpected error in profile iterator");
97 if (Reader->readNextRecord(Record))
10198 *this = InstrProfIterator();
102 }
10399 }
104100
105101 bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) {
115111 // Read the profile variant flag from the header: ":FE" means this is a FE
116112 // generated profile. ":IR" means this is an IR level profile. Other strings
117113 // with a leading ':' will be reported an error format.
118 Error TextInstrProfReader::readHeader() {
114 std::error_code TextInstrProfReader::readHeader() {
119115 Symtab.reset(new InstrProfSymtab());
120116 bool IsIRInstr = false;
121117 if (!Line->startswith(":")) {
128124 else if (Str.equals_lower("fe"))
129125 IsIRInstr = false;
130126 else
131 return error(instrprof_error::bad_header);
127 return instrprof_error::bad_header;
132128
133129 ++Line;
134130 IsIRLevelProfile = IsIRInstr;
135131 return success();
136132 }
137133
138 Error
134 std::error_code
139135 TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) {
140136
141137 #define CHECK_LINE_END(Line) \
199195 #undef VP_READ_ADVANCE
200196 }
201197
202 Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
198 std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
203199 // Skip empty lines and comments.
204200 while (!Line.is_at_end() && (Line->empty() || Line->startswith("#")))
205201 ++Line;
241237 }
242238
243239 // Check if value profile data exists and read it if so.
244 if (Error E = readValueProfileData(Record))
245 return E;
240 if (std::error_code EC = readValueProfileData(Record))
241 return EC;
246242
247243 // This is needed to avoid two pass parsing because llvm-profdata
248244 // does dumping while reading.
261257 }
262258
263259 template
264 Error RawInstrProfReader::readHeader() {
260 std::error_code RawInstrProfReader::readHeader() {
265261 if (!hasFormat(*DataBuffer))
266262 return error(instrprof_error::bad_magic);
267263 if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header))
273269 }
274270
275271 template
276 Error RawInstrProfReader::readNextHeader(const char *CurrentPos) {
272 std::error_code
273 RawInstrProfReader::readNextHeader(const char *CurrentPos) {
277274 const char *End = DataBuffer->getBufferEnd();
278275 // Skip zero padding between profiles.
279276 while (CurrentPos != End && *CurrentPos == 0)
280277 ++CurrentPos;
281278 // If there's nothing left, we're done.
282279 if (CurrentPos == End)
283 return make_error(instrprof_error::eof);
280 return instrprof_error::eof;
284281 // If there isn't enough space for another header, this is probably just
285282 // garbage at the end of the file.
286283 if (CurrentPos + sizeof(RawInstrProf::Header) > End)
287 return make_error(instrprof_error::malformed);
284 return instrprof_error::malformed;
288285 // The writer ensures each profile is padded to start at an aligned address.
289286 if (reinterpret_cast(CurrentPos) % alignOf())
290 return make_error(instrprof_error::malformed);
287 return instrprof_error::malformed;
291288 // The magic should have the same byte order as in the previous header.
292289 uint64_t Magic = *reinterpret_cast(CurrentPos);
293290 if (Magic != swap(RawInstrProf::getMagic()))
294 return make_error(instrprof_error::bad_magic);
291 return instrprof_error::bad_magic;
295292
296293 // There's another profile to read, so we need to process the header.
297294 auto *Header = reinterpret_cast(CurrentPos);
299296 }
300297
301298 template
302 Error RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) {
303 if (Error E = Symtab.create(StringRef(NamesStart, NamesSize)))
304 return error(std::move(E));
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;
305304 for (const RawInstrProf::ProfileData *I = Data; I != DataEnd; ++I) {
306305 const IntPtrT FPtr = swap(I->FunctionPointer);
307306 if (!FPtr)
313312 }
314313
315314 template
316 Error RawInstrProfReader::readHeader(
317 const RawInstrProf::Header &Header) {
315 std::error_code
316 RawInstrProfReader::readHeader(const RawInstrProf::Header &Header) {
318317 Version = swap(Header.Version);
319318 if (GET_VERSION(Version) != RawInstrProf::Version)
320319 return error(instrprof_error::unsupported_version);
346345 ValueDataStart = reinterpret_cast(Start + ValueDataOffset);
347346
348347 std::unique_ptr NewSymtab = make_unique();
349 if (Error E = createSymtab(*NewSymtab.get()))
350 return E;
348 if (auto EC = createSymtab(*NewSymtab.get()))
349 return EC;
351350
352351 Symtab = std::move(NewSymtab);
353352 return success();
354353 }
355354
356355 template
357 Error RawInstrProfReader::readName(InstrProfRecord &Record) {
356 std::error_code RawInstrProfReader::readName(InstrProfRecord &Record) {
358357 Record.Name = getName(Data->NameRef);
359358 return success();
360359 }
361360
362361 template
363 Error RawInstrProfReader::readFuncHash(InstrProfRecord &Record) {
362 std::error_code RawInstrProfReader::readFuncHash(
363 InstrProfRecord &Record) {
364364 Record.Hash = swap(Data->FuncHash);
365365 return success();
366366 }
367367
368368 template
369 Error RawInstrProfReader::readRawCounts(
369 std::error_code RawInstrProfReader::readRawCounts(
370370 InstrProfRecord &Record) {
371371 uint32_t NumCounters = swap(Data->NumCounters);
372372 IntPtrT CounterPtr = Data->CounterPtr;
393393 }
394394
395395 template
396 Error RawInstrProfReader::readValueProfilingData(
397 InstrProfRecord &Record) {
396 std::error_code
397 RawInstrProfReader::readValueProfilingData(InstrProfRecord &Record) {
398398
399399 Record.clearValueData();
400400 CurValueDataSize = 0;
406406 if (!NumValueKinds)
407407 return success();
408408
409 Expected> VDataPtrOrErr =
409 ErrorOr> VDataPtrOrErr =
410410 ValueProfData::getValueProfData(
411411 ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(),
412412 getDataEndianness());
413413
414 if (Error E = VDataPtrOrErr.takeError())
415 return E;
414 if (VDataPtrOrErr.getError())
415 return VDataPtrOrErr.getError();
416416
417417 // Note that besides deserialization, this also performs the conversion for
418418 // indirect call targets. The function pointers from the raw profile are
423423 }
424424
425425 template
426 Error RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
426 std::error_code
427 RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
427428 if (atEnd())
428429 // At this point, ValueDataStart field points to the next header.
429 if (Error E = readNextHeader(getNextHeaderPos()))
430 return E;
430 if (std::error_code EC = readNextHeader(getNextHeaderPos()))
431 return EC;
431432
432433 // Read name ad set it in Record.
433 if (Error E = readName(Record))
434 return E;
434 if (std::error_code EC = readName(Record))
435 return EC;
435436
436437 // Read FuncHash and set it in Record.
437 if (Error E = readFuncHash(Record))
438 return E;
438 if (std::error_code EC = readFuncHash(Record))
439 return EC;
439440
440441 // Read raw counts and set Record.
441 if (Error E = readRawCounts(Record))
442 return E;
442 if (std::error_code EC = readRawCounts(Record))
443 return EC;
443444
444445 // Read value data and set Record.
445 if (Error E = readValueProfilingData(Record))
446 return E;
446 if (std::error_code EC = readValueProfilingData(Record))
447 return EC;
447448
448449 // Iterate.
449450 advanceData();
465466
466467 bool InstrProfLookupTrait::readValueProfilingData(
467468 const unsigned char *&D, const unsigned char *const End) {
468 Expected> VDataPtrOrErr =
469 ErrorOr> VDataPtrOrErr =
469470 ValueProfData::getValueProfData(D, End, ValueProfDataEndianness);
470471
471 if (VDataPtrOrErr.takeError())
472 if (VDataPtrOrErr.getError())
472473 return false;
473474
474475 VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr);
524525 }
525526
526527 template
527 Error InstrProfReaderIndex::getRecords(
528 std::error_code InstrProfReaderIndex::getRecords(
528529 StringRef FuncName, ArrayRef &Data) {
529530 auto Iter = HashTable->find(FuncName);
530531 if (Iter == HashTable->end())
531 return make_error(instrprof_error::unknown_function);
532 return instrprof_error::unknown_function;
532533
533534 Data = (*Iter);
534535 if (Data.empty())
535 return make_error(instrprof_error::malformed);
536
537 return Error::success();
536 return instrprof_error::malformed;
537
538 return instrprof_error::success;
538539 }
539540
540541 template
541 Error InstrProfReaderIndex::getRecords(
542 std::error_code InstrProfReaderIndex::getRecords(
542543 ArrayRef &Data) {
543544 if (atEnd())
544 return make_error(instrprof_error::eof);
545 return instrprof_error::eof;
545546
546547 Data = *RecordIterator;
547548
548549 if (Data.empty())
549 return make_error(instrprof_error::malformed);
550
551 return Error::success();
550 return instrprof_error::malformed;
551
552 return instrprof_error::success;
552553 }
553554
554555 template
607608 }
608609 }
609610
610 Error IndexedInstrProfReader::readHeader() {
611 std::error_code IndexedInstrProfReader::readHeader() {
611612 const unsigned char *Start =
612613 (const unsigned char *)DataBuffer->getBufferStart();
613614 const unsigned char *Cur = Start;
659660 return *Symtab.get();
660661 }
661662
662 Expected
663 ErrorOr
663664 IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName,
664665 uint64_t FuncHash) {
665666 ArrayRef Data;
666 Error Err = Index->getRecords(FuncName, Data);
667 if (Err)
668 return std::move(Err);
667 std::error_code EC = Index->getRecords(FuncName, Data);
668 if (EC != instrprof_error::success)
669 return EC;
669670 // Found it. Look for counters with the right hash.
670671 for (unsigned I = 0, E = Data.size(); I < E; ++I) {
671672 // Check for a match and fill the vector if there is one.
676677 return error(instrprof_error::hash_mismatch);
677678 }
678679
679 Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName,
680 uint64_t FuncHash,
681 std::vector &Counts) {
682 Expected Record = getInstrProfRecord(FuncName, FuncHash);
683 if (Error E = Record.takeError())
684 return error(std::move(E));
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;
685686
686687 Counts = Record.get().Counts;
687688 return success();
688689 }
689690
690 Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) {
691 std::error_code IndexedInstrProfReader::readNextRecord(
692 InstrProfRecord &Record) {
691693 static unsigned RecordIndex = 0;
692694
693695 ArrayRef Data;
694696
695 Error E = Index->getRecords(Data);
696 if (E)
697 return error(std::move(E));
697 std::error_code EC = Index->getRecords(Data);
698 if (EC != instrprof_error::success)
699 return error(EC);
698700
699701 Record = Data[RecordIndex++];
700702 if (RecordIndex >= Data.size()) {
155155 this->Sparse = Sparse;
156156 }
157157
158 Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) {
158 std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I,
159 uint64_t Weight) {
159160 auto &ProfileDataMap = FunctionData[I.Name];
160161
161162 bool NewFunc;
178179
179180 Dest.sortValueData();
180181
181 return Dest.takeError();
182 return Dest.getError();
182183 }
183184
184185 bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) {
373373 return;
374374
375375 std::string CompressedNameStr;
376 if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
376 if (auto EC = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr,
377377 DoNameCompression)) {
378 llvm::report_fatal_error(toString(std::move(E)), false);
378 llvm::report_fatal_error(EC.message(), 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 Expected Result =
584 ErrorOr Result =
585585 PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash);
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 });
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));
606601 return false;
607602 }
608603 ProfileRecord = std::move(Result.get());
858853 auto &Ctx = M.getContext();
859854 // Read the counter array from file.
860855 auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName);
861 if (Error E = ReaderOrErr.takeError()) {
862 handleAllErrors(std::move(E), [&](const InstrProfError &IPE) {
863 Ctx.diagnose(
864 DiagnosticInfoPGOProfile(ProfileFileName.data(), IPE.message()));
865 });
856 if (std::error_code EC = ReaderOrErr.getError()) {
857 Ctx.diagnose(
858 DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message()));
866859 return false;
867860 }
868861
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 (Error E = CoverageOrErr.takeError()) {
212 if (std::error_code EC = CoverageOrErr.getError()) {
213213 colored_ostream(errs(), raw_ostream::RED)
214 << "error: Failed to load coverage: " << toString(std::move(E)) << "\n";
214 << "error: Failed to load coverage: " << EC.message();
215 errs() << "\n";
215216 return nullptr;
216217 }
217218 auto Coverage = std::move(CoverageOrErr.get());
4646 ::exit(1);
4747 }
4848
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 if (instrError == instrprof_error::unrecognized_format) {
54 // Hint for common error of forgetting -sample for sample profiles.
55 exitWithError(IPE.message(), Whence,
56 "Perhaps you forgot to use the -sample option?");
57 }
58 });
59 }
60
61 exitWithError(toString(std::move(E)), Whence);
62 }
63
64 static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
65 exitWithError(EC.message(), Whence);
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);
6660 }
6761
6862 namespace {
6963 enum ProfileKinds { instr, sample };
7064 }
7165
72 static void handleMergeWriterError(Error E, StringRef WhenceFile = "",
66 static void handleMergeWriterError(std::error_code &Error,
67 StringRef WhenceFile = "",
7368 StringRef WhenceFunction = "",
7469 bool ShowHint = true) {
7570 if (!WhenceFile.empty())
7671 errs() << WhenceFile << ": ";
7772 if (!WhenceFunction.empty())
7873 errs() << WhenceFunction << ": ";
79
80 auto IPE = instrprof_error::success;
81 E = handleErrors(std::move(E),
82 [&IPE](std::unique_ptr E) -> Error {
83 IPE = E->get();
84 return Error(std::move(E));
85 });
86 errs() << toString(std::move(E)) << "\n";
74 errs() << Error.message() << "\n";
8775
8876 if (ShowHint) {
8977 StringRef Hint = "";
90 if (IPE != instrprof_error::success) {
91 switch (IPE) {
78 if (Error.category() == instrprof_category()) {
79 instrprof_error instrError = static_cast(Error.value());
80 switch (instrError) {
9281 case instrprof_error::hash_mismatch:
9382 case instrprof_error::count_mismatch:
9483 case instrprof_error::value_site_count_mismatch:
130119 exitWithErrorCode(EC, OutputFilename);
131120
132121 InstrProfWriter Writer(OutputSparse);
133 SmallSet<instrprof_error, 4> WriterErrorCodes;
122 SmallSet<std::error_code, 4> WriterErrorCodes;
134123 for (const auto &Input : Inputs) {
135124 auto ReaderOrErr = InstrProfReader::create(Input.Filename);
136 if (Error E = ReaderOrErr.takeError())
137 exitWithError(std::move(E), Input.Filename);
125 if (std::error_code ec = ReaderOrErr.getError())
126 exitWithErrorCode(ec, Input.Filename);
138127
139128 auto Reader = std::move(ReaderOrErr.get());
140129 bool IsIRProfile = Reader->isIRLevelProfile();
142131 exitWithError("Merge IR generated profile with Clang generated profile.");
143132
144133 for (auto &I : *Reader) {
145 if (Error E = Writer.addRecord(std::move(I), Input.Weight)) {
134 if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) {
146135 // Only show hint the first time an error occurs.
147 instrprof_error IPE = InstrProfError::take(std::move(E));
148 bool firstTime = WriterErrorCodes.insert(IPE).second;
149 handleMergeWriterError(make_error(IPE), Input.Filename,
150 I.Name, firstTime);
136 bool firstTime = WriterErrorCodes.insert(EC).second;
137 handleMergeWriterError(EC, Input.Filename, I.Name, firstTime);
151138 }
152139 }
153140 if (Reader->hasError())
154 exitWithError(Reader->getError(), Input.Filename);
141 exitWithErrorCode(Reader->getError(), Input.Filename);
155142 }
156143 if (OutputFormat == PF_Text)
157144 Writer.writeText(Output);
199186 sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight);
200187 if (Result != sampleprof_error::success) {
201188 std::error_code EC = make_error_code(Result);
202 handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName);
189 handleMergeWriterError(EC, Input.Filename, FName);
203190 }
204191 }
205192 }
280267 Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990};
281268 }
282269 InstrProfSummary PS(Cutoffs);
283 if (Error E = ReaderOrErr.takeError())
284 exitWithError(std::move(E), Filename);
270 if (std::error_code EC = ReaderOrErr.getError())
271 exitWithErrorCode(EC, Filename);
285272
286273 auto Reader = std::move(ReaderOrErr.get());
287274 bool IsIRInstr = Reader->isIRLevelProfile();
347334 }
348335
349336 if (Reader->hasError())
350 exitWithError(Reader->getError(), Filename);
337 exitWithErrorCode(Reader->getError(), Filename);
351338
352339 if (ShowCounts && TextFormat)
353340 return 0;
1919 using namespace llvm;
2020 using namespace coverage;
2121
22 static ::testing::AssertionResult NoError(Error E) {
23 if (!E)
22 static ::testing::AssertionResult NoError(std::error_code EC) {
23 if (!EC)
2424 return ::testing::AssertionSuccess();
25 return ::testing::AssertionFailure() << "error: " << toString(std::move(E))
26 << "\n";
25 return ::testing::AssertionFailure() << "error " << EC.value()
26 << ": " << EC.message();
2727 }
2828
2929 namespace llvm {
6969 CoverageMappingReaderMock(ArrayRef Functions)
7070 : Functions(Functions) {}
7171
72 Error readNextRecord(CoverageMappingRecord &Record) override {
72 std::error_code readNextRecord(CoverageMappingRecord &Record) override {
7373 if (Functions.empty())
74 return make_error(coveragemap_error::eof);
74 return coveragemap_error::eof;
7575
7676 Functions.front().fillCoverageMappingRecord(Record);
7777 Functions = Functions.slice(1);
7878
79 return Error::success();
79 return coveragemap_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.takeError()));
192 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
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.takeError()));
202 ASSERT_TRUE(NoError(CoverageOrErr.getError()));
203203 LoadedCoverage = std::move(CoverageOrErr.get());
204204 }
205205 };
1818
1919 using namespace llvm;
2020
21 static ::testing::AssertionResult NoError(Error E) {
22 if (!E)
21 static ::testing::AssertionResult NoError(std::error_code EC) {
22 if (!EC)
2323 return ::testing::AssertionSuccess();
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 });
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) {
3630 if (Expected == Found)
3731 return ::testing::AssertionSuccess();
38 return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n";
32 return ::testing::AssertionFailure() << "error " << Found.value()
33 << ": " << Found.message();
3934 }
4035
4136 namespace {
4843
4944 void readProfile(std::unique_ptr Profile) {
5045 auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile));
51 ASSERT_TRUE(NoError(ReaderOrErr.takeError()));
46 ASSERT_TRUE(NoError(ReaderOrErr.getError()));
5247 Reader = std::move(ReaderOrErr.get());
5348 }
5449 };
9489 auto Profile = Writer.writeBuffer();
9590 readProfile(std::move(Profile));
9691
97 Expected R = Reader->getInstrProfRecord("foo", 0x1234);
98 ASSERT_TRUE(NoError(R.takeError()));
92 ErrorOr R = Reader->getInstrProfRecord("foo", 0x1234);
93 ASSERT_TRUE(NoError(R.getError()));
9994 ASSERT_EQ(2U, R->Counts.size());
10095 ASSERT_EQ(1U, R->Counts[0]);
10196 ASSERT_EQ(2U, R->Counts[1]);
10297
10398 R = Reader->getInstrProfRecord("foo", 0x1235);
104 ASSERT_TRUE(NoError(R.takeError()));
99 ASSERT_TRUE(NoError(R.getError()));
105100 ASSERT_EQ(2U, R->Counts.size());
106101 ASSERT_EQ(3U, R->Counts[0]);
107102 ASSERT_EQ(4U, R->Counts[1]);
108103
109104 R = Reader->getInstrProfRecord("foo", 0x5678);
110 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.takeError()));
105 ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError()));
111106
112107 R = Reader->getInstrProfRecord("bar", 0x1234);
113 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError()));
108 ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError()));
114109 }
115110
116111 TEST_P(MaybeSparseInstrProfTest, get_function_counts) {
132127 ASSERT_EQ(3U, Counts[0]);
133128 ASSERT_EQ(4U, Counts[1]);
134129
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)));
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));
140136 }
141137
142138 // Profile data is copied from general.proftext
238234 auto Profile = Writer.writeBuffer();
239235 readProfile(std::move(Profile));
240236
241 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
242 ASSERT_TRUE(NoError(R.takeError()));
237 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
238 ASSERT_TRUE(NoError(R.getError()));
243239 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
244240 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
245241 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
269265 NoError(Writer.addRecord(std::move(Record)));
270266 auto Profile = Writer.writeBuffer();
271267 readProfile(std::move(Profile));
272 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
273 ASSERT_TRUE(NoError(R.takeError()));
268 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
269 ASSERT_TRUE(NoError(R.getError()));
274270
275271 LLVMContext Ctx;
276272 std::unique_ptr M(new Module("MyModule", Ctx));
381377 auto Profile = Writer.writeBuffer();
382378 readProfile(std::move(Profile));
383379
384 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
385 ASSERT_TRUE(NoError(R.takeError()));
380 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
381 ASSERT_TRUE(NoError(R.getError()));
386382 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
387383 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
388384 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
434430 // Set big endian input.
435431 Reader->setValueProfDataEndianness(support::big);
436432
437 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
438 ASSERT_TRUE(NoError(R.takeError()));
433 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
434 ASSERT_TRUE(NoError(R.getError()));
439435 ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget));
440436 ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
441437 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
516512 auto Profile = Writer.writeBuffer();
517513 readProfile(std::move(Profile));
518514
519 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
520 ASSERT_TRUE(NoError(R.takeError()));
515 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
516 ASSERT_TRUE(NoError(R.getError()));
521517 ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget));
522518 ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0));
523519 ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1));
569565
570566 InstrProfRecord Record1("foo", 0x1234, {1});
571567 auto Result1 = Writer.addRecord(std::move(Record1));
572 ASSERT_EQ(InstrProfError::take(std::move(Result1)),
573 instrprof_error::success);
568 ASSERT_EQ(Result1, instrprof_error::success);
574569
575570 // Verify counter overflow.
576571 InstrProfRecord Record2("foo", 0x1234, {Max});
577572 auto Result2 = Writer.addRecord(std::move(Record2));
578 ASSERT_EQ(InstrProfError::take(std::move(Result2)),
579 instrprof_error::counter_overflow);
573 ASSERT_EQ(Result2, instrprof_error::counter_overflow);
580574
581575 InstrProfRecord Record3(bar, 0x9012, {8});
582576 auto Result3 = Writer.addRecord(std::move(Record3));
583 ASSERT_EQ(InstrProfError::take(std::move(Result3)),
584 instrprof_error::success);
577 ASSERT_EQ(Result3, instrprof_error::success);
585578
586579 InstrProfRecord Record4("baz", 0x5678, {3, 4});
587580 Record4.reserveSites(IPVK_IndirectCallTarget, 1);
588581 InstrProfValueData VD4[] = {{uint64_t(bar), 1}};
589582 Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr);
590583 auto Result4 = Writer.addRecord(std::move(Record4));
591 ASSERT_EQ(InstrProfError::take(std::move(Result4)),
592 instrprof_error::success);
584 ASSERT_EQ(Result4, instrprof_error::success);
593585
594586 // Verify value data counter overflow.
595587 InstrProfRecord Record5("baz", 0x5678, {5, 6});
597589 InstrProfValueData VD5[] = {{uint64_t(bar), Max}};
598590 Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr);
599591 auto Result5 = Writer.addRecord(std::move(Record5));
600 ASSERT_EQ(InstrProfError::take(std::move(Result5)),
601 instrprof_error::counter_overflow);
592 ASSERT_EQ(Result5, instrprof_error::counter_overflow);
602593
603594 auto Profile = Writer.writeBuffer();
604595 readProfile(std::move(Profile));
605596
606597 // Verify saturation of counts.
607 Expected ReadRecord1 =
598 ErrorOr ReadRecord1 =
608599 Reader->getInstrProfRecord("foo", 0x1234);
609 ASSERT_TRUE(NoError(ReadRecord1.takeError()));
600 ASSERT_TRUE(NoError(ReadRecord1.getError()));
610601 ASSERT_EQ(Max, ReadRecord1->Counts[0]);
611602
612 Expected ReadRecord2 =
603 ErrorOr ReadRecord2 =
613604 Reader->getInstrProfRecord("baz", 0x5678);
614 ASSERT_TRUE(bool(ReadRecord2));
615605 ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget));
616606 std::unique_ptr VD =
617607 ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0);
656646 auto Profile = Writer.writeBuffer();
657647 readProfile(std::move(Profile));
658648
659 Expected R = Reader->getInstrProfRecord("caller", 0x1234);
660 ASSERT_TRUE(NoError(R.takeError()));
649 ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234);
650 ASSERT_TRUE(NoError(R.getError()));
661651 std::unique_ptr VD(
662652 R->getValueForSite(IPVK_IndirectCallTarget, 0));
663653 ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));