@@ -823,7 +823,8 @@ class MCDCDecisionRecorder {
823823
824824Error CoverageMapping::loadFunctionRecord (
825825 const CoverageMappingRecord &Record,
826- IndexedInstrProfReader &ProfileReader) {
826+ const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
827+ &ProfileReader) {
827828 StringRef OrigFuncName = Record.FunctionName ;
828829 if (OrigFuncName.empty ())
829830 return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -837,35 +838,44 @@ Error CoverageMapping::loadFunctionRecord(
837838 CounterMappingContext Ctx (Record.Expressions );
838839
839840 std::vector<uint64_t > Counts;
840- if (Error E = ProfileReader.getFunctionCounts (Record.FunctionName ,
841- Record.FunctionHash , Counts)) {
842- instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
843- if (IPE == instrprof_error::hash_mismatch) {
844- FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
845- Record.FunctionHash );
846- return Error::success ();
841+ if (ProfileReader) {
842+ if (Error E = ProfileReader.value ().get ().getFunctionCounts (
843+ Record.FunctionName , Record.FunctionHash , Counts)) {
844+ instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
845+ if (IPE == instrprof_error::hash_mismatch) {
846+ FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
847+ Record.FunctionHash );
848+ return Error::success ();
849+ }
850+ if (IPE != instrprof_error::unknown_function)
851+ return make_error<InstrProfError>(IPE);
852+ Counts.assign (getMaxCounterID (Ctx, Record) + 1 , 0 );
847853 }
848- if (IPE != instrprof_error::unknown_function)
849- return make_error<InstrProfError>(IPE);
854+ } else {
850855 Counts.assign (getMaxCounterID (Ctx, Record) + 1 , 0 );
851856 }
852857 Ctx.setCounts (Counts);
853858
854859 bool IsVersion11 =
855- ProfileReader.getVersion () < IndexedInstrProf::ProfVersion::Version12;
860+ ProfileReader && ProfileReader.value ().get ().getVersion () <
861+ IndexedInstrProf::ProfVersion::Version12;
856862
857863 BitVector Bitmap;
858- if (Error E = ProfileReader.getFunctionBitmap (Record.FunctionName ,
859- Record.FunctionHash , Bitmap)) {
860- instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
861- if (IPE == instrprof_error::hash_mismatch) {
862- FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
863- Record.FunctionHash );
864- return Error::success ();
864+ if (ProfileReader) {
865+ if (Error E = ProfileReader.value ().get ().getFunctionBitmap (
866+ Record.FunctionName , Record.FunctionHash , Bitmap)) {
867+ instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
868+ if (IPE == instrprof_error::hash_mismatch) {
869+ FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
870+ Record.FunctionHash );
871+ return Error::success ();
872+ }
873+ if (IPE != instrprof_error::unknown_function)
874+ return make_error<InstrProfError>(IPE);
875+ Bitmap = BitVector (getMaxBitmapSize (Record, IsVersion11));
865876 }
866- if (IPE != instrprof_error::unknown_function)
867- return make_error<InstrProfError>(IPE);
868- Bitmap = BitVector (getMaxBitmapSize (Record, IsVersion11));
877+ } else {
878+ Bitmap = BitVector (getMaxBitmapSize (Record, false ));
869879 }
870880 Ctx.setBitmap (std::move (Bitmap));
871881
@@ -960,10 +970,17 @@ Error CoverageMapping::loadFunctionRecord(
960970// of CoverageMappingReader instances.
961971Error CoverageMapping::loadFromReaders (
962972 ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
963- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
973+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
974+ &ProfileReader,
975+ CoverageMapping &Coverage) {
964976 assert (!Coverage.SingleByteCoverage ||
965- *Coverage.SingleByteCoverage == ProfileReader.hasSingleByteCoverage ());
966- Coverage.SingleByteCoverage = ProfileReader.hasSingleByteCoverage ();
977+ !ProfileReader ||
978+ *Coverage.SingleByteCoverage ==
979+ ProfileReader.value ().get ().hasSingleByteCoverage ());
980+ if (ProfileReader) {
981+ Coverage.SingleByteCoverage =
982+ ProfileReader.value ().get ().hasSingleByteCoverage ();
983+ }
967984 for (const auto &CoverageReader : CoverageReaders) {
968985 for (auto RecordOrErr : *CoverageReader) {
969986 if (Error E = RecordOrErr.takeError ())
@@ -978,7 +995,8 @@ Error CoverageMapping::loadFromReaders(
978995
979996Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
980997 ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
981- IndexedInstrProfReader &ProfileReader) {
998+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
999+ &ProfileReader) {
9821000 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
9831001 if (Error E = loadFromReaders (CoverageReaders, ProfileReader, *Coverage))
9841002 return std::move (E);
@@ -987,18 +1005,19 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
9871005
9881006// If E is a no_data_found error, returns success. Otherwise returns E.
9891007static Error handleMaybeNoDataFoundError (Error E) {
990- return handleErrors (
991- std::move (E), [](const CoverageMapError &CME) {
992- if (CME.get () == coveragemap_error::no_data_found)
993- return static_cast <Error>(Error::success ());
994- return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
995- });
1008+ return handleErrors (std::move (E), [](const CoverageMapError &CME) {
1009+ if (CME.get () == coveragemap_error::no_data_found)
1010+ return static_cast <Error>(Error::success ());
1011+ return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
1012+ });
9961013}
9971014
9981015Error CoverageMapping::loadFromFile (
9991016 StringRef Filename, StringRef Arch, StringRef CompilationDir,
1000- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
1001- bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1017+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
1018+ &ProfileReader,
1019+ CoverageMapping &Coverage, bool &DataFound,
1020+ SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
10021021 auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN (
10031022 Filename, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
10041023 if (std::error_code EC = CovMappingBufOrErr.getError ())
@@ -1034,13 +1053,23 @@ Error CoverageMapping::loadFromFile(
10341053}
10351054
10361055Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
1037- ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
1038- vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
1056+ ArrayRef<StringRef> ObjectFilenames,
1057+ std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
1058+ ArrayRef<StringRef> Arches, StringRef CompilationDir,
10391059 const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
1040- auto ProfileReaderOrErr = IndexedInstrProfReader::create (ProfileFilename, FS);
1041- if (Error E = ProfileReaderOrErr.takeError ())
1042- return createFileError (ProfileFilename, std::move (E));
1043- auto ProfileReader = std::move (ProfileReaderOrErr.get ());
1060+ std::unique_ptr<IndexedInstrProfReader> ProfileReader;
1061+ if (ProfileFilename) {
1062+ auto ProfileReaderOrErr =
1063+ IndexedInstrProfReader::create (ProfileFilename.value (), FS);
1064+ if (Error E = ProfileReaderOrErr.takeError ())
1065+ return createFileError (ProfileFilename.value (), std::move (E));
1066+ ProfileReader = std::move (ProfileReaderOrErr.get ());
1067+ }
1068+ auto ProfileReaderRef =
1069+ ProfileReader
1070+ ? std::optional<std::reference_wrapper<IndexedInstrProfReader>>(
1071+ *ProfileReader)
1072+ : std::nullopt ;
10441073 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
10451074 bool DataFound = false ;
10461075
@@ -1054,16 +1083,17 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
10541083
10551084 SmallVector<object::BuildID> FoundBinaryIDs;
10561085 for (const auto &File : llvm::enumerate (ObjectFilenames)) {
1057- if (Error E =
1058- loadFromFile (File. value (), GetArch (File. index ()), CompilationDir ,
1059- *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
1086+ if (Error E = loadFromFile (File. value (), GetArch (File. index ()),
1087+ CompilationDir, ProfileReaderRef, *Coverage ,
1088+ DataFound, &FoundBinaryIDs))
10601089 return std::move (E);
10611090 }
10621091
10631092 if (BIDFetcher) {
10641093 std::vector<object::BuildID> ProfileBinaryIDs;
1065- if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1066- return createFileError (ProfileFilename, std::move (E));
1094+ if (ProfileReader)
1095+ if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1096+ return createFileError (ProfileFilename.value (), std::move (E));
10671097
10681098 SmallVector<object::BuildIDRef> BinaryIDsToFetch;
10691099 if (!ProfileBinaryIDs.empty ()) {
@@ -1083,12 +1113,12 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
10831113 if (PathOpt) {
10841114 std::string Path = std::move (*PathOpt);
10851115 StringRef Arch = Arches.size () == 1 ? Arches.front () : StringRef ();
1086- if (Error E = loadFromFile (Path, Arch, CompilationDir, *ProfileReader ,
1087- *Coverage, DataFound))
1116+ if (Error E = loadFromFile (Path, Arch, CompilationDir, ProfileReaderRef ,
1117+ *Coverage, DataFound))
10881118 return std::move (E);
10891119 } else if (CheckBinaryIDs) {
10901120 return createFileError (
1091- ProfileFilename,
1121+ ProfileFilename. value () ,
10921122 createStringError (errc::no_such_file_or_directory,
10931123 " Missing binary ID: " +
10941124 llvm::toHex (BinaryID, /* LowerCase=*/ true )));
0 commit comments