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),
141 std::unique_ptr<RegAllocPriorityAdvisor>
145 Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
148 Runner = std::make_unique<InteractiveModelRunner>(
153 return std::make_unique<MLPriorityAdvisor>(
154 MF,
RA, &getAnalysis<SlotIndexesWrapperPass>().getSI(), Runner.get());
156 std::unique_ptr<MLModelRunner> Runner;
164#ifdef LLVM_HAVE_TFLITE
165static const TensorSpec Reward = TensorSpec::createSpec<float>(
"reward", {1});
167#define _DECL_TRAIN_FEATURES(type, name, shape, _) \
168 TensorSpec::createSpec<type>(std::string("action_") + #name, shape),
170static const std::vector<TensorSpec> TrainingInputFeatures{
172 TensorSpec::createSpec<float>(
"action_discount", {1}),
173 TensorSpec::createSpec<int32_t>(
"action_step_type", {1}),
174 TensorSpec::createSpec<float>(
"action_reward", {1})}};
175#undef _DECL_TRAIN_FEATURES
185 unsigned getPriority(
const LiveInterval &LI)
const override;
189class DevelopmentModePriorityAdvisorAnalysis final
192 DevelopmentModePriorityAdvisorAnalysis()
196 return R->getAdvisorMode() == AdvisorMode::Development;
201 if (!Log || !Log->hasAnyObservationForContext(MF.
getName()))
207 if (Log->currentContext() != MF.
getName()) {
209 "The training log context shouldn't have had changed.");
211 if (Log->hasObservationInProgress())
212 Log->logReward<
float>(GetReward());
223 bool doInitialization(
Module &M)
override {
225 if (ModelUnderTraining.empty() && TrainingLog.empty()) {
226 Ctx.
emitError(
"Regalloc development mode should be requested with at "
227 "least logging enabled and/or a training model");
230 if (ModelUnderTraining.empty())
231 Runner = std::make_unique<NoInferenceModelRunner>(Ctx,
InputFeatures);
233 Runner = ModelUnderTrainingRunner::createAndEnsureValid(
234 Ctx, ModelUnderTraining,
DecisionName, TrainingInputFeatures);
236 Ctx.
emitError(
"Regalloc: could not set up the model runner");
239 if (TrainingLog.empty())
242 auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
244 M.getContext().emitError(
EC.message() +
":" + TrainingLog);
248 if (
auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get()))
255 Log = std::make_unique<Logger>(std::move(
OS), LFS, Reward,
260 std::unique_ptr<RegAllocPriorityAdvisor>
265 Log->switchContext(MF.
getName());
268 return std::make_unique<DevelopmentModePriorityAdvisor>(
269 MF,
RA, &getAnalysis<SlotIndexesWrapperPass>().getSI(), Runner.get(),
273 std::unique_ptr<MLModelRunner> Runner;
274 std::unique_ptr<Logger> Log;
281 return llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() ||
302 *Runner->
getTensor<int64_t>(1) =
static_cast<int64_t
>(Stage);
312#ifdef LLVM_HAVE_TFLITE
314 return new DevelopmentModePriorityAdvisorAnalysis();
318DevelopmentModePriorityAdvisor::getPriority(
const LiveInterval &LI)
const {
321 if (isa<ModelUnderTrainingRunner>(getRunner())) {
324 Prio = getDefaultAdvisor().getPriority(LI);
327 if (TrainingLog.empty())
333 if (Log->hasObservationInProgress())
334 Log->logReward<
float>(0.0);
336 Log->startObservation();
337 size_t CurrentFeature = 0;
338 for (; CurrentFeature <
InputFeatures.size(); ++CurrentFeature) {
339 Log->logTensorValue(CurrentFeature,
340 reinterpret_cast<const char *
>(
341 getRunner().getTensorUntyped(CurrentFeature)));
344 if (
auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(&getRunner())) {
345 for (
size_t I = 0;
I < MUTR->extraOutputsForLoggingSpecs().size();
346 ++
I, ++CurrentFeature)
349 reinterpret_cast<const char *
>(MUTR->getUntypedExtraOutputValue(
I)));
352 float Ret =
static_cast<float>(Prio);
353 Log->logTensorValue(CurrentFeature,
reinterpret_cast<const char *
>(&Ret));
354 Log->endObservation();
356 return static_cast<unsigned>(Prio);
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)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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.
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.
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...
Interface to the priority advisor, which is responsible for prioritizing live ranges.
SlotIndexes *const Indexes
ReleaseModePriorityAdvisorAnalysis()
static bool classof(const RegAllocPriorityAdvisorAnalysis *R)
An efficient, type-erasing, non-owning reference to a callable.
This is an optimization pass for GlobalISel generic memory operations.
RegAllocPriorityAdvisorAnalysis * createReleaseModePriorityAdvisor()
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
static const TensorSpec DecisionSpec
const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
RegAllocPriorityAdvisorAnalysis * createDevelopmentModePriorityAdvisor()
static const std::vector< int64_t > PerLiveRangeShape
Implement std::hash so that hash_code can be used in STL containers.