@@ -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,15 @@ 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) {
964
- assert (!Coverage.SingleByteCoverage ||
965
- *Coverage.SingleByteCoverage == ProfileReader.hasSingleByteCoverage ());
966
- Coverage.SingleByteCoverage = ProfileReader.hasSingleByteCoverage ();
973
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
974
+ &ProfileReader,
975
+ CoverageMapping &Coverage) {
976
+ assert (!Coverage.SingleByteCoverage || !ProfileReader ||
977
+ *Coverage.SingleByteCoverage ==
978
+ ProfileReader.value ().get ().hasSingleByteCoverage ());
979
+ Coverage.SingleByteCoverage =
980
+ ProfileReader ? ProfileReader.value ().get ().hasSingleByteCoverage ()
981
+ : true ;
967
982
for (const auto &CoverageReader : CoverageReaders) {
968
983
for (auto RecordOrErr : *CoverageReader) {
969
984
if (Error E = RecordOrErr.takeError ())
@@ -978,7 +993,8 @@ Error CoverageMapping::loadFromReaders(
978
993
979
994
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
980
995
ArrayRef<std::unique_ptr<CoverageMappingReader>> CoverageReaders,
981
- IndexedInstrProfReader &ProfileReader) {
996
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
997
+ &ProfileReader) {
982
998
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
983
999
if (Error E = loadFromReaders (CoverageReaders, ProfileReader, *Coverage))
984
1000
return std::move (E);
@@ -987,18 +1003,19 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
987
1003
988
1004
// If E is a no_data_found error, returns success. Otherwise returns E.
989
1005
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
- });
1006
+ return handleErrors (std::move (E), [](const CoverageMapError &CME) {
1007
+ if (CME.get () == coveragemap_error::no_data_found)
1008
+ return static_cast <Error>(Error::success ());
1009
+ return make_error<CoverageMapError>(CME.get (), CME.getMessage ());
1010
+ });
996
1011
}
997
1012
998
1013
Error CoverageMapping::loadFromFile (
999
1014
StringRef Filename, StringRef Arch, StringRef CompilationDir,
1000
- IndexedInstrProfReader &ProfileReader, CoverageMapping &Coverage,
1001
- bool &DataFound, SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1015
+ std::optional<std::reference_wrapper<IndexedInstrProfReader>>
1016
+ &ProfileReader,
1017
+ CoverageMapping &Coverage, bool &DataFound,
1018
+ SmallVectorImpl<object::BuildID> *FoundBinaryIDs) {
1002
1019
auto CovMappingBufOrErr = MemoryBuffer::getFileOrSTDIN (
1003
1020
Filename, /* IsText=*/ false , /* RequiresNullTerminator=*/ false );
1004
1021
if (std::error_code EC = CovMappingBufOrErr.getError ())
@@ -1034,13 +1051,23 @@ Error CoverageMapping::loadFromFile(
1034
1051
}
1035
1052
1036
1053
Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load (
1037
- ArrayRef<StringRef> ObjectFilenames, StringRef ProfileFilename,
1038
- vfs::FileSystem &FS, ArrayRef<StringRef> Arches, StringRef CompilationDir,
1054
+ ArrayRef<StringRef> ObjectFilenames,
1055
+ std::optional<StringRef> ProfileFilename, vfs::FileSystem &FS,
1056
+ ArrayRef<StringRef> Arches, StringRef CompilationDir,
1039
1057
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 ());
1058
+ std::unique_ptr<IndexedInstrProfReader> ProfileReader;
1059
+ if (ProfileFilename) {
1060
+ auto ProfileReaderOrErr =
1061
+ IndexedInstrProfReader::create (ProfileFilename.value (), FS);
1062
+ if (Error E = ProfileReaderOrErr.takeError ())
1063
+ return createFileError (ProfileFilename.value (), std::move (E));
1064
+ ProfileReader = std::move (ProfileReaderOrErr.get ());
1065
+ }
1066
+ auto ProfileReaderRef =
1067
+ ProfileReader
1068
+ ? std::optional<std::reference_wrapper<IndexedInstrProfReader>>(
1069
+ *ProfileReader)
1070
+ : std::nullopt;
1044
1071
auto Coverage = std::unique_ptr<CoverageMapping>(new CoverageMapping ());
1045
1072
bool DataFound = false ;
1046
1073
@@ -1054,16 +1081,17 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1054
1081
1055
1082
SmallVector<object::BuildID> FoundBinaryIDs;
1056
1083
for (const auto &File : llvm::enumerate (ObjectFilenames)) {
1057
- if (Error E =
1058
- loadFromFile (File. value (), GetArch (File. index ()), CompilationDir ,
1059
- *ProfileReader, *Coverage, DataFound, &FoundBinaryIDs))
1084
+ if (Error E = loadFromFile (File. value (), GetArch (File. index ()),
1085
+ CompilationDir, ProfileReaderRef, *Coverage ,
1086
+ DataFound, &FoundBinaryIDs))
1060
1087
return std::move (E);
1061
1088
}
1062
1089
1063
1090
if (BIDFetcher) {
1064
1091
std::vector<object::BuildID> ProfileBinaryIDs;
1065
- if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1066
- return createFileError (ProfileFilename, std::move (E));
1092
+ if (ProfileReader)
1093
+ if (Error E = ProfileReader->readBinaryIds (ProfileBinaryIDs))
1094
+ return createFileError (ProfileFilename.value (), std::move (E));
1067
1095
1068
1096
SmallVector<object::BuildIDRef> BinaryIDsToFetch;
1069
1097
if (!ProfileBinaryIDs.empty ()) {
@@ -1083,12 +1111,12 @@ Expected<std::unique_ptr<CoverageMapping>> CoverageMapping::load(
1083
1111
if (PathOpt) {
1084
1112
std::string Path = std::move (*PathOpt);
1085
1113
StringRef Arch = Arches.size () == 1 ? Arches.front () : StringRef ();
1086
- if (Error E = loadFromFile (Path, Arch, CompilationDir, *ProfileReader ,
1087
- *Coverage, DataFound))
1114
+ if (Error E = loadFromFile (Path, Arch, CompilationDir, ProfileReaderRef ,
1115
+ *Coverage, DataFound))
1088
1116
return std::move (E);
1089
1117
} else if (CheckBinaryIDs) {
1090
1118
return createFileError (
1091
- ProfileFilename,
1119
+ ProfileFilename. value () ,
1092
1120
createStringError (errc::no_such_file_or_directory,
1093
1121
" Missing binary ID: " +
1094
1122
llvm::toHex (BinaryID, /* LowerCase=*/ true )));
0 commit comments