@@ -794,7 +794,8 @@ class MCDCDecisionRecorder {
794794
795795Error CoverageMapping::loadFunctionRecord (
796796 const CoverageMappingRecord &Record,
797- IndexedInstrProfReader &ProfileReader) {
797+ const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
798+ &ProfileReader) {
798799 StringRef OrigFuncName = Record.FunctionName ;
799800 if (OrigFuncName.empty ())
800801 return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -808,35 +809,44 @@ Error CoverageMapping::loadFunctionRecord(
808809 CounterMappingContext Ctx (Record.Expressions );
809810
810811 std::vector<uint64_t > Counts;
811- if (Error E = ProfileReader.getFunctionCounts (Record.FunctionName ,
812- Record.FunctionHash , Counts)) {
813- instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
814- if (IPE == instrprof_error::hash_mismatch) {
815- FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
816- Record.FunctionHash );
817- return Error::success ();
812+ if (ProfileReader) {
813+ if (Error E = ProfileReader.value ().get ().getFunctionCounts (
814+ Record.FunctionName , Record.FunctionHash , Counts)) {
815+ instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
816+ if (IPE == instrprof_error::hash_mismatch) {
817+ FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
818+ Record.FunctionHash );
819+ return Error::success ();
820+ }
821+ if (IPE != instrprof_error::unknown_function)
822+ return make_error<InstrProfError>(IPE);
823+ Counts.assign (getMaxCounterID (Ctx, Record) + 1 , 0 );
818824 }
819- if (IPE != instrprof_error::unknown_function)
820- return make_error<InstrProfError>(IPE);
825+ } else {
821826 Counts.assign (getMaxCounterID (Ctx, Record) + 1 , 0 );
822827 }
823828 Ctx.setCounts (Counts);
824829
825830 bool IsVersion11 =
826- ProfileReader.getVersion () < IndexedInstrProf::ProfVersion::Version12;
831+ ProfileReader && ProfileReader.value ().get ().getVersion () <
832+ IndexedInstrProf::ProfVersion::Version12;
827833
828834 BitVector Bitmap;
829- if (Error E = ProfileReader.getFunctionBitmap (Record.FunctionName ,
830- Record.FunctionHash , Bitmap)) {
831- instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
832- if (IPE == instrprof_error::hash_mismatch) {
833- FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
834- Record.FunctionHash );
835- return Error::success ();
835+ if (ProfileReader) {
836+ if (Error E = ProfileReader.value ().get ().getFunctionBitmap (
837+ Record.FunctionName , Record.FunctionHash , Bitmap)) {
838+ instrprof_error IPE = std::get<0 >(InstrProfError::take (std::move (E)));
839+ if (IPE == instrprof_error::hash_mismatch) {
840+ FuncHashMismatches.emplace_back (std::string (Record.FunctionName ),
841+ Record.FunctionHash );
842+ return Error::success ();
843+ }
844+ if (IPE != instrprof_error::unknown_function)
845+ return make_error<InstrProfError>(IPE);
846+ Bitmap = BitVector (getMaxBitmapSize (Record, IsVersion11));
836847 }
837- if (IPE != instrprof_error::unknown_function)
838- return make_error<InstrProfError>(IPE);
839- Bitmap = BitVector (getMaxBitmapSize (Record, IsVersion11));
848+ } else {
849+ Bitmap = BitVector (getMaxBitmapSize (Record, false ));
840850 }
841851 Ctx.setBitmap (std::move (Bitmap));
842852
@@ -870,8 +880,9 @@ Error CoverageMapping::loadFunctionRecord(
870880 consumeError (std::move (E));
871881 return Error::success ();
872882 }
873- Function.pushRegion (Region, *ExecutionCount, *AltExecutionCount,
874- ProfileReader.hasSingleByteCoverage ());
883+ Function.pushRegion (
884+ Region, *ExecutionCount, *AltExecutionCount,
885+ ProfileReader && ProfileReader.value ().get ().hasSingleByteCoverage ());
875886
876887 // Record ExpansionRegion.
877888 if (Region.Kind == CounterMappingRegion::ExpansionRegion) {
@@ -932,7 +943,9 @@ Error CoverageMapping::loadFunctionRecord(
932943// of CoverageMappingReader instances.
933944Error CoverageMapping::loadFromReaders (
934945 ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
935- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
946+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
947+ &ProfileReader,
948+ CoverageMapping &Coverage) {
936949 for (const auto &CoverageReader : CoverageReaders) {
937950 for (auto RecordOrErr : *CoverageReader) {
938951 if (Error E = RecordOrErr.takeError ())
@@ -947,7 +960,8 @@ Error CoverageMapping::loadFromReaders(
947960
948961Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
949962 ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
950- IndexedInstrProfReader &ProfileReader) {
963+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
964+ &ProfileReader) {
951965 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
952966 if (Error E = loadFromReaders (CoverageReaders, ProfileReader, *Coverage))
953967 return std::move (E);
@@ -956,18 +970,19 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
956970
957971// If E is a no_data_found error, returns success. Otherwise returns E.
958972static Error handleMaybeNoDataFoundError (Error E) {
959- return handleErrors (
960- std::move (E), [](const CoverageMapError &CME) {
961- if (CME.get () == coveragemap_error::no_data_found)
962- return static_cast <Error>(Error::success ());
963- return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
964- });
973+ return handleErrors (std::move (E), [](const CoverageMapError &CME) {
974+ if (CME.get () == coveragemap_error::no_data_found)
975+ return static_cast <Error>(Error::success ());
976+ return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
977+ });
965978}
966979
967980Error CoverageMapping::loadFromFile (
968981 StringRef Filename, StringRef Arch, StringRef CompilationDir,
969- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
970- bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
982+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
983+ &ProfileReader,
984+ CoverageMapping &Coverage, bool &DataFound,
985+ SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
971986 auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN (
972987 Filename, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
973988 if (std::error_code EC = CovMappingBufOrErr.getError ())
@@ -1003,13 +1018,23 @@ Error CoverageMapping::loadFromFile(
10031018}
10041019
10051020Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
1006- ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
1007- vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
1021+ ArrayRef<StringRef> ObjectFilenames,
1022+ std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
1023+ ArrayRef<StringRef> Arches, StringRef CompilationDir,
10081024 const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
1009- auto ProfileReaderOrErr = IndexedInstrProfReader::create (ProfileFilename, FS);
1010- if (Error E = ProfileReaderOrErr.takeError ())
1011- return createFileError (ProfileFilename, std::move (E));
1012- auto ProfileReader = std::move (ProfileReaderOrErr.get ());
1025+ std::unique_ptr<IndexedInstrProfReader> ProfileReader;
1026+ if (ProfileFilename) {
1027+ auto ProfileReaderOrErr =
1028+ IndexedInstrProfReader::create (ProfileFilename.value (), FS);
1029+ if (Error E = ProfileReaderOrErr.takeError ())
1030+ return createFileError (ProfileFilename.value (), std::move (E));
1031+ ProfileReader = std::move (ProfileReaderOrErr.get ());
1032+ }
1033+ auto ProfileReaderRef =
1034+ ProfileReader
1035+ ? std::optional<std::reference_wrapper<IndexedInstrProfReader>>(
1036+ *ProfileReader)
1037+ : std::nullopt ;
10131038 auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
10141039 bool DataFound = false ;
10151040
@@ -1023,16 +1048,17 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
10231048
10241049 SmallVector<object::BuildID> FoundBinaryIDs;
10251050 for (const auto &File : llvm::enumerate (ObjectFilenames)) {
1026- if (Error E =
1027- loadFromFile (File. value (), GetArch (File. index ()), CompilationDir ,
1028- *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
1051+ if (Error E = loadFromFile (File. value (), GetArch (File. index ()),
1052+ CompilationDir, ProfileReaderRef, *Coverage ,
1053+ DataFound, &FoundBinaryIDs))
10291054 return std::move (E);
10301055 }
10311056
10321057 if (BIDFetcher) {
10331058 std::vector<object::BuildID> ProfileBinaryIDs;
1034- if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1035- return createFileError (ProfileFilename, std::move (E));
1059+ if (ProfileReader)
1060+ if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1061+ return createFileError (ProfileFilename.value (), std::move (E));
10361062
10371063 SmallVector<object::BuildIDRef> BinaryIDsToFetch;
10381064 if (!ProfileBinaryIDs.empty ()) {
@@ -1052,12 +1078,12 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
10521078 if (PathOpt) {
10531079 std::string Path = std::move (*PathOpt);
10541080 StringRef Arch = Arches.size () == 1 ? Arches.front () : StringRef ();
1055- if (Error E = loadFromFile (Path, Arch, CompilationDir, *ProfileReader ,
1056- *Coverage, DataFound))
1081+ if (Error E = loadFromFile (Path, Arch, CompilationDir, ProfileReaderRef ,
1082+ *Coverage, DataFound))
10571083 return std::move (E);
10581084 } else if (CheckBinaryIDs) {
10591085 return createFileError (
1060- ProfileFilename,
1086+ ProfileFilename. value () ,
10611087 createStringError (errc::no_such_file_or_directory,
10621088 " Missing binary ID: " +
10631089 llvm::toHex (BinaryID, /* LowerCase=*/ true )));
0 commit comments