36#if defined(LLVM_HAVE_TFLITE)
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"));
56#ifdef LLVM_HAVE_TFLITE
62 cl::desc(
"Training log for the register allocator priority model"));
66 cl::desc(
"The model being trained for register allocation priority"));
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")
79#define DecisionName "priority"
86#define _FEATURE_IDX(_, name, __, ___) name,
113#define _DECL_FEATURES(type, name, shape, _) \
114 TensorSpec::createSpec<type>(#name, shape),
129 std::unique_ptr<RegAllocPriorityAdvisor>
134 Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
137 Runner = std::make_unique<InteractiveModelRunner>(
142 return std::make_unique<MLPriorityAdvisor>(MF,
RA, &SI, Runner.get());
146 std::unique_ptr<MLModelRunner> Runner;
156 return R->getAdvisorMode() == AdvisorMode::Release;
166 bool doInitialization(
Module &M)
override {
167 Provider = std::make_unique<ReleaseModePriorityAdvisorProvider>();
177#ifdef LLVM_HAVE_TFLITE
178static const TensorSpec Reward = TensorSpec::createSpec<float>(
"reward", {1});
180#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
181 TensorSpec::createSpec<type>(std::string("action_") + #name, shape),
183static const std::vector<TensorSpec> TrainingInputFeatures{
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
198 unsigned getPriority(
const LiveInterval &LI)
const override;
202class DevelopmentModePriorityAdvisorProvider final
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");
214 if (ModelUnderTraining.empty())
215 Runner = std::make_unique<NoInferenceModelRunner>(Ctx,
InputFeatures);
217 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
218 Ctx, ModelUnderTraining,
DecisionName, TrainingInputFeatures);
220 Ctx.
emitError(
"Regalloc: could not set up the model runner");
223 if (TrainingLog.empty())
226 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
232 if (
auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get()))
239 Log = std::make_unique<Logger>(std::move(
OS), LFS, Reward,
245 if (!Log || !Log->hasAnyObservationForContext(MF.
getName()))
251 if (Log->currentContext() != MF.
getName()) {
253 "The training log context shouldn't have had changed.");
255 if (Log->hasObservationInProgress())
256 Log->logReward<
float>(GetReward());
259 std::unique_ptr<RegAllocPriorityAdvisor>
265 Log->switchContext(MF.
getName());
267 return std::make_unique<DevelopmentModePriorityAdvisor>(
268 MF,
RA, &SI, Runner.get(), Log.get());
271 std::unique_ptr<MLModelRunner> Runner;
272 std::unique_ptr<Logger> Log;
275class DevelopmentModePriorityAdvisorAnalysisLegacy final
278 DevelopmentModePriorityAdvisorAnalysisLegacy()
283 return R->getAdvisorMode() == AdvisorMode::Development;
288 Provider->logRewardIfNeeded(MF, GetReward);
299 bool doInitialization(
Module &M)
override {
300 Provider = std::make_unique<DevelopmentModePriorityAdvisorProvider>(
312 return llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() ||
333 *Runner->
getTensor<int64_t>(1) =
static_cast<int64_t
>(Stage);
343#ifdef LLVM_HAVE_TFLITE
346 return new DevelopmentModePriorityAdvisorAnalysisLegacy();
350DevelopmentModePriorityAdvisor::getPriority(
const LiveInterval &LI)
const {
353 if (isa<ModelUnderTrainingRunner>(getRunner())) {
356 Prio = getDefaultAdvisor().getPriority(LI);
359 if (TrainingLog.empty())
365 if (Log->hasObservationInProgress())
366 Log->logReward<
float>(0.0);
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)));
376 if (
auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(&getRunner())) {
377 for (
size_t I = 0;
I < MUTR->extraOutputsForLoggingSpecs().size();
378 ++
I, ++CurrentFeature)
381 reinterpret_cast<const char *
>(MUTR->getUntypedExtraOutputValue(
I)));
384 float Ret =
static_cast<float>(Prio);
385 Log->logTensorValue(CurrentFeature,
reinterpret_cast<const char *
>(&Ret));
386 Log->endObservation();
388 return static_cast<unsigned>(Prio);
393 return new DevelopmentModePriorityAdvisorProvider(Ctx);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Module.h This file contains the declarations for the Module class.
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, _)
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
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.
Logging utility - given an ordered specification of features, and assuming a scalar reward,...
MLModelRunner interface: abstraction of a mechanism for evaluating a ML model.
virtual void switchContext(StringRef Name)
T * getTensor(I FeatureID)
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.
A mock class satisfying the interface expected by ReleaseModeModelRunner for its TGen parameter.
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.
SlotIndexes *const Indexes
static bool classof(const RegAllocPriorityAdvisorAnalysisLegacy *R)
ReleaseModePriorityAdvisorAnalysisLegacy()
ReleaseModePriorityAdvisorProvider()
std::unique_ptr< RegAllocPriorityAdvisor > getAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes &SI) override
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
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.
static const std::vector< int64_t > PerLiveRangeShape
Implement std::hash so that hash_code can be used in STL containers.