LLVM: lib/CodeGen/MLRegAllocPriorityAdvisor.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
11
12
35
36#if defined(LLVM_HAVE_TFLITE)
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 .in, while "
50 "the outgoing name should be "
51 ".out"));
52
54
55
56#ifdef LLVM_HAVE_TFLITE
59
61 "regalloc-priority-training-log", cl::Hidden,
62 cl::desc("Training log for the register allocator priority model"));
63
65 "regalloc-priority-model", cl::Hidden,
66 cl::desc("The model being trained for register allocation priority"));
67
68#endif
69
70namespace llvm {
71
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"
82
83
84
86#define _FEATURE_IDX(_, name, __, ___) name,
88#undef _FEATURE_IDX
90};
91
93public:
96
97protected:
101
102
103
107
108private:
111};
112
113#define _DECL_FEATURES(type, name, shape, _) \
114 TensorSpec::createSpec(#name, shape),
115
119#undef _DECL_FEATURES
120
121
122
123
126public:
129 std::unique_ptr
132 if (!Runner) {
134 Runner = std::make_unique<ReleaseModeModelRunner>(
136 else
137 Runner = std::make_unique(
141 }
142 return std::make_unique(MF, RA, &SI, Runner.get());
143 }
144
145private:
146 std::unique_ptr Runner;
147};
148
151public:
154
156 return R->getAdvisorMode() == AdvisorMode::Release;
157 }
158
159private:
160 void getAnalysisUsage(AnalysisUsage &AU) const override {
164 }
165
166 bool doInitialization(Module &M) override {
167 Provider = std::make_unique();
168 return false;
169 }
170};
171
172
173
174
175
176
177#ifdef LLVM_HAVE_TFLITE
179
180#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
181 TensorSpec::createSpec(std::string("action_") + #name, shape),
182
183static const std::vector TrainingInputFeatures{
188#undef _DECL_TRAIN_FEATURES
189
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
207 DevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
208 : RegAllocPriorityAdvisorProvider(AdvisorMode::Development) {
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(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 }
233 append_range(LFS, MUTR->extraOutputsForLoggingSpecs());
234
235
236
238
239 Log = std::make_unique(std::move(OS), LFS, Reward,
240 true);
241 }
242
243 void logRewardIfNeeded(const MachineFunction &MF,
244 llvm::function_ref<float()> GetReward) override {
245 if (!Log || ->hasAnyObservationForContext(MF.getName()))
246 return;
247
248
249
250
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
260 getAdvisor(const MachineFunction &MF, const RAGreedy &RA,
261 SlotIndexes &SI) override {
262 if (!Runner)
263 return nullptr;
264 if (Log) {
266 }
267 return std::make_unique(
268 MF, RA, &SI, Runner.get(), Log.get());
269 }
270
271 std::unique_ptr Runner;
272 std::unique_ptr Log;
273};
274
275class DevelopmentModePriorityAdvisorAnalysisLegacy final
277public:
278 DevelopmentModePriorityAdvisorAnalysisLegacy()
279 : RegAllocPriorityAdvisorAnalysisLegacy(AdvisorMode::Development) {}
280
281
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 {
294 AU.addRequired();
296 }
297
298
299 bool doInitialization(Module &M) override {
300 Provider = std::make_unique(
301 M.getContext());
302 return false;
303 ;
304 }
305};
306#endif
307
308}
309
317
323 Runner(std::move(Runner)) {
324 assert(this->Runner);
325 Runner->switchContext(MF.getName());
326}
327
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
342
343#ifdef LLVM_HAVE_TFLITE
346 return new DevelopmentModePriorityAdvisorAnalysisLegacy();
347}
348
349unsigned
350DevelopmentModePriorityAdvisor::getPriority(const LiveInterval &LI) const {
351 double Prio = 0;
352
355 } else {
356 Prio = getDefaultAdvisor().getPriority(LI);
357 }
358
359 if (TrainingLog.empty())
360 return Prio;
361
362
363
364
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
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
397
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Module.h This file contains the declarations for the Module class.
NoopSavedModelImpl CompiledModelType
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 .in, while the " "outgoing name should be .out"))
#define _FEATURE_IDX(A, B, C, D)
#define _DECL_FEATURES(type, name, shape, _)
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 .in, while " "the outgoing name should be " ".out"))
#define RA_PRIORITY_FEATURES_LIST(M)
Definition MLRegAllocPriorityAdvisor.cpp:74
Machine Check Debug Module
SI optimize exec mask operations pre RA
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.
This is an important class for using LLVM in a threaded context.
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.
LLVM_ABI unsigned getSize() const
getSize - Returns the sum of sizes of all the LiveRange's.
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
const MLModelRunner & getRunner() const
Definition MLRegAllocPriorityAdvisor.cpp:104
MLPriorityAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes *const Indexes, MLModelRunner *Runner)
Definition MLRegAllocPriorityAdvisor.cpp:318
const RegAllocPriorityAdvisor & getDefaultAdvisor() const
Definition MLRegAllocPriorityAdvisor.cpp:98
unsigned getPriority(const LiveInterval &LI) const override
Find the priority value for a live range.
Definition MLRegAllocPriorityAdvisor.cpp:339
float getPriorityImpl(const LiveInterval &LI) const
Definition MLRegAllocPriorityAdvisor.cpp:328
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.
A mock class satisfying the interface expected by ReleaseModeModelRunner for its TGen parameter.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
RegAllocPriorityAdvisorProvider::AdvisorMode AdvisorMode
std::unique_ptr< RegAllocPriorityAdvisorProvider > Provider
RegAllocPriorityAdvisorAnalysisLegacy(AdvisorMode Mode)
Common provider for getting the priority advisor and logging rewards.
RegAllocPriorityAdvisorProvider(AdvisorMode Mode)
RegAllocPriorityAdvisor(const RegAllocPriorityAdvisor &)=delete
SlotIndexes *const Indexes
static bool classof(const RegAllocPriorityAdvisorAnalysisLegacy *R)
Definition MLRegAllocPriorityAdvisor.cpp:155
ReleaseModePriorityAdvisorAnalysisLegacy()
Definition MLRegAllocPriorityAdvisor.cpp:152
ReleaseModePriorityAdvisorProvider()
Definition MLRegAllocPriorityAdvisor.cpp:127
std::unique_ptr< RegAllocPriorityAdvisor > getAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes &SI) override
Definition MLRegAllocPriorityAdvisor.cpp:130
static TensorSpec createSpec(const std::string &Name, const std::vector< int64_t > &Shape, int Port=0)
This is an optimization pass for GlobalISel generic memory operations.
bool isEmbeddedModelEvaluatorValid()
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
decltype(auto) dyn_cast(const From &Val)
dyn_cast - Return the argument parameter cast to the specified type.
FeatureIDs
Definition MLRegAllocPriorityAdvisor.cpp:85
@ FeatureCount
Definition MLRegAllocPriorityAdvisor.cpp:89
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
RegAllocPriorityAdvisorAnalysisLegacy * createReleaseModePriorityAdvisorAnalysis()
Definition MLRegAllocPriorityAdvisor.cpp:311
RegAllocPriorityAdvisorAnalysisLegacy * createDevelopmentModePriorityAdvisorAnalysis()
static const TensorSpec DecisionSpec
Definition MLRegAllocPriorityAdvisor.cpp:80
LLVM_ABI const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
Definition MLRegAllocPriorityAdvisor.cpp:116
LLVM_ATTRIBUTE_RETURNS_NONNULL RegAllocPriorityAdvisorProvider * createDevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
bool isa(const From &Val)
isa - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ATTRIBUTE_RETURNS_NONNULL RegAllocPriorityAdvisorProvider * createReleaseModePriorityAdvisorProvider()
Definition MLRegAllocPriorityAdvisor.cpp:399
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
static const std::vector< int64_t > PerLiveRangeShape
Definition MLRegAllocPriorityAdvisor.cpp:72
Implement std::hash so that hash_code can be used in STL containers.