@@ -370,8 +370,8 @@ class MLEvictAdvisor : public RegAllocEvictionAdvisor {
370
370
class ReleaseModeEvictionAdvisorProvider final
371
371
: public RegAllocEvictionAdvisorProvider {
372
372
public:
373
- ReleaseModeEvictionAdvisorProvider ()
374
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Release) {
373
+ ReleaseModeEvictionAdvisorProvider (LLVMContext &Ctx )
374
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Release, Ctx ) {
375
375
if (EnableDevelopmentFeatures) {
376
376
InputFeatures = {RA_EVICT_FEATURES_LIST (
377
377
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -385,15 +385,6 @@ class ReleaseModeEvictionAdvisorProvider final
385
385
return R->getAdvisorMode () == AdvisorMode::Release;
386
386
}
387
387
388
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
389
- for (auto Analysis : AnalysisList) {
390
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
391
- this ->MBFI = *MBFI;
392
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
393
- this ->Loops = *Loops;
394
- }
395
- }
396
-
397
388
std::unique_ptr<RegAllocEvictionAdvisor>
398
389
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
399
390
if (!Runner) {
@@ -406,15 +397,15 @@ class ReleaseModeEvictionAdvisorProvider final
406
397
InteractiveChannelBaseName + " .out" ,
407
398
InteractiveChannelBaseName + " .in" );
408
399
}
400
+ assert ((MBFI && Loops) &&
401
+ " Invalid provider state: must have analysis available" );
409
402
return std::make_unique<MLEvictAdvisor>(MF, RA, Runner.get (), *MBFI,
410
403
*Loops);
411
404
}
412
405
413
406
private:
414
407
std::vector<TensorSpec> InputFeatures;
415
408
std::unique_ptr<MLModelRunner> Runner;
416
- MachineBlockFrequencyInfo *MBFI;
417
- MachineLoopInfo *Loops;
418
409
};
419
410
420
411
class ReleaseModeEvictionAdvisorAnalysisLegacy final
@@ -427,12 +418,19 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
427
418
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
428
419
auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
429
420
auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
430
- Provider.setAnalyses ({MBFI, Loops});
431
- return Provider.getAdvisor (MF, RA);
421
+ Provider->setAnalyses (MBFI, Loops);
422
+ return Provider->getAdvisor (MF, RA);
423
+ }
424
+
425
+ void logRewardIfNeeded (const MachineFunction &MF,
426
+ llvm::function_ref<float ()> GetReward) override {
427
+ // No-op in release mode
432
428
}
433
429
434
430
bool doInitialization (Module &M) override {
435
- return Provider.doInitialization (M);
431
+ Provider =
432
+ std::make_unique<ReleaseModeEvictionAdvisorProvider>(M.getContext ());
433
+ return false ;
436
434
}
437
435
438
436
static bool classof (const RegAllocEvictionAdvisorAnalysisLegacy *R) {
@@ -446,7 +444,7 @@ class ReleaseModeEvictionAdvisorAnalysisLegacy final
446
444
}
447
445
448
446
private:
449
- ReleaseModeEvictionAdvisorProvider Provider;
447
+ std::unique_ptr< ReleaseModeEvictionAdvisorProvider> Provider;
450
448
};
451
449
452
450
// ===================================
@@ -484,11 +482,8 @@ class DevelopmentModeEvictAdvisor : public MLEvictAdvisor {
484
482
class DevelopmentModeEvictionAdvisorProvider final
485
483
: public RegAllocEvictionAdvisorProvider {
486
484
public:
487
- DevelopmentModeEvictionAdvisorProvider (
488
- MachineBlockFrequencyInfo *MBFI = nullptr ,
489
- MachineLoopInfo *Loops = nullptr )
490
- : RegAllocEvictionAdvisorProvider(AdvisorMode::Development), MBFI(MBFI),
491
- Loops (Loops) {
485
+ DevelopmentModeEvictionAdvisorProvider (LLVMContext &Ctx)
486
+ : RegAllocEvictionAdvisorProvider(AdvisorMode::Development, Ctx) {
492
487
if (EnableDevelopmentFeatures) {
493
488
InputFeatures = {RA_EVICT_FEATURES_LIST (
494
489
_DECL_FEATURES) RA_EVICT_FIRST_DEVELOPMENT_FEATURE (_DECL_FEATURES)
@@ -508,43 +503,10 @@ class DevelopmentModeEvictionAdvisorProvider final
508
503
TensorSpec::createSpec<int32_t >(" action_step_type" , {1 }),
509
504
TensorSpec::createSpec<float >(" action_reward" , {1 })};
510
505
}
511
- }
512
- // support for isa<> and dyn_cast.
513
- static bool classof (const RegAllocEvictionAdvisorProvider *R) {
514
- return R->getAdvisorMode () == AdvisorMode::Development;
515
- }
516
-
517
- void logRewardIfNeeded (const MachineFunction &MF,
518
- llvm::function_ref<float ()> GetReward) override {
519
- if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
520
- return ;
521
- // The function pass manager would run all the function passes for a
522
- // function, so we assume the last context belongs to this function. If
523
- // this invariant ever changes, we can implement at that time switching
524
- // contexts. At this point, it'd be an error
525
- if (Log->currentContext () != MF.getName ()) {
526
- MF.getFunction ().getContext ().emitError (
527
- " The training log context shouldn't have had changed." );
528
- }
529
- if (Log->hasObservationInProgress ())
530
- Log->logReward <float >(GetReward ());
531
- }
532
-
533
- void setAnalyses (std::initializer_list<llvm::Any> AnalysisList) override {
534
- for (auto Analysis : AnalysisList) {
535
- if (auto **MBFI = llvm::any_cast<MachineBlockFrequencyInfo *>(&Analysis))
536
- this ->MBFI = *MBFI;
537
- if (auto **Loops = llvm::any_cast<MachineLoopInfo *>(&Analysis))
538
- this ->Loops = *Loops;
539
- }
540
- }
541
-
542
- bool doInitialization (Module &M) override {
543
- LLVMContext &Ctx = M.getContext ();
544
506
if (ModelUnderTraining.empty () && TrainingLog.empty ()) {
545
507
Ctx.emitError (" Regalloc development mode should be requested with at "
546
508
" least logging enabled and/or a training model" );
547
- return false ;
509
+ return ;
548
510
}
549
511
if (ModelUnderTraining.empty ())
550
512
Runner = std::make_unique<NoInferenceModelRunner>(Ctx, InputFeatures);
@@ -553,15 +515,15 @@ class DevelopmentModeEvictionAdvisorProvider final
553
515
Ctx, ModelUnderTraining, DecisionName, TrainingInputFeatures);
554
516
if (!Runner) {
555
517
Ctx.emitError (" Regalloc: could not set up the model runner" );
556
- return false ;
518
+ return ;
557
519
}
558
520
if (TrainingLog.empty ())
559
- return false ;
521
+ return ;
560
522
std::error_code EC;
561
523
auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
562
524
if (EC) {
563
- M. getContext () .emitError (EC.message () + " :" + TrainingLog);
564
- return false ;
525
+ Ctx .emitError (EC.message () + " :" + TrainingLog);
526
+ return ;
565
527
}
566
528
std::vector<TensorSpec> LFS = InputFeatures;
567
529
if (auto *MUTR = dyn_cast<ModelUnderTrainingRunner>(Runner.get ()))
@@ -573,7 +535,28 @@ class DevelopmentModeEvictionAdvisorProvider final
573
535
574
536
Log = std::make_unique<Logger>(std::move (OS), LFS, Reward,
575
537
/* IncludeReward*/ true );
576
- return false ;
538
+ return ;
539
+ }
540
+
541
+ // support for isa<> and dyn_cast.
542
+ static bool classof (const RegAllocEvictionAdvisorProvider *R) {
543
+ return R->getAdvisorMode () == AdvisorMode::Development;
544
+ }
545
+
546
+ void logRewardIfNeeded (const MachineFunction &MF,
547
+ llvm::function_ref<float ()> GetReward) override {
548
+ if (!Log || !Log->hasAnyObservationForContext (MF.getName ()))
549
+ return ;
550
+ // The function pass manager would run all the function passes for a
551
+ // function, so we assume the last context belongs to this function. If
552
+ // this invariant ever changes, we can implement at that time switching
553
+ // contexts. At this point, it'd be an error
554
+ if (Log->currentContext () != MF.getName ()) {
555
+ MF.getFunction ().getContext ().emitError (
556
+ " The training log context shouldn't have had changed." );
557
+ }
558
+ if (Log->hasObservationInProgress ())
559
+ Log->logReward <float >(GetReward ());
577
560
}
578
561
579
562
std::unique_ptr<RegAllocEvictionAdvisor>
@@ -594,8 +577,6 @@ class DevelopmentModeEvictionAdvisorProvider final
594
577
595
578
std::unique_ptr<MLModelRunner> Runner;
596
579
std::unique_ptr<Logger> Log;
597
- const MachineBlockFrequencyInfo *MBFI;
598
- const MachineLoopInfo *Loops;
599
580
};
600
581
601
582
class DevelopmentModeEvictionAdvisorAnalysisLegacy final
@@ -605,15 +586,22 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
605
586
: RegAllocEvictionAdvisorAnalysisLegacy(AdvisorMode::Development) {}
606
587
607
588
bool doInitialization (Module &M) override {
608
- auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
609
- auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
610
- Provider.setAnalyses ({MBFI, Loops});
611
- return Provider.doInitialization (M);
589
+ Provider = std::make_unique<DevelopmentModeEvictionAdvisorProvider>(
590
+ M.getContext ());
591
+ return false ;
592
+ }
593
+
594
+ void logRewardIfNeeded (const MachineFunction &MF,
595
+ llvm::function_ref<float ()> GetReward) override {
596
+ Provider->logRewardIfNeeded (MF, GetReward);
612
597
}
613
598
614
599
std::unique_ptr<RegAllocEvictionAdvisor>
615
600
getAdvisor (const MachineFunction &MF, const RAGreedy &RA) override {
616
- return Provider.getAdvisor (MF, RA);
601
+ auto *MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI ();
602
+ auto *Loops = &getAnalysis<MachineLoopInfoWrapperPass>().getLI ();
603
+ Provider->setAnalyses (MBFI, Loops);
604
+ return Provider->getAdvisor (MF, RA);
617
605
}
618
606
619
607
// support for isa<> and dyn_cast.
@@ -628,7 +616,7 @@ class DevelopmentModeEvictionAdvisorAnalysisLegacy final
628
616
}
629
617
630
618
private:
631
- DevelopmentModeEvictionAdvisorProvider Provider;
619
+ std::unique_ptr< DevelopmentModeEvictionAdvisorProvider> Provider;
632
620
};
633
621
634
622
#endif // #ifdef LLVM_HAVE_TFLITE
@@ -1156,8 +1144,9 @@ void llvm::extractMBBFrequency(
1156
1144
// Development mode-specific implementations
1157
1145
#ifdef LLVM_HAVE_TFLITE
1158
1146
1159
- RegAllocEvictionAdvisorProvider *llvm::createDevelopmentModeAdvisorProvider () {
1160
- return new DevelopmentModeEvictionAdvisorProvider ();
1147
+ RegAllocEvictionAdvisorProvider *
1148
+ llvm::createDevelopmentModeAdvisorProvider (LLVMContext &Ctx) {
1149
+ return new DevelopmentModeEvictionAdvisorProvider (Ctx);
1161
1150
}
1162
1151
1163
1152
RegAllocEvictionAdvisorAnalysisLegacy *
@@ -1236,8 +1225,9 @@ bool RegAllocScoring::runOnMachineFunction(MachineFunction &MF) {
1236
1225
}
1237
1226
#endif // #ifdef LLVM_HAVE_TFLITE
1238
1227
1239
- RegAllocEvictionAdvisorProvider *llvm::createReleaseModeAdvisorProvider () {
1240
- return new ReleaseModeEvictionAdvisorProvider ();
1228
+ RegAllocEvictionAdvisorProvider *
1229
+ llvm::createReleaseModeAdvisorProvider (LLVMContext &Ctx) {
1230
+ return new ReleaseModeEvictionAdvisorProvider (Ctx);
1241
1231
}
1242
1232
1243
1233
RegAllocEvictionAdvisorAnalysisLegacy *
0 commit comments