LLVM 22.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
27namespace llvm {
28LLVM_ABI cl::opt<double> CopyWeight("regalloc-copy-weight", cl::init(0.2),
30LLVM_ABI cl::opt<double> LoadWeight("regalloc-load-weight", cl::init(4.0),
32LLVM_ABI cl::opt<double> StoreWeight("regalloc-store-weight", cl::init(1.0),
34LLVM_ABI cl::opt<double> CheapRematWeight("regalloc-cheap-remat-weight",
35 cl::init(0.2), cl::Hidden);
36LLVM_ABI cl::opt<double> ExpensiveRematWeight("regalloc-expensive-remat-weight",
37 cl::init(1.0), cl::Hidden);
38} // end namespace llvm
39
40#define DEBUG_TYPE "regalloc-score"
41
43 CopyCounts += Other.copyCounts();
44 LoadCounts += Other.loadCounts();
45 StoreCounts += Other.storeCounts();
46 LoadStoreCounts += Other.loadStoreCounts();
47 CheapRematCounts += Other.cheapRematCounts();
48 ExpensiveRematCounts += Other.expensiveRematCounts();
49 return *this;
50}
51
53 return copyCounts() == Other.copyCounts() &&
54 loadCounts() == Other.loadCounts() &&
55 storeCounts() == Other.storeCounts() &&
56 loadStoreCounts() == Other.loadStoreCounts() &&
57 cheapRematCounts() == Other.cheapRematCounts() &&
58 expensiveRematCounts() == Other.expensiveRematCounts();
59}
60
62 return !(*this == Other);
63}
64
66 double Ret = 0.0;
67 Ret += CopyWeight * copyCounts();
68 Ret += LoadWeight * loadCounts();
69 Ret += StoreWeight * storeCounts();
73
74 return Ret;
75}
76
79 const MachineBlockFrequencyInfo &MBFI) {
81 MF,
82 [&](const MachineBasicBlock &MBB) {
84 },
85 [&](const MachineInstr &MI) {
87 });
88}
89
91 const MachineFunction &MF,
92 llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
93 llvm::function_ref<bool(const MachineInstr &)>
94 IsTriviallyRematerializable) {
96
97 for (const MachineBasicBlock &MBB : MF) {
98 double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
99 RegAllocScore MBBScore;
100
101 for (const MachineInstr &MI : MBB) {
102 if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
103 continue;
104 }
105 if (MI.isCopy()) {
106 MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
107 } else if (IsTriviallyRematerializable(MI)) {
108 if (MI.getDesc().isAsCheapAsAMove()) {
109 MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
110 } else {
111 MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
112 }
113 } else if (MI.mayLoad() && MI.mayStore()) {
114 MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
115 } else if (MI.mayLoad()) {
116 MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
117 } else if (MI.mayStore()) {
118 MBBScore.onStore(BlockFreqRelativeToEntrypoint);
119 }
120 }
121 Total += MBBScore;
122 }
123 return Total;
124}
MachineBasicBlock & MBB
#define LLVM_ABI
Definition Compiler.h:213
IRTranslator LLVM IR MI
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.
LLVM_ABI cl::opt< double > LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden)
LLVM_ABI cl::opt< double > CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden)
LLVM_ABI cl::opt< double > ExpensiveRematWeight("regalloc-expensive-remat-weight", cl::init(1.0), cl::Hidden)
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI)
Calculate a score.
LLVM_ABI cl::opt< double > StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden)
@ Other
Any other memory.
Definition ModRef.h:68
LLVM_ABI cl::opt< double > CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2), cl::Hidden)