diff --git a/llvm/lib/ProfileData/SampleProfWriter.cpp b/llvm/lib/ProfileData/SampleProfWriter.cpp --- a/llvm/lib/ProfileData/SampleProfWriter.cpp +++ b/llvm/lib/ProfileData/SampleProfWriter.cpp @@ -125,6 +125,9 @@ LLVM_DEBUG(dbgs() << "Profile originally has " << OriginalFunctionCount << " functions, reduced to " << ProfileMap.size() << " in " << IterationCount << " iterations\n"); + // Silence warning on Release build. + (void)OriginalFunctionCount; + (void)IterationCount; return sampleprof_error::success; } diff --git a/llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp b/llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp --- a/llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp +++ b/llvm/unittests/tools/llvm-profdata/OutputSizeLimitTest.cpp @@ -33,15 +33,17 @@ _Z3fooi:7711:610 1: 610)"; +static void CheckAssertFalse(std::error_code t) { + ASSERT_FALSE(t) << t.message().c_str(); +} + static SampleProfileMap ReadInput(StringRef Input) { LLVMContext Context; auto InputBuffer = MemoryBuffer::getMemBufferCopy(Input); auto ReaderOrErr = SampleProfileReader::create(InputBuffer, Context); - if (std::error_code EC = ReaderOrErr.getError()) - report_fatal_error(EC.message().c_str()); + CheckAssertFalse(ReaderOrErr.getError()); auto Reader = std::move(ReaderOrErr.get()); - if (std::error_code EC = Reader->read()) - report_fatal_error(EC.message().c_str()); + CheckAssertFalse(Reader->read()); return Reader->getProfiles(); } @@ -51,22 +53,26 @@ new raw_svector_ostream(OutputBuffer)); auto WriterOrErr = SampleProfileWriter::create( BufferStream, llvm::sampleprof::SPF_Ext_Binary); - if (std::error_code EC = WriterOrErr.getError()) - report_fatal_error(EC.message().c_str()); + CheckAssertFalse(WriterOrErr.getError()); return std::move(WriterOrErr.get()); } -// Only returns the actual size of the written profile. +// Returns the actual size of the written profile. static size_t WriteProfile(StringRef Input, size_t SizeLimit) { SampleProfileMap Profiles = ReadInput(Input); SmallVector OutputBuffer; auto Writer = CreateWriter(OutputBuffer); - if (std::error_code EC = Writer->writeWithSizeLimit(Profiles, SizeLimit)) - report_fatal_error(EC.message().c_str()); + std::error_code EC = Writer->writeWithSizeLimit(Profiles, SizeLimit); + // too_large means no sample could be written because SizeLimit is too small. + // Otherwise any other error code indicates unexpected failure. + if (EC == sampleprof_error::too_large) + return 0; + else + CheckAssertFalse(EC); return OutputBuffer.size(); } TEST(TestOutputSizeLimit, TestOutputSizeLimit1) { - for (size_t OutputSizeLimit : {489, 488, 474}) - ASSERT_LE(WriteProfile(Input1, OutputSizeLimit), OutputSizeLimit); + for (size_t OutputSizeLimit : {489, 488, 474, 400}) + EXPECT_LE(WriteProfile(Input1, OutputSizeLimit), OutputSizeLimit); }