@@ -173,6 +173,10 @@ static cl::opt<std::string>
173173
174174extern cl::opt<bool > MemProfReportHintedSizes;
175175
176+ static cl::opt<unsigned > MinMatchedColdBytePercent (
177+ " memprof-matching-cold-threshold" , cl::init(100 ), cl::Hidden,
178+ cl::desc(" Min percent of cold bytes matched to hint allocation cold" ));
179+
176180// Instrumentation statistics
177181STATISTIC (NumInstrumentedReads, " Number of instrumented reads" );
178182STATISTIC (NumInstrumentedWrites, " Number of instrumented writes" );
@@ -1074,6 +1078,8 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
10741078 // contexts. Add them to a Trie specialized for trimming the contexts to
10751079 // the minimal needed to disambiguate contexts with unique behavior.
10761080 CallStackTrie AllocTrie;
1081+ uint64_t TotalSize = 0 ;
1082+ uint64_t TotalColdSize = 0 ;
10771083 for (auto *AllocInfo : AllocInfoIter->second ) {
10781084 // Check the full inlined call stack against this one.
10791085 // If we found and thus matched all frames on the call, include
@@ -1085,6 +1091,9 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
10851091 if (ClPrintMemProfMatchInfo || MemProfReportHintedSizes)
10861092 FullStackId = computeFullStackId (AllocInfo->CallStack );
10871093 auto AllocType = addCallStack (AllocTrie, AllocInfo, FullStackId);
1094+ TotalSize += AllocInfo->Info .getTotalSize ();
1095+ if (AllocType == AllocationType::Cold)
1096+ TotalColdSize += AllocInfo->Info .getTotalSize ();
10881097 // Record information about the allocation if match info printing
10891098 // was requested.
10901099 if (ClPrintMemProfMatchInfo) {
@@ -1094,6 +1103,16 @@ readMemprof(Module &M, Function &F, IndexedInstrProfReader *MemProfReader,
10941103 }
10951104 }
10961105 }
1106+ // If the threshold for the percent of cold bytes is less than 100%,
1107+ // and not all bytes are cold, see if we should still hint this
1108+ // allocation as cold without context sensitivity.
1109+ if (TotalColdSize < TotalSize && MinMatchedColdBytePercent < 100 &&
1110+ TotalColdSize * 100 >= MinMatchedColdBytePercent * TotalSize) {
1111+ AllocTrie.addSingleAllocTypeAttribute (CI, AllocationType::Cold,
1112+ " dominant" );
1113+ continue ;
1114+ }
1115+
10971116 // We might not have matched any to the full inlined call stack.
10981117 // But if we did, create and attach metadata, or a function attribute if
10991118 // all contexts have identical profiled behavior.
0 commit comments