@@ -376,8 +376,8 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
376
376
class ReleaseModeEvictionAdvisorProvider final
377
377
: public RegAllocEvictionAdvisorProvider {
378
378
public:
379
- ReleaseModeEvictionAdvisorProvider ()
380
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Release) {
379
+ ReleaseModeEvictionAdvisorProvider (LLVMContext &Ctx )
380
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Release, Ctx ) {
381
381
if (EnableDevelopmentFeatures) {
382
382
InputFeatures = {RA_EVICT_FEATURES_LIST (
383
383
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -391,15 +391,6 @@ class ReleaseModeEvictionAdvisorProvider final
391
391
return R->getAdvisorMode () == AdvisorMode::Release;
392
392
}
393
393
394
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
395
- for (auto Analysis : AnalysisList) {
396
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
397
- this ->MBFI = *MBFI;
398
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
399
- this ->Loops = *Loops;
400
- }
401
- }
402
-
403
394
std::unique_ptr<RegAllocEvictionAdvisor>
404
395
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
405
396
if (!Runner) {
@@ -412,15 +403,15 @@ class ReleaseModeEvictionAdvisorProvider final
412
403
InteractiveChannelBaseName + " .out" ,
413
404
InteractiveChannelBaseName + " .in" );
414
405
}
406
+ assert ((MBFI && Loops) &&
407
+ " Invalid provider state: must have analysis available" );
415
408
return std::make_unique<MLEvictAdvisor>(MF, RA, Runner.get (), *MBFI,
416
409
*Loops);
417
410
}
418
411
419
412
private:
420
413
std::vector<TensorSpec> InputFeatures;
421
414
std::unique_ptr<MLModelRunner> Runner;
422
- MachineBlockFrequencyInfo *MBFI;
423
- MachineLoopInfo *Loops;
424
415
};
425
416
426
417
class ReleaseModeEvictionAdvisorAnalysisLegacy final
@@ -433,12 +424,19 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
433
424
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
434
425
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
435
426
auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
436
- Provider.setAnalyses ({MBFI, Loops});
437
- return Provider.getAdvisor (MF, RA);
427
+ Provider->setAnalyses (MBFI, Loops);
428
+ return Provider->getAdvisor (MF, RA);
429
+ }
430
+
431
+ void logRewardIfNeeded (const MachineFunction &MF,
432
+ llvm::function_ref<float ()> GetReward) override {
433
+ // No-op in release mode
438
434
}
439
435
440
436
bool doInitialization (Module &M) override {
441
- return Provider.doInitialization (M);
437
+ Provider =
438
+ std::make_unique<ReleaseModeEvictionAdvisorProvider>(M.getContext ());
439
+ return false ;
442
440
}
443
441
444
442
static bool classof (const RegAllocEvictionAdvisorAnalysisLegacy *R) {
@@ -452,7 +450,7 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
452
450
}
453
451
454
452
private:
455
- ReleaseModeEvictionAdvisorProvider Provider;
453
+ std::unique_ptr< ReleaseModeEvictionAdvisorProvider> Provider;
456
454
};
457
455
458
456
// ===================================
@@ -490,11 +488,8 @@ class DevelopmentModeEvictAdvisor : public MLEvictAdvisor {
490
488
class DevelopmentModeEvictionAdvisorProvider final
491
489
: public RegAllocEvictionAdvisorProvider {
492
490
public:
493
- DevelopmentModeEvictionAdvisorProvider (
494
- MachineBlockFrequencyInfo *MBFI = nullptr ,
495
- MachineLoopInfo *Loops = nullptr )
496
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Development), MBFI(MBFI),
497
- Loops (Loops) {
491
+ DevelopmentModeEvictionAdvisorProvider (LLVMContext &Ctx)
492
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Development, Ctx) {
498
493
if (EnableDevelopmentFeatures) {
499
494
InputFeatures = {RA_EVICT_FEATURES_LIST (
500
495
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -514,43 +509,10 @@ class DevelopmentModeEvictionAdvisorProvider final
514
509
TensorSpec::createSpec<int32_t >(" action_step_type" , {1 }),
515
510
TensorSpec::createSpec<float >(" action_reward" , {1 })};
516
511
}
517
- }
518
- // support for isa<> and dyn_cast.
519
- static bool classof (const RegAllocEvictionAdvisorProvider *R) {
520
- return R->getAdvisorMode () == AdvisorMode::Development;
521
- }
522
-
523
- void logRewardIfNeeded (const MachineFunction &MF,
524
- llvm::function_ref<float ()> GetReward) override {
525
- if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
526
- return ;
527
- // The function pass manager would run all the function passes for a
528
- // function, so we assume the last context belongs to this function. If
529
- // this invariant ever changes, we can implement at that time switching
530
- // contexts. At this point, it'd be an error
531
- if (Log->currentContext () != MF.getName ()) {
532
- MF.getFunction ().getContext ().emitError (
533
- " The training log context shouldn't have had changed." );
534
- }
535
- if (Log->hasObservationInProgress ())
536
- Log->logReward <float >(GetReward ());
537
- }
538
-
539
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
540
- for (auto Analysis : AnalysisList) {
541
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
542
- this ->MBFI = *MBFI;
543
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
544
- this ->Loops = *Loops;
545
- }
546
- }
547
-
548
- bool doInitialization (Module &M) override {
549
- LLVMContext &Ctx = M.getContext ();
550
512
if (ModelUnderTraining.empty () && TrainingLog.empty ()) {
551
513
Ctx.emitError (" Regalloc development mode should be requested with at "
552
514
" least logging enabled and/or a training model" );
553
- return false ;
515
+ return ;
554
516
}
555
517
if (ModelUnderTraining.empty ())
556
518
Runner = std::make_unique<NoInferenceModelRunner>(Ctx, InputFeatures);
@@ -559,15 +521,15 @@ class DevelopmentModeEvictionAdvisorProvider final
559
521
Ctx, ModelUnderTraining, DecisionName, TrainingInputFeatures);
560
522
if (!Runner) {
561
523
Ctx.emitError (" Regalloc: could not set up the model runner" );
562
- return false ;
524
+ return ;
563
525
}
564
526
if (TrainingLog.empty ())
565
- return false ;
527
+ return ;
566
528
std::error_code EC;
567
529
auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
568
530
if (EC) {
569
- M. getContext () .emitError (EC.message () + " :" + TrainingLog);
570
- return false ;
531
+ Ctx .emitError (EC.message () + " :" + TrainingLog);
532
+ return ;
571
533
}
572
534
std::vector<TensorSpec> LFS = InputFeatures;
573
535
if (auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get ()))
@@ -579,7 +541,28 @@ class DevelopmentModeEvictionAdvisorProvider final
579
541
580
542
Log = std::make_unique<Logger>(std::move (OS), LFS, Reward,
581
543
/* IncludeReward*/ true );
582
- return false ;
544
+ return ;
545
+ }
546
+
547
+ // support for isa<> and dyn_cast.
548
+ static bool classof (const RegAllocEvictionAdvisorProvider *R) {
549
+ return R->getAdvisorMode () == AdvisorMode::Development;
550
+ }
551
+
552
+ void logRewardIfNeeded (const MachineFunction &MF,
553
+ llvm::function_ref<float ()> GetReward) override {
554
+ if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
555
+ return ;
556
+ // The function pass manager would run all the function passes for a
557
+ // function, so we assume the last context belongs to this function. If
558
+ // this invariant ever changes, we can implement at that time switching
559
+ // contexts. At this point, it'd be an error
560
+ if (Log->currentContext () != MF.getName ()) {
561
+ MF.getFunction ().getContext ().emitError (
562
+ " The training log context shouldn't have had changed." );
563
+ }
564
+ if (Log->hasObservationInProgress ())
565
+ Log->logReward <float >(GetReward ());
583
566
}
584
567
585
568
std::unique_ptr<RegAllocEvictionAdvisor>
@@ -600,8 +583,6 @@ class DevelopmentModeEvictionAdvisorProvider final
600
583
601
584
std::unique_ptr<MLModelRunner> Runner;
602
585
std::unique_ptr<Logger> Log;
603
- const MachineBlockFrequencyInfo *MBFI;
604
- const MachineLoopInfo *Loops;
605
586
};
606
587
607
588
class DevelopmentModeEvictionAdvisorAnalysisLegacy final
@@ -611,15 +592,22 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
611
592
: RegAllocEvictionAdvisorAnalysisLegacy(AdvisorMode::Development) {}
612
593
613
594
bool doInitialization (Module &M) override {
614
- auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
615
- auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
616
- Provider.setAnalyses ({MBFI, Loops});
617
- return Provider.doInitialization (M);
595
+ Provider = std::make_unique<DevelopmentModeEvictionAdvisorProvider>(
596
+ M.getContext ());
597
+ return false ;
598
+ }
599
+
600
+ void logRewardIfNeeded (const MachineFunction &MF,
601
+ llvm::function_ref<float ()> GetReward) override {
602
+ Provider->logRewardIfNeeded (MF, GetReward);
618
603
}
619
604
620
605
std::unique_ptr<RegAllocEvictionAdvisor>
621
606
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
622
- return Provider.getAdvisor (MF, RA);
607
+ auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
608
+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
609
+ Provider->setAnalyses (MBFI, Loops);
610
+ return Provider->getAdvisor (MF, RA);
623
611
}
624
612
625
613
// support for isa<> and dyn_cast.
@@ -634,7 +622,7 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
634
622
}
635
623
636
624
private:
637
- DevelopmentModeEvictionAdvisorProvider Provider;
625
+ std::unique_ptr< DevelopmentModeEvictionAdvisorProvider> Provider;
638
626
};
639
627
640
628
#endif // #ifdef LLVM_HAVE_TFLITE
@@ -1171,8 +1159,9 @@ void llvm::extractMBBFrequency(
1171
1159
// Development mode-specific implementations
1172
1160
#ifdef LLVM_HAVE_TFLITE
1173
1161
1174
- RegAllocEvictionAdvisorProvider *llvm::createDevelopmentModeAdvisorProvider () {
1175
- return new DevelopmentModeEvictionAdvisorProvider ();
1162
+ RegAllocEvictionAdvisorProvider *
1163
+ llvm::createDevelopmentModeAdvisorProvider (LLVMContext &Ctx) {
1164
+ return new DevelopmentModeEvictionAdvisorProvider (Ctx);
1176
1165
}
1177
1166
1178
1167
RegAllocEvictionAdvisorAnalysisLegacy *
@@ -1251,8 +1240,9 @@ bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) {
1251
1240
}
1252
1241
#endif // #ifdef LLVM_HAVE_TFLITE
1253
1242
1254
- RegAllocEvictionAdvisorProvider *llvm::createReleaseModeAdvisorProvider () {
1255
- return new ReleaseModeEvictionAdvisorProvider ();
1243
+ RegAllocEvictionAdvisorProvider *
1244
+ llvm::createReleaseModeAdvisorProvider (LLVMContext &Ctx) {
1245
+ return new ReleaseModeEvictionAdvisorProvider (Ctx);
1256
1246
}
1257
1247
1258
1248
RegAllocEvictionAdvisorAnalysisLegacy *
0 commit comments