diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -1948,7 +1948,12 @@ Reader = std::move(ReaderOrErr.get()); Reader->setSkipFlatProf(LTOPhase == ThinOrFullLTOPhase::ThinLTOPostLink); Reader->collectFuncsFrom(M); - ProfileIsValid = (Reader->read() == sampleprof_error::success); + if (std::error_code EC = Reader->read()) { + std::string Msg = "profile reading failed: " + EC.message(); + Ctx.diagnose(DiagnosticInfoSampleProfile(Filename, Msg)); + return false; + } + PSL = Reader->getProfileSymbolList(); // While profile-sample-accurate is on, ignore symbol list. @@ -2001,8 +2006,6 @@ bool SampleProfileLoader::runOnModule(Module &M, ModuleAnalysisManager *AM, ProfileSummaryInfo *_PSI, CallGraph *CG) { - if (!ProfileIsValid) - return false; GUIDToFuncNameMapper Mapper(M, *Reader, GUIDToFuncNameMap); PSI = _PSI; diff --git a/llvm/test/Transforms/SampleProfile/Inputs/bad.extbinary.afdo b/llvm/test/Transforms/SampleProfile/Inputs/bad.extbinary.afdo new file mode 100644 index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 GIT binary patch literal 0 Hc$@&1 | FileCheck %s -check-prefix=BAD-PROFILE +; RUN: not opt < %s -passes=sample-profile -sample-profile-file=%S/Inputs/bad.extbinary.afdo -S 2>&1 | FileCheck %s -check-prefix=BAD-PROFILE ; Original C++ test case ; @@ -38,6 +40,9 @@ ; CHECK: ![[IDX2]] = !{!"branch_weights", i32 5280, i32 113} ; CHECK: ![[IDX3]] = !{!"branch_weights", i32 1} +; Check sample-profile phase will report error when it is reading a bad profile. +; BAD-PROFILE: error: {{.*}}bad.extbinary.afdo: profile reading failed: Malformed sample profile data + ; Function Attrs: nounwind uwtable define i32 @_Z3sumii(i32 %x, i32 %y) #0 !dbg !4 { entry: