LLVM 24.0.0git
RegAllocScore.cpp
Go to the documentation of this file.
1//===- RegAllocScore.cpp - evaluate regalloc policy quality ---------------===//
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/// Calculate a measure of the register allocation policy quality. This is used
9/// to construct a reward for the training of the ML-driven allocation policy.
10/// Currently, the score is the sum of the machine basic block frequency-weighed
11/// number of loads, stores, copies, and remat instructions, each factored with
12/// a relative weight.
13//===----------------------------------------------------------------------===//
14
15#include "RegAllocScore.h"
22#include "llvm/MC/MCInstrDesc.h"
24
25using namespace llvm;
26
27static cl::opt<double> CopyWeight("regalloc-copy-weight", cl::init(0.2),
29static cl::opt<double> LoadWeight("regalloc-load-weight", cl::init(4.0),
31static cl::opt<double> StoreWeight("regalloc-store-weight", cl::init(1.0),
33static cl::opt<double> CheapRematWeight("regalloc-cheap-remat-weight",
34 cl::init(0.2), cl::Hidden);
35static cl::opt<double> ExpensiveRematWeight("regalloc-expensive-remat-weight",
36 cl::init(1.0), cl::Hidden);
37
38#define DEBUG_TYPE "regalloc-score"
39
41 CopyCounts += Other.copyCounts();
42 LoadCounts += Other.loadCounts();
43 StoreCounts += Other.storeCounts();
44 LoadStoreCounts += Other.loadStoreCounts();
45 CheapRematCounts += Other.cheapRematCounts();
46 ExpensiveRematCounts += Other.expensiveRematCounts();
47 return *this;
48}
49
51 return copyCounts() == Other.copyCounts() &&
52 loadCounts() == Other.loadCounts() &&
53 storeCounts() == Other.storeCounts() &&
54 loadStoreCounts() == Other.loadStoreCounts() &&
55 cheapRematCounts() == Other.cheapRematCounts() &&
56 expensiveRematCounts() == Other.expensiveRematCounts();
57}
58
60 return !(*this == Other);
61}
62
64 double Ret = 0.0;
65 Ret += CopyWeight * copyCounts();
66 Ret += LoadWeight * loadCounts();
67 Ret += StoreWeight * storeCounts();
71
72 return Ret;
73}
74
77 const MachineBlockFrequencyInfo &MBFI) {
79 MF,
80 [&](const MachineBasicBlock &MBB) {
82 },
83 [&](const MachineInstr &MI) {
85 });
86}
87
89 const MachineFunction &MF,
90 llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
91 llvm::function_ref<bool(const MachineInstr &)>
92 IsTriviallyRematerializable) {
94
95 for (const MachineBasicBlock &MBB : MF) {
96 double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
97 RegAllocScore MBBScore;
98
99 for (const MachineInstr &MI : MBB) {
100 if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
101 continue;
102 }
103 if (MI.isCopy()) {
104 MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
105 } else if (IsTriviallyRematerializable(MI)) {
106 if (MI.getDesc().isAsCheapAsAMove()) {
107 MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
108 } else {
109 MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
110 }
111 } else if (MI.mayLoad() && MI.mayStore()) {
112 MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
113 } else if (MI.mayLoad()) {
114 MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
115 } else if (MI.mayStore()) {
116 MBBScore.onStore(BlockFreqRelativeToEntrypoint);
117 }
118 }
119 Total += MBBScore;
120 }
121 return Total;
122}
MachineBasicBlock & MBB
IRTranslator LLVM IR MI
static cl::opt< double > CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2), cl::Hidden)
static cl::opt< double > StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden)
static cl::opt< double > CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden)
static cl::opt< double > ExpensiveRematWeight("regalloc-expensive-remat-weight", cl::init(1.0), cl::Hidden)
static cl::opt< double > LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
double getBlockFreqRelativeToEntryBlock(const MachineBasicBlock *MBB) const
Compute the frequency of the block, relative to the entry block.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
Representation of each machine instruction.
Regalloc score.
LLVM_ABI_FOR_TEST bool operator==(const RegAllocScore &Other) const
double copyCounts() const
void onLoadStore(double Freq)
RegAllocScore & operator+=(const RegAllocScore &Other)
void onCheapRemat(double Freq)
bool operator!=(const RegAllocScore &Other) const
double cheapRematCounts() const
void onStore(double Freq)
RegAllocScore()=default
double storeCounts() const
LLVM_ABI_FOR_TEST double getScore() const
void onExpensiveRemat(double Freq)
double expensiveRematCounts() const
void onCopy(double Freq)
double loadStoreCounts() const
void onLoad(double Freq)
double loadCounts() const
bool isReMaterializable(const MachineInstr &MI) const
Return true if the instruction would be materializable at a point in the containing function where al...
virtual const TargetInstrInfo * getInstrInfo() const
An efficient, type-erasing, non-owning reference to a callable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI)
Calculate a score.
@ Other
Any other memory.
Definition ModRef.h:68