39#if defined(LLVM_HAVE_TFLITE)
49 "regalloc-priority-interactive-channel-base",
cl::Hidden,
51 "Base file path for the interactive mode. The incoming filename should "
52 "have the name <regalloc-priority-interactive-channel-base>.in, while "
53 "the outgoing name should be "
54 "<regalloc-priority-interactive-channel-base>.out"));
64#ifdef LLVM_HAVE_TFLITE
70 cl::desc(
"Training log for the register allocator priority model"));
74 cl::desc(
"The model being trained for register allocation priority"));
82#define RA_PRIORITY_FEATURES_LIST(M) \
83 M(int64_t, li_size, PerLiveRangeShape, "size") \
84 M(int64_t, stage, PerLiveRangeShape, "stage") \
85 M(float, weight, PerLiveRangeShape, "weight")
87#define DecisionName "priority"
94#define _FEATURE_IDX(_, name, __, ___) name,
121#define _DECL_FEATURES(type, name, shape, _) \
122 TensorSpec::createSpec<type>(#name, shape),
137 std::unique_ptr<RegAllocPriorityAdvisor>
142 Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
145 Runner = std::make_unique<InteractiveModelRunner>(
150 return std::make_unique<MLPriorityAdvisor>(MF,
RA, &
SI, Runner.get());
154 std::unique_ptr<MLModelRunner> Runner;
164 return R->getAdvisorMode() == AdvisorMode::Release;
174 bool doInitialization(
Module &M)
override {
175 Provider = std::make_unique<ReleaseModePriorityAdvisorProvider>();
185#ifdef LLVM_HAVE_TFLITE
188#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
189 TensorSpec::createSpec<type>(std::string("action_") + #name, shape),
191static const std::vector<TensorSpec> TrainingInputFeatures{
196#undef _DECL_TRAIN_FEATURES
201 SlotIndexes *
const Indexes,
202 MLModelRunner *Runner, Logger *Log)
203 : MLPriorityAdvisor(MF,
RA, Indexes, Runner),
Log(
Log) {}
206 unsigned getPriority(
const LiveInterval &LI)
const override;
210class DevelopmentModePriorityAdvisorProvider final
215 DevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
216 : RegAllocPriorityAdvisorProvider(AdvisorMode::
Development) {
217 if (ModelUnderTraining.empty() && TrainingLog.empty()) {
218 Ctx.emitError(
"Regalloc development mode should be requested with at "
219 "least logging enabled and/or a training model");
222 if (ModelUnderTraining.empty())
223 Runner = std::make_unique<NoInferenceModelRunner>(Ctx,
InputFeatures);
225 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
226 Ctx, ModelUnderTraining,
DecisionName, TrainingInputFeatures);
228 Ctx.
emitError(
"Regalloc: could not set up the model runner");
231 if (TrainingLog.empty())
234 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
247 Log = std::make_unique<Logger>(std::move(OS), LFS, Reward,
252 llvm::function_ref<
float()> GetReward)
override {
253 if (!Log || !
Log->hasAnyObservationForContext(MF.
getName()))
261 "The training log context shouldn't have had changed.");
263 if (
Log->hasObservationInProgress())
264 Log->logReward<
float>(GetReward());
267 std::unique_ptr<RegAllocPriorityAdvisor>
269 SlotIndexes &SI)
override {
275 return std::make_unique<DevelopmentModePriorityAdvisor>(
276 MF,
RA, &SI, Runner.get(),
Log.get());
279 std::unique_ptr<MLModelRunner> Runner;
280 std::unique_ptr<Logger>
Log;
283class DevelopmentModePriorityAdvisorAnalysisLegacy final
286 DevelopmentModePriorityAdvisorAnalysisLegacy()
287 : RegAllocPriorityAdvisorAnalysisLegacy(AdvisorMode::
Development) {}
290 static bool classof(
const RegAllocPriorityAdvisorAnalysisLegacy *R) {
291 return R->getAdvisorMode() == AdvisorMode::Development;
295 llvm::function_ref<
float()> GetReward)
override {
296 Provider->logRewardIfNeeded(MF, GetReward);
300 void getAnalysisUsage(AnalysisUsage &AU)
const override {
307 bool doInitialization(
Module &M)
override {
308 Provider = std::make_unique<DevelopmentModePriorityAdvisorProvider>(
332 Runner->switchContext(MF.
getName());
338 assert(!std::isnan(Advice) &&
"model produced a NaN priority");
341 if (Advice >=
static_cast<double>(std::numeric_limits<unsigned>::max()))
342 return std::numeric_limits<unsigned>::max();
343 return static_cast<unsigned>(Advice);
350 *Runner->getTensor<int64_t>(0) =
static_cast<int64_t
>(
Size);
351 *Runner->getTensor<int64_t>(1) =
static_cast<int64_t
>(Stage);
352 *Runner->getTensor<
float>(2) =
static_cast<float>(LI.
weight());
354 return Runner->evaluate<
float>();
361#ifdef LLVM_HAVE_TFLITE
364 return new DevelopmentModePriorityAdvisorAnalysisLegacy();
368DevelopmentModePriorityAdvisor::getPriority(
const LiveInterval &LI)
const {
374 Prio = getDefaultAdvisor().getPriority(LI);
377 if (TrainingLog.empty())
383 if (
Log->hasObservationInProgress())
384 Log->logReward<
float>(0.0);
386 Log->startObservation();
387 size_t CurrentFeature = 0;
388 for (; CurrentFeature <
InputFeatures.size(); ++CurrentFeature) {
389 Log->logTensorValue(CurrentFeature,
390 reinterpret_cast<const char *
>(
391 getRunner().getTensorUntyped(CurrentFeature)));
395 for (
size_t I = 0;
I < MUTR->extraOutputsForLoggingSpecs().
size();
396 ++
I, ++CurrentFeature)
399 reinterpret_cast<const char *
>(MUTR->getUntypedExtraOutputValue(
I)));
402 float Ret =
static_cast<float>(Prio);
403 Log->logTensorValue(CurrentFeature,
reinterpret_cast<const char *
>(&Ret));
404 Log->endObservation();
411 return new DevelopmentModePriorityAdvisorProvider(Ctx);
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 <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 bool hasReleaseModePriorityModel()
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"))
static unsigned convertAdviceToPriority(double Advice)
#define RA_PRIORITY_FEATURES_LIST(M)
Machine Check Debug Module
SI optimize exec mask operations pre RA
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
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
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.
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)
ReleaseModePriorityAdvisorAnalysisLegacy()
ReleaseModePriorityAdvisorProvider()
std::unique_ptr< RegAllocPriorityAdvisor > getAdvisor(const MachineFunction &MF, const RAGreedy &RA, SlotIndexes &SI) override
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<X> - Return the argument parameter cast to the specified type.
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
LLVM_ABI RegAllocPriorityAdvisorAnalysisLegacy * createReleaseModePriorityAdvisorAnalysis()
static const TensorSpec DecisionSpec
LLVM_ABI const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
LLVM_ABI RegAllocPriorityAdvisorProvider * createReleaseModePriorityAdvisorProvider()
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
LLVM_ATTRIBUTE_RETURNS_NONNULL LLVM_ABI RegAllocPriorityAdvisorProvider * createDevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
LLVM_ABI RegAllocPriorityAdvisorAnalysisLegacy * createDevelopmentModePriorityAdvisorAnalysis()
static const std::vector< int64_t > PerLiveRangeShape
Implement std::hash so that hash_code can be used in STL containers.