From 8007bdd41fa4e8095a170ba9d7cc9bbcc7b50a4f Mon Sep 17 00:00:00 2001 From: Vedant Kumar Date: Fri, 13 May 2016 20:09:39 +0000 Subject: [PATCH] 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 --- .../llvm/ProfileData/Coverage/CoverageMapping.h | 33 ++- .../ProfileData/Coverage/CoverageMappingReader.h | 24 +-- include/llvm/ProfileData/InstrProf.h | 68 +++--- include/llvm/ProfileData/InstrProfData.inc | 7 +- include/llvm/ProfileData/InstrProfReader.h | 82 ++++---- include/llvm/ProfileData/InstrProfWriter.h | 12 +- include/llvm/ProfileData/ProfileCommon.h | 24 --- lib/ProfileData/Coverage/CoverageMapping.cpp | 86 ++++---- lib/ProfileData/Coverage/CoverageMappingReader.cpp | 227 ++++++++++----------- lib/ProfileData/InstrProf.cpp | 133 ++++++------ lib/ProfileData/InstrProfReader.cpp | 176 ++++++++-------- lib/ProfileData/InstrProfWriter.cpp | 5 +- lib/Transforms/Instrumentation/InstrProfiling.cpp | 4 +- .../Instrumentation/PGOInstrumentation.cpp | 43 ++-- tools/llvm-cov/CodeCoverage.cpp | 5 +- tools/llvm-profdata/llvm-profdata.cpp | 67 +++--- unittests/ProfileData/CoverageMappingTest.cpp | 18 +- unittests/ProfileData/InstrProfTest.cpp | 88 ++++---- 18 files changed, 502 insertions(+), 600 deletions(-) diff --git a/include/llvm/ProfileData/Coverage/CoverageMapping.h b/include/llvm/ProfileData/Coverage/CoverageMapping.h index 0f3bcd112cf..56be7ac0f5d 100644 --- a/include/llvm/ProfileData/Coverage/CoverageMapping.h +++ b/include/llvm/ProfileData/Coverage/CoverageMapping.h @@ -23,13 +23,13 @@ #include "llvm/ProfileData/InstrProf.h" #include "llvm/Support/Debug.h" #include "llvm/Support/Endian.h" +#include "llvm/Support/ErrorOr.h" #include "llvm/Support/raw_ostream.h" #include #include namespace llvm { namespace coverage { - enum class coveragemap_error { success = 0, eof, @@ -38,16 +38,13 @@ enum class coveragemap_error { truncated, malformed }; +} // end of coverage namespace. +} -class CoverageMapError : public ProfErrorInfoBase { -public: - CoverageMapError(coveragemap_error Err) - : ProfErrorInfoBase(Err) {} - - std::string message() const override; +namespace std { +template <> +struct is_error_code_enum : std::true_type { }; - -} // end of coverage namespace. } namespace llvm { @@ -268,7 +265,7 @@ public: /// \brief Return the number of times that a region of code associated with /// this counter was executed. - Expected evaluate(const Counter &C) const; + ErrorOr evaluate(const Counter &C) const; }; /// \brief Code coverage information for a single function. @@ -418,12 +415,12 @@ class CoverageMapping { public: /// \brief Load the coverage mapping using the given readers. - static Expected> + static ErrorOr> load(CoverageMappingReader &CoverageReader, IndexedInstrProfReader &ProfileReader); /// \brief Load the coverage mapping from the given files. - static Expected> + static ErrorOr> load(StringRef ObjectFilename, StringRef ProfileFilename, StringRef Arch = StringRef()); @@ -504,13 +501,14 @@ template struct CovMapFunctionRecordV1 { } // Return the PGO name of the function */ template - Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const { + std::error_code 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 make_error(coveragemap_error::malformed); - return Error::success(); + return coveragemap_error::malformed; + return std::error_code(); } }; @@ -532,10 +530,11 @@ struct CovMapFunctionRecord { } // Return the PGO name of the function */ template - Error getFuncName(InstrProfSymtab &ProfileNames, StringRef &FuncName) const { + std::error_code getFuncName(InstrProfSymtab &ProfileNames, + StringRef &FuncName) const { uint64_t NameRef = getFuncNameRef(); FuncName = ProfileNames.getFuncName(NameRef); - return Error::success(); + return std::error_code(); } }; diff --git a/include/llvm/ProfileData/Coverage/CoverageMappingReader.h b/include/llvm/ProfileData/Coverage/CoverageMappingReader.h index ce743a96d1d..709006fb113 100644 --- a/include/llvm/ProfileData/Coverage/CoverageMappingReader.h +++ b/include/llvm/ProfileData/Coverage/CoverageMappingReader.h @@ -69,7 +69,7 @@ public: class CoverageMappingReader { public: - virtual Error readNextRecord(CoverageMappingRecord &Record) = 0; + virtual std::error_code readNextRecord(CoverageMappingRecord &Record) = 0; CoverageMappingIterator begin() { return CoverageMappingIterator(this); } CoverageMappingIterator end() { return CoverageMappingIterator(); } virtual ~CoverageMappingReader() {} @@ -82,10 +82,10 @@ protected: RawCoverageReader(StringRef Data) : Data(Data) {} - Error readULEB128(uint64_t &Result); - Error readIntMax(uint64_t &Result, uint64_t MaxPlus1); - Error readSize(uint64_t &Result); - Error readString(StringRef &Result); + 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); }; /// \brief Reader for the raw coverage filenames. @@ -100,7 +100,7 @@ public: RawCoverageFilenamesReader(StringRef Data, std::vector &Filenames) : RawCoverageReader(Data), Filenames(Filenames) {} - Error read(); + std::error_code read(); }; /// \brief Reader for the raw coverage mapping data. @@ -125,12 +125,12 @@ public: Filenames(Filenames), Expressions(Expressions), MappingRegions(MappingRegions) {} - Error read(); + std::error_code read(); private: - Error decodeCounter(unsigned Value, Counter &C); - Error readCounter(Counter &C); - Error + std::error_code decodeCounter(unsigned Value, Counter &C); + std::error_code readCounter(Counter &C); + std::error_code readMappingRegionsSubArray(std::vector &MappingRegions, unsigned InferredFileID, size_t NumFileIDs); }; @@ -170,11 +170,11 @@ private: BinaryCoverageReader() : CurrentRecord(0) {} public: - static Expected> + static ErrorOr> create(std::unique_ptr &ObjectBuffer, StringRef Arch); - Error readNextRecord(CoverageMappingRecord &Record) override; + std::error_code readNextRecord(CoverageMappingRecord &Record) override; }; } // end namespace coverage diff --git a/include/llvm/ProfileData/InstrProf.h b/include/llvm/ProfileData/InstrProf.h index 27d441f48e9..1d065ee13f9 100644 --- a/include/llvm/ProfileData/InstrProf.h +++ b/include/llvm/ProfileData/InstrProf.h @@ -25,6 +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/MD5.h" #include "llvm/Support/MathExtras.h" #include @@ -203,17 +204,20 @@ StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, /// 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. -Error collectPGOFuncNameStrings(const std::vector &NameStrs, - bool doCompression, std::string &Result); +std::error_code +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. -Error collectPGOFuncNameStrings(const std::vector &NameVars, - std::string &Result, bool doCompression = true); +std::error_code +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. -Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab); +std::error_code readPGOFuncNameStrings(StringRef NameStrings, + InstrProfSymtab &Symtab); enum InstrProfValueKind : uint32_t { #define VALUE_PROF_KIND(Enumerator, Value) Enumerator = Value, @@ -280,25 +284,6 @@ inline std::error_code make_error_code(instrprof_error E) { return std::error_code(static_cast(E), instrprof_category()); } -class InstrProfError : public ProfErrorInfoBase { -public: - InstrProfError(instrprof_error Err) - : ProfErrorInfoBase(Err) {} - - std::string message() const override; - - /// Consume an Error and return the raw enum value contained within it. The - /// Error must either be a success value, or contain a single InstrProfError. - 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; - } -}; - class SoftInstrProfErrors { /// Count the number of soft instrprof_errors encountered and keep track of /// the first such error for reporting purposes. @@ -324,11 +309,6 @@ public: NumCountMismatches(0), NumCounterOverflows(0), NumValueSiteCountMismatches(0) {} - ~SoftInstrProfErrors() { - assert(FirstError == instrprof_error::success && - "Unchecked soft error encountered"); - } - /// Track a soft error (\p IE) and increment its associated counter. void addError(instrprof_error IE); @@ -346,15 +326,8 @@ public: return NumValueSiteCountMismatches; } - /// Return the first encountered error and reset FirstError to a success - /// value. - Error takeError() { - if (FirstError == instrprof_error::success) - return Error::success(); - auto E = make_error(FirstError); - FirstError = instrprof_error::success; - return E; - } + /// Return an error code for the first encountered error. + std::error_code getError() const { return make_error_code(FirstError); } }; namespace object { @@ -399,14 +372,14 @@ public: /// 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. - Error create(object::SectionRef &Section); + std::error_code create(object::SectionRef &Section); /// This interface is used by reader of CoverageMapping test /// format. - inline Error create(StringRef D, uint64_t BaseAddr); + inline std::error_code 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 Error create(StringRef NameStrings); + inline std::error_code 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 @@ -451,13 +424,13 @@ public: inline StringRef getNameData() const { return Data; } }; -Error InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) { +std::error_code InstrProfSymtab::create(StringRef D, uint64_t BaseAddr) { Data = D; Address = BaseAddr; - return Error::success(); + return std::error_code(); } -Error InstrProfSymtab::create(StringRef NameStrings) { +std::error_code InstrProfSymtab::create(StringRef NameStrings) { return readPGOFuncNameStrings(NameStrings, *this); } @@ -599,7 +572,7 @@ struct InstrProfRecord { } /// Get the error contained within the record's soft error counter. - Error takeError() { return SIPE.takeError(); } + std::error_code getError() const { return SIPE.getError(); } private: std::vector IndirectCallSites; @@ -917,4 +890,9 @@ struct Header { } // end namespace llvm +namespace std { +template <> +struct is_error_code_enum : std::true_type {}; +} + #endif // LLVM_PROFILEDATA_INSTRPROF_H diff --git a/include/llvm/ProfileData/InstrProfData.inc b/include/llvm/ProfileData/InstrProfData.inc index 7358efc18d6..77d44f15bed 100644 --- a/include/llvm/ProfileData/InstrProfData.inc +++ b/include/llvm/ProfileData/InstrProfData.inc @@ -295,15 +295,16 @@ typedef struct ValueProfData { static std::unique_ptr serializeFrom(const InstrProfRecord &Record); /*! - * Check the integrity of the record. + * Check the integrity of the record. Return the error code when + * an error is detected, otherwise return instrprof_error::success. */ - Error checkIntegrity(); + instrprof_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 Expected> + static ErrorOr> getValueProfData(const unsigned char *SrcBuffer, const unsigned char *const SrcBufferEnd, support::endianness SrcDataEndianness); diff --git a/include/llvm/ProfileData/InstrProfReader.h b/include/llvm/ProfileData/InstrProfReader.h index 4332be30f1d..a5d06264536 100644 --- a/include/llvm/ProfileData/InstrProfReader.h +++ b/include/llvm/ProfileData/InstrProfReader.h @@ -19,6 +19,7 @@ #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" @@ -50,16 +51,16 @@ public: /// Base class and interface for reading profiling data of any known instrprof /// format. Provides an iterator over InstrProfRecords. class InstrProfReader { - instrprof_error LastError; + std::error_code LastError; public: InstrProfReader() : LastError(instrprof_error::success), Symtab() {} virtual ~InstrProfReader() {} /// Read the header. Required before reading first record. - virtual Error readHeader() = 0; + virtual std::error_code readHeader() = 0; /// Read a single record. - virtual Error readNextRecord(InstrProfRecord &Record) = 0; + virtual std::error_code readNextRecord(InstrProfRecord &Record) = 0; /// Iterator over profile data. InstrProfIterator begin() { return InstrProfIterator(this); } InstrProfIterator end() { return InstrProfIterator(); } @@ -79,31 +80,28 @@ public: protected: std::unique_ptr Symtab; - /// Set the current error and return same. - Error error(instrprof_error Err) { - LastError = Err; - if (Err == instrprof_error::success) - return Error::success(); - return make_error(Err); + /// Set the current std::error_code and return same. + std::error_code error(std::error_code EC) { + LastError = EC; + return EC; } - Error error(Error E) { return error(InstrProfError::take(std::move(E))); } - /// Clear the current error and return a successful one. - Error success() { return error(instrprof_error::success); } + /// Clear the current error code and return a successful one. + std::error_code 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 != instrprof_error::success && !isEOF(); } - /// Get the current error. - Error getError() { return make_error(LastError); } + bool hasError() { return LastError && !isEOF(); } + /// Get the current error code. + std::error_code getError() { return LastError; } /// Factory method to create an appropriately typed reader for the given /// instrprof file. - static Expected> create(std::string Path); + static ErrorOr> create(std::string Path); - static Expected> + static ErrorOr> create(std::unique_ptr Buffer); }; @@ -125,7 +123,7 @@ private: TextInstrProfReader(const TextInstrProfReader &) = delete; TextInstrProfReader &operator=(const TextInstrProfReader &) = delete; - Error readValueProfileData(InstrProfRecord &Record); + std::error_code readValueProfileData(InstrProfRecord &Record); public: TextInstrProfReader(std::unique_ptr DataBuffer_) @@ -138,9 +136,9 @@ public: bool isIRLevelProfile() const override { return IsIRLevelProfile; } /// Read the header. - Error readHeader() override; + std::error_code readHeader() override; /// Read a single record. - Error readNextRecord(InstrProfRecord &Record) override; + std::error_code readNextRecord(InstrProfRecord &Record) override; InstrProfSymtab &getSymtab() override { assert(Symtab.get()); @@ -187,8 +185,8 @@ public: : DataBuffer(std::move(DataBuffer)) { } static bool hasFormat(const MemoryBuffer &DataBuffer); - Error readHeader() override; - Error readNextRecord(InstrProfRecord &Record) override; + std::error_code readHeader() override; + std::error_code readNextRecord(InstrProfRecord &Record) override; bool isIRLevelProfile() const override { return (Version & VARIANT_MASK_IR_PROF) != 0; } @@ -199,9 +197,9 @@ public: } private: - Error createSymtab(InstrProfSymtab &Symtab); - Error readNextHeader(const char *CurrentPos); - Error readHeader(const RawInstrProf::Header &Header); + std::error_code createSymtab(InstrProfSymtab &Symtab); + std::error_code readNextHeader(const char *CurrentPos); + std::error_code readHeader(const RawInstrProf::Header &Header); template IntT swap(IntT Int) const { return ShouldSwapBytes ? sys::getSwappedBytes(Int) : Int; } @@ -218,10 +216,10 @@ private: inline uint8_t getNumPaddingBytes(uint64_t SizeInBytes) { return 7 & (sizeof(uint64_t) - SizeInBytes % sizeof(uint64_t)); } - Error readName(InstrProfRecord &Record); - Error readFuncHash(InstrProfRecord &Record); - Error readRawCounts(InstrProfRecord &Record); - Error readValueProfilingData(InstrProfRecord &Record); + std::error_code readName(InstrProfRecord &Record); + std::error_code readFuncHash(InstrProfRecord &Record); + std::error_code readRawCounts(InstrProfRecord &Record); + std::error_code readValueProfilingData(InstrProfRecord &Record); bool atEnd() const { return Data == DataEnd; } void advanceData() { Data++; @@ -302,9 +300,9 @@ public: struct InstrProfReaderIndexBase { // Read all the profile records with the same key pointed to the current // iterator. - virtual Error getRecords(ArrayRef &Data) = 0; + virtual std::error_code getRecords(ArrayRef &Data) = 0; // Read all the profile records with the key equal to FuncName - virtual Error getRecords(StringRef FuncName, + virtual std::error_code getRecords(StringRef FuncName, ArrayRef &Data) = 0; virtual void advanceToNextKey() = 0; virtual bool atEnd() const = 0; @@ -332,9 +330,9 @@ public: const unsigned char *const Base, IndexedInstrProf::HashT HashType, uint64_t Version); - Error getRecords(ArrayRef &Data) override; - Error getRecords(StringRef FuncName, - ArrayRef &Data) override; + std::error_code getRecords(ArrayRef &Data) override; + std::error_code getRecords(StringRef FuncName, + ArrayRef &Data) override; void advanceToNextKey() override { RecordIterator++; } bool atEnd() const override { return RecordIterator == HashTable->data_end(); @@ -381,27 +379,27 @@ public: static bool hasFormat(const MemoryBuffer &DataBuffer); /// Read the file header. - Error readHeader() override; + std::error_code readHeader() override; /// Read a single record. - Error readNextRecord(InstrProfRecord &Record) override; + std::error_code readNextRecord(InstrProfRecord &Record) override; /// Return the pointer to InstrProfRecord associated with FuncName /// and FuncHash - Expected getInstrProfRecord(StringRef FuncName, - uint64_t FuncHash); + ErrorOr getInstrProfRecord(StringRef FuncName, + uint64_t FuncHash); /// Fill Counts with the profile data for the given function name. - Error getFunctionCounts(StringRef FuncName, uint64_t FuncHash, - std::vector &Counts); + std::error_code 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 Expected> + static ErrorOr> create(std::string Path); - static Expected> + static ErrorOr> create(std::unique_ptr Buffer); // Used for testing purpose only. diff --git a/include/llvm/ProfileData/InstrProfWriter.h b/include/llvm/ProfileData/InstrProfWriter.h index 7d292731ccc..8b02d3cc9ed 100644 --- a/include/llvm/ProfileData/InstrProfWriter.h +++ b/include/llvm/ProfileData/InstrProfWriter.h @@ -46,7 +46,7 @@ public: /// 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. - Error addRecord(InstrProfRecord &&I, uint64_t Weight = 1); + std::error_code 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,15 +58,13 @@ public: std::unique_ptr writeBuffer(); /// Set the ProfileKind. Report error if mixing FE and IR level profiles. - Error setIsIRLevelProfile(bool IsIRLevel) { + std::error_code setIsIRLevelProfile(bool IsIRLevel) { if (ProfileKind == PF_Unknown) { ProfileKind = IsIRLevel ? PF_IRLevel: PF_FE; - return Error::success(); + return instrprof_error::success; } - return (IsIRLevel == (ProfileKind == PF_IRLevel)) - ? Error::success() - : make_error( - instrprof_error::unsupported_version); + return (IsIRLevel == (ProfileKind == PF_IRLevel)) ? + instrprof_error::success : instrprof_error::unsupported_version; } // Internal interface for testing purpose only. diff --git a/include/llvm/ProfileData/ProfileCommon.h b/include/llvm/ProfileData/ProfileCommon.h index c367b5951c4..ae196aa5d00 100644 --- a/include/llvm/ProfileData/ProfileCommon.h +++ b/include/llvm/ProfileData/ProfileCommon.h @@ -21,7 +21,6 @@ #include #include "llvm/Support/Casting.h" -#include "llvm/Support/Error.h" namespace llvm { class Function; @@ -37,29 +36,6 @@ class Metadata; class MDTuple; class MDNode; -template -class ProfErrorInfoBase : public ErrorInfo> { -public: - ProfErrorInfoBase(ErrT Err) : Err(Err) { - assert(Err != ErrT::success && "Not an error"); - } - - virtual std::string message() const override = 0; - - void log(raw_ostream &OS) const override { OS << message(); } - - std::error_code convertToErrorCode() const override { - return make_error_code(Err); - } - - ErrT get() const { return Err; } - - static char ID; - -protected: - ErrT Err; -}; - inline const char *getHotSectionPrefix() { return ".hot"; } inline const char *getUnlikelySectionPrefix() { return ".unlikely"; } diff --git a/lib/ProfileData/Coverage/CoverageMapping.cpp b/lib/ProfileData/Coverage/CoverageMapping.cpp index 7133697bb87..9fdd37081c9 100644 --- a/lib/ProfileData/Coverage/CoverageMapping.cpp +++ b/lib/ProfileData/Coverage/CoverageMapping.cpp @@ -143,30 +143,28 @@ void CounterMappingContext::dump(const Counter &C, } if (CounterValues.empty()) return; - Expected Value = evaluate(C); - if (auto E = Value.takeError()) { - llvm::consumeError(std::move(E)); + ErrorOr Value = evaluate(C); + if (!Value) return; - } OS << '[' << *Value << ']'; } -Expected CounterMappingContext::evaluate(const Counter &C) const { +ErrorOr CounterMappingContext::evaluate(const Counter &C) const { switch (C.getKind()) { case Counter::Zero: return 0; case Counter::CounterValueReference: if (C.getCounterID() >= CounterValues.size()) - return errorCodeToError(errc::argument_out_of_domain); + return make_error_code(errc::argument_out_of_domain); return CounterValues[C.getCounterID()]; case Counter::Expression: { if (C.getExpressionID() >= Expressions.size()) - return errorCodeToError(errc::argument_out_of_domain); + return make_error_code(errc::argument_out_of_domain); const auto &E = Expressions[C.getExpressionID()]; - Expected LHS = evaluate(E.LHS); + ErrorOr LHS = evaluate(E.LHS); if (!LHS) return LHS; - Expected RHS = evaluate(E.RHS); + ErrorOr RHS = evaluate(E.RHS); if (!RHS) return RHS; return E.Kind == CounterExpression::Subtract ? *LHS - *RHS : *LHS + *RHS; @@ -183,7 +181,7 @@ void FunctionRecordIterator::skipOtherFiles() { *this = FunctionRecordIterator(); } -Expected> +ErrorOr> CoverageMapping::load(CoverageMappingReader &CoverageReader, IndexedInstrProfReader &ProfileReader) { auto Coverage = std::unique_ptr(new CoverageMapping()); @@ -193,14 +191,13 @@ CoverageMapping::load(CoverageMappingReader &CoverageReader, CounterMappingContext Ctx(Record.Expressions); Counts.clear(); - if (Error E = ProfileReader.getFunctionCounts( + if (std::error_code EC = ProfileReader.getFunctionCounts( Record.FunctionName, Record.FunctionHash, Counts)) { - instrprof_error IPE = InstrProfError::take(std::move(E)); - if (IPE == instrprof_error::hash_mismatch) { + if (EC == instrprof_error::hash_mismatch) { Coverage->MismatchedFunctionCount++; continue; - } else if (IPE != instrprof_error::unknown_function) - return make_error(IPE); + } else if (EC != instrprof_error::unknown_function) + return EC; Counts.assign(Record.MappingRegions.size(), 0); } Ctx.setCounts(Counts); @@ -215,11 +212,9 @@ CoverageMapping::load(CoverageMappingReader &CoverageReader, getFuncNameWithoutPrefix(OrigFuncName, Record.Filenames[0]); FunctionRecord Function(OrigFuncName, Record.Filenames); for (const auto &Region : Record.MappingRegions) { - Expected ExecutionCount = Ctx.evaluate(Region.Count); - if (auto E = ExecutionCount.takeError()) { - llvm::consumeError(std::move(E)); + ErrorOr ExecutionCount = Ctx.evaluate(Region.Count); + if (!ExecutionCount) break; - } Function.pushRegion(Region, *ExecutionCount); } if (Function.CountedRegions.size() != Record.MappingRegions.size()) { @@ -233,20 +228,20 @@ CoverageMapping::load(CoverageMappingReader &CoverageReader, return std::move(Coverage); } -Expected> +ErrorOr> CoverageMapping::load(StringRef ObjectFilename, StringRef ProfileFilename, StringRef Arch) { auto CounterMappingBuff = MemoryBuffer::getFileOrSTDIN(ObjectFilename); if (std::error_code EC = CounterMappingBuff.getError()) - return errorCodeToError(EC); + return EC; auto CoverageReaderOrErr = BinaryCoverageReader::create(CounterMappingBuff.get(), Arch); - if (Error E = CoverageReaderOrErr.takeError()) - return std::move(E); + if (std::error_code EC = CoverageReaderOrErr.getError()) + return EC; auto CoverageReader = std::move(CoverageReaderOrErr.get()); auto ProfileReaderOrErr = IndexedInstrProfReader::create(ProfileFilename); - if (Error E = ProfileReaderOrErr.takeError()) - return std::move(E); + if (auto EC = ProfileReaderOrErr.getError()) + return EC; auto ProfileReader = std::move(ProfileReaderOrErr.get()); return load(*CoverageReader, *ProfileReader); } @@ -538,34 +533,27 @@ CoverageMapping::getCoverageForExpansion(const ExpansionRecord &Expansion) { } namespace { -std::string getCoverageMapErrString(coveragemap_error Err) { - 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"; - } - llvm_unreachable("A value of coveragemap_error has no message."); -} - class CoverageMappingErrorCategoryType : public std::error_category { const char *name() const LLVM_NOEXCEPT override { return "llvm.coveragemap"; } std::string message(int IE) const override { - return getCoverageMapErrString(static_cast(IE)); + 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."); } }; -} // end anonymous namespace - -std::string CoverageMapError::message() const { - return getCoverageMapErrString(Err); } static ManagedStatic ErrorCategory; @@ -573,5 +561,3 @@ static ManagedStatic ErrorCategory; const std::error_category &llvm::coverage::coveragemap_category() { return *ErrorCategory; } - -template <> char ProfErrorInfoBase::ID = 0; diff --git a/lib/ProfileData/Coverage/CoverageMappingReader.cpp b/lib/ProfileData/Coverage/CoverageMappingReader.cpp index 0919a5e63f3..9f27de91bc5 100644 --- a/lib/ProfileData/Coverage/CoverageMappingReader.cpp +++ b/lib/ProfileData/Coverage/CoverageMappingReader.cpp @@ -31,54 +31,49 @@ using namespace object; void CoverageMappingIterator::increment() { // Check if all the records were read or if an error occurred while reading // the next record. - if (auto E = Reader->readNextRecord(Record)) { - handleAllErrors(std::move(E), [&](const CoverageMapError &CME) { - if (CME.get() == coveragemap_error::eof) - *this = CoverageMappingIterator(); - else - llvm_unreachable("Unexpected error in coverage mapping iterator"); - }); - } + if (Reader->readNextRecord(Record)) + *this = CoverageMappingIterator(); } -Error RawCoverageReader::readULEB128(uint64_t &Result) { +std::error_code RawCoverageReader::readULEB128(uint64_t &Result) { if (Data.size() < 1) - return make_error(coveragemap_error::truncated); + return coveragemap_error::truncated; unsigned N = 0; Result = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; Data = Data.substr(N); - return Error::success(); + return std::error_code(); } -Error RawCoverageReader::readIntMax(uint64_t &Result, uint64_t MaxPlus1) { +std::error_code RawCoverageReader::readIntMax(uint64_t &Result, + uint64_t MaxPlus1) { if (auto Err = readULEB128(Result)) return Err; if (Result >= MaxPlus1) - return make_error(coveragemap_error::malformed); - return Error::success(); + return coveragemap_error::malformed; + return std::error_code(); } -Error RawCoverageReader::readSize(uint64_t &Result) { +std::error_code RawCoverageReader::readSize(uint64_t &Result) { if (auto Err = readULEB128(Result)) return Err; // Sanity check the number. if (Result > Data.size()) - return make_error(coveragemap_error::malformed); - return Error::success(); + return coveragemap_error::malformed; + return std::error_code(); } -Error RawCoverageReader::readString(StringRef &Result) { +std::error_code RawCoverageReader::readString(StringRef &Result) { uint64_t Length; if (auto Err = readSize(Length)) return Err; Result = Data.substr(0, Length); Data = Data.substr(Length); - return Error::success(); + return std::error_code(); } -Error RawCoverageFilenamesReader::read() { +std::error_code RawCoverageFilenamesReader::read() { uint64_t NumFilenames; if (auto Err = readSize(NumFilenames)) return Err; @@ -88,18 +83,19 @@ Error RawCoverageFilenamesReader::read() { return Err; Filenames.push_back(Filename); } - return Error::success(); + return std::error_code(); } -Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) { +std::error_code RawCoverageMappingReader::decodeCounter(unsigned Value, + Counter &C) { auto Tag = Value & Counter::EncodingTagMask; switch (Tag) { case Counter::Zero: C = Counter::getZero(); - return Error::success(); + return std::error_code(); case Counter::CounterValueReference: C = Counter::getCounter(Value >> Counter::EncodingTagBits); - return Error::success(); + return std::error_code(); default: break; } @@ -109,25 +105,25 @@ Error RawCoverageMappingReader::decodeCounter(unsigned Value, Counter &C) { case CounterExpression::Add: { auto ID = Value >> Counter::EncodingTagBits; if (ID >= Expressions.size()) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; Expressions[ID].Kind = CounterExpression::ExprKind(Tag); C = Counter::getExpression(ID); break; } default: - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; } - return Error::success(); + return std::error_code(); } -Error RawCoverageMappingReader::readCounter(Counter &C) { +std::error_code 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 Error::success(); + return std::error_code(); } static const unsigned EncodingExpansionRegionBit = 1 @@ -136,7 +132,7 @@ static const unsigned EncodingExpansionRegionBit = 1 /// \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. -Error RawCoverageMappingReader::readMappingRegionsSubArray( +std::error_code RawCoverageMappingReader::readMappingRegionsSubArray( std::vector &MappingRegions, unsigned InferredFileID, size_t NumFileIDs) { uint64_t NumRegions; @@ -164,7 +160,7 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( ExpandedFileID = EncodedCounterAndRegion >> Counter::EncodingCounterTagAndExpansionRegionTagBits; if (ExpandedFileID >= NumFileIDs) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; } else { switch (EncodedCounterAndRegion >> Counter::EncodingCounterTagAndExpansionRegionTagBits) { @@ -175,7 +171,7 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( Kind = CounterMappingRegion::SkippedRegion; break; default: - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; } } } @@ -188,7 +184,7 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( if (auto Err = readULEB128(ColumnStart)) return Err; if (ColumnStart > std::numeric_limits::max()) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; if (auto Err = readIntMax(NumLines, std::numeric_limits::max())) return Err; if (auto Err = readIntMax(ColumnEnd, std::numeric_limits::max())) @@ -222,10 +218,10 @@ Error RawCoverageMappingReader::readMappingRegionsSubArray( C, InferredFileID, ExpandedFileID, LineStart, ColumnStart, LineStart + NumLines, ColumnEnd, Kind)); } - return Error::success(); + return std::error_code(); } -Error RawCoverageMappingReader::read() { +std::error_code RawCoverageMappingReader::read() { // Read the virtual file mapping. llvm::SmallVector VirtualFileMapping; @@ -291,14 +287,14 @@ Error RawCoverageMappingReader::read() { } } - return Error::success(); + return std::error_code(); } -Error InstrProfSymtab::create(SectionRef &Section) { - if (auto EC = Section.getContents(Data)) - return errorCodeToError(EC); +std::error_code InstrProfSymtab::create(SectionRef &Section) { + if (auto Err = Section.getContents(Data)) + return Err; Address = Section.getAddress(); - return Error::success(); + return std::error_code(); } StringRef InstrProfSymtab::getFuncName(uint64_t Pointer, size_t Size) { @@ -316,10 +312,11 @@ struct CovMapFuncRecordReader { // 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 Error readFunctionRecords(const char *&Buf, const char *End) = 0; + virtual std::error_code readFunctionRecords(const char *&Buf, + const char *End) = 0; virtual ~CovMapFuncRecordReader() {} template - static Expected> + static ErrorOr> get(coverage::CovMapVersion Version, InstrProfSymtab &P, std::vector &R, std::vector &F); @@ -347,10 +344,11 @@ public: : ProfileNames(P), Filenames(F), Records(R) {} ~VersionedCovMapFuncRecordReader() override {} - Error readFunctionRecords(const char *&Buf, const char *End) override { + std::error_code readFunctionRecords(const char *&Buf, + const char *End) override { using namespace support; if (Buf + sizeof(CovMapHeader) > End) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; auto CovHeader = reinterpret_cast(Buf); uint32_t NRecords = CovHeader->getNRecords(); uint32_t FilenamesSize = CovHeader->getFilenamesSize(); @@ -365,7 +363,7 @@ public: // Get the filenames. if (Buf + FilenamesSize > End) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; size_t FilenamesBegin = Filenames.size(); RawCoverageFilenamesReader Reader(StringRef(Buf, FilenamesSize), Filenames); if (auto Err = Reader.read()) @@ -378,7 +376,7 @@ public: const char *CovEnd = Buf; if (Buf > End) - return make_error(coveragemap_error::malformed); + return 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); @@ -391,7 +389,7 @@ public: // Now use that to read the coverage data. if (CovBuf + DataSize > CovEnd) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; auto Mapping = StringRef(CovBuf, DataSize); CovBuf += DataSize; @@ -405,20 +403,21 @@ public: } StringRef FuncName; - if (Error E = CFR->template getFuncName(ProfileNames, FuncName)) - return E; + if (std::error_code EC = + CFR->template getFuncName(ProfileNames, FuncName)) + return EC; Records.push_back(BinaryCoverageReader::ProfileMappingRecord( Version, FuncName, FuncHash, Mapping, FilenamesBegin, Filenames.size() - FilenamesBegin)); CFR++; } - return Error::success(); + return std::error_code(); } }; } // end anonymous namespace template -Expected> CovMapFuncRecordReader::get( +ErrorOr> CovMapFuncRecordReader::get( coverage::CovMapVersion Version, InstrProfSymtab &P, std::vector &R, std::vector &F) { @@ -429,8 +428,8 @@ Expected> CovMapFuncRecordReader::get( CovMapVersion::Version1, IntPtrT, Endian>>(P, R, F); case CovMapVersion::Version2: // Decompress the name data. - if (Error E = P.create(P.getNameData())) - return std::move(E); + if (auto EC = P.create(P.getNameData())) + return EC; return llvm::make_unique>(P, R, F); } @@ -438,7 +437,7 @@ Expected> CovMapFuncRecordReader::get( } template -static Error readCoverageMappingData( +static std::error_code readCoverageMappingData( InstrProfSymtab &ProfileNames, StringRef Data, std::vector &Records, std::vector &Filenames) { @@ -448,89 +447,89 @@ static Error readCoverageMappingData( reinterpret_cast(Data.data()); CovMapVersion Version = (CovMapVersion)CovHeader->getVersion(); if (Version > coverage::CovMapVersion::CurrentVersion) - return make_error(coveragemap_error::unsupported_version); - Expected> ReaderExpected = + return coveragemap_error::unsupported_version; + ErrorOr> ReaderErrorOr = CovMapFuncRecordReader::get(Version, ProfileNames, Records, Filenames); - if (Error E = ReaderExpected.takeError()) - return E; - auto Reader = std::move(ReaderExpected.get()); + if (auto EC = ReaderErrorOr.getError()) + return EC; + auto Reader = std::move(ReaderErrorOr.get()); for (const char *Buf = Data.data(), *End = Buf + Data.size(); Buf < End;) { - if (Error E = Reader->readFunctionRecords(Buf, End)) - return E; + if (std::error_code EC = Reader->readFunctionRecords(Buf, End)) + return EC; } - return Error::success(); + return std::error_code(); } static const char *TestingFormatMagic = "llvmcovmtestdata"; -static Error loadTestingFormat(StringRef Data, InstrProfSymtab &ProfileNames, - StringRef &CoverageMapping, - uint8_t &BytesInAddress, - support::endianness &Endian) { +static std::error_code 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 make_error(coveragemap_error::truncated); + return coveragemap_error::truncated; unsigned N = 0; auto ProfileNamesSize = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; Data = Data.substr(N); if (Data.size() < 1) - return make_error(coveragemap_error::truncated); + return coveragemap_error::truncated; N = 0; uint64_t Address = decodeULEB128(reinterpret_cast(Data.data()), &N); if (N > Data.size()) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; Data = Data.substr(N); if (Data.size() < ProfileNamesSize) - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; ProfileNames.create(Data.substr(0, ProfileNamesSize), Address); CoverageMapping = Data.substr(ProfileNamesSize); - return Error::success(); + return std::error_code(); } -static Expected lookupSection(ObjectFile &OF, StringRef Name) { +static ErrorOr lookupSection(ObjectFile &OF, StringRef Name) { StringRef FoundName; for (const auto &Section : OF.sections()) { if (auto EC = Section.getName(FoundName)) - return errorCodeToError(EC); + return EC; if (FoundName == Name) return Section; } - return make_error(coveragemap_error::no_data_found); + return coveragemap_error::no_data_found; } -static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer, - InstrProfSymtab &ProfileNames, - StringRef &CoverageMapping, - uint8_t &BytesInAddress, - support::endianness &Endian, StringRef Arch) { +static std::error_code +loadBinaryFormat(MemoryBufferRef ObjectBuffer, InstrProfSymtab &ProfileNames, + StringRef &CoverageMapping, uint8_t &BytesInAddress, + support::endianness &Endian, StringRef Arch) { auto BinOrErr = object::createBinary(ObjectBuffer); if (!BinOrErr) - return BinOrErr.takeError(); + return errorToErrorCode(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 (auto EC = ObjectFileOrErr.getError()) - return errorCodeToError(EC); + if (std::error_code EC = ObjectFileOrErr.getError()) + return 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 errorCodeToError(object_error::arch_not_found); + return object_error::arch_not_found; } else // We can only handle object files. - return make_error(coveragemap_error::malformed); + return coveragemap_error::malformed; // The coverage uses native pointer sizes for the object it's written in. BytesInAddress = OF->getBytesInAddress(); @@ -539,23 +538,23 @@ static Error loadBinaryFormat(MemoryBufferRef ObjectBuffer, // Look for the sections that we are interested in. auto NamesSection = lookupSection(*OF, getInstrProfNameSectionName(false)); - if (auto E = NamesSection.takeError()) - return E; + if (auto EC = NamesSection.getError()) + return EC; auto CoverageSection = lookupSection(*OF, getInstrProfCoverageSectionName(false)); - if (auto E = CoverageSection.takeError()) - return E; + if (auto EC = CoverageSection.getError()) + return EC; // Get the contents of the given sections. - if (auto EC = CoverageSection->getContents(CoverageMapping)) - return errorCodeToError(EC); - if (Error E = ProfileNames.create(*NamesSection)) - return E; + if (std::error_code EC = CoverageSection->getContents(CoverageMapping)) + return EC; + if (std::error_code EC = ProfileNames.create(*NamesSection)) + return EC; - return Error::success(); + return std::error_code(); } -Expected> +ErrorOr> BinaryCoverageReader::create(std::unique_ptr &ObjectBuffer, StringRef Arch) { std::unique_ptr Reader(new BinaryCoverageReader()); @@ -563,44 +562,44 @@ BinaryCoverageReader::create(std::unique_ptr &ObjectBuffer, StringRef Coverage; uint8_t BytesInAddress; support::endianness Endian; - Error E; - consumeError(std::move(E)); + std::error_code EC; if (ObjectBuffer->getBuffer().startswith(TestingFormatMagic)) // This is a special format used for testing. - E = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames, - Coverage, BytesInAddress, Endian); + EC = loadTestingFormat(ObjectBuffer->getBuffer(), Reader->ProfileNames, + Coverage, BytesInAddress, Endian); else - E = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames, - Coverage, BytesInAddress, Endian, Arch); - if (E) - return std::move(E); + EC = loadBinaryFormat(ObjectBuffer->getMemBufferRef(), Reader->ProfileNames, + Coverage, BytesInAddress, Endian, Arch); + if (EC) + return EC; if (BytesInAddress == 4 && Endian == support::endianness::little) - E = readCoverageMappingData( + EC = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 4 && Endian == support::endianness::big) - E = readCoverageMappingData( + EC = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 8 && Endian == support::endianness::little) - E = readCoverageMappingData( + EC = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else if (BytesInAddress == 8 && Endian == support::endianness::big) - E = readCoverageMappingData( + EC = readCoverageMappingData( Reader->ProfileNames, Coverage, Reader->MappingRecords, Reader->Filenames); else - return make_error(coveragemap_error::malformed); - if (E) - return std::move(E); + return coveragemap_error::malformed; + if (EC) + return EC; return std::move(Reader); } -Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { +std::error_code +BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { if (CurrentRecord >= MappingRecords.size()) - return make_error(coveragemap_error::eof); + return coveragemap_error::eof; FunctionsFilenames.clear(); Expressions.clear(); @@ -620,5 +619,5 @@ Error BinaryCoverageReader::readNextRecord(CoverageMappingRecord &Record) { Record.MappingRegions = MappingRegions; ++CurrentRecord; - return Error::success(); + return std::error_code(); } diff --git a/lib/ProfileData/InstrProf.cpp b/lib/ProfileData/InstrProf.cpp index 7042a5825cd..bcb31993d20 100644 --- a/lib/ProfileData/InstrProf.cpp +++ b/lib/ProfileData/InstrProf.cpp @@ -27,50 +27,47 @@ using namespace llvm; namespace { -std::string getInstrProfErrString(instrprof_error Err) { - 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."); -} - class InstrProfErrorCategoryType : public std::error_category { const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } std::string message(int IE) const override { - return getInstrProfErrString(static_cast(IE)); + 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 @@ -108,12 +105,6 @@ void SoftInstrProfErrors::addError(instrprof_error IE) { } } -std::string InstrProfError::message() const { - return getInstrProfErrString(Err); -} - -template <> char ProfErrorInfoBase::ID = 0; - std::string getPGOFuncName(StringRef RawFuncName, GlobalValue::LinkageTypes Linkage, StringRef FileName, @@ -223,8 +214,9 @@ void InstrProfSymtab::create(Module &M, bool InLTO) { finalizeSymtab(); } -Error collectPGOFuncNameStrings(const std::vector &NameStrs, - bool doCompression, std::string &Result) { +std::error_code +collectPGOFuncNameStrings(const std::vector &NameStrs, + bool doCompression, std::string &Result) { assert(NameStrs.size() && "No name data to emit"); uint8_t Header[16], *P = Header; @@ -246,12 +238,11 @@ Error collectPGOFuncNameStrings(const std::vector &NameStrs, unsigned HeaderLen = P - &Header[0]; Result.append(HeaderStr, HeaderLen); Result += InputStr; - return Error::success(); + return make_error_code(instrprof_error::success); }; - if (!doCompression) { + if (!doCompression) return WriteStringToResult(0, UncompressedNameStrings); - } SmallVector CompressedNameStrings; zlib::Status Success = @@ -259,7 +250,7 @@ Error collectPGOFuncNameStrings(const std::vector &NameStrs, zlib::BestSizeCompression); if (Success != zlib::StatusOK) - return make_error(instrprof_error::compress_failed); + return make_error_code(instrprof_error::compress_failed); return WriteStringToResult( CompressedNameStrings.size(), @@ -273,8 +264,9 @@ StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) { return NameStr; } -Error collectPGOFuncNameStrings(const std::vector &NameVars, - std::string &Result, bool doCompression) { +std::error_code +collectPGOFuncNameStrings(const std::vector &NameVars, + std::string &Result, bool doCompression) { std::vector NameStrs; for (auto *NameVar : NameVars) { NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar)); @@ -283,7 +275,8 @@ Error collectPGOFuncNameStrings(const std::vector &NameVars, NameStrs, zlib::isAvailable() && doCompression, Result); } -Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { +std::error_code readPGOFuncNameStrings(StringRef NameStrings, + InstrProfSymtab &Symtab) { const uint8_t *P = reinterpret_cast(NameStrings.data()); const uint8_t *EndP = reinterpret_cast(NameStrings.data() + NameStrings.size()); @@ -301,7 +294,7 @@ Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { CompressedSize); if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, UncompressedSize) != zlib::StatusOK) - return make_error(instrprof_error::uncompress_failed); + return make_error_code(instrprof_error::uncompress_failed); P += CompressedSize; NameStrings = StringRef(UncompressedNameStrings.data(), UncompressedNameStrings.size()); @@ -320,7 +313,7 @@ Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { P++; } Symtab.finalizeSymtab(); - return Error::success(); + return make_error_code(instrprof_error::success); } void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE, @@ -584,45 +577,45 @@ static std::unique_ptr allocValueProfData(uint32_t TotalSize) { ValueProfData()); } -Error ValueProfData::checkIntegrity() { +instrprof_error ValueProfData::checkIntegrity() { if (NumValueKinds > IPVK_Last + 1) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; // Total size needs to be mulltiple of quadword size. if (TotalSize % sizeof(uint64_t)) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; ValueProfRecord *VR = getFirstValueProfRecord(this); for (uint32_t K = 0; K < this->NumValueKinds; K++) { if (VR->Kind > IPVK_Last) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; VR = getValueProfRecordNext(VR); if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; } - return Error::success(); + return instrprof_error::success; } -Expected> +ErrorOr> ValueProfData::getValueProfData(const unsigned char *D, const unsigned char *const BufferEnd, support::endianness Endianness) { using namespace support; if (D + sizeof(ValueProfData) > BufferEnd) - return make_error(instrprof_error::truncated); + return instrprof_error::truncated; const unsigned char *Header = D; uint32_t TotalSize = swapToHostOrder(Header, Endianness); if (D + TotalSize > BufferEnd) - return make_error(instrprof_error::too_large); + return instrprof_error::too_large; std::unique_ptr VPD = allocValueProfData(TotalSize); memcpy(VPD.get(), D, TotalSize); // Byte swap. VPD->swapBytesToHost(Endianness); - Error E = VPD->checkIntegrity(); - if (E) - return std::move(E); + instrprof_error EC = VPD->checkIntegrity(); + if (EC != instrprof_error::success) + return EC; return std::move(VPD); } diff --git a/lib/ProfileData/InstrProfReader.cpp b/lib/ProfileData/InstrProfReader.cpp index ece1f8704d6..0828bb2c3fc 100644 --- a/lib/ProfileData/InstrProfReader.cpp +++ b/lib/ProfileData/InstrProfReader.cpp @@ -18,33 +18,33 @@ using namespace llvm; -static Expected> +static ErrorOr> setupMemoryBuffer(std::string Path) { ErrorOr> BufferOrErr = MemoryBuffer::getFileOrSTDIN(Path); if (std::error_code EC = BufferOrErr.getError()) - return errorCodeToError(EC); + return EC; return std::move(BufferOrErr.get()); } -static Error initializeReader(InstrProfReader &Reader) { +static std::error_code initializeReader(InstrProfReader &Reader) { return Reader.readHeader(); } -Expected> +ErrorOr> InstrProfReader::create(std::string Path) { // Set up the buffer to read. auto BufferOrError = setupMemoryBuffer(Path); - if (Error E = BufferOrError.takeError()) - return std::move(E); + if (std::error_code EC = BufferOrError.getError()) + return EC; return InstrProfReader::create(std::move(BufferOrError.get())); } -Expected> +ErrorOr> InstrProfReader::create(std::unique_ptr Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits::max()) - return make_error(instrprof_error::too_large); + return instrprof_error::too_large; std::unique_ptr Result; // Create the reader. @@ -57,50 +57,46 @@ InstrProfReader::create(std::unique_ptr Buffer) { else if (TextInstrProfReader::hasFormat(*Buffer)) Result.reset(new TextInstrProfReader(std::move(Buffer))); else - return make_error(instrprof_error::unrecognized_format); + return instrprof_error::unrecognized_format; // Initialize the reader and return the result. - if (Error E = initializeReader(*Result)) - return std::move(E); + if (std::error_code EC = initializeReader(*Result)) + return EC; return std::move(Result); } -Expected> +ErrorOr> IndexedInstrProfReader::create(std::string Path) { // Set up the buffer to read. auto BufferOrError = setupMemoryBuffer(Path); - if (Error E = BufferOrError.takeError()) - return std::move(E); + if (std::error_code EC = BufferOrError.getError()) + return EC; return IndexedInstrProfReader::create(std::move(BufferOrError.get())); } -Expected> +ErrorOr> IndexedInstrProfReader::create(std::unique_ptr Buffer) { // Sanity check the buffer. if (Buffer->getBufferSize() > std::numeric_limits::max()) - return make_error(instrprof_error::too_large); + return instrprof_error::too_large; // Create the reader. if (!IndexedInstrProfReader::hasFormat(*Buffer)) - return make_error(instrprof_error::bad_magic); + return instrprof_error::bad_magic; auto Result = llvm::make_unique(std::move(Buffer)); // Initialize the reader and return the result. - if (Error E = initializeReader(*Result)) - return std::move(E); + if (std::error_code EC = initializeReader(*Result)) + return EC; return std::move(Result); } void InstrProfIterator::Increment() { - 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"); + if (Reader->readNextRecord(Record)) *this = InstrProfIterator(); - } } bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) { @@ -116,7 +112,7 @@ bool TextInstrProfReader::hasFormat(const MemoryBuffer &Buffer) { // 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. -Error TextInstrProfReader::readHeader() { +std::error_code TextInstrProfReader::readHeader() { Symtab.reset(new InstrProfSymtab()); bool IsIRInstr = false; if (!Line->startswith(":")) { @@ -129,14 +125,14 @@ Error TextInstrProfReader::readHeader() { else if (Str.equals_lower("fe")) IsIRInstr = false; else - return error(instrprof_error::bad_header); + return instrprof_error::bad_header; ++Line; IsIRLevelProfile = IsIRInstr; return success(); } -Error +std::error_code TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { #define CHECK_LINE_END(Line) \ @@ -200,7 +196,7 @@ TextInstrProfReader::readValueProfileData(InstrProfRecord &Record) { #undef VP_READ_ADVANCE } -Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { +std::error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { // Skip empty lines and comments. while (!Line.is_at_end() && (Line->empty() || Line->startswith("#"))) ++Line; @@ -242,8 +238,8 @@ Error TextInstrProfReader::readNextRecord(InstrProfRecord &Record) { } // Check if value profile data exists and read it if so. - if (Error E = readValueProfileData(Record)) - return E; + if (std::error_code EC = readValueProfileData(Record)) + return EC; // This is needed to avoid two pass parsing because llvm-profdata // does dumping while reading. @@ -262,7 +258,7 @@ bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) { } template -Error RawInstrProfReader::readHeader() { +std::error_code RawInstrProfReader::readHeader() { if (!hasFormat(*DataBuffer)) return error(instrprof_error::bad_magic); if (DataBuffer->getBufferSize() < sizeof(RawInstrProf::Header)) @@ -274,25 +270,26 @@ Error RawInstrProfReader::readHeader() { } template -Error RawInstrProfReader::readNextHeader(const char *CurrentPos) { +std::error_code +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 make_error(instrprof_error::eof); + return 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 make_error(instrprof_error::malformed); + return instrprof_error::malformed; // The writer ensures each profile is padded to start at an aligned address. if (reinterpret_cast(CurrentPos) % alignOf()) - return make_error(instrprof_error::malformed); + return 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 make_error(instrprof_error::bad_magic); + return instrprof_error::bad_magic; // There's another profile to read, so we need to process the header. auto *Header = reinterpret_cast(CurrentPos); @@ -300,9 +297,11 @@ Error RawInstrProfReader::readNextHeader(const char *CurrentPos) { } template -Error RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) { - if (Error E = Symtab.create(StringRef(NamesStart, NamesSize))) - return error(std::move(E)); +std::error_code +RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) { + std::error_code EC = Symtab.create(StringRef(NamesStart, NamesSize)); + if (EC) + return EC; for (const RawInstrProf::ProfileData *I = Data; I != DataEnd; ++I) { const IntPtrT FPtr = swap(I->FunctionPointer); if (!FPtr) @@ -314,8 +313,8 @@ Error RawInstrProfReader::createSymtab(InstrProfSymtab &Symtab) { } template -Error RawInstrProfReader::readHeader( - const RawInstrProf::Header &Header) { +std::error_code +RawInstrProfReader::readHeader(const RawInstrProf::Header &Header) { Version = swap(Header.Version); if (GET_VERSION(Version) != RawInstrProf::Version) return error(instrprof_error::unsupported_version); @@ -347,27 +346,28 @@ Error RawInstrProfReader::readHeader( ValueDataStart = reinterpret_cast(Start + ValueDataOffset); std::unique_ptr NewSymtab = make_unique(); - if (Error E = createSymtab(*NewSymtab.get())) - return E; + if (auto EC = createSymtab(*NewSymtab.get())) + return EC; Symtab = std::move(NewSymtab); return success(); } template -Error RawInstrProfReader::readName(InstrProfRecord &Record) { +std::error_code RawInstrProfReader::readName(InstrProfRecord &Record) { Record.Name = getName(Data->NameRef); return success(); } template -Error RawInstrProfReader::readFuncHash(InstrProfRecord &Record) { +std::error_code RawInstrProfReader::readFuncHash( + InstrProfRecord &Record) { Record.Hash = swap(Data->FuncHash); return success(); } template -Error RawInstrProfReader::readRawCounts( +std::error_code RawInstrProfReader::readRawCounts( InstrProfRecord &Record) { uint32_t NumCounters = swap(Data->NumCounters); IntPtrT CounterPtr = Data->CounterPtr; @@ -394,8 +394,8 @@ Error RawInstrProfReader::readRawCounts( } template -Error RawInstrProfReader::readValueProfilingData( - InstrProfRecord &Record) { +std::error_code +RawInstrProfReader::readValueProfilingData(InstrProfRecord &Record) { Record.clearValueData(); CurValueDataSize = 0; @@ -407,13 +407,13 @@ Error RawInstrProfReader::readValueProfilingData( if (!NumValueKinds) return success(); - Expected> VDataPtrOrErr = + ErrorOr> VDataPtrOrErr = ValueProfData::getValueProfData( ValueDataStart, (const unsigned char *)DataBuffer->getBufferEnd(), getDataEndianness()); - if (Error E = VDataPtrOrErr.takeError()) - return E; + if (VDataPtrOrErr.getError()) + return VDataPtrOrErr.getError(); // Note that besides deserialization, this also performs the conversion for // indirect call targets. The function pointers from the raw profile are @@ -424,27 +424,28 @@ Error RawInstrProfReader::readValueProfilingData( } template -Error RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { +std::error_code +RawInstrProfReader::readNextRecord(InstrProfRecord &Record) { if (atEnd()) // At this point, ValueDataStart field points to the next header. - if (Error E = readNextHeader(getNextHeaderPos())) - return E; + if (std::error_code EC = readNextHeader(getNextHeaderPos())) + return EC; // Read name ad set it in Record. - if (Error E = readName(Record)) - return E; + if (std::error_code EC = readName(Record)) + return EC; // Read FuncHash and set it in Record. - if (Error E = readFuncHash(Record)) - return E; + if (std::error_code EC = readFuncHash(Record)) + return EC; // Read raw counts and set Record. - if (Error E = readRawCounts(Record)) - return E; + if (std::error_code EC = readRawCounts(Record)) + return EC; // Read value data and set Record. - if (Error E = readValueProfilingData(Record)) - return E; + if (std::error_code EC = readValueProfilingData(Record)) + return EC; // Iterate. advanceData(); @@ -466,10 +467,10 @@ typedef InstrProfLookupTrait::offset_type offset_type; bool InstrProfLookupTrait::readValueProfilingData( const unsigned char *&D, const unsigned char *const End) { - Expected> VDataPtrOrErr = + ErrorOr> VDataPtrOrErr = ValueProfData::getValueProfData(D, End, ValueProfDataEndianness); - if (VDataPtrOrErr.takeError()) + if (VDataPtrOrErr.getError()) return false; VDataPtrOrErr.get()->deserializeTo(DataBuffer.back(), nullptr); @@ -525,31 +526,31 @@ data_type InstrProfLookupTrait::ReadData(StringRef K, const unsigned char *D, } template -Error InstrProfReaderIndex::getRecords( +std::error_code InstrProfReaderIndex::getRecords( StringRef FuncName, ArrayRef &Data) { auto Iter = HashTable->find(FuncName); if (Iter == HashTable->end()) - return make_error(instrprof_error::unknown_function); + return instrprof_error::unknown_function; Data = (*Iter); if (Data.empty()) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; - return Error::success(); + return instrprof_error::success; } template -Error InstrProfReaderIndex::getRecords( +std::error_code InstrProfReaderIndex::getRecords( ArrayRef &Data) { if (atEnd()) - return make_error(instrprof_error::eof); + return instrprof_error::eof; Data = *RecordIterator; if (Data.empty()) - return make_error(instrprof_error::malformed); + return instrprof_error::malformed; - return Error::success(); + return instrprof_error::success; } template @@ -608,7 +609,7 @@ IndexedInstrProfReader::readSummary(IndexedInstrProf::ProfVersion Version, } } -Error IndexedInstrProfReader::readHeader() { +std::error_code IndexedInstrProfReader::readHeader() { const unsigned char *Start = (const unsigned char *)DataBuffer->getBufferStart(); const unsigned char *Cur = Start; @@ -660,13 +661,13 @@ InstrProfSymtab &IndexedInstrProfReader::getSymtab() { return *Symtab.get(); } -Expected +ErrorOr IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, uint64_t FuncHash) { ArrayRef Data; - Error Err = Index->getRecords(FuncName, Data); - if (Err) - return std::move(Err); + std::error_code EC = Index->getRecords(FuncName, Data); + if (EC != instrprof_error::success) + return EC; // 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. @@ -677,25 +678,26 @@ IndexedInstrProfReader::getInstrProfRecord(StringRef FuncName, return error(instrprof_error::hash_mismatch); } -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)); +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; Counts = Record.get().Counts; return success(); } -Error IndexedInstrProfReader::readNextRecord(InstrProfRecord &Record) { +std::error_code IndexedInstrProfReader::readNextRecord( + InstrProfRecord &Record) { static unsigned RecordIndex = 0; ArrayRef Data; - Error E = Index->getRecords(Data); - if (E) - return error(std::move(E)); + std::error_code EC = Index->getRecords(Data); + if (EC != instrprof_error::success) + return error(EC); Record = Data[RecordIndex++]; if (RecordIndex >= Data.size()) { diff --git a/lib/ProfileData/InstrProfWriter.cpp b/lib/ProfileData/InstrProfWriter.cpp index 9b01dac313b..d6caca2a7a9 100644 --- a/lib/ProfileData/InstrProfWriter.cpp +++ b/lib/ProfileData/InstrProfWriter.cpp @@ -156,7 +156,8 @@ void InstrProfWriter::setOutputSparse(bool Sparse) { this->Sparse = Sparse; } -Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) { +std::error_code InstrProfWriter::addRecord(InstrProfRecord &&I, + uint64_t Weight) { auto &ProfileDataMap = FunctionData[I.Name]; bool NewFunc; @@ -179,7 +180,7 @@ Error InstrProfWriter::addRecord(InstrProfRecord &&I, uint64_t Weight) { Dest.sortValueData(); - return Dest.takeError(); + return Dest.getError(); } bool InstrProfWriter::shouldEncodeData(const ProfilingData &PD) { diff --git a/lib/Transforms/Instrumentation/InstrProfiling.cpp b/lib/Transforms/Instrumentation/InstrProfiling.cpp index 11369d6f70d..930001fd684 100644 --- a/lib/Transforms/Instrumentation/InstrProfiling.cpp +++ b/lib/Transforms/Instrumentation/InstrProfiling.cpp @@ -374,9 +374,9 @@ void InstrProfiling::emitNameData() { return; std::string CompressedNameStr; - if (Error E = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, + if (auto EC = collectPGOFuncNameStrings(ReferencedNames, CompressedNameStr, DoNameCompression)) { - llvm::report_fatal_error(toString(std::move(E)), false); + llvm::report_fatal_error(EC.message(), false); } auto &Ctx = M->getContext(); diff --git a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp index af1809ffd0c..379f7c5ebed 100644 --- a/lib/Transforms/Instrumentation/PGOInstrumentation.cpp +++ b/lib/Transforms/Instrumentation/PGOInstrumentation.cpp @@ -582,28 +582,23 @@ void PGOUseFunc::setEdgeCount(DirectEdges &Edges, uint64_t Value) { // Return true if the profile are successfully read, and false on errors. bool PGOUseFunc::readCounters(IndexedInstrProfReader *PGOReader) { auto &Ctx = M->getContext(); - Expected Result = + ErrorOr Result = PGOReader->getInstrProfRecord(FuncInfo.FuncName, FuncInfo.FunctionHash); - if (Error E = Result.takeError()) { - handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { - auto Err = IPE.get(); - bool SkipWarning = false; - if (Err == instrprof_error::unknown_function) { - NumOfPGOMissing++; - SkipWarning = NoPGOWarnMissing; - } else if (Err == instrprof_error::hash_mismatch || - Err == instrprof_error::malformed) { - NumOfPGOMismatch++; - SkipWarning = NoPGOWarnMismatch; - } - - if (SkipWarning) - return; + if (std::error_code EC = Result.getError()) { + if (EC == instrprof_error::unknown_function) { + NumOfPGOMissing++; + if (NoPGOWarnMissing) + return false; + } else if (EC == instrprof_error::hash_mismatch || + EC == llvm::instrprof_error::malformed) { + NumOfPGOMismatch++; + if (NoPGOWarnMismatch) + return false; + } - std::string Msg = IPE.message() + std::string(" ") + F.getName().str(); - Ctx.diagnose( - DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning)); - }); + std::string Msg = EC.message() + std::string(" ") + F.getName().str(); + Ctx.diagnose( + DiagnosticInfoPGOProfile(M->getName().data(), Msg, DS_Warning)); return false; } ProfileRecord = std::move(Result.get()); @@ -859,11 +854,9 @@ static bool annotateAllFunctions( auto &Ctx = M.getContext(); // Read the counter array from file. auto ReaderOrErr = IndexedInstrProfReader::create(ProfileFileName); - if (Error E = ReaderOrErr.takeError()) { - handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { - Ctx.diagnose( - DiagnosticInfoPGOProfile(ProfileFileName.data(), IPE.message())); - }); + if (std::error_code EC = ReaderOrErr.getError()) { + Ctx.diagnose( + DiagnosticInfoPGOProfile(ProfileFileName.data(), EC.message())); return false; } diff --git a/tools/llvm-cov/CodeCoverage.cpp b/tools/llvm-cov/CodeCoverage.cpp index cd786edc25d..1c11fe60197 100644 --- a/tools/llvm-cov/CodeCoverage.cpp +++ b/tools/llvm-cov/CodeCoverage.cpp @@ -210,9 +210,10 @@ std::unique_ptr CodeCoverageTool::load() { errs() << "warning: profile data may be out of date - object is newer\n"; auto CoverageOrErr = CoverageMapping::load(ObjectFilename, PGOFilename, CoverageArch); - if (Error E = CoverageOrErr.takeError()) { + if (std::error_code EC = CoverageOrErr.getError()) { colored_ostream(errs(), raw_ostream::RED) - << "error: Failed to load coverage: " << toString(std::move(E)) << "\n"; + << "error: Failed to load coverage: " << EC.message(); + errs() << "\n"; return nullptr; } auto Coverage = std::move(CoverageOrErr.get()); diff --git a/tools/llvm-profdata/llvm-profdata.cpp b/tools/llvm-profdata/llvm-profdata.cpp index 33c0e1d0711..5cdf962e885 100644 --- a/tools/llvm-profdata/llvm-profdata.cpp +++ b/tools/llvm-profdata/llvm-profdata.cpp @@ -47,49 +47,38 @@ static void exitWithError(const Twine &Message, StringRef Whence = "", ::exit(1); } -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?"); - } - }); +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?"); + } } - - exitWithError(toString(std::move(E)), Whence); -} - -static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") { - exitWithError(EC.message(), Whence); + exitWithError(Error.message(), Whence); } namespace { enum ProfileKinds { instr, sample }; } -static void handleMergeWriterError(Error E, StringRef WhenceFile = "", +static void handleMergeWriterError(std::error_code &Error, + StringRef WhenceFile = "", StringRef WhenceFunction = "", bool ShowHint = true) { if (!WhenceFile.empty()) errs() << WhenceFile << ": "; if (!WhenceFunction.empty()) errs() << WhenceFunction << ": "; - - 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"; + errs() << Error.message() << "\n"; if (ShowHint) { StringRef Hint = ""; - if (IPE != instrprof_error::success) { - switch (IPE) { + if (Error.category() == instrprof_category()) { + instrprof_error instrError = static_cast(Error.value()); + switch (instrError) { case instrprof_error::hash_mismatch: case instrprof_error::count_mismatch: case instrprof_error::value_site_count_mismatch: @@ -131,11 +120,11 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs, exitWithErrorCode(EC, OutputFilename); InstrProfWriter Writer(OutputSparse); - SmallSet WriterErrorCodes; + SmallSet WriterErrorCodes; for (const auto &Input : Inputs) { auto ReaderOrErr = InstrProfReader::create(Input.Filename); - if (Error E = ReaderOrErr.takeError()) - exitWithError(std::move(E), Input.Filename); + if (std::error_code ec = ReaderOrErr.getError()) + exitWithErrorCode(ec, Input.Filename); auto Reader = std::move(ReaderOrErr.get()); bool IsIRProfile = Reader->isIRLevelProfile(); @@ -143,16 +132,14 @@ static void mergeInstrProfile(const WeightedFileVector &Inputs, exitWithError("Merge IR generated profile with Clang generated profile."); for (auto &I : *Reader) { - if (Error E = Writer.addRecord(std::move(I), Input.Weight)) { + if (std::error_code EC = Writer.addRecord(std::move(I), Input.Weight)) { // Only show hint the first time an error occurs. - instrprof_error IPE = InstrProfError::take(std::move(E)); - bool firstTime = WriterErrorCodes.insert(IPE).second; - handleMergeWriterError(make_error(IPE), Input.Filename, - I.Name, firstTime); + bool firstTime = WriterErrorCodes.insert(EC).second; + handleMergeWriterError(EC, Input.Filename, I.Name, firstTime); } } if (Reader->hasError()) - exitWithError(Reader->getError(), Input.Filename); + exitWithErrorCode(Reader->getError(), Input.Filename); } if (OutputFormat == PF_Text) Writer.writeText(Output); @@ -200,7 +187,7 @@ static void mergeSampleProfile(const WeightedFileVector &Inputs, sampleprof_error Result = ProfileMap[FName].merge(Samples, Input.Weight); if (Result != sampleprof_error::success) { std::error_code EC = make_error_code(Result); - handleMergeWriterError(errorCodeToError(EC), Input.Filename, FName); + handleMergeWriterError(EC, Input.Filename, FName); } } } @@ -281,8 +268,8 @@ static int showInstrProfile(std::string Filename, bool ShowCounts, Cutoffs = {800000, 900000, 950000, 990000, 999000, 999900, 999990}; } InstrProfSummary PS(Cutoffs); - if (Error E = ReaderOrErr.takeError()) - exitWithError(std::move(E), Filename); + if (std::error_code EC = ReaderOrErr.getError()) + exitWithErrorCode(EC, Filename); auto Reader = std::move(ReaderOrErr.get()); bool IsIRInstr = Reader->isIRLevelProfile(); @@ -348,7 +335,7 @@ static int showInstrProfile(std::string Filename, bool ShowCounts, } if (Reader->hasError()) - exitWithError(Reader->getError(), Filename); + exitWithErrorCode(Reader->getError(), Filename); if (ShowCounts && TextFormat) return 0; diff --git a/unittests/ProfileData/CoverageMappingTest.cpp b/unittests/ProfileData/CoverageMappingTest.cpp index 53b40ebae85..81e9cb13233 100644 --- a/unittests/ProfileData/CoverageMappingTest.cpp +++ b/unittests/ProfileData/CoverageMappingTest.cpp @@ -20,11 +20,11 @@ using namespace llvm; using namespace coverage; -static ::testing::AssertionResult NoError(Error E) { - if (!E) +static ::testing::AssertionResult NoError(std::error_code EC) { + if (!EC) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) - << "\n"; + return ::testing::AssertionFailure() << "error " << EC.value() + << ": " << EC.message(); } namespace llvm { @@ -70,14 +70,14 @@ struct CoverageMappingReaderMock : CoverageMappingReader { CoverageMappingReaderMock(ArrayRef Functions) : Functions(Functions) {} - Error readNextRecord(CoverageMappingRecord &Record) override { + std::error_code readNextRecord(CoverageMappingRecord &Record) override { if (Functions.empty()) - return make_error(coveragemap_error::eof); + return coveragemap_error::eof; Functions.front().fillCoverageMappingRecord(Record); Functions = Functions.slice(1); - return Error::success(); + return coveragemap_error::success; } }; @@ -190,7 +190,7 @@ struct CoverageMappingTest : ::testing::Test { void readProfCounts() { auto Profile = ProfileWriter.writeBuffer(); auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.takeError())); + ASSERT_TRUE(NoError(ReaderOrErr.getError())); ProfileReader = std::move(ReaderOrErr.get()); } @@ -200,7 +200,7 @@ struct CoverageMappingTest : ::testing::Test { CoverageMappingReaderMock CovReader(OutputFunctions); auto CoverageOrErr = CoverageMapping::load(CovReader, *ProfileReader); - ASSERT_TRUE(NoError(CoverageOrErr.takeError())); + ASSERT_TRUE(NoError(CoverageOrErr.getError())); LoadedCoverage = std::move(CoverageOrErr.get()); } }; diff --git a/unittests/ProfileData/InstrProfTest.cpp b/unittests/ProfileData/InstrProfTest.cpp index 9ef3487ba18..2d9a2218e55 100644 --- a/unittests/ProfileData/InstrProfTest.cpp +++ b/unittests/ProfileData/InstrProfTest.cpp @@ -19,24 +19,19 @@ using namespace llvm; -static ::testing::AssertionResult NoError(Error E) { - if (!E) +static ::testing::AssertionResult NoError(std::error_code EC) { + if (!EC) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error: " << toString(std::move(E)) - << "\n"; + return ::testing::AssertionFailure() << "error " << EC.value() + << ": " << EC.message(); } -static ::testing::AssertionResult ErrorEquals(instrprof_error Expected, - Error E) { - instrprof_error Found; - std::string FoundMsg; - handleAllErrors(std::move(E), [&](const InstrProfError &IPE) { - Found = IPE.get(); - FoundMsg = IPE.message(); - }); +static ::testing::AssertionResult ErrorEquals(std::error_code Expected, + std::error_code Found) { if (Expected == Found) return ::testing::AssertionSuccess(); - return ::testing::AssertionFailure() << "error: " << FoundMsg << "\n"; + return ::testing::AssertionFailure() << "error " << Found.value() + << ": " << Found.message(); } namespace { @@ -49,7 +44,7 @@ struct InstrProfTest : ::testing::Test { void readProfile(std::unique_ptr Profile) { auto ReaderOrErr = IndexedInstrProfReader::create(std::move(Profile)); - ASSERT_TRUE(NoError(ReaderOrErr.takeError())); + ASSERT_TRUE(NoError(ReaderOrErr.getError())); Reader = std::move(ReaderOrErr.get()); } }; @@ -95,23 +90,23 @@ TEST_P(MaybeSparseInstrProfTest, get_instr_prof_record) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("foo", 0x1234); + ASSERT_TRUE(NoError(R.getError())); 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.takeError())); + ASSERT_TRUE(NoError(R.getError())); 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.takeError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, R.getError())); R = Reader->getInstrProfRecord("bar", 0x1234); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.takeError())); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, R.getError())); } TEST_P(MaybeSparseInstrProfTest, get_function_counts) { @@ -133,11 +128,12 @@ TEST_P(MaybeSparseInstrProfTest, get_function_counts) { ASSERT_EQ(3U, Counts[0]); ASSERT_EQ(4U, Counts[1]); - Error E1 = Reader->getFunctionCounts("foo", 0x5678, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, std::move(E1))); + std::error_code EC; + EC = Reader->getFunctionCounts("foo", 0x5678, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::hash_mismatch, EC)); - Error E2 = Reader->getFunctionCounts("bar", 0x1234, Counts); - ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, std::move(E2))); + EC = Reader->getFunctionCounts("bar", 0x1234, Counts); + ASSERT_TRUE(ErrorEquals(instrprof_error::unknown_function, EC)); } // Profile data is copied from general.proftext @@ -239,8 +235,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -270,8 +266,8 @@ TEST_P(MaybeSparseInstrProfTest, annotate_vp_data) { NoError(Writer.addRecord(std::move(Record))); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); LLVMContext Ctx; std::unique_ptr M(new Module("MyModule", Ctx)); @@ -382,8 +378,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_with_weight) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -435,8 +431,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_read_write_big_endian) { // Set big endian input. Reader->setValueProfDataEndianness(support::big); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); ASSERT_EQ(4U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(3U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -517,8 +513,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); ASSERT_EQ(5U, R->getNumValueSites(IPVK_IndirectCallTarget)); ASSERT_EQ(4U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(0U, R->getNumValueDataForSite(IPVK_IndirectCallTarget, 1)); @@ -570,27 +566,23 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { InstrProfRecord Record1("foo", 0x1234, {1}); auto Result1 = Writer.addRecord(std::move(Record1)); - ASSERT_EQ(InstrProfError::take(std::move(Result1)), - instrprof_error::success); + ASSERT_EQ(Result1, instrprof_error::success); // Verify counter overflow. InstrProfRecord Record2("foo", 0x1234, {Max}); auto Result2 = Writer.addRecord(std::move(Record2)); - ASSERT_EQ(InstrProfError::take(std::move(Result2)), - instrprof_error::counter_overflow); + ASSERT_EQ(Result2, instrprof_error::counter_overflow); InstrProfRecord Record3(bar, 0x9012, {8}); auto Result3 = Writer.addRecord(std::move(Record3)); - ASSERT_EQ(InstrProfError::take(std::move(Result3)), - instrprof_error::success); + ASSERT_EQ(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(InstrProfError::take(std::move(Result4)), - instrprof_error::success); + ASSERT_EQ(Result4, instrprof_error::success); // Verify value data counter overflow. InstrProfRecord Record5("baz", 0x5678, {5, 6}); @@ -598,21 +590,19 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge1_saturation) { InstrProfValueData VD5[] = {{uint64_t(bar), Max}}; Record5.addValueData(IPVK_IndirectCallTarget, 0, VD5, 1, nullptr); auto Result5 = Writer.addRecord(std::move(Record5)); - ASSERT_EQ(InstrProfError::take(std::move(Result5)), - instrprof_error::counter_overflow); + ASSERT_EQ(Result5, instrprof_error::counter_overflow); auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); // Verify saturation of counts. - Expected ReadRecord1 = + ErrorOr ReadRecord1 = Reader->getInstrProfRecord("foo", 0x1234); - ASSERT_TRUE(NoError(ReadRecord1.takeError())); + ASSERT_TRUE(NoError(ReadRecord1.getError())); ASSERT_EQ(Max, ReadRecord1->Counts[0]); - Expected ReadRecord2 = + ErrorOr 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); @@ -657,8 +647,8 @@ TEST_P(MaybeSparseInstrProfTest, get_icall_data_merge_site_trunc) { auto Profile = Writer.writeBuffer(); readProfile(std::move(Profile)); - Expected R = Reader->getInstrProfRecord("caller", 0x1234); - ASSERT_TRUE(NoError(R.takeError())); + ErrorOr R = Reader->getInstrProfRecord("caller", 0x1234); + ASSERT_TRUE(NoError(R.getError())); std::unique_ptr VD( R->getValueForSite(IPVK_IndirectCallTarget, 0)); ASSERT_EQ(2U, R->getNumValueSites(IPVK_IndirectCallTarget)); -- 2.11.0