LLVM 22.0.0git
MLRegAllocPriorityAdvisor.cpp
Go to the documentation of this file.
1//===- MLRegAllocPriorityAdvisor.cpp - ML priority advisor-----------------===//
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// Implementation of the ML priority advisor and reward injection pass
10//
11//===----------------------------------------------------------------------===//
12
13#include "AllocationOrder.h"
14#include "RegAllocGreedy.h"
26#include "llvm/CodeGen/Passes.h"
32#include "llvm/Pass.h"
33#include "llvm/PassRegistry.h"
35
36#if defined(LLVM_HAVE_TFLITE)
40#include "llvm/IR/Module.h"
41#endif
42
43using namespace llvm;
44
46 "regalloc-priority-interactive-channel-base", cl::Hidden,
48 "Base file path for the interactive mode. The incoming filename should "
49 "have the name <regalloc-priority-interactive-channel-base>.in, while "
50 "the outgoing name should be "
51 "<regalloc-priority-interactive-channel-base>.out"));
52
54
55// Options that only make sense in development mode
56#ifdef LLVM_HAVE_TFLITE
57#include "RegAllocScore.h"
59
60static cl::opt<std::string> TrainingLog(
61 "regalloc-priority-training-log", cl::Hidden,
62 cl::desc("Training log for the register allocator priority model"));
63
64static cl::opt<std::string> ModelUnderTraining(
65 "regalloc-priority-model", cl::Hidden,
66 cl::desc("The model being trained for register allocation priority"));
67
68#endif // #ifdef LLVM_HAVE_TFLITE
69
70namespace llvm {
71
72static const std::vector<int64_t> PerLiveRangeShape{1};
73
74#define RA_PRIORITY_FEATURES_LIST(M) \
75 M(int64_t, li_size, PerLiveRangeShape, "size") \
76 M(int64_t, stage, PerLiveRangeShape, "stage") \
77 M(float, weight, PerLiveRangeShape, "weight")
78
79#define DecisionName "priority"
81 TensorSpec::createSpec<float>(DecisionName, {1});
82
83
84// Named features index.
86#define _FEATURE_IDX(_, name, __, ___) name,
88#undef _FEATURE_IDX
90};
91
93public:
95 SlotIndexes *const Indexes, MLModelRunner *Runner);
96
97protected:
99 return static_cast<const RegAllocPriorityAdvisor &>(DefaultAdvisor);
100 }
101
102 // The assumption is that if the Runner could not be constructed, we emit-ed
103 // error, and we shouldn't be asking for it here.
104 const MLModelRunner &getRunner() const { return *Runner; }
105 float getPriorityImpl(const LiveInterval &LI) const;
106 unsigned getPriority(const LiveInterval &LI) const override;
107
108private:
109 const DefaultPriorityAdvisor DefaultAdvisor;
110 MLModelRunner *const Runner;
111};
112
113#define _DECL_FEATURES(type, name, shape, _) \
114 TensorSpec::createSpec<type>(#name, shape),
115
116static const std::vector<TensorSpec> InputFeatures{
118};
119#undef _DECL_FEATURES
120
121// ===================================
122// Release (AOT) - specifics
123// ===================================
126public:
129 std::unique_ptr<RegAllocPriorityAdvisor>
131 SlotIndexes &SI) override {
132 if (!Runner) {
133 if (InteractiveChannelBaseName.empty())
134 Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
136 else
137 Runner = std::make_unique<InteractiveModelRunner>(
141 }
142 return std::make_unique<MLPriorityAdvisor>(MF, RA, &SI, Runner.get());
143 }
144
145private:
146 std::unique_ptr<MLModelRunner> Runner;
147};
148
151public:
154 // support for isa<> and dyn_cast.
156 return R->getAdvisorMode() == AdvisorMode::Release;
157 }
158
159private:
160 void getAnalysisUsage(AnalysisUsage &AU) const override {
161 AU.setPreservesAll();
164 }
165
166 bool doInitialization(Module &M) override {
167 Provider = std::make_unique<ReleaseModePriorityAdvisorProvider>();
168 return false;
169 }
170};
171
172// ===================================
173// Development mode-specifics
174// ===================================
175//
176// Features we log
177#ifdef LLVM_HAVE_TFLITE
178static const TensorSpec Reward = TensorSpec::createSpec<float>("reward", {1});
179
180#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
181 TensorSpec::createSpec<type>(std::string("action_") + #name, shape),
182
183static const std::vector<TensorSpec> TrainingInputFeatures{
184 {RA_PRIORITY_FEATURES_LIST(_DECL_TRAIN_FEATURES)
185 TensorSpec::createSpec<float>("action_discount", {1}),
186 TensorSpec::createSpec<int32_t>("action_step_type", {1}),
187 TensorSpec::createSpec<float>("action_reward", {1})}};
188#undef _DECL_TRAIN_FEATURES
189
190class DevelopmentModePriorityAdvisor : public MLPriorityAdvisor {
191public:
192 DevelopmentModePriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA,
193 SlotIndexes *const Indexes,
194 MLModelRunner *Runner, Logger *Log)
195 : MLPriorityAdvisor(MF, RA, Indexes, Runner), Log(Log) {}
196
197private:
198 unsigned getPriority(const LiveInterval &LI) const override;
199 Logger *const Log;
200};
201
202class DevelopmentModePriorityAdvisorProvider final
204
205public:
206 // Save all the logs (when requested).
207 DevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
209 if (ModelUnderTraining.empty() && TrainingLog.empty()) {
210 Ctx.emitError("Regalloc development mode should be requested with at "
211 "least logging enabled and/or a training model");
212 return;
213 }
214 if (ModelUnderTraining.empty())
215 Runner = std::make_unique<NoInferenceModelRunner>(Ctx, InputFeatures);
216 else
217 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
218 Ctx, ModelUnderTraining, DecisionName, TrainingInputFeatures);
219 if (!Runner) {
220 Ctx.emitError("Regalloc: could not set up the model runner");
221 return;
222 }
223 if (TrainingLog.empty())
224 return;
225 std::error_code EC;
226 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
227 if (EC) {
228 Ctx.emitError(EC.message() + ":" + TrainingLog);
229 return;
230 }
231 std::vector<TensorSpec> LFS = InputFeatures;
232 if (auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get()))
233 append_range(LFS, MUTR->extraOutputsForLoggingSpecs());
234 // We always log the output; in particular, if we're not evaluating, we
235 // don't have an output spec json file. That's why we handle the
236 // 'normal' output separately.
237 LFS.push_back(DecisionSpec);
238
239 Log = std::make_unique<Logger>(std::move(OS), LFS, Reward,
240 /*IncludeReward*/ true);
241 }
242
243 void logRewardIfNeeded(const MachineFunction &MF,
244 llvm::function_ref<float()> GetReward) override {
245 if (!Log || !Log->hasAnyObservationForContext(MF.getName()))
246 return;
247 // The function pass manager would run all the function passes for a
248 // function, so we assume the last context belongs to this function. If
249 // this invariant ever changes, we can implement at that time switching
250 // contexts. At this point, it'd be an error
251 if (Log->currentContext() != MF.getName()) {
253 "The training log context shouldn't have had changed.");
254 }
255 if (Log->hasObservationInProgress())
256 Log->logReward<float>(GetReward());
257 }
258
259 std::unique_ptr<RegAllocPriorityAdvisor>
260 getAdvisor(const MachineFunction &MF, const RAGreedy &RA,
261 SlotIndexes &SI) override {
262 if (!Runner)
263 return nullptr;
264 if (Log) {
265 Log->switchContext(MF.getName());
266 }
267 return std::make_unique<DevelopmentModePriorityAdvisor>(
268 MF, RA, &SI, Runner.get(), Log.get());
269 }
270
271 std::unique_ptr<MLModelRunner> Runner;
272 std::unique_ptr<Logger> Log;
273};
274
275class DevelopmentModePriorityAdvisorAnalysisLegacy final
277public:
278 DevelopmentModePriorityAdvisorAnalysisLegacy()
280
281 // support for isa<> and dyn_cast.
282 static bool classof(const RegAllocPriorityAdvisorAnalysisLegacy *R) {
283 return R->getAdvisorMode() == AdvisorMode::Development;
284 }
285
286 void logRewardIfNeeded(const MachineFunction &MF,
287 llvm::function_ref<float()> GetReward) override {
288 Provider->logRewardIfNeeded(MF, GetReward);
289 }
290
291private:
292 void getAnalysisUsage(AnalysisUsage &AU) const override {
293 AU.setPreservesAll();
296 }
297
298 // Save all the logs (when requested).
299 bool doInitialization(Module &M) override {
300 Provider = std::make_unique<DevelopmentModePriorityAdvisorProvider>(
301 M.getContext());
302 return false;
303 ;
304 }
305};
306#endif //#ifdef LLVM_HAVE_TFLITE
307
308} // namespace llvm
309
312 return llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() ||
315 : nullptr;
316}
317
319 const RAGreedy &RA,
320 SlotIndexes *const Indexes,
321 MLModelRunner *Runner)
322 : RegAllocPriorityAdvisor(MF, RA, Indexes), DefaultAdvisor(MF, RA, Indexes),
323 Runner(std::move(Runner)) {
324 assert(this->Runner);
325 Runner->switchContext(MF.getName());
326}
327
329 const unsigned Size = LI.getSize();
331
332 *Runner->getTensor<int64_t>(0) = static_cast<int64_t>(Size);
333 *Runner->getTensor<int64_t>(1) = static_cast<int64_t>(Stage);
334 *Runner->getTensor<float>(2) = static_cast<float>(LI.weight());
335
336 return Runner->evaluate<float>();
337}
338
340 return static_cast<unsigned>(getPriorityImpl(LI));
341}
342
343#ifdef LLVM_HAVE_TFLITE
346 return new DevelopmentModePriorityAdvisorAnalysisLegacy();
347}
348
349unsigned
350DevelopmentModePriorityAdvisor::getPriority(const LiveInterval &LI) const {
351 double Prio = 0;
352
353 if (isa<ModelUnderTrainingRunner>(getRunner())) {
355 } else {
356 Prio = getDefaultAdvisor().getPriority(LI);
357 }
358
359 if (TrainingLog.empty())
360 return Prio;
361
362 // TODO(mtrofin): when we support optional rewards, this can go away. In the
363 // meantime, we log the "pretend" reward (0) for the previous observation
364 // before starting a new one.
365 if (Log->hasObservationInProgress())
366 Log->logReward<float>(0.0);
367
368 Log->startObservation();
369 size_t CurrentFeature = 0;
370 for (; CurrentFeature < InputFeatures.size(); ++CurrentFeature) {
371 Log->logTensorValue(CurrentFeature,
372 reinterpret_cast<const char *>(
373 getRunner().getTensorUntyped(CurrentFeature)));
374 }
375
376 if (auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(&getRunner())) {
377 for (size_t I = 0; I < MUTR->extraOutputsForLoggingSpecs().size();
378 ++I, ++CurrentFeature)
379 Log->logTensorValue(
380 CurrentFeature,
381 reinterpret_cast<const char *>(MUTR->getUntypedExtraOutputValue(I)));
382 }
383
384 float Ret = static_cast<float>(Prio);
385 Log->logTensorValue(CurrentFeature, reinterpret_cast<const char *>(&Ret));
386 Log->endObservation();
387
388 return static_cast<unsigned>(Prio);
389}
390
393 return new DevelopmentModePriorityAdvisorProvider(Ctx);
394}
395
396#endif // #ifdef LLVM_HAVE_TFLITE
397
401}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Size
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
static cl::opt< std::string > InteractiveChannelBaseName("inliner-interactive-channel-base", cl::Hidden, cl::desc("Base file path for the interactive mode. The incoming filename should " "have the name <inliner-interactive-channel-base>.in, while the " "outgoing name should be <inliner-interactive-channel-base>.out"))
#define _FEATURE_IDX(A, B, C, D)
#define _DECL_FEATURES(type, name, shape, _)
#define DecisionName
static cl::opt< std::string > InteractiveChannelBaseName("regalloc-priority-interactive-channel-base", cl::Hidden, cl::desc("Base file path for the interactive mode. The incoming filename should " "have the name <regalloc-priority-interactive-channel-base>.in, while " "the outgoing name should be " "<regalloc-priority-interactive-channel-base>.out"))
#define RA_PRIORITY_FEATURES_LIST(M)
SI optimize exec mask operations pre RA
raw_pwrite_stream & OS
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition: Function.cpp:359
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
LiveInterval - This class represents the liveness of a register, or stack slot.
Definition: LiveInterval.h:690
float weight() const
Definition: LiveInterval.h:722
LLVM_ABI unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
Definition: MLModelRunner.h:26
virtual void switchContext(StringRef Name)
Definition: MLModelRunner.h:54
T * getTensor(I FeatureID)
Definition: MLModelRunner.h:37
const MLModelRunner & getRunner() const
MLPriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes *const Indexes, MLModelRunner *Runner)
const RegAllocPriorityAdvisor & getDefaultAdvisor() const
unsigned getPriority(const LiveInterval &LI) const override
Find the priority value for a live range.
float getPriorityImpl(const LiveInterval &LI) const
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
Function & getFunction()
Return the LLVM function that this machine code represents.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
A mock class satisfying the interface expected by ReleaseModeModelRunner for its TGen parameter.
LiveRangeStage getStage(Register Reg) const
const ExtraRegInfo & getExtraInfo() const
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
std::unique_ptr< RegAllocPriorityAdvisorProvider > Provider
Common provider for getting the priority advisor and logging rewards.
Interface to the priority advisor, which is responsible for prioritizing live ranges.
static bool classof(const RegAllocPriorityAdvisorAnalysisLegacy *R)
std::unique_ptr< RegAllocPriorityAdvisor > getAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes &SI) override
SlotIndexes pass.
Definition: SlotIndexes.h:298
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
RegAllocPriorityAdvisorAnalysisLegacy * createReleaseModePriorityAdvisorAnalysis()
RegAllocPriorityAdvisorAnalysisLegacy * createDevelopmentModePriorityAdvisorAnalysis()
static const TensorSpec DecisionSpec
LLVM_ABI const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
LLVM_ATTRIBUTE_RETURNS_NONNULL RegAllocPriorityAdvisorProvider * createDevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
LLVM_ATTRIBUTE_RETURNS_NONNULL RegAllocPriorityAdvisorProvider * createReleaseModePriorityAdvisorProvider()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1886
static const std::vector< int64_t > PerLiveRangeShape
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856