Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 1 | //===-- HeatUtils.cpp - Utility for printing heat colors --------*- C++ -*-===// |
| 2 | // |
Christopher Di Bella | c874dd5 | 2021-08-05 03:16:17 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // Utility for printing heat colors based on heuristics or profiling |
| 10 | // information. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Analysis/HeatUtils.h" |
Simon Pilgrim | 9b95186 | 2020-04-18 13:36:37 +0100 | [diff] [blame] | 15 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Instructions.h" |
| 17 | |
serge-sans-paille | 16544cb | 2022-09-28 18:39:23 +0200 | [diff] [blame] | 18 | #include <cmath> |
| 19 | |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 20 | namespace llvm { |
| 21 | |
| 22 | static const unsigned heatSize = 100; |
Benjamin Kramer | 547b6da | 2020-06-17 13:00:32 +0200 | [diff] [blame] | 23 | static const char heatPalette[heatSize][8] = { |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 24 | "#3d50c3", "#4055c8", "#4358cb", "#465ecf", "#4961d2", "#4c66d6", "#4f69d9", |
| 25 | "#536edd", "#5572df", "#5977e3", "#5b7ae5", "#5f7fe8", "#6282ea", "#6687ed", |
| 26 | "#6a8bef", "#6c8ff1", "#7093f3", "#7396f5", "#779af7", "#7a9df8", "#7ea1fa", |
| 27 | "#81a4fb", "#85a8fc", "#88abfd", "#8caffe", "#8fb1fe", "#93b5fe", "#96b7ff", |
| 28 | "#9abbff", "#9ebeff", "#a1c0ff", "#a5c3fe", "#a7c5fe", "#abc8fd", "#aec9fc", |
| 29 | "#b2ccfb", "#b5cdfa", "#b9d0f9", "#bbd1f8", "#bfd3f6", "#c1d4f4", "#c5d6f2", |
| 30 | "#c7d7f0", "#cbd8ee", "#cedaeb", "#d1dae9", "#d4dbe6", "#d6dce4", "#d9dce1", |
| 31 | "#dbdcde", "#dedcdb", "#e0dbd8", "#e3d9d3", "#e5d8d1", "#e8d6cc", "#ead5c9", |
| 32 | "#ecd3c5", "#eed0c0", "#efcebd", "#f1ccb8", "#f2cab5", "#f3c7b1", "#f4c5ad", |
| 33 | "#f5c1a9", "#f6bfa6", "#f7bca1", "#f7b99e", "#f7b599", "#f7b396", "#f7af91", |
| 34 | "#f7ac8e", "#f7a889", "#f6a385", "#f5a081", "#f59c7d", "#f4987a", "#f39475", |
| 35 | "#f29072", "#f08b6e", "#ef886b", "#ed8366", "#ec7f63", "#e97a5f", "#e8765c", |
| 36 | "#e57058", "#e36c55", "#e16751", "#de614d", "#dc5d4a", "#d85646", "#d65244", |
| 37 | "#d24b40", "#d0473d", "#cc403a", "#ca3b37", "#c53334", "#c32e31", "#be242e", |
| 38 | "#bb1b2c", "#b70d28"}; |
| 39 | |
Kirill Naumov | 369d00d | 2020-06-16 18:39:54 +0000 | [diff] [blame] | 40 | uint64_t |
| 41 | getNumOfCalls(Function &callerFunction, Function &calledFunction) { |
| 42 | uint64_t counter = 0; |
| 43 | for (User *U : calledFunction.users()) { |
| 44 | if (auto CI = dyn_cast<CallInst>(U)) { |
| 45 | if (CI->getCaller() == (&callerFunction)) { |
| 46 | counter += 1; |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | return counter; |
| 51 | } |
| 52 | |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 53 | uint64_t getMaxFreq(const Function &F, const BlockFrequencyInfo *BFI) { |
| 54 | uint64_t maxFreq = 0; |
| 55 | for (const BasicBlock &BB : F) { |
| 56 | uint64_t freqVal = BFI->getBlockFreq(&BB).getFrequency(); |
| 57 | if (freqVal >= maxFreq) |
| 58 | maxFreq = freqVal; |
| 59 | } |
| 60 | return maxFreq; |
| 61 | } |
| 62 | |
| 63 | std::string getHeatColor(uint64_t freq, uint64_t maxFreq) { |
| 64 | if (freq > maxFreq) |
| 65 | freq = maxFreq; |
Kirill Bobyrev | 3847737 | 2020-06-17 10:52:18 +0200 | [diff] [blame] | 66 | double percent = (freq > 0) ? log2(double(freq)) / log2(maxFreq) : 0; |
Kirill Naumov | 8b67853 | 2020-03-31 18:43:25 +0000 | [diff] [blame] | 67 | return getHeatColor(percent); |
| 68 | } |
| 69 | |
| 70 | std::string getHeatColor(double percent) { |
| 71 | if (percent > 1.0) |
| 72 | percent = 1.0; |
| 73 | if (percent < 0.0) |
| 74 | percent = 0.0; |
| 75 | unsigned colorId = unsigned(round(percent * (heatSize - 1.0))); |
| 76 | return heatPalette[colorId]; |
| 77 | } |
| 78 | |
Simon Pilgrim | 9b95186 | 2020-04-18 13:36:37 +0100 | [diff] [blame] | 79 | } // namespace llvm |