Index: include/llvm/IR/ModuleSummaryIndex.h =================================================================== --- include/llvm/IR/ModuleSummaryIndex.h +++ include/llvm/IR/ModuleSummaryIndex.h @@ -26,6 +26,7 @@ #include "llvm/IR/GlobalValue.h" #include "llvm/IR/Module.h" #include "llvm/Support/MathExtras.h" +#include "llvm/Support/ScaledNumber.h" #include #include #include @@ -60,6 +61,7 @@ // added to HotnessType enum. uint32_t Hotness : 3; uint32_t RelBlockFreq : 29; + static constexpr int32_t ScaleShift = 8; static constexpr uint64_t MaxRelBlockFreq = (1 << 29) - 1; CalleeInfo() @@ -73,10 +75,18 @@ HotnessType getHotness() const { return HotnessType(Hotness); } - // When there are multiple edges between the same (caller, callee) pair, the - // relative block frequencies are summed up. - void updateRelBlockFreq(uint64_t RBF) { - uint64_t Sum = SaturatingAdd(RelBlockFreq, RBF); + /// Update \p RelBlockFreq from \p BlockFreq and \p EntryFreq + /// + /// BlockFreq is divided by EntryFreq and added to RelBlockFreq. To represent + /// fractional values, the result is represented as a fixed point number with + /// scale of -ScaleShift. + void updateRelBlockFreq(uint64_t BlockFreq, uint64_t EntryFreq) { + using Scaled64 = ScaledNumber; + Scaled64 Temp(BlockFreq, ScaleShift); + Temp /= Scaled64::get(EntryFreq); + + uint64_t Sum = + SaturatingAdd(Temp.toInt(), RelBlockFreq); Sum = std::min(Sum, uint64_t(MaxRelBlockFreq)); RelBlockFreq = static_cast(Sum); } Index: lib/Analysis/ModuleSummaryAnalysis.cpp =================================================================== --- lib/Analysis/ModuleSummaryAnalysis.cpp +++ lib/Analysis/ModuleSummaryAnalysis.cpp @@ -279,17 +279,9 @@ // Add the relative block frequency to CalleeInfo if there is no profile // information. if (BFI != nullptr && Hotness == CalleeInfo::HotnessType::Unknown) { - auto BBFreq = BFI->getBlockFreq(&BB).getFrequency(); - // FIXME: This might need some scaling to prevent BBFreq values from - // being rounded down to 0. - auto EntryFreq = BFI->getEntryFreq(); - // Block frequencies can be directly set for a block and so we need to - // handle the case of entry frequency being 0. - if (EntryFreq) - BBFreq /= EntryFreq; - else - BBFreq = 0; - ValueInfo.updateRelBlockFreq(BBFreq); + uint64_t BBFreq = BFI->getBlockFreq(&BB).getFrequency(); + uint64_t EntryFreq = BFI->getEntryFreq(); + ValueInfo.updateRelBlockFreq(BBFreq, EntryFreq); } } else { // Skip inline assembly calls. Index: test/Bitcode/thinlto-function-summary-callgraph-relbf.ll =================================================================== --- test/Bitcode/thinlto-function-summary-callgraph-relbf.ll +++ test/Bitcode/thinlto-function-summary-callgraph-relbf.ll @@ -11,7 +11,7 @@ ; CHECK: ; CHECK: