1616
1717#include " llvm/ADT/BitVector.h"
1818#include " llvm/Analysis/CallGraph.h"
19- #include " llvm/Analysis/InlineSizeEstimatorAnalysis.h"
2019#include " llvm/Analysis/MLInlineAdvisor.h"
2120#include " llvm/Analysis/ModelUnderTrainingRunner.h"
2221#include " llvm/Analysis/NoInferenceModelRunner.h"
@@ -89,9 +88,6 @@ struct InlineEvent {
8988 // / error, even if AdvisedDecision were true, otherwise it agrees with
9089 // / AdvisedDecision.
9190 bool Effect = false ;
92-
93- // / What the change in size was: size_after - size_before
94- int64_t Reward = 0 ;
9591};
9692
9793// / Collect data we may use for training a model.
@@ -150,31 +146,15 @@ class DevelopmentModeMLInlineAdvisor : public MLInlineAdvisor {
150146 GetModelRunner,
151147 std::function<bool (CallBase &)> GetDefaultAdvice);
152148
153- size_t getTotalSizeEstimate ();
154-
155- void updateNativeSizeEstimate (int64_t Change) {
156- *CurrentNativeSize += Change;
157- }
158- void resetNativeSize (Function *F) {
159- PreservedAnalyses PA = PreservedAnalyses::all ();
160- PA.abandon <InlineSizeEstimatorAnalysis>();
161- FAM.invalidate (*F, PA);
162- }
163-
164149 std::unique_ptr<MLInlineAdvice>
165150 getAdviceFromModel (CallBase &CB, OptimizationRemarkEmitter &ORE) override ;
166151
167- std::optional<size_t > getNativeSizeEstimate (const Function &F) const ;
168-
169152private:
170153 bool isLogging () const { return !!Logger; }
171154 std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl (CallBase &CB) override ;
172155
173156 const bool IsDoingInference;
174157 std::unique_ptr<TrainingLogger> Logger;
175-
176- const std::optional<int32_t > InitialNativeSize;
177- std::optional<int32_t > CurrentNativeSize;
178158};
179159
180160// / A variant of MLInlineAdvice that tracks all non-trivial inlining
@@ -183,13 +163,9 @@ class LoggingMLInlineAdvice : public MLInlineAdvice {
183163public:
184164 LoggingMLInlineAdvice (DevelopmentModeMLInlineAdvisor *Advisor, CallBase &CB,
185165 OptimizationRemarkEmitter &ORE, bool Recommendation,
186- TrainingLogger &Logger,
187- std::optional<size_t > CallerSizeEstimateBefore,
188- std::optional<size_t > CalleeSizeEstimateBefore,
189- bool DefaultDecision, bool Mandatory = false )
166+ TrainingLogger &Logger, bool DefaultDecision,
167+ bool Mandatory = false )
190168 : MLInlineAdvice(Advisor, CB, ORE, Recommendation), Logger(Logger),
191- CallerSizeEstimateBefore (CallerSizeEstimateBefore),
192- CalleeSizeEstimateBefore(CalleeSizeEstimateBefore),
193169 DefaultDecision (DefaultDecision), Mandatory(Mandatory) {}
194170
195171 virtual ~LoggingMLInlineAdvice () = default ;
@@ -200,59 +176,35 @@ class LoggingMLInlineAdvice : public MLInlineAdvice {
200176 }
201177 void recordInliningImpl () override {
202178 MLInlineAdvice::recordInliningImpl ();
203- getAdvisor ()->resetNativeSize (Caller);
204- int Reward = std::numeric_limits<int >::max ();
205- if (InlineSizeEstimatorAnalysis::isEvaluatorRequested () &&
206- !getAdvisor ()->isForcedToStop ()) {
207- int NativeSizeAfter = *getAdvisor ()->getNativeSizeEstimate (*Caller) +
208- *CalleeSizeEstimateBefore;
209- Reward = NativeSizeAfter -
210- (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
211- getAdvisor ()->updateNativeSizeEstimate (Reward);
212- }
213- log (Reward, /* Success=*/ true );
179+ log (/* Success=*/ true );
214180 }
215181
216182 void recordInliningWithCalleeDeletedImpl () override {
217183 MLInlineAdvice::recordInliningWithCalleeDeletedImpl ();
218- getAdvisor ()->resetNativeSize (Caller);
219- if (InlineSizeEstimatorAnalysis::isEvaluatorRequested () &&
220- !getAdvisor ()->isForcedToStop ()) {
221- int NativeSizeAfter = *getAdvisor ()->getNativeSizeEstimate (*Caller);
222- int Reward = NativeSizeAfter -
223- (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
224- getAdvisor ()->updateNativeSizeEstimate (Reward);
225- log (Reward, /* Success=*/ true );
226- } else {
227- log (NoReward, /* Success=*/ true );
228- }
184+ log (/* Success=*/ true );
229185 }
230186
231187 void recordUnsuccessfulInliningImpl (const InlineResult &Result) override {
232188 MLInlineAdvice::recordUnsuccessfulInliningImpl (Result);
233- log (NoReward, /* Success=*/ false );
189+ log (/* Success=*/ false );
234190 }
235191
236192 void recordUnattemptedInliningImpl () override {
237193 MLInlineAdvice::recordUnattemptedInliningImpl ();
238- log (NoReward, /* Success=*/ false );
194+ log (/* Success=*/ false );
239195 }
240196
241- void log (int64_t Reward, bool Success) {
197+ void log (bool Success) {
242198 if (Mandatory)
243199 return ;
244200 InlineEvent Event;
245201 Event.AdvisedDecision = isInliningRecommended ();
246202 Event.DefaultDecision = DefaultDecision;
247203 Event.Effect = Success;
248- Event.Reward = Reward;
249204 Logger.logInlineEvent (Event, getAdvisor ()->getModelRunner ());
250205 }
251206
252- static const int64_t NoReward = 0 ;
253207 TrainingLogger &Logger;
254- const std::optional<size_t > CallerSizeEstimateBefore;
255- const std::optional<size_t > CalleeSizeEstimateBefore;
256208 const int64_t DefaultDecision;
257209 const int64_t Mandatory;
258210};
@@ -296,9 +248,9 @@ TrainingLogger::TrainingLogger(StringRef LogFileName,
296248 if (EC)
297249 dbgs () << (EC.message () + " :" + TrainingLog);
298250
299- L = std::make_unique<Logger>(
300- std::move (OS), FT, TensorSpec::createSpec<int64_t >(RewardName, {1 }),
301- InlineSizeEstimatorAnalysis::isEvaluatorRequested () );
251+ L = std::make_unique<Logger>(std::move (OS), FT,
252+ TensorSpec::createSpec<int64_t >(RewardName, {1 }),
253+ false );
302254 L->switchContext (" " );
303255}
304256
@@ -326,8 +278,6 @@ void TrainingLogger::logInlineEvent(const InlineEvent &Event,
326278 L->logTensorValue (DecisionPos,
327279 reinterpret_cast <const char *>(&Event.AdvisedDecision ));
328280 L->endObservation ();
329- if (InlineSizeEstimatorAnalysis::isEvaluatorRequested ())
330- L->logReward (Event.Reward );
331281
332282 // For debugging / later use
333283 Effects.push_back (Event.Effect );
@@ -340,9 +290,7 @@ DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
340290 GetModelRunner,
341291 std::function<bool(CallBase &)> GetDefaultAdvice)
342292 : MLInlineAdvisor(M, MAM, GetModelRunner, GetDefaultAdvice),
343- IsDoingInference(isa<ModelUnderTrainingRunner>(getModelRunner())),
344- InitialNativeSize(isLogging() ? getTotalSizeEstimate() : 0),
345- CurrentNativeSize(InitialNativeSize) {
293+ IsDoingInference(isa<ModelUnderTrainingRunner>(getModelRunner())) {
346294 // We cannot have the case of neither inference nor logging.
347295 if (!TrainingLog.empty ())
348296 Logger = std::make_unique<TrainingLogger>(
@@ -351,29 +299,12 @@ DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
351299 assert (IsDoingInference || isLogging ());
352300}
353301
354- std::optional<size_t >
355- DevelopmentModeMLInlineAdvisor::getNativeSizeEstimate (const Function &F) const {
356- if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested ())
357- return std::nullopt ;
358- auto &R =
359- FAM.getResult <InlineSizeEstimatorAnalysis>(const_cast <Function &>(F));
360- if (!R) {
361- F.getParent ()->getContext ().emitError (
362- " Native size estimator is not present." );
363- return 0 ;
364- }
365- return *R;
366- }
367-
368302std::unique_ptr<MLInlineAdvice>
369303DevelopmentModeMLInlineAdvisor::getMandatoryAdviceImpl (CallBase &CB) {
370304 return std::make_unique<LoggingMLInlineAdvice>(
371305 /* Advisor=*/ this ,
372306 /* CB=*/ CB, /* ORE=*/ getCallerORE (CB), /* Recommendation=*/ true ,
373307 /* Logger=*/ *Logger,
374- /* CallerSizeEstimateBefore=*/ getNativeSizeEstimate (*CB.getCaller ()),
375- /* CalleeSizeEstimateBefore=*/
376- getNativeSizeEstimate (*CB.getCalledFunction ()),
377308 /* DefaultDecision=*/ true , /* Mandatory*/ true );
378309}
379310
@@ -391,24 +322,9 @@ DevelopmentModeMLInlineAdvisor::getAdviceFromModel(
391322 /* Advisor=*/ this ,
392323 /* CB=*/ CB, /* ORE=*/ ORE, /* Recommendation=*/ Recommendation,
393324 /* Logger=*/ *Logger,
394- /* CallerSizeEstimateBefore=*/ getNativeSizeEstimate (*CB.getCaller ()),
395- /* CalleeSizeEstimateBefore=*/
396- getNativeSizeEstimate (*CB.getCalledFunction ()),
397325 /* DefaultDecision=*/ DefaultAdvice);
398326}
399327
400- size_t DevelopmentModeMLInlineAdvisor::getTotalSizeEstimate () {
401- if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested ())
402- return 0 ;
403- size_t Ret = 0 ;
404- for (auto &F : M) {
405- if (F.isDeclaration ())
406- continue ;
407- Ret += *getNativeSizeEstimate (F);
408- }
409- return Ret;
410- }
411-
412328std::unique_ptr<InlineAdvisor> llvm::getDevelopmentModeAdvisor (
413329 Module &M, ModuleAnalysisManager &MAM,
414330 std::function<bool (CallBase &)> GetDefaultAdvice) {
0 commit comments