1313#include " AllocationOrder.h"
1414#include " RegAllocEvictionAdvisor.h"
1515#include " RegAllocGreedy.h"
16+ #include " llvm/Analysis/InteractiveModelRunner.h"
1617#include " llvm/Analysis/MLModelRunner.h"
1718#include " llvm/Analysis/TensorSpec.h"
1819#if defined(LLVM_HAVE_TF_AOT_REGALLOCEVICTMODEL) || defined(LLVM_HAVE_TFLITE)
@@ -52,6 +53,14 @@ using CompiledModelType = RegallocEvictModel;
5253using CompiledModelType = NoopSavedModelImpl;
5354#endif
5455
56+ static cl::opt<std::string> InteractiveChannelBaseName (
57+ " regalloc-evict-interactive-channel-base" , cl::Hidden,
58+ cl::desc (
59+ " Base file path for the interactive mode. The incoming filename should "
60+ " have the name <regalloc-evict-interactive-channel-base>.in, while the "
61+ " outgoing name should be "
62+ " <regalloc-evict-interactive-channel-base>.out" ));
63+
5564// Options that only make sense in development mode
5665#ifdef LLVM_HAVE_TFLITE
5766#include " RegAllocScore.h"
@@ -213,6 +222,8 @@ static const std::vector<int64_t> PerLiveRangeShape{1, NumberOfInterferences};
213222// will be guaranteed to be to a mask == 1 position. Using a macro here to
214223// avoid 'not used' warnings (and keep cond compilation to a minimum)
215224#define DecisionName " index_to_evict"
225+ static const TensorSpec DecisionSpec =
226+ TensorSpec::createSpec<int64_t >(DecisionName, {1 });
216227
217228// Named features index.
218229enum FeatureIDs {
@@ -382,14 +393,21 @@ class ReleaseModeEvictionAdvisorAnalysis final
382393
383394 std::unique_ptr<RegAllocEvictionAdvisor>
384395 getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
385- if (!Runner)
386- Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
387- MF.getFunction ().getContext (), InputFeatures, DecisionName);
396+ if (!Runner) {
397+ if (InteractiveChannelBaseName.empty ())
398+ Runner = std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
399+ MF.getFunction ().getContext (), InputFeatures, DecisionName);
400+ else
401+ Runner = std::make_unique<InteractiveModelRunner>(
402+ MF.getFunction ().getContext (), InputFeatures, DecisionSpec,
403+ InteractiveChannelBaseName + " .out" ,
404+ InteractiveChannelBaseName + " .in" );
405+ }
388406 return std::make_unique<MLEvictAdvisor>(
389407 MF, RA, Runner.get (), getAnalysis<MachineBlockFrequencyInfo>(),
390408 getAnalysis<MachineLoopInfo>());
391409 }
392- std::unique_ptr<ReleaseModeModelRunner<CompiledModelType> > Runner;
410+ std::unique_ptr<MLModelRunner > Runner;
393411};
394412
395413// ===================================
@@ -398,8 +416,6 @@ class ReleaseModeEvictionAdvisorAnalysis final
398416//
399417// Features we log
400418#ifdef LLVM_HAVE_TFLITE
401- static const TensorSpec Output =
402- TensorSpec::createSpec<int64_t >(DecisionName, {1 });
403419static const TensorSpec Reward = TensorSpec::createSpec<float >(" reward" , {1 });
404420
405421// Features we bind on the model. The tensor names have a prefix, and we also
@@ -512,7 +528,7 @@ class DevelopmentModeEvictionAdvisorAnalysis final
512528 // We always log the output; in particular, if we're not evaluating, we
513529 // don't have an output spec json file. That's why we handle the
514530 // 'normal' output separately.
515- LFS.push_back (Output );
531+ LFS.push_back (DecisionSpec );
516532
517533 Log = std::make_unique<Logger>(std::move (OS), LFS, Reward,
518534 /* IncludeReward*/ true );
@@ -557,6 +573,7 @@ MLEvictAdvisor::MLEvictAdvisor(const MachineFunction &MF, const RAGreedy &RA,
557573 Runner(std::move(Runner)), MBFI(MBFI), Loops(Loops),
558574 InitialQSize(MLEvictAdvisor::getInitialQueueSize(MF)) {
559575 assert (this ->Runner );
576+ Runner->switchContext (MF.getName ());
560577 DoNotNormalize.set (FeatureIDs::mask);
561578 DoNotNormalize.set (FeatureIDs::is_free);
562579 DoNotNormalize.set (FeatureIDs::is_hint);
@@ -1134,7 +1151,10 @@ bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) {
11341151#endif // #ifdef LLVM_HAVE_TFLITE
11351152
11361153RegAllocEvictionAdvisorAnalysis *llvm::createReleaseModeAdvisor () {
1137- return new ReleaseModeEvictionAdvisorAnalysis ();
1154+ return llvm::isEmbeddedModelEvaluatorValid<CompiledModelType>() ||
1155+ !InteractiveChannelBaseName.empty ()
1156+ ? new ReleaseModeEvictionAdvisorAnalysis ()
1157+ : nullptr ;
11381158}
11391159
11401160// In all cases except development mode, we don't need scoring.
0 commit comments