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: test/tools/llvm-profdata/Inputs/IR_profile.proftext =================================================================== --- test/tools/llvm-profdata/Inputs/IR_profile.proftext +++ test/tools/llvm-profdata/Inputs/IR_profile.proftext @@ -0,0 +1,9 @@ +:ir +main +# Func Hash: +12884901887 +# Num Counters: +1 +# Counter Values: +1 + Index: test/tools/llvm-profdata/Inputs/clang_profile.proftext =================================================================== --- test/tools/llvm-profdata/Inputs/clang_profile.proftext +++ test/tools/llvm-profdata/Inputs/clang_profile.proftext @@ -0,0 +1,8 @@ +main +# Func Hash: +0 +# Num Counters: +1 +# Counter Values: +1 + Index: test/tools/llvm-profdata/merge_empty_profile.test =================================================================== --- test/tools/llvm-profdata/merge_empty_profile.test +++ test/tools/llvm-profdata/merge_empty_profile.test @@ -0,0 +1,16 @@ +# Tests for merge of empty profile files. + +RUN: llvm-profdata merge -text -o %t_clang.proftext %p/Inputs/empty.proftext %p/Inputs/clang_profile.proftext +RUN: FileCheck --input-file=%t_clang.proftext %s -check-prefix=CLANG_PROF_TEXT +CLANG_PROF_TEXT: main +CLANG_PROF_TEXT: 0 +CLANG_PROF_TEXT: 1 +CLANG_PROF_TEXT: 1 + +RUN: llvm-profdata merge -text -o %t_ir.proftext %p/Inputs/empty.proftext %p/Inputs/IR_profile.proftext +RUN: FileCheck --input-file=%t_ir.proftext %s -check-prefix=IR_PROF_TEXT +IR_PROF_TEXT: :ir +IR_PROF_TEXT: main +IR_PROF_TEXT: 0 +IR_PROF_TEXT: 1 +IR_PROF_TEXT: 1 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(