@@ -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
@@ -960,10 +970,17 @@ Error CoverageMapping::loadFunctionRecord(
960
970
// of CoverageMappingReader instances.
961
971
Error CoverageMapping::loadFromReaders (
962
972
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
963
- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage) {
973
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
974
+ &ProfileReader,
975
+ CoverageMapping &Coverage) {
964
976
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
+ }
967
984
for (const auto &CoverageReader : CoverageReaders) {
968
985
for (auto RecordOrErr : *CoverageReader) {
969
986
if (Error E = RecordOrErr.takeError ())
@@ -978,7 +995,8 @@ Error CoverageMapping::loadFromReaders(
978
995
979
996
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
980
997
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
981
- IndexedInstrProfReader &ProfileReader) {
998
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
999
+ &ProfileReader) {
982
1000
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
983
1001
if (Error E = loadFromReaders (CoverageReaders, ProfileReader, *Coverage))
984
1002
return std::move (E);
@@ -987,18 +1005,19 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
987
1005
988
1006
// If E is a no_data_found error, returns success. Otherwise returns E.
989
1007
static 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
+ });
996
1013
}
997
1014
998
1015
Error CoverageMapping::loadFromFile (
999
1016
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) {
1002
1021
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN (
1003
1022
Filename, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
1004
1023
if (std::error_code EC = CovMappingBufOrErr.getError ())
@@ -1034,13 +1053,23 @@ Error CoverageMapping::loadFromFile(
1034
1053
}
1035
1054
1036
1055
Expected<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,
1039
1059
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;
1044
1073
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
1045
1074
bool DataFound = false ;
1046
1075
@@ -1054,16 +1083,17 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1054
1083
1055
1084
SmallVector<object::BuildID> FoundBinaryIDs;
1056
1085
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))
1060
1089
return std::move (E);
1061
1090
}
1062
1091
1063
1092
if (BIDFetcher) {
1064
1093
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));
1067
1097
1068
1098
SmallVector<object::BuildIDRef> BinaryIDsToFetch;
1069
1099
if (!ProfileBinaryIDs.empty ()) {
@@ -1083,12 +1113,12 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1083
1113
if (PathOpt) {
1084
1114
std::string Path = std::move (*PathOpt);
1085
1115
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))
1088
1118
return std::move (E);
1089
1119
} else if (CheckBinaryIDs) {
1090
1120
return createFileError (
1091
- ProfileFilename,
1121
+ ProfileFilename. value () ,
1092
1122
createStringError (errc::no_such_file_or_directory,
1093
1123
" Missing binary ID: " +
1094
1124
llvm::toHex (BinaryID, /* LowerCase=*/ true )));
0 commit comments