@@ -228,26 +228,32 @@ static MDNode *createMIBNode(LLVMContext &Ctx, ArrayRef<uint64_t> MIBCallStack,
228228 {buildCallstackMetadata (MIBCallStack, Ctx)});
229229 MIBPayload.push_back (
230230 MDString::get (Ctx, getAllocTypeAttributeString (AllocType)));
231- if (!ContextSizeInfo.empty ()) {
232- for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
233- TotalBytes += TotalSize;
234- if (AllocType == AllocationType::Cold)
235- ColdBytes += TotalSize;
236- // Only add the context size info as metadata if we need it in the thin
237- // link (currently if reporting of hinted sizes is enabled or we have
238- // specified a threshold for marking allocations cold after cloning).
239- if (MemProfReportHintedSizes || MinClonedColdBytePercent < 100 ) {
240- auto *FullStackIdMD = ValueAsMetadata::get (
241- ConstantInt::get (Type::getInt64Ty (Ctx), FullStackId));
242- auto *TotalSizeMD = ValueAsMetadata::get (
243- ConstantInt::get (Type::getInt64Ty (Ctx), TotalSize));
244- auto *ContextSizeMD = MDNode::get (Ctx, {FullStackIdMD, TotalSizeMD});
245- MIBPayload.push_back (ContextSizeMD);
246- }
231+
232+ if (ContextSizeInfo.empty ()) {
233+ // The profile matcher should have provided context size info if there was a
234+ // MinCallsiteColdBytePercent < 100. Here we check >=100 to gracefully
235+ // handle a user-provided percent larger than 100.
236+ assert (MinCallsiteColdBytePercent >= 100 );
237+ return MDNode::get (Ctx, MIBPayload);
238+ }
239+
240+ for (const auto &[FullStackId, TotalSize] : ContextSizeInfo) {
241+ TotalBytes += TotalSize;
242+ if (AllocType == AllocationType::Cold)
243+ ColdBytes += TotalSize;
244+ // Only add the context size info as metadata if we need it in the thin
245+ // link (currently if reporting of hinted sizes is enabled or we have
246+ // specified a threshold for marking allocations cold after cloning).
247+ if (MemProfReportHintedSizes || MinClonedColdBytePercent < 100 ) {
248+ auto *FullStackIdMD = ValueAsMetadata::get (
249+ ConstantInt::get (Type::getInt64Ty (Ctx), FullStackId));
250+ auto *TotalSizeMD = ValueAsMetadata::get (
251+ ConstantInt::get (Type::getInt64Ty (Ctx), TotalSize));
252+ auto *ContextSizeMD = MDNode::get (Ctx, {FullStackIdMD, TotalSizeMD});
253+ MIBPayload.push_back (ContextSizeMD);
247254 }
248255 }
249- assert (MinCallsiteColdBytePercent >= 100 ||
250- (!ContextSizeInfo.empty () && TotalBytes > 0 ));
256+ assert (TotalBytes > 0 );
251257 return MDNode::get (Ctx, MIBPayload);
252258}
253259
@@ -273,8 +279,9 @@ static void saveFilteredNewMIBNodes(std::vector<Metadata *> &NewMIBNodes,
273279 std::vector<Metadata *> &SavedMIBNodes,
274280 unsigned CallerContextLength,
275281 uint64_t TotalBytes, uint64_t ColdBytes) {
276- bool MostlyCold = MinCallsiteColdBytePercent < 100 &&
277- ColdBytes * 100 >= MinCallsiteColdBytePercent * TotalBytes;
282+ const bool MostlyCold =
283+ MinCallsiteColdBytePercent < 100 &&
284+ ColdBytes * 100 >= MinCallsiteColdBytePercent * TotalBytes;
278285
279286 // In the simplest case, with pruning disabled, keep all the new MIB nodes.
280287 if (MemProfKeepAllNotColdContexts && !MostlyCold) {
@@ -300,6 +307,9 @@ static void saveFilteredNewMIBNodes(std::vector<Metadata *> &NewMIBNodes,
300307 }
301308 };
302309
310+ // If the cold bytes at the current callsite exceed the given threshold, we
311+ // discard all non-cold contexts so do not need any of the later pruning
312+ // handling. We can simply copy over all the cold contexts and return early.
303313 if (MostlyCold) {
304314 auto NewColdMIBNodes =
305315 make_filter_range (NewMIBNodes, [&](const Metadata *M) {
@@ -308,7 +318,7 @@ static void saveFilteredNewMIBNodes(std::vector<Metadata *> &NewMIBNodes,
308318 if (getMIBAllocType (MIBMD) == AllocationType::Cold)
309319 return true ;
310320 if (MemProfReportHintedSizes) {
311- float PercentCold = ColdBytes * 100.0 / TotalBytes;
321+ const float PercentCold = ColdBytes * 100.0 / TotalBytes;
312322 std::string PercentStr;
313323 llvm::raw_string_ostream OS (PercentStr);
314324 OS << format (" for %5.2f%% cold bytes" , PercentCold);
0 commit comments