@@ -393,8 +393,8 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
393
393
class ReleaseModeEvictionAdvisorProvider final
394
394
: public RegAllocEvictionAdvisorProvider {
395
395
public:
396
- ReleaseModeEvictionAdvisorProvider ()
397
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Release) {
396
+ ReleaseModeEvictionAdvisorProvider (LLVMContext &Ctx )
397
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Release, Ctx ) {
398
398
if (EnableDevelopmentFeatures) {
399
399
InputFeatures = {RA_EVICT_FEATURES_LIST (
400
400
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -408,15 +408,6 @@ class ReleaseModeEvictionAdvisorProvider final
408
408
return R->getAdvisorMode () == AdvisorMode::Release;
409
409
}
410
410
411
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
412
- for (auto Analysis : AnalysisList) {
413
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
414
- this ->MBFI = *MBFI;
415
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
416
- this ->Loops = *Loops;
417
- }
418
- }
419
-
420
411
std::unique_ptr<RegAllocEvictionAdvisor>
421
412
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
422
413
if (!Runner) {
@@ -429,15 +420,15 @@ class ReleaseModeEvictionAdvisorProvider final
429
420
InteractiveChannelBaseName + " .out" ,
430
421
InteractiveChannelBaseName + " .in" );
431
422
}
423
+ assert ((MBFI && Loops) &&
424
+ " Invalid provider state: must have analysis available" );
432
425
return std::make_unique<MLEvictAdvisor>(MF, RA, Runner.get (), *MBFI,
433
426
*Loops);
434
427
}
435
428
436
429
private:
437
430
std::vector<TensorSpec> InputFeatures;
438
431
std::unique_ptr<MLModelRunner> Runner;
439
- MachineBlockFrequencyInfo *MBFI;
440
- MachineLoopInfo *Loops;
441
432
};
442
433
443
434
class ReleaseModeEvictionAdvisorAnalysisLegacy final
@@ -450,12 +441,19 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
450
441
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
451
442
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
452
443
auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
453
- Provider.setAnalyses ({MBFI, Loops});
454
- return Provider.getAdvisor (MF, RA);
444
+ Provider->setAnalyses (MBFI, Loops);
445
+ return Provider->getAdvisor (MF, RA);
446
+ }
447
+
448
+ void logRewardIfNeeded (const MachineFunction &MF,
449
+ llvm::function_ref<float ()> GetReward) override {
450
+ // No-op in release mode
455
451
}
456
452
457
453
bool doInitialization (Module &M) override {
458
- return Provider.doInitialization (M);
454
+ Provider =
455
+ std::make_unique<ReleaseModeEvictionAdvisorProvider>(M.getContext ());
456
+ return false ;
459
457
}
460
458
461
459
static bool classof (const RegAllocEvictionAdvisorAnalysisLegacy *R) {
@@ -469,7 +467,7 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
469
467
}
470
468
471
469
private:
472
- ReleaseModeEvictionAdvisorProvider Provider;
470
+ std::unique_ptr< ReleaseModeEvictionAdvisorProvider> Provider;
473
471
};
474
472
475
473
// ===================================
@@ -507,11 +505,8 @@ class DevelopmentModeEvictAdvisor : public MLEvictAdvisor {
507
505
class DevelopmentModeEvictionAdvisorProvider final
508
506
: public RegAllocEvictionAdvisorProvider {
509
507
public:
510
- DevelopmentModeEvictionAdvisorProvider (
511
- MachineBlockFrequencyInfo *MBFI = nullptr ,
512
- MachineLoopInfo *Loops = nullptr )
513
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Development), MBFI(MBFI),
514
- Loops (Loops) {
508
+ DevelopmentModeEvictionAdvisorProvider (LLVMContext &Ctx)
509
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Development, Ctx) {
515
510
if (EnableDevelopmentFeatures) {
516
511
InputFeatures = {RA_EVICT_FEATURES_LIST (
517
512
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -531,43 +526,10 @@ class DevelopmentModeEvictionAdvisorProvider final
531
526
TensorSpec::createSpec<int32_t >(" action_step_type" , {1 }),
532
527
TensorSpec::createSpec<float >(" action_reward" , {1 })};
533
528
}
534
- }
535
- // support for isa<> and dyn_cast.
536
- static bool classof (const RegAllocEvictionAdvisorProvider *R) {
537
- return R->getAdvisorMode () == AdvisorMode::Development;
538
- }
539
-
540
- void logRewardIfNeeded (const MachineFunction &MF,
541
- llvm::function_ref<float ()> GetReward) override {
542
- if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
543
- return ;
544
- // The function pass manager would run all the function passes for a
545
- // function, so we assume the last context belongs to this function. If
546
- // this invariant ever changes, we can implement at that time switching
547
- // contexts. At this point, it'd be an error
548
- if (Log->currentContext () != MF.getName ()) {
549
- MF.getFunction ().getContext ().emitError (
550
- " The training log context shouldn't have had changed." );
551
- }
552
- if (Log->hasObservationInProgress ())
553
- Log->logReward <float >(GetReward ());
554
- }
555
-
556
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
557
- for (auto Analysis : AnalysisList) {
558
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
559
- this ->MBFI = *MBFI;
560
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
561
- this ->Loops = *Loops;
562
- }
563
- }
564
-
565
- bool doInitialization (Module &M) override {
566
- LLVMContext &Ctx = M.getContext ();
567
529
if (ModelUnderTraining.empty () && TrainingLog.empty ()) {
568
530
Ctx.emitError (" Regalloc development mode should be requested with at "
569
531
" least logging enabled and/or a training model" );
570
- return false ;
532
+ return ;
571
533
}
572
534
if (ModelUnderTraining.empty ())
573
535
Runner = std::make_unique<NoInferenceModelRunner>(Ctx, InputFeatures);
@@ -576,15 +538,15 @@ class DevelopmentModeEvictionAdvisorProvider final
576
538
Ctx, ModelUnderTraining, DecisionName, TrainingInputFeatures);
577
539
if (!Runner) {
578
540
Ctx.emitError (" Regalloc: could not set up the model runner" );
579
- return false ;
541
+ return ;
580
542
}
581
543
if (TrainingLog.empty ())
582
- return false ;
544
+ return ;
583
545
std::error_code EC;
584
546
auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
585
547
if (EC) {
586
- M. getContext () .emitError (EC.message () + " :" + TrainingLog);
587
- return false ;
548
+ Ctx .emitError (EC.message () + " :" + TrainingLog);
549
+ return ;
588
550
}
589
551
std::vector<TensorSpec> LFS = InputFeatures;
590
552
if (auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get ()))
@@ -596,7 +558,28 @@ class DevelopmentModeEvictionAdvisorProvider final
596
558
597
559
Log = std::make_unique<Logger>(std::move (OS), LFS, Reward,
598
560
/* IncludeReward*/ true );
599
- return false ;
561
+ return ;
562
+ }
563
+
564
+ // support for isa<> and dyn_cast.
565
+ static bool classof (const RegAllocEvictionAdvisorProvider *R) {
566
+ return R->getAdvisorMode () == AdvisorMode::Development;
567
+ }
568
+
569
+ void logRewardIfNeeded (const MachineFunction &MF,
570
+ llvm::function_ref<float ()> GetReward) override {
571
+ if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
572
+ return ;
573
+ // The function pass manager would run all the function passes for a
574
+ // function, so we assume the last context belongs to this function. If
575
+ // this invariant ever changes, we can implement at that time switching
576
+ // contexts. At this point, it'd be an error
577
+ if (Log->currentContext () != MF.getName ()) {
578
+ MF.getFunction ().getContext ().emitError (
579
+ " The training log context shouldn't have had changed." );
580
+ }
581
+ if (Log->hasObservationInProgress ())
582
+ Log->logReward <float >(GetReward ());
600
583
}
601
584
602
585
std::unique_ptr<RegAllocEvictionAdvisor>
@@ -617,8 +600,6 @@ class DevelopmentModeEvictionAdvisorProvider final
617
600
618
601
std::unique_ptr<MLModelRunner> Runner;
619
602
std::unique_ptr<Logger> Log;
620
- const MachineBlockFrequencyInfo *MBFI;
621
- const MachineLoopInfo *Loops;
622
603
};
623
604
624
605
class DevelopmentModeEvictionAdvisorAnalysisLegacy final
@@ -628,15 +609,22 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
628
609
: RegAllocEvictionAdvisorAnalysisLegacy(AdvisorMode::Development) {}
629
610
630
611
bool doInitialization (Module &M) override {
631
- auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
632
- auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
633
- Provider.setAnalyses ({MBFI, Loops});
634
- return Provider.doInitialization (M);
612
+ Provider = std::make_unique<DevelopmentModeEvictionAdvisorProvider>(
613
+ M.getContext ());
614
+ return false ;
615
+ }
616
+
617
+ void logRewardIfNeeded (const MachineFunction &MF,
618
+ llvm::function_ref<float ()> GetReward) override {
619
+ Provider->logRewardIfNeeded (MF, GetReward);
635
620
}
636
621
637
622
std::unique_ptr<RegAllocEvictionAdvisor>
638
623
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
639
- return Provider.getAdvisor (MF, RA);
624
+ auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
625
+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
626
+ Provider->setAnalyses (MBFI, Loops);
627
+ return Provider->getAdvisor (MF, RA);
640
628
}
641
629
642
630
// support for isa<> and dyn_cast.
@@ -651,7 +639,7 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
651
639
}
652
640
653
641
private:
654
- DevelopmentModeEvictionAdvisorProvider Provider;
642
+ std::unique_ptr< DevelopmentModeEvictionAdvisorProvider> Provider;
655
643
};
656
644
657
645
#endif // #ifdef LLVM_HAVE_TFLITE
@@ -1204,8 +1192,9 @@ void llvm::extractMBBFrequency(
1204
1192
// Development mode-specific implementations
1205
1193
#ifdef LLVM_HAVE_TFLITE
1206
1194
1207
- RegAllocEvictionAdvisorProvider *llvm::createDevelopmentModeAdvisorProvider () {
1208
- return new DevelopmentModeEvictionAdvisorProvider ();
1195
+ RegAllocEvictionAdvisorProvider *
1196
+ llvm::createDevelopmentModeAdvisorProvider (LLVMContext &Ctx) {
1197
+ return new DevelopmentModeEvictionAdvisorProvider (Ctx);
1209
1198
}
1210
1199
1211
1200
RegAllocEvictionAdvisorAnalysisLegacy *
@@ -1284,8 +1273,9 @@ bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) {
1284
1273
}
1285
1274
#endif // #ifdef LLVM_HAVE_TFLITE
1286
1275
1287
- RegAllocEvictionAdvisorProvider *llvm::createReleaseModeAdvisorProvider () {
1288
- return new ReleaseModeEvictionAdvisorProvider ();
1276
+ RegAllocEvictionAdvisorProvider *
1277
+ llvm::createReleaseModeAdvisorProvider (LLVMContext &Ctx) {
1278
+ return new ReleaseModeEvictionAdvisorProvider (Ctx);
1289
1279
}
1290
1280
1291
1281
RegAllocEvictionAdvisorAnalysisLegacy *
0 commit comments