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"));
59#ifdef LLVM_HAVE_TFLITE
65 cl::desc(
"Training log for the register allocator priority model"));
69 cl::desc(
"The model being trained for register allocation priority"));
77#define RA_PRIORITY_FEATURES_LIST(M) \
78 M(int64_t, li_size, PerLiveRangeShape, "size") \
79 M(int64_t, stage, PerLiveRangeShape, "stage") \
80 M(float, weight, PerLiveRangeShape, "weight")
82#define DecisionName "priority"
89#define _FEATURE_IDX(_, name, __, ___) name,
116#define _DECL_FEATURES(type, name, shape, _) \
117 TensorSpec::createSpec<type>(#name, shape),
132 std::unique_ptr<RegAllocPriorityAdvisor>
137 Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
140 Runner = std::make_unique<InteractiveModelRunner>(
145 return std::make_unique<MLPriorityAdvisor>(MF,
RA, &
SI, Runner.get());
149 std::unique_ptr<MLModelRunner> Runner;
159 return R->getAdvisorMode() == AdvisorMode::Release;
169 bool doInitialization(
Module &M)
override {
170 Provider = std::make_unique<ReleaseModePriorityAdvisorProvider>();
180#ifdef LLVM_HAVE_TFLITE
183#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
184 TensorSpec::createSpec<type>(std::string("action_") + #name, shape),
186static const std::vector<TensorSpec> TrainingInputFeatures{
191#undef _DECL_TRAIN_FEATURES
196 SlotIndexes *
const Indexes,
197 MLModelRunner *Runner, Logger *Log)
198 : MLPriorityAdvisor(MF,
RA, Indexes, Runner),
Log(
Log) {}
201 unsigned getPriority(
const LiveInterval &LI)
const override;
205class DevelopmentModePriorityAdvisorProvider final
210 DevelopmentModePriorityAdvisorProvider(LLVMContext &Ctx)
211 : RegAllocPriorityAdvisorProvider(AdvisorMode::
Development) {
212 if (ModelUnderTraining.empty() && TrainingLog.empty()) {
213 Ctx.emitError(
"Regalloc development mode should be requested with at "
214 "least logging enabled and/or a training model");
217 if (ModelUnderTraining.empty())
218 Runner = std::make_unique<NoInferenceModelRunner>(Ctx,
InputFeatures);
220 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
221 Ctx, ModelUnderTraining,
DecisionName, TrainingInputFeatures);
223 Ctx.
emitError(
"Regalloc: could not set up the model runner");
226 if (TrainingLog.empty())
229 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
242 Log = std::make_unique<Logger>(std::move(OS), LFS, Reward,
247 llvm::function_ref<
float()> GetReward)
override {
248 if (!Log || !
Log->hasAnyObservationForContext(MF.
getName()))
256 "The training log context shouldn't have had changed.");
258 if (
Log->hasObservationInProgress())
259 Log->logReward<
float>(GetReward());
262 std::unique_ptr<RegAllocPriorityAdvisor>
264 SlotIndexes &SI)
override {
270 return std::make_unique<DevelopmentModePriorityAdvisor>(
271 MF,
RA, &SI, Runner.get(),
Log.get());
274 std::unique_ptr<MLModelRunner> Runner;
275 std::unique_ptr<Logger>
Log;
278class DevelopmentModePriorityAdvisorAnalysisLegacy final
281 DevelopmentModePriorityAdvisorAnalysisLegacy()
282 : RegAllocPriorityAdvisorAnalysisLegacy(AdvisorMode::
Development) {}
285 static bool classof(
const RegAllocPriorityAdvisorAnalysisLegacy *R) {
286 return R->getAdvisorMode() == AdvisorMode::Development;
290 llvm::function_ref<
float()> GetReward)
override {
291 Provider->logRewardIfNeeded(MF, GetReward);
295 void getAnalysisUsage(AnalysisUsage &AU)
const override {
302 bool doInitialization(
Module &M)
override {
303 Provider = std::make_unique<DevelopmentModePriorityAdvisorProvider>(
328 Runner->switchContext(MF.
getName());
334 assert(!std::isnan(Advice) &&
"model produced a NaN priority");
337 if (Advice >=
static_cast<double>(std::numeric_limits<unsigned>::max()))
338 return std::numeric_limits<unsigned>::max();
339 return static_cast<unsigned>(Advice);
346 *Runner->getTensor<int64_t>(0) =
static_cast<int64_t
>(
Size);
347 *Runner->getTensor<int64_t>(1) =
static_cast<int64_t
>(Stage);
348 *Runner->getTensor<
float>(2) =
static_cast<float>(LI.
weight());
350 return Runner->evaluate<
float>();
357#ifdef LLVM_HAVE_TFLITE
360 return new DevelopmentModePriorityAdvisorAnalysisLegacy();
364DevelopmentModePriorityAdvisor::getPriority(
const LiveInterval &LI)
const {
370 Prio = getDefaultAdvisor().getPriority(LI);
373 if (TrainingLog.empty())
379 if (
Log->hasObservationInProgress())
380 Log->logReward<
float>(0.0);
382 Log->startObservation();
383 size_t CurrentFeature = 0;
384 for (; CurrentFeature <
InputFeatures.size(); ++CurrentFeature) {
385 Log->logTensorValue(CurrentFeature,
386 reinterpret_cast<const char *
>(
387 getRunner().getTensorUntyped(CurrentFeature)));
391 for (
size_t I = 0;
I < MUTR->extraOutputsForLoggingSpecs().
size();
392 ++
I, ++CurrentFeature)
395 reinterpret_cast<const char *
>(MUTR->getUntypedExtraOutputValue(
I)));
398 float Ret =
static_cast<float>(Prio);
399 Log->logTensorValue(CurrentFeature,
reinterpret_cast<const char *
>(&Ret));
400 Log->endObservation();
407 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 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_ATTRIBUTE_RETURNS_NONNULL 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.