LLVM  14.0.0git
HeatUtils.cpp
Go to the documentation of this file.
1 //===-- HeatUtils.cpp - Utility for printing heat colors --------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Utility for printing heat colors based on heuristics or profiling
10 // information.
11 //
12 //===----------------------------------------------------------------------===//
13 
16 #include "llvm/IR/Instructions.h"
17 
18 namespace llvm {
19 
20 static const unsigned heatSize = 100;
21 static const char heatPalette[heatSize][8] = {
22  "#3d50c3", "#4055c8", "#4358cb", "#465ecf", "#4961d2", "#4c66d6", "#4f69d9",
23  "#536edd", "#5572df", "#5977e3", "#5b7ae5", "#5f7fe8", "#6282ea", "#6687ed",
24  "#6a8bef", "#6c8ff1", "#7093f3", "#7396f5", "#779af7", "#7a9df8", "#7ea1fa",
25  "#81a4fb", "#85a8fc", "#88abfd", "#8caffe", "#8fb1fe", "#93b5fe", "#96b7ff",
26  "#9abbff", "#9ebeff", "#a1c0ff", "#a5c3fe", "#a7c5fe", "#abc8fd", "#aec9fc",
27  "#b2ccfb", "#b5cdfa", "#b9d0f9", "#bbd1f8", "#bfd3f6", "#c1d4f4", "#c5d6f2",
28  "#c7d7f0", "#cbd8ee", "#cedaeb", "#d1dae9", "#d4dbe6", "#d6dce4", "#d9dce1",
29  "#dbdcde", "#dedcdb", "#e0dbd8", "#e3d9d3", "#e5d8d1", "#e8d6cc", "#ead5c9",
30  "#ecd3c5", "#eed0c0", "#efcebd", "#f1ccb8", "#f2cab5", "#f3c7b1", "#f4c5ad",
31  "#f5c1a9", "#f6bfa6", "#f7bca1", "#f7b99e", "#f7b599", "#f7b396", "#f7af91",
32  "#f7ac8e", "#f7a889", "#f6a385", "#f5a081", "#f59c7d", "#f4987a", "#f39475",
33  "#f29072", "#f08b6e", "#ef886b", "#ed8366", "#ec7f63", "#e97a5f", "#e8765c",
34  "#e57058", "#e36c55", "#e16751", "#de614d", "#dc5d4a", "#d85646", "#d65244",
35  "#d24b40", "#d0473d", "#cc403a", "#ca3b37", "#c53334", "#c32e31", "#be242e",
36  "#bb1b2c", "#b70d28"};
37 
39 getNumOfCalls(Function &callerFunction, Function &calledFunction) {
40  uint64_t counter = 0;
41  for (User *U : calledFunction.users()) {
42  if (auto CI = dyn_cast<CallInst>(U)) {
43  if (CI->getCaller() == (&callerFunction)) {
44  counter += 1;
45  }
46  }
47  }
48  return counter;
49 }
50 
52  uint64_t maxFreq = 0;
53  for (const BasicBlock &BB : F) {
54  uint64_t freqVal = BFI->getBlockFreq(&BB).getFrequency();
55  if (freqVal >= maxFreq)
56  maxFreq = freqVal;
57  }
58  return maxFreq;
59 }
60 
61 std::string getHeatColor(uint64_t freq, uint64_t maxFreq) {
62  if (freq > maxFreq)
63  freq = maxFreq;
64  double percent = (freq > 0) ? log2(double(freq)) / log2(maxFreq) : 0;
65  return getHeatColor(percent);
66 }
67 
68 std::string getHeatColor(double percent) {
69  if (percent > 1.0)
70  percent = 1.0;
71  if (percent < 0.0)
72  percent = 0.0;
73  unsigned colorId = unsigned(round(percent * (heatSize - 1.0)));
74  return heatPalette[colorId];
75 }
76 
77 } // namespace llvm
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::Function
Definition: Function.h:62
llvm::getMaxFreq
uint64_t getMaxFreq(const Function &F, const BlockFrequencyInfo *BFI)
Definition: HeatUtils.cpp:51
freq
block freq
Definition: BlockFrequencyInfo.cpp:301
llvm::getHeatColor
std::string getHeatColor(uint64_t freq, uint64_t maxFreq)
Definition: HeatUtils.cpp:61
F
#define F(x, y, z)
Definition: MD5.cpp:56
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:58
llvm::heatPalette
static const char heatPalette[heatSize][8]
Definition: HeatUtils.cpp:21
llvm::BlockFrequencyInfo
BlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate IR basic block frequen...
Definition: BlockFrequencyInfo.h:37
llvm::User
Definition: User.h:44
round
static uint64_t round(uint64_t Acc, uint64_t Input)
Definition: xxhash.cpp:57
uint64_t
HeatUtils.h
BlockFrequencyInfo.h
llvm::AMDGPUISD::BFI
@ BFI
Definition: AMDGPUISelLowering.h:421
llvm::log2
static double log2(double V)
Definition: AMDGPULibCalls.cpp:842
Instructions.h
llvm::heatSize
static const unsigned heatSize
Definition: HeatUtils.cpp:20
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::Value::users
iterator_range< user_iterator > users()
Definition: Value.h:421
llvm::getNumOfCalls
uint64_t getNumOfCalls(Function &callerFunction, Function &calledFunction)
Definition: HeatUtils.cpp:39