Index: include/llvm/ProfileData/Coverage/CoverageMapping.h =================================================================== --- include/llvm/ProfileData/Coverage/CoverageMapping.h +++ include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -23,28 +23,52 @@ #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h" -#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Error.h" #include "llvm/Support/raw_ostream.h" #include #include namespace llvm { namespace coverage { + enum class coveragemap_error { - success = 0, + success, eof, no_data_found, unsupported_version, truncated, malformed }; -} // end of coverage namespace. -} -namespace std { -template <> -struct is_error_code_enum : std::true_type { +class CoverageMapError : public ErrorInfo { +public: + CoverageMapError(coveragemap_error Err) : Err(Err) { + assert(Err != coveragemap_error::success && "Not an error"); + } + + static Error create(coveragemap_error Err = coveragemap_error::success) { + if (Err == coveragemap_error::success) + return Error::success(); + return make_error(Err); + } + + std::error_code convertToErrorCode() const override { + llvm_unreachable("Not supported"); + } + + void log(raw_ostream &OS) const override; + + std::string message() const override; + + coveragemap_error get() const { return Err; } + + static char ID; + +protected: + coveragemap_error Err; }; + +} // end of coverage namespace. } namespace llvm { @@ -265,7 +289,7 @@ /// \brief Return the number of times that a region of code associated with /// this counter was executed. - ErrorOr evaluate(const Counter &C) const; + Expected evaluate(const Counter &C) const; }; /// \brief Code coverage information for a single function. @@ -415,12 +439,12 @@ public: /// \brief Load the coverage mapping using the given readers. - static ErrorOr> + static Expected> load(CoverageMappingReader &CoverageReader, IndexedInstrProfReader &ProfileReader); /// \brief Load the coverage mapping from the given files. - static ErrorOr> + static Expected> load(StringRef ObjectFilename, StringRef ProfileFilename, StringRef Arch = StringRef()); @@ -466,12 +490,6 @@ CoverageData getCoverageForExpansion(const ExpansionRecord &Expansion); }; -const std::error_category &coveragemap_category(); - -inline std::error_code make_error_code(coveragemap_error E) { - return std::error_code(static_cast(E), coveragemap_category()); -} - // Profile coverage map has the following layout: // [CoverageMapFileHeader] // [ArrayStart] @@ -501,14 +519,13 @@ } // Return the PGO name of the function */ template - std::error_code getFuncName(InstrProfSymtab &ProfileNames, - StringRef &FuncName) const { + Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const { IntPtrT NameRef = getFuncNameRef(); uint32_t NameS = support::endian::byte_swap(NameSize); FuncName = ProfileNames.getFuncName(NameRef, NameS); if (NameS && FuncName.empty()) - return coveragemap_error::malformed; - return std::error_code(); + return CoverageMapError::create(coveragemap_error::malformed); + return CoverageMapError::create(); } }; @@ -530,11 +547,10 @@ } // Return the PGO name of the function */ template - std::error_code getFuncName(InstrProfSymtab &ProfileNames, - StringRef &FuncName) const { + Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const { uint64_t NameRef = getFuncNameRef(); FuncName = ProfileNames.getFuncName(NameRef); - return std::error_code(); + return CoverageMapError::create(); } }; Index: include/llvm/ProfileData/Coverage/CoverageMappingReader.h =================================================================== --- include/llvm/ProfileData/Coverage/CoverageMappingReader.h +++ include/llvm/ProfileData/Coverage/CoverageMappingReader.h @@ -69,7 +69,7 @@ class CoverageMappingReader { public: - virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0; + virtual Error readNextRecord(CoverageMappingRecord &Record) = 0; CoverageMappingIterator begin() { return CoverageMappingIterator(this); } CoverageMappingIterator end() { return CoverageMappingIterator(); } virtual ~CoverageMappingReader() {} @@ -82,10 +82,10 @@ RawCoverageReader(StringRef Data) : Data(Data) {} - std::error_code readULEB128(uint64_t &Result); - std::error_code readIntMax(uint64_t &Result, uint64_t MaxPlus1); - std::error_code readSize(uint64_t &Result); - std::error_code readString(StringRef &Result); + Error readULEB128(uint64_t &Result); + Error readIntMax(uint64_t &Result, uint64_t MaxPlus1); + Error readSize(uint64_t &Result); + Error readString(StringRef &Result); }; /// \brief Reader for the raw coverage filenames. @@ -100,7 +100,7 @@ RawCoverageFilenamesReader(StringRef Data, std::vector &Filenames) : RawCoverageReader(Data), Filenames(Filenames) {} - std::error_code read(); + Error read(); }; /// \brief Reader for the raw coverage mapping data. @@ -125,12 +125,12 @@ Filenames(Filenames), Expressions(Expressions), MappingRegions(MappingRegions) {} - std::error_code read(); + Error read(); private: - std::error_code decodeCounter(unsigned Value, Counter &C); - std::error_code readCounter(Counter &C); - std::error_code + Error decodeCounter(unsigned Value, Counter &C); + Error readCounter(Counter &C); + Error readMappingRegionsSubArray(std::vector &MappingRegions, unsigned InferredFileID, size_t NumFileIDs); }; @@ -170,11 +170,11 @@ BinaryCoverageReader() : CurrentRecord(0) {} public: - static ErrorOr> + static Expected> create(std::unique_ptr &ObjectBuffer, StringRef Arch); - std::error_code readNextRecord(CoverageMappingRecord &Record) override; + Error readNextRecord(CoverageMappingRecord &Record) override; }; } // end namespace coverage Index: include/llvm/ProfileData/InstrProf.h =================================================================== --- include/llvm/ProfileData/InstrProf.h +++ include/llvm/ProfileData/InstrProf.h @@ -25,7 +25,7 @@ #include "llvm/ProfileData/ProfileCommon.h" #include "llvm/Support/Endian.h" #include "llvm/Support/ErrorHandling.h" -#include "llvm/Support/ErrorOr.h" +#include "llvm/Support/Error.h" #include "llvm/Support/MD5.h" #include "llvm/Support/MathExtras.h" #include @@ -204,20 +204,17 @@ /// third field is the uncompressed strings; otherwise it is the /// compressed string. When the string compression is off, the /// second field will have value zero. -std::error_code -collectPGOFuncNameStrings(const std::vector &NameStrs, - bool doCompression, std::string &Result); +Error collectPGOFuncNameStrings(const std::vector &NameStrs, + bool doCompression, std::string &Result); /// Produce \c Result string with the same format described above. The input /// is vector of PGO function name variables that are referenced. -std::error_code -collectPGOFuncNameStrings(const std::vector &NameVars, - std::string &Result, bool doCompression = true); +Error collectPGOFuncNameStrings(const std::vector &NameVars, + std::string &Result, bool doCompression = true); class InstrProfSymtab; /// \c NameStrings is a string composed of one of more sub-strings encoded in /// the format described above. The substrings are seperated by 0 or more zero /// bytes. This method decodes the string and populates the \c Symtab. -std::error_code readPGOFuncNameStrings(StringRef NameStrings, - InstrProfSymtab &Symtab); +Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab); enum InstrProfValueKind : uint32_t { #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value, @@ -258,10 +255,8 @@ /// declared by users only. void createPGOFuncNameMetadata(Function &F, const std::string &PGOFuncName); -const std::error_category &instrprof_category(); - enum class instrprof_error { - success = 0, + success, eof, unrecognized_format, bad_magic, @@ -280,19 +275,51 @@ uncompress_failed }; -inline std::error_code make_error_code(instrprof_error E) { - return std::error_code(static_cast(E), instrprof_category()); -} +class InstrProfError : public ErrorInfo { +public: + InstrProfError(instrprof_error Err) : Err(Err) { + assert(Err != instrprof_error::success && "Not an error"); + } -inline instrprof_error MergeResult(instrprof_error &Accumulator, - instrprof_error Result) { - // Prefer first error encountered as later errors may be secondary effects of - // the initial problem. - if (Accumulator == instrprof_error::success && - Result != instrprof_error::success) - Accumulator = Result; - return Accumulator; -} + static Error create(instrprof_error Err = instrprof_error::success) { + if (Err == instrprof_error::success) + return Error::success(); + return make_error(Err); + } + + std::error_code convertToErrorCode() const override { + llvm_unreachable("Not implemented"); + } + + void log(raw_ostream &OS) const override; + + std::string message() const override; + + instrprof_error get() const { return Err; } + + static instrprof_error take(Error E) { + auto Err = instrprof_error::success; + handleAllErrors(std::move(E), [&Err](const InstrProfError &IPE) { + assert(Err == instrprof_error::success && "Multiple errors encountered"); + Err = IPE.get(); + }); + return Err; + } + + static void merge(instrprof_error &Acc, Error E) { + // Prefer first error encountered as later errors may be secondary effects + // of the initial problem. + if (Acc != instrprof_error::success) + consumeError(std::move(E)); + else + Acc = InstrProfError::take(std::move(E)); + } + + static char ID; + +protected: + instrprof_error Err; +}; namespace object { class SectionRef; @@ -336,14 +363,14 @@ /// only initialize the symtab with reference to the data and /// the section base address. The decompression will be delayed /// until before it is used. See also \c create(StringRef) method. - std::error_code create(object::SectionRef &Section); + Error create(object::SectionRef &Section); /// This interface is used by reader of CoverageMapping test /// format. - inline std::error_code create(StringRef D, uint64_t BaseAddr); + inline Error create(StringRef D, uint64_t BaseAddr); /// \c NameStrings is a string composed of one of more sub-strings /// encoded in the format described in \c collectPGOFuncNameStrings. /// This method is a wrapper to \c readPGOFuncNameStrings method. - inline std::error_code create(StringRef NameStrings); + inline Error create(StringRef NameStrings); /// A wrapper interface to populate the PGO symtab with functions /// decls from module \c M. This interface is used by transformation /// passes such as indirect function call promotion. Variable \c InLTO @@ -388,13 +415,13 @@ inline StringRef getNameData() const { return Data; } }; -std::error_code InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) { +Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) { Data = D; Address = BaseAddr; - return std::error_code(); + return Error::success(); } -std::error_code InstrProfSymtab::create(StringRef NameStrings) { +Error InstrProfSymtab::create(StringRef NameStrings) { return readPGOFuncNameStrings(NameStrings, *this); } @@ -465,9 +492,9 @@ /// Merge data from another InstrProfValueSiteRecord /// Optionally scale merged counts by \p Weight. - instrprof_error merge(InstrProfValueSiteRecord &Input, uint64_t Weight = 1); + Error merge(InstrProfValueSiteRecord &Input, uint64_t Weight = 1); /// Scale up value profile data counts. - instrprof_error scale(uint64_t Weight); + Error scale(uint64_t Weight); }; /// Profiling information for a single function. @@ -512,11 +539,11 @@ /// Merge the counts in \p Other into this one. /// Optionally scale merged counts by \p Weight. - instrprof_error merge(InstrProfRecord &Other, uint64_t Weight = 1); + Error merge(InstrProfRecord &Other, uint64_t Weight = 1); /// Scale up profile counts (including value profile data) by /// \p Weight. - instrprof_error scale(uint64_t Weight); + Error scale(uint64_t Weight); /// Sort value profile data (per site) by count. void sortValueData() { @@ -559,10 +586,10 @@ // Merge Value Profile data from Src record to this record for ValueKind. // Scale merged value counts by \p Weight. - instrprof_error mergeValueProfData(uint32_t ValueKind, InstrProfRecord &Src, - uint64_t Weight); + Error mergeValueProfData(uint32_t ValueKind, InstrProfRecord &Src, + uint64_t Weight); // Scale up value profile data count. - instrprof_error scaleValueProfData(uint32_t ValueKind, uint64_t Weight); + Error scaleValueProfData(uint32_t ValueKind, uint64_t Weight); }; uint32_t InstrProfRecord::getNumValueKinds() const { @@ -847,9 +874,4 @@ } // end namespace llvm -namespace std { -template <> -struct is_error_code_enum : std::true_type {}; -} - #endif // LLVM_PROFILEDATA_INSTRPROF_H Index: include/llvm/ProfileData/InstrProfData.inc =================================================================== --- include/llvm/ProfileData/InstrProfData.inc +++ include/llvm/ProfileData/InstrProfData.inc @@ -297,16 +297,15 @@ static std::unique_ptr serializeFrom(const InstrProfRecord &Record); /*! - * Check the integrity of the record. Return the error code when - * an error is detected, otherwise return instrprof_error::success. + * Check the integrity of the record. */ - instrprof_error checkIntegrity(); + Error checkIntegrity(); /*! * Return a pointer to \c ValueProfileData instance ready to be read. * All data in the instance are properly byte swapped. The input * data is assumed to be in little endian order. */ - static ErrorOr> + static Expected> getValueProfData(const unsigned char *SrcBuffer, const unsigned char *const SrcBufferEnd, support::endianness SrcDataEndianness); Index: include/llvm/ProfileData/InstrProfReader.h =================================================================== --- include/llvm/ProfileData/InstrProfReader.h +++ include/llvm/ProfileData/InstrProfReader.h @@ -19,7 +19,6 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/EndianStream.h" -#include "llvm/Support/ErrorOr.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/OnDiskHashTable.h" @@ -51,16 +50,16 @@ /// Base class and interface for reading profiling data of any known instrprof /// format. Provides an iterator over InstrProfRecords. class InstrProfReader { - std::error_code LastError; + instrprof_error LastError; public: InstrProfReader() : LastError(instrprof_error::success), Symtab() {} virtual ~InstrProfReader() {} /// Read the header. Required before reading first record. - virtual std::error_code readHeader() = 0; + virtual Error readHeader() = 0; /// Read a single record. - virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0; + virtual Error readNextRecord(InstrProfRecord &Record) = 0; /// Iterator over profile data. InstrProfIterator begin() { return InstrProfIterator(this); } InstrProfIterator end() { return InstrProfIterator(); } @@ -80,28 +79,29 @@ protected: std::unique_ptr Symtab; - /// Set the current std::error_code and return same. - std::error_code error(std::error_code EC) { - LastError = EC; - return EC; + /// Set the current error and return same. + Error error(instrprof_error Err) { + LastError = Err; + return InstrProfError::create(Err); } + Error error(Error E) { return error(InstrProfError::take(std::move(E))); } - /// Clear the current error code and return a successful one. - std::error_code success() { return error(instrprof_error::success); } + /// Clear the current error and return a successful one. + Error success() { return error(instrprof_error::success); } public: /// Return true if the reader has finished reading the profile data. bool isEOF() { return LastError == instrprof_error::eof; } /// Return true if the reader encountered an error reading profiling data. - bool hasError() { return LastError && !isEOF(); } + bool hasError() { return LastError != instrprof_error::success && !isEOF(); } /// Get the current error code. - std::error_code getError() { return LastError; } + Error getError() { return InstrProfError::create(LastError); } /// Factory method to create an appropriately typed reader for the given /// instrprof file. - static ErrorOr> create(std::string Path); + static Expected> create(std::string Path); - static ErrorOr> + static Expected> create(std::unique_ptr Buffer); }; @@ -123,7 +123,7 @@ TextInstrProfReader(const TextInstrProfReader &) = delete; TextInstrProfReader &operator=(const TextInstrProfReader &) = delete; - std::error_code readValueProfileData(InstrProfRecord &Record); + Error readValueProfileData(InstrProfRecord &Record); public: TextInstrProfReader(std::unique_ptr DataBuffer_) @@ -136,9 +136,9 @@ bool isIRLevelProfile() const override { return IsIRLevelProfile; } /// Read the header. - std::error_code readHeader() override; + Error readHeader() override; /// Read a single record. - std::error_code readNextRecord(InstrProfRecord &Record) override; + Error readNextRecord(InstrProfRecord &Record) override; InstrProfSymtab &getSymtab() override { assert(Symtab.get()); @@ -184,8 +184,8 @@ : DataBuffer(std::move(DataBuffer)) { } static bool hasFormat(const MemoryBuffer &DataBuffer); - std::error_code readHeader() override; - std::error_code readNextRecord(InstrProfRecord &Record) override; + Error readHeader() override; + Error readNextRecord(InstrProfRecord &Record) override; bool isIRLevelProfile() const override { return (Version & VARIANT_MASK_IR_PROF) != 0; } @@ -196,9 +196,9 @@ } private: - std::error_code createSymtab(InstrProfSymtab &Symtab); - std::error_code readNextHeader(const char *CurrentPos); - std::error_code readHeader(const RawInstrProf::Header &Header); + Error createSymtab(InstrProfSymtab &Symtab); + Error readNextHeader(const char *CurrentPos); + Error readHeader(const RawInstrProf::Header &Header); template IntT swap(IntT Int) const { return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int; } @@ -215,10 +215,10 @@ inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) { return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t)); } - std::error_code readName(InstrProfRecord &Record); - std::error_code readFuncHash(InstrProfRecord &Record); - std::error_code readRawCounts(InstrProfRecord &Record); - std::error_code readValueProfilingData(InstrProfRecord &Record); + Error readName(InstrProfRecord &Record); + Error readFuncHash(InstrProfRecord &Record); + Error readRawCounts(InstrProfRecord &Record); + Error readValueProfilingData(InstrProfRecord &Record); bool atEnd() const { return Data == DataEnd; } void advanceData() { Data++; @@ -295,9 +295,9 @@ struct InstrProfReaderIndexBase { // Read all the profile records with the same key pointed to the current // iterator. - virtual std::error_code getRecords(ArrayRef &Data) = 0; + virtual Error getRecords(ArrayRef &Data) = 0; // Read all the profile records with the key equal to FuncName - virtual std::error_code getRecords(StringRef FuncName, + virtual Error getRecords(StringRef FuncName, ArrayRef &Data) = 0; virtual void advanceToNextKey() = 0; virtual bool atEnd() const = 0; @@ -325,9 +325,9 @@ const unsigned char *const Base, IndexedInstrProf::HashT HashType, uint64_t Version); - std::error_code getRecords(ArrayRef &Data) override; - std::error_code getRecords(StringRef FuncName, - ArrayRef &Data) override; + Error getRecords(ArrayRef &Data) override; + Error getRecords(StringRef FuncName, + ArrayRef &Data) override; void advanceToNextKey() override { RecordIterator++; } bool atEnd() const override { return RecordIterator == HashTable->data_end(); @@ -374,27 +374,27 @@ static bool hasFormat(const MemoryBuffer &DataBuffer); /// Read the file header. - std::error_code readHeader() override; + Error readHeader() override; /// Read a single record. - std::error_code readNextRecord(InstrProfRecord &Record) override; + Error readNextRecord(InstrProfRecord &Record) override; /// Return the pointer to InstrProfRecord associated with FuncName /// and FuncHash - ErrorOr getInstrProfRecord(StringRef FuncName, - uint64_t FuncHash); + Expected getInstrProfRecord(StringRef FuncName, + uint64_t FuncHash); /// Fill Counts with the profile data for the given function name. - std::error_code getFunctionCounts(StringRef FuncName, uint64_t FuncHash, - std::vector &Counts); + Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash, + std::vector &Counts); /// Return the maximum of all known function counts. uint64_t getMaximumFunctionCount() { return Summary->getMaxFunctionCount(); } /// Factory method to create an indexed reader. - static ErrorOr> + static Expected> create(std::string Path); - static ErrorOr> + static Expected> create(std::unique_ptr Buffer); // Used for testing purpose only. Index: include/llvm/ProfileData/InstrProfWriter.h =================================================================== --- include/llvm/ProfileData/InstrProfWriter.h +++ include/llvm/ProfileData/InstrProfWriter.h @@ -46,7 +46,7 @@ /// Add function counts for the given function. If there are already counts /// for this function and the hash and number of counts match, each counter is /// summed. Optionally scale counts by \p Weight. - std::error_code addRecord(InstrProfRecord &&I, uint64_t Weight = 1); + Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1); /// Write the profile to \c OS void write(raw_fd_ostream &OS); /// Write the profile in text format to \c OS @@ -58,13 +58,14 @@ std::unique_ptr writeBuffer(); /// Set the ProfileKind. Report error if mixing FE and IR level profiles. - std::error_code setIsIRLevelProfile(bool IsIRLevel) { + Error setIsIRLevelProfile(bool IsIRLevel) { if (ProfileKind == PF_Unknown) { ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE; - return instrprof_error::success; + return InstrProfError::create(); } - return (IsIRLevel == (ProfileKind == PF_IRLevel)) ? - instrprof_error::success : instrprof_error::unsupported_version; + return InstrProfError::create((IsIRLevel == (ProfileKind == PF_IRLevel)) + ? instrprof_error::success + : instrprof_error::unsupported_version); } // Internal interface for testing purpose only. Index: lib/ProfileData/Coverage/CoverageMapping.cpp =================================================================== --- lib/ProfileData/Coverage/CoverageMapping.cpp +++ lib/ProfileData/Coverage/CoverageMapping.cpp @@ -143,28 +143,28 @@ } if (CounterValues.empty()) return; - ErrorOr Value = evaluate(C); + Expected Value = evaluate(C); if (!Value) return; OS << '[' << *Value << ']'; } -ErrorOr CounterMappingContext::evaluate(const Counter &C) const { +Expected CounterMappingContext::evaluate(const Counter &C) const { switch (C.getKind()) { case Counter::Zero: return 0; case Counter::CounterValueReference: if (C.getCounterID() >= CounterValues.size()) - return make_error_code(errc::argument_out_of_domain); + return errorCodeToError(errc::argument_out_of_domain); return CounterValues[C.getCounterID()]; case Counter::Expression: { if (C.getExpressionID() >= Expressions.size()) - return make_error_code(errc::argument_out_of_domain); + return errorCodeToError(errc::argument_out_of_domain); const auto &E = Expressions[C.getExpressionID()]; - ErrorOr LHS = evaluate(E.LHS); + Expected LHS = evaluate(E.LHS); if (!LHS) return LHS; - ErrorOr RHS = evaluate(E.RHS); + Expected RHS = evaluate(E.RHS); if (!RHS) return RHS; return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS; @@ -181,7 +181,7 @@ *this = FunctionRecordIterator(); } -ErrorOr> +Expected> CoverageMapping::load(CoverageMappingReader &CoverageReader, IndexedInstrProfReader &ProfileReader) { auto Coverage = std::unique_ptr(new CoverageMapping()); @@ -191,13 +191,14 @@ CounterMappingContext Ctx(Record.Expressions); Counts.clear(); - if (std::error_code EC = ProfileReader.getFunctionCounts( + if (Error E = ProfileReader.getFunctionCounts( Record.FunctionName, Record.FunctionHash, Counts)) { - if (EC == instrprof_error::hash_mismatch) { + instrprof_error IPE = InstrProfError::take(std::move(E)); + if (IPE == instrprof_error::hash_mismatch) { Coverage->MismatchedFunctionCount++; continue; - } else if (EC != instrprof_error::unknown_function) - return EC; + } else if (IPE != instrprof_error::unknown_function) + return InstrProfError::create(IPE); Counts.assign(Record.MappingRegions.size(), 0); } Ctx.setCounts(Counts); @@ -212,7 +213,7 @@ getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]); FunctionRecord Function(OrigFuncName, Record.Filenames); for (const auto &Region : Record.MappingRegions) { - ErrorOr ExecutionCount = Ctx.evaluate(Region.Count); + Expected ExecutionCount = Ctx.evaluate(Region.Count); if (!ExecutionCount) break; Function.pushRegion(Region, *ExecutionCount); @@ -228,20 +229,20 @@ return std::move(Coverage); } -ErrorOr> +Expected> CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename, StringRef Arch) { auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename); if (std::error_code EC = CounterMappingBuff.getError()) - return EC; + return errorCodeToError(EC); auto CoverageReaderOrErr = BinaryCoverageReader::create(CounterMappingBuff.get(), Arch); - if (std::error_code EC = CoverageReaderOrErr.getError()) - return EC; + if (Error E = CoverageReaderOrErr.takeError()) + return std::move(E); auto CoverageReader = std::move(CoverageReaderOrErr.get()); auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); - if (auto EC = ProfileReaderOrErr.getError()) - return EC; + if (Error E = ProfileReaderOrErr.takeError()) + return std::move(E); auto ProfileReader = std::move(ProfileReaderOrErr.get()); return load(*CoverageReader, *ProfileReader); } @@ -517,32 +518,24 @@ return ExpansionCoverage; } -namespace { -class CoverageMappingErrorCategoryType : public std::error_category { - const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; } - std::string message(int IE) const override { - auto E = static_cast(IE); - switch (E) { - case coveragemap_error::success: - return "Success"; - case coveragemap_error::eof: - return "End of File"; - case coveragemap_error::no_data_found: - return "No coverage data found"; - case coveragemap_error::unsupported_version: - return "Unsupported coverage format version"; - case coveragemap_error::truncated: - return "Truncated coverage data"; - case coveragemap_error::malformed: - return "Malformed coverage data"; - } - llvm_unreachable("A value of coveragemap_error has no message."); +char CoverageMapError::ID; + +void CoverageMapError::log(raw_ostream &OS) const { OS << message(); } + +std::string CoverageMapError::message() const { + switch (Err) { + case coveragemap_error::success: + return "Success"; + case coveragemap_error::eof: + return "End of File"; + case coveragemap_error::no_data_found: + return "No coverage data found"; + case coveragemap_error::unsupported_version: + return "Unsupported coverage format version"; + case coveragemap_error::truncated: + return "Truncated coverage data"; + case coveragemap_error::malformed: + return "Malformed coverage data"; } -}; -} - -static ManagedStatic ErrorCategory; - -const std::error_category &llvm::coverage::coveragemap_category() { - return *ErrorCategory; + llvm_unreachable("A value of coveragemap_error has no message."); } Index: lib/ProfileData/Coverage/CoverageMappingReader.cpp =================================================================== --- lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -31,49 +31,53 @@ void CoverageMappingIterator::increment() { // Check if all the records were read or if an error occurred while reading // the next record. - if (Reader->readNextRecord(Record)) - *this = CoverageMappingIterator(); + if (auto E = Reader->readNextRecord(Record)) { + handleAllErrors(std::move(E), [&](const CoverageMapError &CME) { + if (CME.get() != coveragemap_error::eof) + llvm_unreachable("Unexpected error in coverage mapping iterator"); + *this = CoverageMappingIterator(); + }); + } } -std::error_code RawCoverageReader::readULEB128(uint64_t &Result) { +Error RawCoverageReader::readULEB128(uint64_t &Result) { if (Data.size() < 1) - return coveragemap_error::truncated; + return CoverageMapError::create(coveragemap_error::truncated); unsigned N = 0; Result = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); Data = Data.substr(N); - return std::error_code(); + return CoverageMapError::create(); } -std::error_code RawCoverageReader::readIntMax(uint64_t &Result, - uint64_t MaxPlus1) { +Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) { if (auto Err = readULEB128(Result)) return Err; if (Result >= MaxPlus1) - return coveragemap_error::malformed; - return std::error_code(); + return CoverageMapError::create(coveragemap_error::malformed); + return CoverageMapError::create(); } -std::error_code RawCoverageReader::readSize(uint64_t &Result) { +Error RawCoverageReader::readSize(uint64_t &Result) { if (auto Err = readULEB128(Result)) return Err; // Sanity check the number. if (Result > Data.size()) - return coveragemap_error::malformed; - return std::error_code(); + return CoverageMapError::create(coveragemap_error::malformed); + return CoverageMapError::create(); } -std::error_code RawCoverageReader::readString(StringRef &Result) { +Error RawCoverageReader::readString(StringRef &Result) { uint64_t Length; if (auto Err = readSize(Length)) return Err; Result = Data.substr(0, Length); Data = Data.substr(Length); - return std::error_code(); + return CoverageMapError::create(); } -std::error_code RawCoverageFilenamesReader::read() { +Error RawCoverageFilenamesReader::read() { uint64_t NumFilenames; if (auto Err = readSize(NumFilenames)) return Err; @@ -83,19 +87,18 @@ return Err; Filenames.push_back(Filename); } - return std::error_code(); + return CoverageMapError::create(); } -std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value, - Counter &C) { +Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) { auto Tag = Value & Counter::EncodingTagMask; switch (Tag) { case Counter::Zero: C = Counter::getZero(); - return std::error_code(); + return CoverageMapError::create(); case Counter::CounterValueReference: C = Counter::getCounter(Value >> Counter::EncodingTagBits); - return std::error_code(); + return CoverageMapError::create(); default: break; } @@ -105,25 +108,25 @@ case CounterExpression::Add: { auto ID = Value >> Counter::EncodingTagBits; if (ID >= Expressions.size()) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); Expressions[ID].Kind = CounterExpression::ExprKind(Tag); C = Counter::getExpression(ID); break; } default: - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); } - return std::error_code(); + return CoverageMapError::create(); } -std::error_code RawCoverageMappingReader::readCounter(Counter &C) { +Error RawCoverageMappingReader::readCounter(Counter &C) { uint64_t EncodedCounter; if (auto Err = readIntMax(EncodedCounter, std::numeric_limits::max())) return Err; if (auto Err = decodeCounter(EncodedCounter, C)) return Err; - return std::error_code(); + return CoverageMapError::create(); } static const unsigned EncodingExpansionRegionBit = 1 @@ -132,7 +135,7 @@ /// \brief Read the sub-array of regions for the given inferred file id. /// \param NumFileIDs the number of file ids that are defined for this /// function. -std::error_code RawCoverageMappingReader::readMappingRegionsSubArray( +Error RawCoverageMappingReader::readMappingRegionsSubArray( std::vector &MappingRegions, unsigned InferredFileID, size_t NumFileIDs) { uint64_t NumRegions; @@ -160,7 +163,7 @@ ExpandedFileID = EncodedCounterAndRegion >> Counter::EncodingCounterTagAndExpansionRegionTagBits; if (ExpandedFileID >= NumFileIDs) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); } else { switch (EncodedCounterAndRegion >> Counter::EncodingCounterTagAndExpansionRegionTagBits) { @@ -171,7 +174,7 @@ Kind = CounterMappingRegion::SkippedRegion; break; default: - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); } } } @@ -184,7 +187,7 @@ if (auto Err = readULEB128(ColumnStart)) return Err; if (ColumnStart > std::numeric_limits::max()) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); if (auto Err = readIntMax(NumLines, std::numeric_limits::max())) return Err; if (auto Err = readIntMax(ColumnEnd, std::numeric_limits::max())) @@ -218,10 +221,10 @@ C, InferredFileID, ExpandedFileID, LineStart, ColumnStart, LineStart + NumLines, ColumnEnd, Kind)); } - return std::error_code(); + return CoverageMapError::create(); } -std::error_code RawCoverageMappingReader::read() { +Error RawCoverageMappingReader::read() { // Read the virtual file mapping. llvm::SmallVector VirtualFileMapping; @@ -287,14 +290,14 @@ } } - return std::error_code(); + return CoverageMapError::create(); } -std::error_code InstrProfSymtab::create(SectionRef &Section) { - if (auto Err = Section.getContents(Data)) - return Err; +Error InstrProfSymtab::create(SectionRef &Section) { + if (auto EC = Section.getContents(Data)) + return errorCodeToError(EC); Address = Section.getAddress(); - return std::error_code(); + return CoverageMapError::create(); } StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) { @@ -312,11 +315,10 @@ // a module. \p Buf is a reference to the buffer pointer pointing // to the \c CovHeader of coverage mapping data associated with // the module. - virtual std::error_code readFunctionRecords(const char *&Buf, - const char *End) = 0; + virtual Error readFunctionRecords(const char *&Buf, const char *End) = 0; virtual ~CovMapFuncRecordReader() {} template - static ErrorOr> + static Expected> get(coverage::CovMapVersion Version, InstrProfSymtab &P, std::vector &R, std::vector &F); @@ -344,11 +346,10 @@ : ProfileNames(P), Filenames(F), Records(R) {} ~VersionedCovMapFuncRecordReader() override {} - std::error_code readFunctionRecords(const char *&Buf, - const char *End) override { + Error readFunctionRecords(const char *&Buf, const char *End) override { using namespace support; if (Buf + sizeof(CovMapHeader) > End) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); auto CovHeader = reinterpret_cast(Buf); uint32_t NRecords = CovHeader->getNRecords(); uint32_t FilenamesSize = CovHeader->getFilenamesSize(); @@ -363,7 +364,7 @@ // Get the filenames. if (Buf + FilenamesSize > End) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); size_t FilenamesBegin = Filenames.size(); RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames); if (auto Err = Reader.read()) @@ -376,7 +377,7 @@ const char *CovEnd = Buf; if (Buf > End) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); // Each coverage map has an alignment of 8, so we need to adjust alignment // before reading the next map. Buf += alignmentAdjustment(Buf, 8); @@ -389,7 +390,7 @@ // Now use that to read the coverage data. if (CovBuf + DataSize > CovEnd) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); auto Mapping = StringRef(CovBuf, DataSize); CovBuf += DataSize; @@ -403,21 +404,20 @@ } StringRef FuncName; - if (std::error_code EC = - CFR->template getFuncName(ProfileNames, FuncName)) - return EC; + if (Error E = CFR->template getFuncName(ProfileNames, FuncName)) + return E; Records.push_back(BinaryCoverageReader::ProfileMappingRecord( Version, FuncName, FuncHash, Mapping, FilenamesBegin, Filenames.size() - FilenamesBegin)); CFR++; } - return std::error_code(); + return CoverageMapError::create(); } }; } // end anonymous namespace template -ErrorOr> CovMapFuncRecordReader::get( +Expected> CovMapFuncRecordReader::get( coverage::CovMapVersion Version, InstrProfSymtab &P, std::vector &R, std::vector &F) { @@ -428,8 +428,8 @@ CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F); case CovMapVersion::Version2: // Decompress the name data. - if (auto EC = P.create(P.getNameData())) - return EC; + if (Error E = P.create(P.getNameData())) + return std::move(E); return llvm::make_unique>(P, R, F); } @@ -437,7 +437,7 @@ } template -static std::error_code readCoverageMappingData( +static Error readCoverageMappingData( InstrProfSymtab &ProfileNames, StringRef Data, std::vector &Records, std::vector &Filenames) { @@ -447,89 +447,89 @@ reinterpret_cast(Data.data()); CovMapVersion Version = (CovMapVersion)CovHeader->getVersion(); if (Version > coverage::CovMapVersion::CurrentVersion) - return coveragemap_error::unsupported_version; - ErrorOr> ReaderErrorOr = + return CoverageMapError::create(coveragemap_error::unsupported_version); + Expected> ReaderExpected = CovMapFuncRecordReader::get(Version, ProfileNames, Records, Filenames); - if (auto EC = ReaderErrorOr.getError()) - return EC; - auto Reader = std::move(ReaderErrorOr.get()); + if (Error E = ReaderExpected.takeError()) + return E; + auto Reader = std::move(ReaderExpected.get()); for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) { - if (std::error_code EC = Reader->readFunctionRecords(Buf, End)) - return EC; + if (Error E = Reader->readFunctionRecords(Buf, End)) + return E; } - return std::error_code(); + return CoverageMapError::create(); } static const char *TestingFormatMagic = "llvmcovmtestdata"; -static std::error_code loadTestingFormat(StringRef Data, - InstrProfSymtab &ProfileNames, - StringRef &CoverageMapping, - uint8_t &BytesInAddress, - support::endianness &Endian) { +static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames, + StringRef &CoverageMapping, + uint8_t &BytesInAddress, + support::endianness &Endian) { BytesInAddress = 8; Endian = support::endianness::little; Data = Data.substr(StringRef(TestingFormatMagic).size()); if (Data.size() < 1) - return coveragemap_error::truncated; + return CoverageMapError::create(coveragemap_error::truncated); unsigned N = 0; auto ProfileNamesSize = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); Data = Data.substr(N); if (Data.size() < 1) - return coveragemap_error::truncated; + return CoverageMapError::create(coveragemap_error::truncated); N = 0; uint64_t Address = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); Data = Data.substr(N); if (Data.size() < ProfileNamesSize) - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); ProfileNames.create(Data.substr(0, ProfileNamesSize), Address); CoverageMapping = Data.substr(ProfileNamesSize); - return std::error_code(); + return CoverageMapError::create(); } -static ErrorOr lookupSection(ObjectFile &OF, StringRef Name) { +static Expected lookupSection(ObjectFile &OF, StringRef Name) { StringRef FoundName; for (const auto &Section : OF.sections()) { if (auto EC = Section.getName(FoundName)) - return EC; + return errorCodeToError(EC); if (FoundName == Name) return Section; } - return coveragemap_error::no_data_found; + return CoverageMapError::create(coveragemap_error::no_data_found); } -static std::error_code -loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames, - StringRef &CoverageMapping, uint8_t &BytesInAddress, - support::endianness &Endian, StringRef Arch) { +static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer, + InstrProfSymtab &ProfileNames, + StringRef &CoverageMapping, + uint8_t &BytesInAddress, + support::endianness &Endian, StringRef Arch) { auto BinOrErr = object::createBinary(ObjectBuffer); if (!BinOrErr) - return errorToErrorCode(BinOrErr.takeError()); + return BinOrErr.takeError(); auto Bin = std::move(BinOrErr.get()); std::unique_ptr OF; if (auto *Universal = dyn_cast(Bin.get())) { // If we have a universal binary, try to look up the object for the // appropriate architecture. auto ObjectFileOrErr = Universal->getObjectForArch(Arch); - if (std::error_code EC = ObjectFileOrErr.getError()) - return EC; + if (auto EC = ObjectFileOrErr.getError()) + return errorCodeToError(EC); OF = std::move(ObjectFileOrErr.get()); } else if (isa(Bin.get())) { // For any other object file, upcast and take ownership. OF.reset(cast(Bin.release())); // If we've asked for a particular arch, make sure they match. if (!Arch.empty() && OF->getArch() != Triple(Arch).getArch()) - return object_error::arch_not_found; + return errorCodeToError(object_error::arch_not_found); } else // We can only handle object files. - return coveragemap_error::malformed; + return CoverageMapError::create(coveragemap_error::malformed); // The coverage uses native pointer sizes for the object it's written in. BytesInAddress = OF->getBytesInAddress(); @@ -538,23 +538,23 @@ // Look for the sections that we are interested in. auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false)); - if (auto EC = NamesSection.getError()) - return EC; + if (auto E = NamesSection.takeError()) + return E; auto CoverageSection = lookupSection(*OF, getInstrProfCoverageSectionName(false)); - if (auto EC = CoverageSection.getError()) - return EC; + if (auto E = CoverageSection.takeError()) + return E; // Get the contents of the given sections. - if (std::error_code EC = CoverageSection->getContents(CoverageMapping)) - return EC; - if (std::error_code EC = ProfileNames.create(*NamesSection)) - return EC; + if (auto EC = CoverageSection->getContents(CoverageMapping)) + return errorCodeToError(EC); + if (Error E = ProfileNames.create(*NamesSection)) + return E; - return std::error_code(); + return CoverageMapError::create(); } -ErrorOr> +Expected> BinaryCoverageReader::create(std::unique_ptr &ObjectBuffer, StringRef Arch) { std::unique_ptr Reader(new BinaryCoverageReader()); @@ -562,44 +562,44 @@ StringRef Coverage; uint8_t BytesInAddress; support::endianness Endian; - std::error_code EC; + Error E; + consumeError(std::move(E)); if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) // This is a special format used for testing. - EC = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames, - Coverage, BytesInAddress, Endian); + E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames, + Coverage, BytesInAddress, Endian); else - EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames, - Coverage, BytesInAddress, Endian, Arch); - if (EC) - return EC; + E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames, + Coverage, BytesInAddress, Endian, Arch); + if (E) + return std::move(E); if (BytesInAddress == 4 && Endian == support::endianness::little) - EC = readCoverageMappingData( + E = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 4 && Endian == support::endianness::big) - EC = readCoverageMappingData( + E = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 8 && Endian == support::endianness::little) - EC = readCoverageMappingData( + E = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 8 && Endian == support::endianness::big) - EC = readCoverageMappingData( + E = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else - return coveragemap_error::malformed; - if (EC) - return EC; + return CoverageMapError::create(coveragemap_error::malformed); + if (E) + return std::move(E); return std::move(Reader); } -std::error_code -BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { +Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { if (CurrentRecord >= MappingRecords.size()) - return coveragemap_error::eof; + return CoverageMapError::create(coveragemap_error::eof); FunctionsFilenames.clear(); Expressions.clear(); @@ -619,5 +619,5 @@ Record.MappingRegions = MappingRegions; ++CurrentRecord; - return std::error_code(); + return CoverageMapError::create(); } Index: lib/ProfileData/InstrProf.cpp =================================================================== --- lib/ProfileData/InstrProf.cpp +++ lib/ProfileData/InstrProf.cpp @@ -22,64 +22,55 @@ #include "llvm/Support/Compression.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/LEB128.h" -#include "llvm/Support/ManagedStatic.h" using namespace llvm; -namespace { -class InstrProfErrorCategoryType : public std::error_category { - const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } - std::string message(int IE) const override { - instrprof_error E = static_cast(IE); - switch (E) { - case instrprof_error::success: - return "Success"; - case instrprof_error::eof: - return "End of File"; - case instrprof_error::unrecognized_format: - return "Unrecognized instrumentation profile encoding format"; - case instrprof_error::bad_magic: - return "Invalid instrumentation profile data (bad magic)"; - case instrprof_error::bad_header: - return "Invalid instrumentation profile data (file header is corrupt)"; - case instrprof_error::unsupported_version: - return "Unsupported instrumentation profile format version"; - case instrprof_error::unsupported_hash_type: - return "Unsupported instrumentation profile hash type"; - case instrprof_error::too_large: - return "Too much profile data"; - case instrprof_error::truncated: - return "Truncated profile data"; - case instrprof_error::malformed: - return "Malformed instrumentation profile data"; - case instrprof_error::unknown_function: - return "No profile data available for function"; - case instrprof_error::hash_mismatch: - return "Function control flow change detected (hash mismatch)"; - case instrprof_error::count_mismatch: - return "Function basic block count change detected (counter mismatch)"; - case instrprof_error::counter_overflow: - return "Counter overflow"; - case instrprof_error::value_site_count_mismatch: - return "Function value site count change detected (counter mismatch)"; - case instrprof_error::compress_failed: - return "Failed to compress data (zlib)"; - case instrprof_error::uncompress_failed: - return "Failed to uncompress data (zlib)"; - } - llvm_unreachable("A value of instrprof_error has no message."); - } -}; -} // end anonymous namespace - -static ManagedStatic ErrorCategory; +namespace llvm { -const std::error_category &llvm::instrprof_category() { - return *ErrorCategory; +char InstrProfError::ID; + +void InstrProfError::log(raw_ostream &OS) const { OS << message(); } + +std::string InstrProfError::message() const { + switch (Err) { + case instrprof_error::success: + return "Success"; + case instrprof_error::eof: + return "End of File"; + case instrprof_error::unrecognized_format: + return "Unrecognized instrumentation profile encoding format"; + case instrprof_error::bad_magic: + return "Invalid instrumentation profile data (bad magic)"; + case instrprof_error::bad_header: + return "Invalid instrumentation profile data (file header is corrupt)"; + case instrprof_error::unsupported_version: + return "Unsupported instrumentation profile format version"; + case instrprof_error::unsupported_hash_type: + return "Unsupported instrumentation profile hash type"; + case instrprof_error::too_large: + return "Too much profile data"; + case instrprof_error::truncated: + return "Truncated profile data"; + case instrprof_error::malformed: + return "Malformed instrumentation profile data"; + case instrprof_error::unknown_function: + return "No profile data available for function"; + case instrprof_error::hash_mismatch: + return "Function control flow change detected (hash mismatch)"; + case instrprof_error::count_mismatch: + return "Function basic block count change detected (counter mismatch)"; + case instrprof_error::counter_overflow: + return "Counter overflow"; + case instrprof_error::value_site_count_mismatch: + return "Function value site count change detected (counter mismatch)"; + case instrprof_error::compress_failed: + return "Failed to compress data (zlib)"; + case instrprof_error::uncompress_failed: + return "Failed to uncompress data (zlib)"; + } + llvm_unreachable("A value of instrprof_error has no message."); } -namespace llvm { - std::string getPGOFuncName(StringRef RawFuncName, GlobalValue::LinkageTypes Linkage, StringRef FileName, @@ -189,9 +180,8 @@ finalizeSymtab(); } -std::error_code -collectPGOFuncNameStrings(const std::vector &NameStrs, - bool doCompression, std::string &Result) { +Error collectPGOFuncNameStrings(const std::vector &NameStrs, + bool doCompression, std::string &Result) { assert(NameStrs.size() && "No name data to emit"); uint8_t Header[16], *P = Header; @@ -213,11 +203,12 @@ unsigned HeaderLen = P - &Header[0]; Result.append(HeaderStr, HeaderLen); Result += InputStr; - return make_error_code(instrprof_error::success); + return InstrProfError::create(); }; - if (!doCompression) + if (!doCompression) { return WriteStringToResult(0, UncompressedNameStrings); + } SmallVector CompressedNameStrings; zlib::Status Success = @@ -225,7 +216,7 @@ zlib::BestSizeCompression); if (Success != zlib::StatusOK) - return make_error_code(instrprof_error::compress_failed); + return InstrProfError::create(instrprof_error::compress_failed); return WriteStringToResult( CompressedNameStrings.size(), @@ -239,9 +230,8 @@ return NameStr; } -std::error_code -collectPGOFuncNameStrings(const std::vector &NameVars, - std::string &Result, bool doCompression) { +Error collectPGOFuncNameStrings(const std::vector &NameVars, + std::string &Result, bool doCompression) { std::vector NameStrs; for (auto *NameVar : NameVars) { NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar)); @@ -250,8 +240,7 @@ NameStrs, zlib::isAvailable() && doCompression, Result); } -std::error_code readPGOFuncNameStrings(StringRef NameStrings, - InstrProfSymtab &Symtab) { +Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { const uint8_t *P = reinterpret_cast(NameStrings.data()); const uint8_t *EndP = reinterpret_cast(NameStrings.data() + NameStrings.size()); @@ -269,7 +258,7 @@ CompressedSize); if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, UncompressedSize) != zlib::StatusOK) - return make_error_code(instrprof_error::uncompress_failed); + return InstrProfError::create(instrprof_error::uncompress_failed); P += CompressedSize; NameStrings = StringRef(UncompressedNameStrings.data(), UncompressedNameStrings.size()); @@ -288,10 +277,10 @@ P++; } Symtab.finalizeSymtab(); - return make_error_code(instrprof_error::success); + return InstrProfError::create(); } -instrprof_error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input, +Error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input, uint64_t Weight) { this->sortByTargetValues(); Input.sortByTargetValues(); @@ -312,10 +301,10 @@ } ValueData.insert(I, *J); } - return Result; + return InstrProfError::create(Result); } -instrprof_error InstrProfValueSiteRecord::scale(uint64_t Weight) { +Error InstrProfValueSiteRecord::scale(uint64_t Weight) { instrprof_error Result = instrprof_error::success; for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) { bool Overflowed; @@ -323,34 +312,34 @@ if (Overflowed) Result = instrprof_error::counter_overflow; } - return Result; + return InstrProfError::create(Result); } // Merge Value Profile data from Src record to this record for ValueKind. // Scale merged value counts by \p Weight. -instrprof_error InstrProfRecord::mergeValueProfData(uint32_t ValueKind, - InstrProfRecord &Src, - uint64_t Weight) { +Error InstrProfRecord::mergeValueProfData(uint32_t ValueKind, + InstrProfRecord &Src, + uint64_t Weight) { uint32_t ThisNumValueSites = getNumValueSites(ValueKind); uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind); if (ThisNumValueSites != OtherNumValueSites) - return instrprof_error::value_site_count_mismatch; + return InstrProfError::create(instrprof_error::value_site_count_mismatch); std::vector &ThisSiteRecords = getValueSitesForKind(ValueKind); std::vector &OtherSiteRecords = Src.getValueSitesForKind(ValueKind); instrprof_error Result = instrprof_error::success; for (uint32_t I = 0; I < ThisNumValueSites; I++) - MergeResult(Result, ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight)); - return Result; + InstrProfError::merge( + Result, ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight)); + return InstrProfError::create(Result); } -instrprof_error InstrProfRecord::merge(InstrProfRecord &Other, - uint64_t Weight) { +Error InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) { // If the number of counters doesn't match we either have bad data // or a hash collision. if (Counts.size() != Other.Counts.size()) - return instrprof_error::count_mismatch; + return InstrProfError::create(instrprof_error::count_mismatch); instrprof_error Result = instrprof_error::success; @@ -363,35 +352,33 @@ } for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) - MergeResult(Result, mergeValueProfData(Kind, Other, Weight)); + InstrProfError::merge(Result, mergeValueProfData(Kind, Other, Weight)); - return Result; + return InstrProfError::create(Result); } -instrprof_error InstrProfRecord::scaleValueProfData(uint32_t ValueKind, - uint64_t Weight) { +Error InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) { uint32_t ThisNumValueSites = getNumValueSites(ValueKind); std::vector &ThisSiteRecords = getValueSitesForKind(ValueKind); instrprof_error Result = instrprof_error::success; for (uint32_t I = 0; I < ThisNumValueSites; I++) - MergeResult(Result, ThisSiteRecords[I].scale(Weight)); - return Result; + InstrProfError::merge(Result, ThisSiteRecords[I].scale(Weight)); + return InstrProfError::create(Result); } -instrprof_error InstrProfRecord::scale(uint64_t Weight) { +Error InstrProfRecord::scale(uint64_t Weight) { instrprof_error Result = instrprof_error::success; for (auto &Count : this->Counts) { bool Overflowed; Count = SaturatingMultiply(Count, Weight, &Overflowed); - if (Overflowed && Result == instrprof_error::success) { + if (Overflowed) Result = instrprof_error::counter_overflow; - } } for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) - MergeResult(Result, scaleValueProfData(Kind, Weight)); + InstrProfError::merge(Result, scaleValueProfData(Kind, Weight)); - return Result; + return InstrProfError::create(Result); } // Map indirect call target name hash to name string. @@ -564,45 +551,45 @@ ValueProfData()); } -instrprof_error ValueProfData::checkIntegrity() { +Error ValueProfData::checkIntegrity() { if (NumValueKinds > IPVK_Last + 1) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); // Total size needs to be mulltiple of quadword size. if (TotalSize % sizeof(uint64_t)) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); ValueProfRecord *VR = getFirstValueProfRecord(this); for (uint32_t K = 0; K < this->NumValueKinds; K++) { if (VR->Kind > IPVK_Last) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); VR = getValueProfRecordNext(VR); if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); } - return instrprof_error::success; + return InstrProfError::create(); } -ErrorOr> +Expected> ValueProfData::getValueProfData(const unsigned char *D, const unsigned char *const BufferEnd, support::endianness Endianness) { using namespace support; if (D + sizeof(ValueProfData) > BufferEnd) - return instrprof_error::truncated; + return InstrProfError::create(instrprof_error::truncated); const unsigned char *Header = D; uint32_t TotalSize = swapToHostOrder(Header, Endianness); if (D + TotalSize > BufferEnd) - return instrprof_error::too_large; + return InstrProfError::create(instrprof_error::too_large); std::unique_ptr VPD = allocValueProfData(TotalSize); memcpy(VPD.get(), D, TotalSize); // Byte swap. VPD->swapBytesToHost(Endianness); - instrprof_error EC = VPD->checkIntegrity(); - if (EC != instrprof_error::success) - return EC; + Error E = VPD->checkIntegrity(); + if (E) + return std::move(E); return std::move(VPD); } Index: lib/ProfileData/InstrProfReader.cpp =================================================================== --- lib/ProfileData/InstrProfReader.cpp +++ lib/ProfileData/InstrProfReader.cpp @@ -18,33 +18,33 @@ using namespace llvm; -static ErrorOr> +static Expected> setupMemoryBuffer(std::string Path) { ErrorOr> BufferOrErr = MemoryBuffer::getFileOrSTDIN(Path); if (std::error_code EC = BufferOrErr.getError()) - return EC; + return errorCodeToError(EC); return std::move(BufferOrErr.get()); } -static std::error_code initializeReader(InstrProfReader &Reader) { +static Error initializeReader(InstrProfReader &Reader) { return Reader.readHeader(); } -ErrorOr> +Expected> InstrProfReader::create(std::string Path) { // Set up the buffer to read. auto BufferOrError = setupMemoryBuffer(Path); - if (std::error_code EC = BufferOrError.getError()) - return EC; + if (Error E = BufferOrError.takeError()) + return std::move(E); return InstrProfReader::create(std::move(BufferOrError.get())); } -ErrorOr> +Expected> InstrProfReader::create(std::unique_ptr Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits::max()) - return instrprof_error::too_large; + return InstrProfError::create(instrprof_error::too_large); std::unique_ptr Result; // Create the reader. @@ -57,46 +57,50 @@ else if (TextInstrProfReader::hasFormat(*Buffer)) Result.reset(new TextInstrProfReader(std::move(Buffer))); else - return instrprof_error::unrecognized_format; + return InstrProfError::create(instrprof_error::unrecognized_format); // Initialize the reader and return the result. - if (std::error_code EC = initializeReader(*Result)) - return EC; + if (Error E = initializeReader(*Result)) + return std::move(E); return std::move(Result); } -ErrorOr> +Expected> IndexedInstrProfReader::create(std::string Path) { // Set up the buffer to read. auto BufferOrError = setupMemoryBuffer(Path); - if (std::error_code EC = BufferOrError.getError()) - return EC; + if (Error E = BufferOrError.takeError()) + return std::move(E); return IndexedInstrProfReader::create(std::move(BufferOrError.get())); } -ErrorOr> +Expected> IndexedInstrProfReader::create(std::unique_ptr Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits::max()) - return instrprof_error::too_large; + return InstrProfError::create(instrprof_error::too_large); // Create the reader. if (!IndexedInstrProfReader::hasFormat(*Buffer)) - return instrprof_error::bad_magic; + return InstrProfError::create(instrprof_error::bad_magic); auto Result = llvm::make_unique(std::move(Buffer)); // Initialize the reader and return the result. - if (std::error_code EC = initializeReader(*Result)) - return EC; + if (Error E = initializeReader(*Result)) + return std::move(E); return std::move(Result); } void InstrProfIterator::Increment() { - if (Reader->readNextRecord(Record)) + if (auto E = Reader->readNextRecord(Record)) { + instrprof_error Err = InstrProfError::take(std::move(E)); + if (Err != instrprof_error::eof) + llvm_unreachable("Unexpected error in profile iterator"); *this = InstrProfIterator(); + } } bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) { @@ -112,7 +116,7 @@ // Read the profile variant flag from the header: ":FE" means this is a FE // generated profile. ":IR" means this is an IR level profile. Other strings // with a leading ':' will be reported an error format. -std::error_code TextInstrProfReader::readHeader() { +Error TextInstrProfReader::readHeader() { Symtab.reset(new InstrProfSymtab()); bool IsIRInstr = false; if (!Line->startswith(":")) { @@ -125,14 +129,14 @@ else if (Str.equals_lower("fe")) IsIRInstr = false; else - return instrprof_error::bad_header; + return error(instrprof_error::bad_header); ++Line; IsIRLevelProfile = IsIRInstr; return success(); } -std::error_code +Error TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { #define CHECK_LINE_END(Line) \ @@ -196,7 +200,7 @@ #undef VP_READ_ADVANCE } -std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { +Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { // Skip empty lines and comments. while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) ++Line; @@ -238,8 +242,8 @@ } // Check if value profile data exists and read it if so. - if (std::error_code EC = readValueProfileData(Record)) - return EC; + if (Error E = readValueProfileData(Record)) + return E; // This is needed to avoid two pass parsing because llvm-profdata // does dumping while reading. @@ -258,7 +262,7 @@ } template -std::error_code RawInstrProfReader::readHeader() { +Error RawInstrProfReader::readHeader() { if (!hasFormat(*DataBuffer)) return error(instrprof_error::bad_magic); if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) @@ -270,26 +274,25 @@ } template -std::error_code -RawInstrProfReader::readNextHeader(const char *CurrentPos) { +Error RawInstrProfReader::readNextHeader(const char *CurrentPos) { const char *End = DataBuffer->getBufferEnd(); // Skip zero padding between profiles. while (CurrentPos != End && *CurrentPos == 0) ++CurrentPos; // If there's nothing left, we're done. if (CurrentPos == End) - return instrprof_error::eof; + return InstrProfError::create(instrprof_error::eof); // If there isn't enough space for another header, this is probably just // garbage at the end of the file. if (CurrentPos + sizeof(RawInstrProf::Header) > End) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); // The writer ensures each profile is padded to start at an aligned address. if (reinterpret_cast(CurrentPos) % alignOf()) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); // The magic should have the same byte order as in the previous header. uint64_t Magic = *reinterpret_cast(CurrentPos); if (Magic != swap(RawInstrProf::getMagic())) - return instrprof_error::bad_magic; + return InstrProfError::create(instrprof_error::bad_magic); // There's another profile to read, so we need to process the header. auto *Header = reinterpret_cast(CurrentPos); @@ -297,11 +300,9 @@ } template -std::error_code -RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) { - std::error_code EC = Symtab.create(StringRef(NamesStart, NamesSize)); - if (EC) - return EC; +Error RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) { + if (Error E = Symtab.create(StringRef(NamesStart, NamesSize))) + return error(std::move(E)); for (const RawInstrProf::ProfileData *I = Data; I != DataEnd; ++I) { const IntPtrT FPtr = swap(I->FunctionPointer); if (!FPtr) @@ -313,8 +314,8 @@ } template -std::error_code -RawInstrProfReader::readHeader(const RawInstrProf::Header &Header) { +Error RawInstrProfReader::readHeader( + const RawInstrProf::Header &Header) { Version = swap(Header.Version); if (GET_VERSION(Version) != RawInstrProf::Version) return error(instrprof_error::unsupported_version); @@ -349,28 +350,27 @@ ProfileEnd = Start + ProfileSize; std::unique_ptr NewSymtab = make_unique(); - if (auto EC = createSymtab(*NewSymtab.get())) - return EC; + if (Error E = createSymtab(*NewSymtab.get())) + return E; Symtab = std::move(NewSymtab); return success(); } template -std::error_code RawInstrProfReader::readName(InstrProfRecord &Record) { +Error RawInstrProfReader::readName(InstrProfRecord &Record) { Record.Name = getName(Data->NameRef); return success(); } template -std::error_code RawInstrProfReader::readFuncHash( - InstrProfRecord &Record) { +Error RawInstrProfReader::readFuncHash(InstrProfRecord &Record) { Record.Hash = swap(Data->FuncHash); return success(); } template -std::error_code RawInstrProfReader::readRawCounts( +Error RawInstrProfReader::readRawCounts( InstrProfRecord &Record) { uint32_t NumCounters = swap(Data->NumCounters); IntPtrT CounterPtr = Data->CounterPtr; @@ -397,8 +397,8 @@ } template -std::error_code -RawInstrProfReader::readValueProfilingData(InstrProfRecord &Record) { +Error RawInstrProfReader::readValueProfilingData( + InstrProfRecord &Record) { Record.clearValueData(); CurValueDataSize = 0; @@ -410,13 +410,13 @@ if (!NumValueKinds) return success(); - ErrorOr> VDataPtrOrErr = + Expected> VDataPtrOrErr = ValueProfData::getValueProfData(ValueDataStart, (const unsigned char *)ProfileEnd, getDataEndianness()); - if (VDataPtrOrErr.getError()) - return VDataPtrOrErr.getError(); + if (Error E = VDataPtrOrErr.takeError()) + return E; // Note that besides deserialization, this also performs the conversion for // indirect call targets. The function pointers from the raw profile are @@ -427,27 +427,26 @@ } template -std::error_code -RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { +Error RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { if (atEnd()) - if (std::error_code EC = readNextHeader(ProfileEnd)) - return EC; + if (Error E = readNextHeader(ProfileEnd)) + return E; // Read name ad set it in Record. - if (std::error_code EC = readName(Record)) - return EC; + if (Error E = readName(Record)) + return E; // Read FuncHash and set it in Record. - if (std::error_code EC = readFuncHash(Record)) - return EC; + if (Error E = readFuncHash(Record)) + return E; // Read raw counts and set Record. - if (std::error_code EC = readRawCounts(Record)) - return EC; + if (Error E = readRawCounts(Record)) + return E; // Read value data and set Record. - if (std::error_code EC = readValueProfilingData(Record)) - return EC; + if (Error E = readValueProfilingData(Record)) + return E; // Iterate. advanceData(); @@ -469,10 +468,10 @@ bool InstrProfLookupTrait::readValueProfilingData( const unsigned char *&D, const unsigned char *const End) { - ErrorOr> VDataPtrOrErr = + Expected> VDataPtrOrErr = ValueProfData::getValueProfData(D, End, ValueProfDataEndianness); - if (VDataPtrOrErr.getError()) + if (VDataPtrOrErr.takeError()) return false; VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr); @@ -528,31 +527,31 @@ } template -std::error_code InstrProfReaderIndex::getRecords( +Error InstrProfReaderIndex::getRecords( StringRef FuncName, ArrayRef &Data) { auto Iter = HashTable->find(FuncName); if (Iter == HashTable->end()) - return instrprof_error::unknown_function; + return InstrProfError::create(instrprof_error::unknown_function); Data = (*Iter); if (Data.empty()) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); - return instrprof_error::success; + return InstrProfError::create(instrprof_error::success); } template -std::error_code InstrProfReaderIndex::getRecords( +Error InstrProfReaderIndex::getRecords( ArrayRef &Data) { if (atEnd()) - return instrprof_error::eof; + return InstrProfError::create(instrprof_error::eof); Data = *RecordIterator; if (Data.empty()) - return instrprof_error::malformed; + return InstrProfError::create(instrprof_error::malformed); - return instrprof_error::success; + return InstrProfError::create(instrprof_error::success); } template @@ -611,7 +610,7 @@ } } -std::error_code IndexedInstrProfReader::readHeader() { +Error IndexedInstrProfReader::readHeader() { const unsigned char *Start = (const unsigned char *)DataBuffer->getBufferStart(); const unsigned char *Cur = Start; @@ -663,13 +662,13 @@ return *Symtab.get(); } -ErrorOr +Expected IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, uint64_t FuncHash) { ArrayRef Data; - std::error_code EC = Index->getRecords(FuncName, Data); - if (EC != instrprof_error::success) - return EC; + Error Err = Index->getRecords(FuncName, Data); + if (Err) + return std::move(Err); // Found it. Look for counters with the right hash. for (unsigned I = 0, E = Data.size(); I < E; ++I) { // Check for a match and fill the vector if there is one. @@ -680,26 +679,25 @@ return error(instrprof_error::hash_mismatch); } -std::error_code -IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, uint64_t FuncHash, - std::vector &Counts) { - ErrorOr Record = getInstrProfRecord(FuncName, FuncHash); - if (std::error_code EC = Record.getError()) - return EC; +Error IndexedInstrProfReader::getFunctionCounts(StringRef FuncName, + uint64_t FuncHash, + std::vector &Counts) { + Expected Record = getInstrProfRecord(FuncName, FuncHash); + if (Error E = Record.takeError()) + return error(std::move(E)); Counts = Record.get().Counts; return success(); } -std::error_code IndexedInstrProfReader::readNextRecord( - InstrProfRecord &Record) { +Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) { static unsigned RecordIndex = 0; ArrayRef Data; - std::error_code EC = Index->getRecords(Data); - if (EC != instrprof_error::success) - return error(EC); + Error E = Index->getRecords(Data); + if (E) + return error(std::move(E)); Record = Data[RecordIndex++]; if (RecordIndex >= Data.size()) { Index: lib/ProfileData/InstrProfWriter.cpp =================================================================== --- lib/ProfileData/InstrProfWriter.cpp +++ lib/ProfileData/InstrProfWriter.cpp @@ -156,8 +156,7 @@ this->Sparse = Sparse; } -std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I, - uint64_t Weight) { +Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) { auto &ProfileDataMap = FunctionData[I.Name]; bool NewFunc; @@ -166,22 +165,22 @@ ProfileDataMap.insert(std::make_pair(I.Hash, InstrProfRecord())); InstrProfRecord &Dest = Where->second; - instrprof_error Result = instrprof_error::success; + auto Err = instrprof_error::success; if (NewFunc) { // We've never seen a function with this name and hash, add it. Dest = std::move(I); // Fix up the name to avoid dangling reference. Dest.Name = FunctionData.find(Dest.Name)->getKey(); if (Weight > 1) - Result = Dest.scale(Weight); + Err = InstrProfError::take(Dest.scale(Weight)); } else { // We're updating a function we've seen before. - Result = Dest.merge(I, Weight); + Err = InstrProfError::take(Dest.merge(I, Weight)); } Dest.sortValueData(); - return Result; + return InstrProfError::create(Err); } bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) { Index: lib/Transforms/Instrumentation/InstrProfiling.cpp =================================================================== --- lib/Transforms/Instrumentation/InstrProfiling.cpp +++ lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -374,9 +374,9 @@ return; std::string CompressedNameStr; - if (auto EC = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, + if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, DoNameCompression)) { - llvm::report_fatal_error(EC.message(), false); + llvm::report_fatal_error(toString(std::move(E)), false); } auto &Ctx = M->getContext(); Index: lib/Transforms/Instrumentation/PGOInstrumentation.cpp =================================================================== --- lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -569,18 +569,21 @@ // Return true if the profile are successfully read, and false on errors. bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader) { auto &Ctx = M->getContext(); - ErrorOr Result = + Expected Result = PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash); - if (std::error_code EC = Result.getError()) { - if (EC == instrprof_error::unknown_function) - NumOfPGOMissing++; - else if (EC == instrprof_error::hash_mismatch || - EC == llvm::instrprof_error::malformed) - NumOfPGOMismatch++; - - std::string Msg = EC.message() + std::string(" ") + F.getName().str(); - Ctx.diagnose( - DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning)); + if (Error E = Result.takeError()) { + handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { + auto Err = IPE.get(); + if (Err == instrprof_error::unknown_function) + NumOfPGOMissing++; + else if (Err == instrprof_error::hash_mismatch || + Err == llvm::instrprof_error::malformed) + NumOfPGOMismatch++; + + std::string Msg = IPE.message() + std::string(" ") + F.getName().str(); + Ctx.diagnose( + DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning)); + }); return false; } ProfileRecord = std::move(Result.get()); @@ -818,9 +821,11 @@ auto &Ctx = M.getContext(); // Read the counter array from file. auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName); - if (std::error_code EC = ReaderOrErr.getError()) { - Ctx.diagnose( - DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message())); + if (Error E = ReaderOrErr.takeError()) { + handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { + Ctx.diagnose( + DiagnosticInfoPGOProfile(ProfileFileName.data(), IPE.message())); + }); return false; } Index: tools/llvm-cov/CodeCoverage.cpp =================================================================== --- tools/llvm-cov/CodeCoverage.cpp +++ tools/llvm-cov/CodeCoverage.cpp @@ -210,10 +210,9 @@ errs() << "warning: profile data may be out of date - object is newer\n"; auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename, CoverageArch); - if (std::error_code EC = CoverageOrErr.getError()) { + if (Error E = CoverageOrErr.takeError()) { colored_ostream(errs(), raw_ostream::RED) - << "error: Failed to load coverage: " << EC.message(); - errs() << "\n"; + << "error: Failed to load coverage: " << toString(std::move(E)) << "\n"; return nullptr; } auto Coverage = std::move(CoverageOrErr.get()); Index: tools/llvm-profdata/llvm-profdata.cpp =================================================================== --- tools/llvm-profdata/llvm-profdata.cpp +++ tools/llvm-profdata/llvm-profdata.cpp @@ -47,38 +47,49 @@ ::exit(1); } -static void exitWithErrorCode(const std::error_code &Error, - StringRef Whence = "") { - if (Error.category() == instrprof_category()) { - instrprof_error instrError = static_cast(Error.value()); - if (instrError == instrprof_error::unrecognized_format) { - // Hint for common error of forgetting -sample for sample profiles. - exitWithError(Error.message(), Whence, - "Perhaps you forgot to use the -sample option?"); - } +static void exitWithError(Error E, StringRef Whence = "") { + if (E.isA()) { + handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { + instrprof_error instrError = IPE.get(); + if (instrError == instrprof_error::unrecognized_format) { + // Hint for common error of forgetting -sample for sample profiles. + exitWithError(IPE.message(), Whence, + "Perhaps you forgot to use the -sample option?"); + } + }); } - exitWithError(Error.message(), Whence); + + exitWithError(toString(std::move(E)), Whence); +} + +static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { + exitWithError(EC.message(), Whence); } namespace { enum ProfileKinds { instr, sample }; } -static void handleMergeWriterError(std::error_code &Error, - StringRef WhenceFile = "", +static void handleMergeWriterError(Error E, StringRef WhenceFile = "", StringRef WhenceFunction = "", bool ShowHint = true) { if (!WhenceFile.empty()) errs() << WhenceFile << ": "; if (!WhenceFunction.empty()) errs() << WhenceFunction << ": "; - errs() << Error.message() << "\n"; + + auto IPE = instrprof_error::success; + E = handleErrors(std::move(E), + [&IPE](std::unique_ptr E) -> Error { + IPE = E->get(); + return Error(std::move(E)); + }); + errs() << toString(std::move(E)) << "\n"; if (ShowHint) { StringRef Hint = ""; - if (Error.category() == instrprof_category()) { - instrprof_error instrError = static_cast(Error.value()); - switch (instrError) { + if (IPE != instrprof_error::success) { + switch (IPE) { case instrprof_error::hash_mismatch: case instrprof_error::count_mismatch: case instrprof_error::value_site_count_mismatch: @@ -120,11 +131,11 @@ exitWithErrorCode(EC, OutputFilename); InstrProfWriter Writer(OutputSparse); - SmallSet WriterErrorCodes; + SmallSet WriterErrorCodes; for (const auto &Input : Inputs) { auto ReaderOrErr = InstrProfReader::create(Input.Filename); - if (std::error_code ec = ReaderOrErr.getError()) - exitWithErrorCode(ec, Input.Filename); + if (Error E = ReaderOrErr.takeError()) + exitWithError(std::move(E), Input.Filename); auto Reader = std::move(ReaderOrErr.get()); bool IsIRProfile = Reader->isIRLevelProfile(); @@ -132,14 +143,16 @@ exitWithError("Merge IR generated profile with Clang generated profile."); for (auto &I : *Reader) { - if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) { + if (Error E = Writer.addRecord(std::move(I), Input.Weight)) { // Only show hint the first time an error occurs. - bool firstTime = WriterErrorCodes.insert(EC).second; - handleMergeWriterError(EC, Input.Filename, I.Name, firstTime); + instrprof_error IPE = InstrProfError::take(std::move(E)); + bool firstTime = WriterErrorCodes.insert(IPE).second; + handleMergeWriterError(InstrProfError::create(IPE), Input.Filename, + I.Name, firstTime); } } if (Reader->hasError()) - exitWithErrorCode(Reader->getError(), Input.Filename); + exitWithError(Reader->getError(), Input.Filename); } if (OutputFormat == PF_Text) Writer.writeText(Output); @@ -187,7 +200,7 @@ sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight); if (Result != sampleprof_error::success) { std::error_code EC = make_error_code(Result); - handleMergeWriterError(EC, Input.Filename, FName); + handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName); } } } @@ -268,8 +281,8 @@ Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990}; } InstrProfSummary PS(Cutoffs); - if (std::error_code EC = ReaderOrErr.getError()) - exitWithErrorCode(EC, Filename); + if (Error E = ReaderOrErr.takeError()) + exitWithError(std::move(E), Filename); auto Reader = std::move(ReaderOrErr.get()); bool IsIRInstr = Reader->isIRLevelProfile(); @@ -335,7 +348,7 @@ } if (Reader->hasError()) - exitWithErrorCode(Reader->getError(), Filename); + exitWithError(Reader->getError(), Filename); if (ShowCounts && TextFormat) return 0; Index: unittests/ProfileData/CoverageMappingTest.cpp =================================================================== --- unittests/ProfileData/CoverageMappingTest.cpp +++ unittests/ProfileData/CoverageMappingTest.cpp @@ -20,11 +20,11 @@ using namespace llvm; using namespace coverage; -static ::testing::AssertionResult NoError(std::error_code EC) { - if (!EC) +static ::testing::AssertionResult NoError(Error E) { + if (!E) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << EC.value() - << ": " << EC.message(); + return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) + << "\n"; } namespace llvm { @@ -70,14 +70,14 @@ CoverageMappingReaderMock(ArrayRef Functions) : Functions(Functions) {} - std::error_code readNextRecord(CoverageMappingRecord &Record) override { + Error readNextRecord(CoverageMappingRecord &Record) override { if (Functions.empty()) - return instrprof_error::eof; + return InstrProfError::create(instrprof_error::eof); Functions.front().fillCoverageMappingRecord(Record); Functions = Functions.slice(1); - return instrprof_error::success; + return CoverageMapError::create(); } }; @@ -190,7 +190,7 @@ void readProfCounts() { auto Profile = ProfileWriter.writeBuffer(); auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.getError())); + ASSERT_TRUE(NoError(ReaderOrErr.takeError())); ProfileReader = std::move(ReaderOrErr.get()); } @@ -200,7 +200,7 @@ CoverageMappingReaderMock CovReader(OutputFunctions); auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader); - ASSERT_TRUE(NoError(CoverageOrErr.getError())); + ASSERT_TRUE(NoError(CoverageOrErr.takeError())); LoadedCoverage = std::move(CoverageOrErr.get()); } }; Index: unittests/ProfileData/InstrProfTest.cpp =================================================================== --- unittests/ProfileData/InstrProfTest.cpp +++ unittests/ProfileData/InstrProfTest.cpp @@ -19,19 +19,19 @@ using namespace llvm; -static ::testing::AssertionResult NoError(std::error_code EC) { - if (!EC) +static ::testing::AssertionResult NoError(Error E) { + if (!E) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << EC.value() - << ": " << EC.message(); + return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) + << "\n"; } -static ::testing::AssertionResult ErrorEquals(std::error_code Expected, - std::error_code Found) { +static ::testing::AssertionResult ErrorEquals(instrprof_error Expected, + instrprof_error Found) { if (Expected == Found) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error " << Found.value() - << ": " << Found.message(); + return ::testing::AssertionFailure() + << "error: " << toString(InstrProfError::create(Found)) << "\n"; } namespace { @@ -44,7 +44,7 @@ void readProfile(std::unique_ptr Profile) { auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.getError())); + ASSERT_TRUE(NoError(ReaderOrErr.takeError())); Reader = std::move(ReaderOrErr.get()); } }; @@ -90,23 +90,25 @@ auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("foo", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(2U, R->Counts.size()); ASSERT_EQ(1U, R->Counts[0]); ASSERT_EQ(2U, R->Counts[1]); R = Reader->getInstrProfRecord("foo", 0x1235); - ASSERT_TRUE(NoError(R.getError())); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(2U, R->Counts.size()); ASSERT_EQ(3U, R->Counts[0]); ASSERT_EQ(4U, R->Counts[1]); R = Reader->getInstrProfRecord("foo", 0x5678); - ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, + InstrProfError::take(R.takeError()))); R = Reader->getInstrProfRecord("bar", 0x1234); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, + InstrProfError::take(R.takeError()))); } TEST_P(MaybeSparseInstrProfTest, get_function_counts) { @@ -128,12 +130,13 @@ ASSERT_EQ(3U, Counts[0]); ASSERT_EQ(4U, Counts[1]); - std::error_code EC; - EC = Reader->getFunctionCounts("foo", 0x5678, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); + Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, + InstrProfError::take(std::move(E1)))); - EC = Reader->getFunctionCounts("bar", 0x1234, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); + Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, + InstrProfError::take(std::move(E2)))); } // Profile data is copied from general.proftext @@ -235,8 +238,8 @@ auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -266,8 +269,8 @@ NoError(Writer.addRecord(std::move(Record))); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); LLVMContext Ctx; std::unique_ptr M(new Module("MyModule", Ctx)); @@ -378,8 +381,8 @@ auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -431,8 +434,8 @@ // Set big endian input. Reader->setValueProfDataEndianness(support::big); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -513,8 +516,8 @@ auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -566,23 +569,27 @@ InstrProfRecord Record1("foo", 0x1234, {1}); auto Result1 = Writer.addRecord(std::move(Record1)); - ASSERT_EQ(Result1, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result1)), + instrprof_error::success); // Verify counter overflow. InstrProfRecord Record2("foo", 0x1234, {Max}); auto Result2 = Writer.addRecord(std::move(Record2)); - ASSERT_EQ(Result2, instrprof_error::counter_overflow); + ASSERT_EQ(InstrProfError::take(std::move(Result2)), + instrprof_error::counter_overflow); InstrProfRecord Record3(bar, 0x9012, {8}); auto Result3 = Writer.addRecord(std::move(Record3)); - ASSERT_EQ(Result3, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result3)), + instrprof_error::success); InstrProfRecord Record4("baz", 0x5678, {3, 4}); Record4.reserveSites(IPVK_IndirectCallTarget, 1); InstrProfValueData VD4[] = {{uint64_t(bar), 1}}; Record4.addValueData(IPVK_IndirectCallTarget, 0, VD4, 1, nullptr); auto Result4 = Writer.addRecord(std::move(Record4)); - ASSERT_EQ(Result4, instrprof_error::success); + ASSERT_EQ(InstrProfError::take(std::move(Result4)), + instrprof_error::success); // Verify value data counter overflow. InstrProfRecord Record5("baz", 0x5678, {5, 6}); @@ -590,19 +597,21 @@ InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); auto Result5 = Writer.addRecord(std::move(Record5)); - ASSERT_EQ(Result5, instrprof_error::counter_overflow); + ASSERT_EQ(InstrProfError::take(std::move(Result5)), + instrprof_error::counter_overflow); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); // Verify saturation of counts. - ErrorOr ReadRecord1 = + Expected ReadRecord1 = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(ReadRecord1.getError())); + ASSERT_TRUE(NoError(ReadRecord1.takeError())); ASSERT_EQ(Max, ReadRecord1->Counts[0]); - ErrorOr ReadRecord2 = + Expected ReadRecord2 = Reader->getInstrProfRecord("baz", 0x5678); + ASSERT_TRUE(bool(ReadRecord2)); ASSERT_EQ(1U, ReadRecord2->getNumValueSites(IPVK_IndirectCallTarget)); std::unique_ptr VD = ReadRecord2->getValueForSite(IPVK_IndirectCallTarget, 0); @@ -647,8 +656,8 @@ auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.getError())); + Expected R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.takeError())); std::unique_ptr VD( R->getValueForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget));