From f415d74d1df3995795c889333d493b0dcc31a863 Mon Sep 17 00:00:00 2001 From: minglotus-6 Date: Mon, 21 Feb 2022 19:17:06 -0800 Subject: [PATCH] [SampleProfile] Handle the case when the option `MaxNumPromotions` is zero. In places where `MaxNumPromotions` is used to allocated an array, bail out early to prevent allocating an array of length 0. Differential Revision: https://reviews.llvm.org/D120295 --- llvm/lib/Transforms/IPO/SampleProfile.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/llvm/lib/Transforms/IPO/SampleProfile.cpp b/llvm/lib/Transforms/IPO/SampleProfile.cpp index fc0b6f5991a1..5985281019b0 100644 --- a/llvm/lib/Transforms/IPO/SampleProfile.cpp +++ b/llvm/lib/Transforms/IPO/SampleProfile.cpp @@ -841,6 +841,13 @@ static void updateIDTMetaData(Instruction &Inst, const SmallVectorImpl &CallTargets, uint64_t Sum) { + // Bail out early if MaxNumPromotions is zero. + // This prevents allocating an array of zero length below. + // + // Note `updateIDTMetaData` is called in two places so check + // `MaxNumPromotions` inside it. + if (MaxNumPromotions == 0) + return; uint32_t NumVals = 0; // OldSum is the existing total count in the value profile data. uint64_t OldSum = 0; @@ -924,6 +931,10 @@ updateIDTMetaData(Instruction &Inst, bool SampleProfileLoader::tryPromoteAndInlineCandidate( Function &F, InlineCandidate &Candidate, uint64_t SumOrigin, uint64_t &Sum, SmallVector *InlinedCallSite) { + // Bail out early if MaxNumPromotions is zero. + // This prevents allocating an array of zero length in callees below. + if (MaxNumPromotions == 0) + return false; auto CalleeFunctionName = Candidate.CalleeSamples->getFuncName(); auto R = SymbolMap.find(CalleeFunctionName); if (R == SymbolMap.end() || !R->getValue())