@@ -823,7 +823,8 @@ class MCDCDecisionRecorder {
823
823
824
824
Error CoverageMapping::loadFunctionRecord (
825
825
const CoverageMappingRecord &Record,
826
- IndexedInstrProfReader &ProfileReader) {
826
+ const std::optional<std::reference_wrapper<IndexedInstrProfReader>>
827
+ &ProfileReader) {
827
828
StringRef OrigFuncName = Record.FunctionName ;
828
829
if (OrigFuncName.empty ())
829
830
return make_error<CoverageMapError>(coveragemap_error::malformed,
@@ -837,35 +838,44 @@ Error CoverageMapping::loadFunctionRecord(
837
838
CounterMappingContext Ctx (Record.Expressions );
838
839
839
840
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 );
847
853
}
848
- if (IPE != instrprof_error::unknown_function)
849
- return make_error<InstrProfError>(IPE);
854
+ } else {
850
855
Counts.assign (getMaxCounterID (Ctx, Record) + 1 , 0 );
851
856
}
852
857
Ctx.setCounts (Counts);
853
858
854
859
bool IsVersion11 =
855
- ProfileReader.getVersion () < IndexedInstrProf::ProfVersion::Version12;
860
+ ProfileReader && ProfileReader.value ().get ().getVersion () <
861
+ IndexedInstrProf::ProfVersion::Version12;
856
862
857
863
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));
865
876
}
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 ));
869
879
}
870
880
Ctx.setBitmap (std::move (Bitmap));
871
881
@@ -959,10 +969,14 @@ Error CoverageMapping::loadFunctionRecord(
959
969
// of CoverageMappingReader instances.
960
970
Error CoverageMapping::loadFromReaders (
961
971
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
962
- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
963
- assert (!Coverage.SingleByteCoverage ||
964
- *Coverage.SingleByteCoverage == ProfileReader.hasSingleByteCoverage ());
965
- Coverage.SingleByteCoverage = ProfileReader.hasSingleByteCoverage ();
972
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
973
+ &ProfileReader,
974
+ CoverageMapping &Coverage) {
975
+ assert (!Coverage.SingleByteCoverage || !ProfileReader ||
976
+ *Coverage.SingleByteCoverage ==
977
+ ProfileReader.value ().get ().hasSingleByteCoverage ());
978
+ Coverage.SingleByteCoverage =
979
+ !ProfileReader || ProfileReader.value ().get ().hasSingleByteCoverage ();
966
980
for (const auto &CoverageReader : CoverageReaders) {
967
981
for (auto RecordOrErr : *CoverageReader) {
968
982
if (Error E = RecordOrErr.takeError ())
@@ -977,7 +991,8 @@ Error CoverageMapping::loadFromReaders(
977
991
978
992
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
979
993
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
980
- IndexedInstrProfReader &ProfileReader) {
994
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
995
+ &ProfileReader) {
981
996
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
982
997
if (Error E = loadFromReaders (CoverageReaders, ProfileReader, *Coverage))
983
998
return std::move (E);
@@ -986,18 +1001,19 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
986
1001
987
1002
// If E is a no_data_found error, returns success. Otherwise returns E.
988
1003
static Error handleMaybeNoDataFoundError (Error E) {
989
- return handleErrors (
990
- std::move (E), [](const CoverageMapError &CME) {
991
- if (CME.get () == coveragemap_error::no_data_found)
992
- return static_cast <Error>(Error::success ());
993
- return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
994
- });
1004
+ return handleErrors (std::move (E), [](const CoverageMapError &CME) {
1005
+ if (CME.get () == coveragemap_error::no_data_found)
1006
+ return static_cast <Error>(Error::success ());
1007
+ return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
1008
+ });
995
1009
}
996
1010
997
1011
Error CoverageMapping::loadFromFile (
998
1012
StringRef Filename, StringRef Arch, StringRef CompilationDir,
999
- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
1000
- bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1013
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
1014
+ &ProfileReader,
1015
+ CoverageMapping &Coverage, bool &DataFound,
1016
+ SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1001
1017
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN (
1002
1018
Filename, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
1003
1019
if (std::error_code EC = CovMappingBufOrErr.getError ())
@@ -1033,13 +1049,23 @@ Error CoverageMapping::loadFromFile(
1033
1049
}
1034
1050
1035
1051
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
1036
- ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
1037
- vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
1052
+ ArrayRef<StringRef> ObjectFilenames,
1053
+ std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
1054
+ ArrayRef<StringRef> Arches, StringRef CompilationDir,
1038
1055
const object::BuildIDFetcher *BIDFetcher, bool CheckBinaryIDs) {
1039
- auto ProfileReaderOrErr = IndexedInstrProfReader::create (ProfileFilename, FS);
1040
- if (Error E = ProfileReaderOrErr.takeError ())
1041
- return createFileError (ProfileFilename, std::move (E));
1042
- auto ProfileReader = std::move (ProfileReaderOrErr.get ());
1056
+ std::unique_ptr<IndexedInstrProfReader> ProfileReader;
1057
+ if (ProfileFilename) {
1058
+ auto ProfileReaderOrErr =
1059
+ IndexedInstrProfReader::create (ProfileFilename.value (), FS);
1060
+ if (Error E = ProfileReaderOrErr.takeError ())
1061
+ return createFileError (ProfileFilename.value (), std::move (E));
1062
+ ProfileReader = std::move (ProfileReaderOrErr.get ());
1063
+ }
1064
+ auto ProfileReaderRef =
1065
+ ProfileReader
1066
+ ? std::optional<std::reference_wrapper<IndexedInstrProfReader>>(
1067
+ *ProfileReader)
1068
+ : std::nullopt;
1043
1069
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
1044
1070
bool DataFound = false ;
1045
1071
@@ -1053,16 +1079,17 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1053
1079
1054
1080
SmallVector<object::BuildID> FoundBinaryIDs;
1055
1081
for (const auto &File : llvm::enumerate (ObjectFilenames)) {
1056
- if (Error E =
1057
- loadFromFile (File. value (), GetArch (File. index ()), CompilationDir ,
1058
- *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
1082
+ if (Error E = loadFromFile (File. value (), GetArch (File. index ()),
1083
+ CompilationDir, ProfileReaderRef, *Coverage ,
1084
+ DataFound, &FoundBinaryIDs))
1059
1085
return std::move (E);
1060
1086
}
1061
1087
1062
1088
if (BIDFetcher) {
1063
1089
std::vector<object::BuildID> ProfileBinaryIDs;
1064
- if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1065
- return createFileError (ProfileFilename, std::move (E));
1090
+ if (ProfileReader)
1091
+ if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1092
+ return createFileError (ProfileFilename.value (), std::move (E));
1066
1093
1067
1094
SmallVector<object::BuildIDRef> BinaryIDsToFetch;
1068
1095
if (!ProfileBinaryIDs.empty ()) {
@@ -1082,12 +1109,12 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1082
1109
if (PathOpt) {
1083
1110
std::string Path = std::move (*PathOpt);
1084
1111
StringRef Arch = Arches.size () == 1 ? Arches.front () : StringRef ();
1085
- if (Error E = loadFromFile (Path, Arch, CompilationDir, *ProfileReader ,
1086
- *Coverage, DataFound))
1112
+ if (Error E = loadFromFile (Path, Arch, CompilationDir, ProfileReaderRef ,
1113
+ *Coverage, DataFound))
1087
1114
return std::move (E);
1088
1115
} else if (CheckBinaryIDs) {
1089
1116
return createFileError (
1090
- ProfileFilename,
1117
+ ProfileFilename. value () ,
1091
1118
createStringError (errc::no_such_file_or_directory,
1092
1119
" Missing binary ID: " +
1093
1120
llvm::toHex (BinaryID, /* LowerCase=*/ true )));
0 commit comments