LLVM 18.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"
25#include "llvm/MC/MCInstrDesc.h"
27
28using namespace llvm;
29cl::opt<double> CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden);
30cl::opt<double> LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden);
31cl::opt<double> StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden);
32cl::opt<double> CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2),
34cl::opt<double> ExpensiveRematWeight("regalloc-expensive-remat-weight",
35 cl::init(1.0), cl::Hidden);
36#define DEBUG_TYPE "regalloc-score"
37
39 CopyCounts += Other.copyCounts();
40 LoadCounts += Other.loadCounts();
41 StoreCounts += Other.storeCounts();
42 LoadStoreCounts += Other.loadStoreCounts();
43 CheapRematCounts += Other.cheapRematCounts();
44 ExpensiveRematCounts += Other.expensiveRematCounts();
45 return *this;
46}
47
49 return copyCounts() == Other.copyCounts() &&
50 loadCounts() == Other.loadCounts() &&
51 storeCounts() == Other.storeCounts() &&
52 loadStoreCounts() == Other.loadStoreCounts() &&
53 cheapRematCounts() == Other.cheapRematCounts() &&
54 expensiveRematCounts() == Other.expensiveRematCounts();
55}
56
58 return !(*this == Other);
59}
60
62 double Ret = 0.0;
63 Ret += CopyWeight * copyCounts();
64 Ret += LoadWeight * loadCounts();
65 Ret += StoreWeight * storeCounts();
69
70 return Ret;
71}
72
75 const MachineBlockFrequencyInfo &MBFI) {
77 MF,
78 [&](const MachineBasicBlock &MBB) {
80 },
81 [&](const MachineInstr &MI) {
83 MI);
84 });
85}
86
88 const MachineFunction &MF,
89 llvm::function_ref<double(const MachineBasicBlock &)> GetBBFreq,
90 llvm::function_ref<bool(const MachineInstr &)>
91 IsTriviallyRematerializable) {
93
94 for (const MachineBasicBlock &MBB : MF) {
95 double BlockFreqRelativeToEntrypoint = GetBBFreq(MBB);
96 RegAllocScore MBBScore;
97
98 for (const MachineInstr &MI : MBB) {
99 if (MI.isDebugInstr() || MI.isKill() || MI.isInlineAsm()) {
100 continue;
101 }
102 if (MI.isCopy()) {
103 MBBScore.onCopy(BlockFreqRelativeToEntrypoint);
104 } else if (IsTriviallyRematerializable(MI)) {
105 if (MI.getDesc().isAsCheapAsAMove()) {
106 MBBScore.onCheapRemat(BlockFreqRelativeToEntrypoint);
107 } else {
108 MBBScore.onExpensiveRemat(BlockFreqRelativeToEntrypoint);
109 }
110 } else if (MI.mayLoad() && MI.mayStore()) {
111 MBBScore.onLoadStore(BlockFreqRelativeToEntrypoint);
112 } else if (MI.mayLoad()) {
113 MBBScore.onLoad(BlockFreqRelativeToEntrypoint);
114 } else if (MI.mayStore()) {
115 MBBScore.onStore(BlockFreqRelativeToEntrypoint);
116 }
117 }
118 Total += MBBScore;
119 }
120 return Total;
121}
MachineBasicBlock & MBB
This file defines DenseMapInfo traits for DenseMap.
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1272
IRTranslator LLVM IR MI
cl::opt< double > CheapRematWeight("regalloc-cheap-remat-weight", cl::init(0.2), cl::Hidden)
cl::opt< double > StoreWeight("regalloc-store-weight", cl::init(1.0), cl::Hidden)
cl::opt< double > CopyWeight("regalloc-copy-weight", cl::init(0.2), cl::Hidden)
cl::opt< double > ExpensiveRematWeight("regalloc-expensive-remat-weight", cl::init(1.0), cl::Hidden)
cl::opt< double > LoadWeight("regalloc-load-weight", cl::init(4.0), cl::Hidden)
MachineBlockFrequencyInfo pass uses BlockFrequencyInfoImpl implementation to estimate machine basic b...
float 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.
Definition: MachineInstr.h:68
Regalloc score.
Definition: RegAllocScore.h:28
bool operator==(const RegAllocScore &Other) const
double copyCounts() const
Definition: RegAllocScore.h:40
void onLoadStore(double Freq)
Definition: RegAllocScore.h:50
RegAllocScore & operator+=(const RegAllocScore &Other)
void onCheapRemat(double Freq)
Definition: RegAllocScore.h:52
bool operator!=(const RegAllocScore &Other) const
double cheapRematCounts() const
Definition: RegAllocScore.h:45
void onStore(double Freq)
Definition: RegAllocScore.h:49
double storeCounts() const
Definition: RegAllocScore.h:42
double getScore() const
void onExpensiveRemat(double Freq)
Definition: RegAllocScore.h:51
double expensiveRematCounts() const
Definition: RegAllocScore.h:44
void onCopy(double Freq)
Definition: RegAllocScore.h:47
double loadStoreCounts() const
Definition: RegAllocScore.h:43
void onLoad(double Freq)
Definition: RegAllocScore.h:48
double loadCounts() const
Definition: RegAllocScore.h:41
bool isTriviallyReMaterializable(const MachineInstr &MI) const
Return true if the instruction is trivially rematerializable, meaning it has no side effects and requ...
virtual const TargetInstrInfo * getInstrInfo() const
An efficient, type-erasing, non-owning reference to a callable.
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
RegAllocScore calculateRegAllocScore(const MachineFunction &MF, const MachineBlockFrequencyInfo &MBFI)
Calculate a score.
@ Other
Any other memory.