Index: include/llvm/ProfileData/InstrProfReader.h =================================================================== --- include/llvm/ProfileData/InstrProfReader.h +++ include/llvm/ProfileData/InstrProfReader.h @@ -104,7 +104,7 @@ } /// Factory method to create an appropriately typed reader for the given - /// instrprof file. + /// instrprof file. Nullptr will be returned for an empty profile file. static Expected> create(const Twine &Path); static Expected> Index: lib/ProfileData/InstrProfReader.cpp =================================================================== --- lib/ProfileData/InstrProfReader.cpp +++ lib/ProfileData/InstrProfReader.cpp @@ -46,6 +46,10 @@ if (Buffer->getBufferSize() > std::numeric_limits::max()) return make_error(instrprof_error::too_large); + // Return nullptr for an empty file. + if (Buffer->getBufferSize() == 0) + return std::unique_ptr{}; + std::unique_ptr Result; // Create the reader. if (IndexedInstrProfReader::hasFormat(*Buffer)) Index: tools/llvm-profdata/llvm-profdata.cpp =================================================================== --- tools/llvm-profdata/llvm-profdata.cpp +++ tools/llvm-profdata/llvm-profdata.cpp @@ -144,6 +144,9 @@ return; auto Reader = std::move(ReaderOrErr.get()); + // Nullptr Reader means the file is empty. Skip it. + if (!Reader) + return; bool IsIRProfile = Reader->isIRLevelProfile(); if (WC->Writer.setIsIRLevelProfile(IsIRProfile)) { WC->Err = make_error( @@ -153,6 +156,7 @@ } for (auto &I : *Reader) { + const StringRef FuncName = I.Name; if (Error E = WC->Writer.addRecord(std::move(I), Input.Weight)) { // Only show hint the first time an error occurs. instrprof_error IPE = InstrProfError::take(std::move(E)); @@ -159,7 +163,7 @@ std::unique_lock ErrGuard{WC->ErrLock}; bool firstTime = WC->WriterErrorCodes.insert(IPE).second; handleMergeWriterError(make_error(IPE), Input.Filename, - I.Name, firstTime); + FuncName, firstTime); } } if (Reader->hasError())