xref: /freebsd/contrib/llvm-project/llvm/lib/Analysis/DevelopmentModeInlineAdvisor.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1e8d8bef9SDimitry Andric //===- DevelopmentModeInlineAdvisor.cpp - runtime-loadable model runner  --===//
2e8d8bef9SDimitry Andric //
3349cc55cSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4349cc55cSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5349cc55cSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e8d8bef9SDimitry Andric //
7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
8e8d8bef9SDimitry Andric //
95f757f3fSDimitry Andric // This file implements a model runner using TFLite, allowing the
10e8d8bef9SDimitry Andric // loading of a model from a command line option.
11e8d8bef9SDimitry Andric //
12e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===//
13bdd1243dSDimitry Andric #include "llvm/Analysis/TensorSpec.h"
14e8d8bef9SDimitry Andric #include "llvm/Config/config.h"
15bdd1243dSDimitry Andric #if defined(LLVM_HAVE_TFLITE)
16e8d8bef9SDimitry Andric 
1704eeddc0SDimitry Andric #include "llvm/ADT/BitVector.h"
18e8d8bef9SDimitry Andric #include "llvm/Analysis/CallGraph.h"
19e8d8bef9SDimitry Andric #include "llvm/Analysis/InlineSizeEstimatorAnalysis.h"
20e8d8bef9SDimitry Andric #include "llvm/Analysis/MLInlineAdvisor.h"
210eae32dcSDimitry Andric #include "llvm/Analysis/ModelUnderTrainingRunner.h"
220eae32dcSDimitry Andric #include "llvm/Analysis/NoInferenceModelRunner.h"
23e8d8bef9SDimitry Andric #include "llvm/Analysis/Utils/TFUtils.h"
24bdd1243dSDimitry Andric #include "llvm/Analysis/Utils/TrainingLogger.h"
25e8d8bef9SDimitry Andric #include "llvm/IR/LLVMContext.h"
26*0fca6ea1SDimitry Andric #include "llvm/IR/Module.h"
27e8d8bef9SDimitry Andric #include "llvm/Support/CommandLine.h"
28e8d8bef9SDimitry Andric #include "llvm/Support/ManagedStatic.h"
29e8d8bef9SDimitry Andric 
30e8d8bef9SDimitry Andric #include <vector>
31bdd1243dSDimitry Andric #include <optional>
32e8d8bef9SDimitry Andric 
33e8d8bef9SDimitry Andric using namespace llvm;
34e8d8bef9SDimitry Andric 
35e8d8bef9SDimitry Andric static cl::opt<std::string> TrainingLog(
36e8d8bef9SDimitry Andric     "training-log", cl::Hidden,
37e8d8bef9SDimitry Andric     cl::desc("Path where the development - mode inlining log is saved."));
38e8d8bef9SDimitry Andric 
39e8d8bef9SDimitry Andric static cl::opt<std::string> TFModelUnderTrainingPath(
40e8d8bef9SDimitry Andric     "ml-inliner-model-under-training", cl::Hidden,
41e8d8bef9SDimitry Andric     cl::desc(R"(Path to SavedModel from the previous training iteration.
42e8d8bef9SDimitry Andric The directory is also expected to contain a JSON specification of the
43e8d8bef9SDimitry Andric outputs expected to be logged, where the first entry must be the
44e8d8bef9SDimitry Andric inlining decision. The file containing the specification should be
45e8d8bef9SDimitry Andric called output_spec.json. The expected JSON value is an array of
46e8d8bef9SDimitry Andric dictionaries. Each dictionary should have 2 keys:
47e8d8bef9SDimitry Andric 
48e8d8bef9SDimitry Andric - "tensor_spec, followed by the TensorSpec description of the
49e8d8bef9SDimitry Andric output; and
50e8d8bef9SDimitry Andric - "logging_name", a string indicating the name to use when
51e8d8bef9SDimitry Andric logging the output values.
52e8d8bef9SDimitry Andric 
53e8d8bef9SDimitry Andric Example:
54e8d8bef9SDimitry Andric [
55e8d8bef9SDimitry Andric   {
56e8d8bef9SDimitry Andric     "logging_name" : "some_name",
57e8d8bef9SDimitry Andric     "tensor_spec" : {
58e8d8bef9SDimitry Andric       "name" : "model_name",
59e8d8bef9SDimitry Andric       "port" : 0,
60e8d8bef9SDimitry Andric       "shape" : [2, 3],
61e8d8bef9SDimitry Andric       "type" : "float"
62e8d8bef9SDimitry Andric       }
63e8d8bef9SDimitry Andric   }
64e8d8bef9SDimitry Andric ]
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric The first value must always correspond to the decision.)"));
67e8d8bef9SDimitry Andric 
68e8d8bef9SDimitry Andric static cl::opt<std::string> TFOutputSpecOverride(
69e8d8bef9SDimitry Andric     "ml-inliner-output-spec-override", cl::Hidden,
70e8d8bef9SDimitry Andric     cl::desc("Override the path to the output spec json file. See "
71e8d8bef9SDimitry Andric              "-ml-inliner-model-under-training documentation for the "
72e8d8bef9SDimitry Andric              "specification of that file."));
73e8d8bef9SDimitry Andric 
74e8d8bef9SDimitry Andric static cl::opt<std::string> TFFeedPrefix("ml-inliner-trained-model-feed-prefix",
75e8d8bef9SDimitry Andric                                          cl::Hidden, cl::init("action_"),
76e8d8bef9SDimitry Andric                                          cl::desc("Prefix for feature names."));
77e8d8bef9SDimitry Andric 
78e8d8bef9SDimitry Andric namespace {
79e8d8bef9SDimitry Andric /// An InlineEvent, used by TrainingLogger.
80e8d8bef9SDimitry Andric struct InlineEvent {
81e8d8bef9SDimitry Andric   /// What the default policy's decision would have been.
82e8d8bef9SDimitry Andric   int64_t DefaultDecision = 0;
83e8d8bef9SDimitry Andric 
84e8d8bef9SDimitry Andric   /// What we advised. When training off the default policy, this is the same as
85e8d8bef9SDimitry Andric   /// DefaultDecision.
86e8d8bef9SDimitry Andric   int64_t AdvisedDecision = 0;
87e8d8bef9SDimitry Andric 
88e8d8bef9SDimitry Andric   /// What actually happened. This would be 'false' in the case of an inline
89e8d8bef9SDimitry Andric   /// error, even if AdvisedDecision were true, otherwise it agrees with
90e8d8bef9SDimitry Andric   /// AdvisedDecision.
91e8d8bef9SDimitry Andric   bool Effect = false;
92e8d8bef9SDimitry Andric 
93e8d8bef9SDimitry Andric   /// What the change in size was: size_after - size_before
94e8d8bef9SDimitry Andric   int64_t Reward = 0;
95e8d8bef9SDimitry Andric };
96e8d8bef9SDimitry Andric 
97bdd1243dSDimitry Andric /// Collect data we may use for training a model.
98e8d8bef9SDimitry Andric class TrainingLogger final {
99e8d8bef9SDimitry Andric public:
100e8d8bef9SDimitry Andric   TrainingLogger(StringRef LogFileName, const ModelUnderTrainingRunner *MUTR);
101e8d8bef9SDimitry Andric 
102e8d8bef9SDimitry Andric   /// Log one inlining event.
103e8d8bef9SDimitry Andric   void logInlineEvent(const InlineEvent &Event,
104e8d8bef9SDimitry Andric                       const MLModelRunner &ModelRunner);
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric private:
107e8d8bef9SDimitry Andric   StringRef LogFileName;
108e8d8bef9SDimitry Andric   const ModelUnderTrainingRunner *const MUTR;
109e8d8bef9SDimitry Andric   std::unique_ptr<Logger> L;
11004eeddc0SDimitry Andric   BitVector Effects;
111e8d8bef9SDimitry Andric   /// Set these 2 clearly OOB, to make sure we set them later.
112e8d8bef9SDimitry Andric   size_t DefaultDecisionPos = std::numeric_limits<size_t>::max();
113e8d8bef9SDimitry Andric   size_t DecisionPos = std::numeric_limits<size_t>::max();
114e8d8bef9SDimitry Andric };
115e8d8bef9SDimitry Andric 
116e8d8bef9SDimitry Andric /// An extension of the MLInlineAdvisor for the 'development' mode, targeting
117e8d8bef9SDimitry Andric /// the offline training scenario. Note that training happens outside of the
118e8d8bef9SDimitry Andric /// compiler, this facility is concerned with producing training data ("logs").
119e8d8bef9SDimitry Andric /// This InlineAdvisor can operate in the following modes:
120e8d8bef9SDimitry Andric ///
121e8d8bef9SDimitry Andric /// 1) collect logs for the default policy. This is useful for bootstrapping
122e8d8bef9SDimitry Andric /// training, which will be considerably faster by starting from a reasonable
123e8d8bef9SDimitry Andric /// policy.
124e8d8bef9SDimitry Andric ///
125e8d8bef9SDimitry Andric /// 2) collect logs for the ML policy, using a model from a previous
126e8d8bef9SDimitry Andric /// training. Potentially, that model uses internally some small random
127e8d8bef9SDimitry Andric /// perturbation of its weights, to induce exploration (setting this up is the
128e8d8bef9SDimitry Andric /// responsibility of the training algorithm). The logs would then be used to
129e8d8bef9SDimitry Andric /// retrain and improve on this model.
130e8d8bef9SDimitry Andric ///
131e8d8bef9SDimitry Andric /// 3) use the provided model, with no logging. This is useful for end to end
132e8d8bef9SDimitry Andric /// validation - the model, in this case, is a release candidate and shouldn't
133e8d8bef9SDimitry Andric /// have random perturbations. It is a convenience feature: rather than needing
134e8d8bef9SDimitry Andric /// to take the release candidate model and compile it in 'release' mode,
135e8d8bef9SDimitry Andric /// validate it, then potentially discard it, it's easier to just pass the model
136e8d8bef9SDimitry Andric /// to the compiler, albeit compilation would be slower, as a one-off. Once the
137e8d8bef9SDimitry Andric /// model behaves satisfactorily, it can be compiled AOT, for efficiency, in
138e8d8bef9SDimitry Andric /// release mode. The expectation is that a well-trained model provides a good
139e8d8bef9SDimitry Andric /// policy over a sufficiently diverse codebase, over many changes (i.e.
140e8d8bef9SDimitry Andric /// training happens seldom).
141e8d8bef9SDimitry Andric class DevelopmentModeMLInlineAdvisor : public MLInlineAdvisor {
142e8d8bef9SDimitry Andric public:
143e8d8bef9SDimitry Andric   DevelopmentModeMLInlineAdvisor(
144e8d8bef9SDimitry Andric       Module &M, ModuleAnalysisManager &MAM,
145e8d8bef9SDimitry Andric       std::unique_ptr<MLModelRunner> ModelRunner,
14604eeddc0SDimitry Andric       std::function<bool(CallBase &)> GetDefaultAdvice,
147e8d8bef9SDimitry Andric       std::unique_ptr<TrainingLogger> Logger);
148e8d8bef9SDimitry Andric 
149e8d8bef9SDimitry Andric   size_t getTotalSizeEstimate();
150e8d8bef9SDimitry Andric 
updateNativeSizeEstimate(int64_t Change)151e8d8bef9SDimitry Andric   void updateNativeSizeEstimate(int64_t Change) {
152e8d8bef9SDimitry Andric     *CurrentNativeSize += Change;
153e8d8bef9SDimitry Andric   }
154e8d8bef9SDimitry Andric   void resetNativeSize(Function *F) {
155fe6060f1SDimitry Andric     PreservedAnalyses PA = PreservedAnalyses::all();
156fe6060f1SDimitry Andric     PA.abandon<InlineSizeEstimatorAnalysis>();
157fe6060f1SDimitry Andric     FAM.invalidate(*F, PA);
158e8d8bef9SDimitry Andric   }
159e8d8bef9SDimitry Andric 
160e8d8bef9SDimitry Andric   std::unique_ptr<MLInlineAdvice>
161e8d8bef9SDimitry Andric   getAdviceFromModel(CallBase &CB, OptimizationRemarkEmitter &ORE) override;
162e8d8bef9SDimitry Andric 
163bdd1243dSDimitry Andric   std::optional<size_t> getNativeSizeEstimate(const Function &F) const;
164e8d8bef9SDimitry Andric 
165e8d8bef9SDimitry Andric private:
166e8d8bef9SDimitry Andric   bool isLogging() const { return !!Logger; }
167e8d8bef9SDimitry Andric   std::unique_ptr<MLInlineAdvice> getMandatoryAdviceImpl(CallBase &CB) override;
168e8d8bef9SDimitry Andric 
169e8d8bef9SDimitry Andric   const bool IsDoingInference;
170e8d8bef9SDimitry Andric   std::unique_ptr<TrainingLogger> Logger;
171e8d8bef9SDimitry Andric 
172bdd1243dSDimitry Andric   const std::optional<int32_t> InitialNativeSize;
173bdd1243dSDimitry Andric   std::optional<int32_t> CurrentNativeSize;
174e8d8bef9SDimitry Andric };
175e8d8bef9SDimitry Andric 
176e8d8bef9SDimitry Andric /// A variant of MLInlineAdvice that tracks all non-trivial inlining
177e8d8bef9SDimitry Andric /// decisions, for training/logging.
178e8d8bef9SDimitry Andric class LoggingMLInlineAdvice : public MLInlineAdvice {
179e8d8bef9SDimitry Andric public:
180e8d8bef9SDimitry Andric   LoggingMLInlineAdvice(DevelopmentModeMLInlineAdvisor *Advisor, CallBase &CB,
181e8d8bef9SDimitry Andric                         OptimizationRemarkEmitter &ORE, bool Recommendation,
182e8d8bef9SDimitry Andric                         TrainingLogger &Logger,
183bdd1243dSDimitry Andric                         std::optional<size_t> CallerSizeEstimateBefore,
184bdd1243dSDimitry Andric                         std::optional<size_t> CalleeSizeEstimateBefore,
185e8d8bef9SDimitry Andric                         bool DefaultDecision, bool Mandatory = false)
186e8d8bef9SDimitry Andric       : MLInlineAdvice(Advisor, CB, ORE, Recommendation), Logger(Logger),
187e8d8bef9SDimitry Andric         CallerSizeEstimateBefore(CallerSizeEstimateBefore),
188e8d8bef9SDimitry Andric         CalleeSizeEstimateBefore(CalleeSizeEstimateBefore),
189e8d8bef9SDimitry Andric         DefaultDecision(DefaultDecision), Mandatory(Mandatory) {}
190e8d8bef9SDimitry Andric 
191e8d8bef9SDimitry Andric   virtual ~LoggingMLInlineAdvice() = default;
192e8d8bef9SDimitry Andric 
193e8d8bef9SDimitry Andric private:
194e8d8bef9SDimitry Andric   DevelopmentModeMLInlineAdvisor *getAdvisor() const {
195e8d8bef9SDimitry Andric     return static_cast<DevelopmentModeMLInlineAdvisor *>(Advisor);
196e8d8bef9SDimitry Andric   }
197e8d8bef9SDimitry Andric   void recordInliningImpl() override {
198e8d8bef9SDimitry Andric     MLInlineAdvice::recordInliningImpl();
199e8d8bef9SDimitry Andric     getAdvisor()->resetNativeSize(Caller);
200e8d8bef9SDimitry Andric     int Reward = std::numeric_limits<int>::max();
201e8d8bef9SDimitry Andric     if (InlineSizeEstimatorAnalysis::isEvaluatorRequested() &&
202e8d8bef9SDimitry Andric         !getAdvisor()->isForcedToStop()) {
203e8d8bef9SDimitry Andric       int NativeSizeAfter = *getAdvisor()->getNativeSizeEstimate(*Caller) +
204e8d8bef9SDimitry Andric                             *CalleeSizeEstimateBefore;
205e8d8bef9SDimitry Andric       Reward = NativeSizeAfter -
206e8d8bef9SDimitry Andric                (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
207e8d8bef9SDimitry Andric       getAdvisor()->updateNativeSizeEstimate(Reward);
208e8d8bef9SDimitry Andric     }
209e8d8bef9SDimitry Andric     log(Reward, /*Success=*/true);
210e8d8bef9SDimitry Andric   }
211e8d8bef9SDimitry Andric 
212e8d8bef9SDimitry Andric   void recordInliningWithCalleeDeletedImpl() override {
213e8d8bef9SDimitry Andric     MLInlineAdvice::recordInliningWithCalleeDeletedImpl();
214e8d8bef9SDimitry Andric     getAdvisor()->resetNativeSize(Caller);
215e8d8bef9SDimitry Andric     if (InlineSizeEstimatorAnalysis::isEvaluatorRequested() &&
216e8d8bef9SDimitry Andric         !getAdvisor()->isForcedToStop()) {
217e8d8bef9SDimitry Andric       int NativeSizeAfter = *getAdvisor()->getNativeSizeEstimate(*Caller);
218e8d8bef9SDimitry Andric       int Reward = NativeSizeAfter -
219e8d8bef9SDimitry Andric                    (*CallerSizeEstimateBefore + *CalleeSizeEstimateBefore);
220e8d8bef9SDimitry Andric       getAdvisor()->updateNativeSizeEstimate(Reward);
221e8d8bef9SDimitry Andric       log(Reward, /*Success=*/true);
222349cc55cSDimitry Andric     } else {
223349cc55cSDimitry Andric       log(NoReward, /*Success=*/true);
224e8d8bef9SDimitry Andric     }
225e8d8bef9SDimitry Andric   }
226e8d8bef9SDimitry Andric 
227e8d8bef9SDimitry Andric   void recordUnsuccessfulInliningImpl(const InlineResult &Result) override {
228e8d8bef9SDimitry Andric     MLInlineAdvice::recordUnsuccessfulInliningImpl(Result);
229e8d8bef9SDimitry Andric     log(NoReward, /*Success=*/false);
230e8d8bef9SDimitry Andric   }
231e8d8bef9SDimitry Andric 
232e8d8bef9SDimitry Andric   void recordUnattemptedInliningImpl() override {
233e8d8bef9SDimitry Andric     MLInlineAdvice::recordUnattemptedInliningImpl();
234e8d8bef9SDimitry Andric     log(NoReward, /*Success=*/false);
235e8d8bef9SDimitry Andric   }
236e8d8bef9SDimitry Andric 
237e8d8bef9SDimitry Andric   void log(int64_t Reward, bool Success) {
238e8d8bef9SDimitry Andric     if (Mandatory)
239e8d8bef9SDimitry Andric       return;
240e8d8bef9SDimitry Andric     InlineEvent Event;
241e8d8bef9SDimitry Andric     Event.AdvisedDecision = isInliningRecommended();
242e8d8bef9SDimitry Andric     Event.DefaultDecision = DefaultDecision;
243e8d8bef9SDimitry Andric     Event.Effect = Success;
244e8d8bef9SDimitry Andric     Event.Reward = Reward;
245e8d8bef9SDimitry Andric     Logger.logInlineEvent(Event, getAdvisor()->getModelRunner());
246e8d8bef9SDimitry Andric   }
247e8d8bef9SDimitry Andric 
248e8d8bef9SDimitry Andric   static const int64_t NoReward = 0;
249e8d8bef9SDimitry Andric   TrainingLogger &Logger;
250bdd1243dSDimitry Andric   const std::optional<size_t> CallerSizeEstimateBefore;
251bdd1243dSDimitry Andric   const std::optional<size_t> CalleeSizeEstimateBefore;
252e8d8bef9SDimitry Andric   const int64_t DefaultDecision;
253e8d8bef9SDimitry Andric   const int64_t Mandatory;
254e8d8bef9SDimitry Andric };
255e8d8bef9SDimitry Andric 
2560eae32dcSDimitry Andric static const std::vector<TensorSpec> TrainingOnlyFeatures{
257e8d8bef9SDimitry Andric     TensorSpec::createSpec<float>(TFFeedPrefix + "discount", {1}),
258e8d8bef9SDimitry Andric     TensorSpec::createSpec<float>(TFFeedPrefix + "reward", {1}),
259e8d8bef9SDimitry Andric     TensorSpec::createSpec<int32_t>(TFFeedPrefix + "step_type", {1})};
2600eae32dcSDimitry Andric 
2610eae32dcSDimitry Andric static const std::vector<TensorSpec> getInputFeatures() {
2620eae32dcSDimitry Andric   std::vector<TensorSpec> InputSpecs;
2630eae32dcSDimitry Andric   for (size_t I = 0; I < NumberOfFeatures; ++I)
26481ad6265SDimitry Andric     InputSpecs.push_back(TensorSpec::createSpec<int64_t>(
26581ad6265SDimitry Andric         TFFeedPrefix + FeatureMap[I].name(), FeatureMap[I].shape()));
2660eae32dcSDimitry Andric   append_range(InputSpecs, TrainingOnlyFeatures);
2670eae32dcSDimitry Andric   return InputSpecs;
2680eae32dcSDimitry Andric }
2690eae32dcSDimitry Andric 
270e8d8bef9SDimitry Andric } // namespace
271e8d8bef9SDimitry Andric 
272e8d8bef9SDimitry Andric TrainingLogger::TrainingLogger(StringRef LogFileName,
273e8d8bef9SDimitry Andric                                const ModelUnderTrainingRunner *MUTR)
274e8d8bef9SDimitry Andric     : LogFileName(LogFileName), MUTR(MUTR) {
275e8d8bef9SDimitry Andric   // The first output is the inlining decision.
276bdd1243dSDimitry Andric   std::vector<TensorSpec> FT(FeatureMap.begin(), FeatureMap.end());
277e8d8bef9SDimitry Andric 
278bdd1243dSDimitry Andric   if (MUTR)
279bdd1243dSDimitry Andric     append_range(FT, MUTR->extraOutputsForLoggingSpecs());
280e8d8bef9SDimitry Andric 
281e8d8bef9SDimitry Andric   DefaultDecisionPos = FT.size();
28206c3fb27SDimitry Andric   FT.push_back(DefaultDecisionSpec);
283e8d8bef9SDimitry Andric 
284e8d8bef9SDimitry Andric   DecisionPos = FT.size();
28506c3fb27SDimitry Andric   FT.push_back(InlineDecisionSpec);
286bdd1243dSDimitry Andric   std::error_code EC;
287bdd1243dSDimitry Andric   auto OS = std::make_unique<raw_fd_ostream>(TrainingLog, EC);
288bdd1243dSDimitry Andric   if (EC)
289bdd1243dSDimitry Andric     dbgs() << (EC.message() + ":" + TrainingLog);
290e8d8bef9SDimitry Andric 
291e8d8bef9SDimitry Andric   L = std::make_unique<Logger>(
292bdd1243dSDimitry Andric       std::move(OS), FT, TensorSpec::createSpec<int64_t>(RewardName, {1}),
293e8d8bef9SDimitry Andric       InlineSizeEstimatorAnalysis::isEvaluatorRequested());
294bdd1243dSDimitry Andric   L->switchContext("");
295e8d8bef9SDimitry Andric }
296e8d8bef9SDimitry Andric 
297e8d8bef9SDimitry Andric /// Log one inlining event.
298e8d8bef9SDimitry Andric void TrainingLogger::logInlineEvent(const InlineEvent &Event,
299e8d8bef9SDimitry Andric                                     const MLModelRunner &ModelRunner) {
300bdd1243dSDimitry Andric   L->startObservation();
301e8d8bef9SDimitry Andric   size_t CurrentFeature = 0;
302bdd1243dSDimitry Andric   for (; CurrentFeature < NumberOfFeatures; ++CurrentFeature)
303bdd1243dSDimitry Andric     L->logTensorValue(CurrentFeature,
304bdd1243dSDimitry Andric                       reinterpret_cast<const char *>(
305bdd1243dSDimitry Andric                           ModelRunner.getTensorUntyped(CurrentFeature)));
306e8d8bef9SDimitry Andric 
307bdd1243dSDimitry Andric   if (MUTR)
308bdd1243dSDimitry Andric     for (size_t I = 0; I < MUTR->extraOutputsForLoggingSpecs().size(); ++I) {
309e8d8bef9SDimitry Andric       const char *RawData =
310bdd1243dSDimitry Andric           reinterpret_cast<const char *>(MUTR->getUntypedExtraOutputValue(I));
311bdd1243dSDimitry Andric       L->logTensorValue(CurrentFeature, RawData);
312e8d8bef9SDimitry Andric       ++CurrentFeature;
313e8d8bef9SDimitry Andric     }
314e8d8bef9SDimitry Andric 
315e8d8bef9SDimitry Andric   assert(CurrentFeature == DefaultDecisionPos);
316bdd1243dSDimitry Andric   L->logTensorValue(DefaultDecisionPos,
317bdd1243dSDimitry Andric                     reinterpret_cast<const char *>(&Event.DefaultDecision));
318bdd1243dSDimitry Andric   L->logTensorValue(DecisionPos,
319bdd1243dSDimitry Andric                     reinterpret_cast<const char *>(&Event.AdvisedDecision));
320bdd1243dSDimitry Andric   L->endObservation();
321e8d8bef9SDimitry Andric   if (InlineSizeEstimatorAnalysis::isEvaluatorRequested())
322bdd1243dSDimitry Andric     L->logReward(Event.Reward);
323e8d8bef9SDimitry Andric 
324e8d8bef9SDimitry Andric   // For debugging / later use
325e8d8bef9SDimitry Andric   Effects.push_back(Event.Effect);
326e8d8bef9SDimitry Andric }
327e8d8bef9SDimitry Andric 
328e8d8bef9SDimitry Andric DevelopmentModeMLInlineAdvisor::DevelopmentModeMLInlineAdvisor(
329e8d8bef9SDimitry Andric     Module &M, ModuleAnalysisManager &MAM,
330e8d8bef9SDimitry Andric     std::unique_ptr<MLModelRunner> ModelRunner,
33104eeddc0SDimitry Andric     std::function<bool(CallBase &)> GetDefaultAdvice,
332e8d8bef9SDimitry Andric     std::unique_ptr<TrainingLogger> Logger)
33306c3fb27SDimitry Andric     : MLInlineAdvisor(M, MAM, std::move(ModelRunner), GetDefaultAdvice),
33404eeddc0SDimitry Andric       IsDoingInference(isa<ModelUnderTrainingRunner>(getModelRunner())),
335e8d8bef9SDimitry Andric       Logger(std::move(Logger)),
336e8d8bef9SDimitry Andric       InitialNativeSize(isLogging() ? getTotalSizeEstimate() : 0),
337e8d8bef9SDimitry Andric       CurrentNativeSize(InitialNativeSize) {
338e8d8bef9SDimitry Andric   // We cannot have the case of neither inference nor logging.
339e8d8bef9SDimitry Andric   assert(IsDoingInference || isLogging());
340e8d8bef9SDimitry Andric }
341e8d8bef9SDimitry Andric 
342bdd1243dSDimitry Andric std::optional<size_t>
343e8d8bef9SDimitry Andric DevelopmentModeMLInlineAdvisor::getNativeSizeEstimate(const Function &F) const {
344e8d8bef9SDimitry Andric   if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested())
345bdd1243dSDimitry Andric     return std::nullopt;
346e8d8bef9SDimitry Andric   auto &R =
347e8d8bef9SDimitry Andric       FAM.getResult<InlineSizeEstimatorAnalysis>(const_cast<Function &>(F));
348e8d8bef9SDimitry Andric   if (!R) {
349e8d8bef9SDimitry Andric     F.getParent()->getContext().emitError(
350e8d8bef9SDimitry Andric         "Native size estimator is not present.");
351e8d8bef9SDimitry Andric     return 0;
352e8d8bef9SDimitry Andric   }
353e8d8bef9SDimitry Andric   return *R;
354e8d8bef9SDimitry Andric }
355e8d8bef9SDimitry Andric 
356e8d8bef9SDimitry Andric std::unique_ptr<MLInlineAdvice>
357e8d8bef9SDimitry Andric DevelopmentModeMLInlineAdvisor::getMandatoryAdviceImpl(CallBase &CB) {
358e8d8bef9SDimitry Andric   return std::make_unique<LoggingMLInlineAdvice>(
359e8d8bef9SDimitry Andric       /*Advisor=*/this,
360e8d8bef9SDimitry Andric       /*CB=*/CB, /*ORE=*/getCallerORE(CB), /*Recommendation=*/true,
361e8d8bef9SDimitry Andric       /*Logger=*/*Logger,
362e8d8bef9SDimitry Andric       /*CallerSizeEstimateBefore=*/getNativeSizeEstimate(*CB.getCaller()),
363e8d8bef9SDimitry Andric       /*CalleeSizeEstimateBefore=*/
364e8d8bef9SDimitry Andric       getNativeSizeEstimate(*CB.getCalledFunction()),
365e8d8bef9SDimitry Andric       /*DefaultDecision=*/true, /*Mandatory*/ true);
366e8d8bef9SDimitry Andric }
367e8d8bef9SDimitry Andric 
368e8d8bef9SDimitry Andric std::unique_ptr<MLInlineAdvice>
369e8d8bef9SDimitry Andric DevelopmentModeMLInlineAdvisor::getAdviceFromModel(
370e8d8bef9SDimitry Andric     CallBase &CB, OptimizationRemarkEmitter &ORE) {
371e8d8bef9SDimitry Andric   if (IsDoingInference && !isLogging())
372e8d8bef9SDimitry Andric     return MLInlineAdvisor::getAdviceFromModel(CB, ORE);
373e8d8bef9SDimitry Andric 
374e8d8bef9SDimitry Andric   bool DefaultAdvice = GetDefaultAdvice(CB);
3750eae32dcSDimitry Andric   auto Recommendation =
3760eae32dcSDimitry Andric       IsDoingInference ? static_cast<bool>(ModelRunner->evaluate<int64_t>())
3770eae32dcSDimitry Andric                        : DefaultAdvice;
378e8d8bef9SDimitry Andric   return std::make_unique<LoggingMLInlineAdvice>(
379e8d8bef9SDimitry Andric       /*Advisor=*/this,
380e8d8bef9SDimitry Andric       /*CB=*/CB, /*ORE=*/ORE, /*Recommendation=*/Recommendation,
381e8d8bef9SDimitry Andric       /*Logger=*/*Logger,
382e8d8bef9SDimitry Andric       /*CallerSizeEstimateBefore=*/getNativeSizeEstimate(*CB.getCaller()),
383e8d8bef9SDimitry Andric       /*CalleeSizeEstimateBefore=*/
384e8d8bef9SDimitry Andric       getNativeSizeEstimate(*CB.getCalledFunction()),
385e8d8bef9SDimitry Andric       /*DefaultDecision=*/DefaultAdvice);
386e8d8bef9SDimitry Andric }
387e8d8bef9SDimitry Andric 
388e8d8bef9SDimitry Andric size_t DevelopmentModeMLInlineAdvisor::getTotalSizeEstimate() {
389e8d8bef9SDimitry Andric   if (!InlineSizeEstimatorAnalysis::isEvaluatorRequested())
390e8d8bef9SDimitry Andric     return 0;
391e8d8bef9SDimitry Andric   size_t Ret = 0;
392e8d8bef9SDimitry Andric   for (auto &F : M) {
393e8d8bef9SDimitry Andric     if (F.isDeclaration())
394e8d8bef9SDimitry Andric       continue;
395e8d8bef9SDimitry Andric     Ret += *getNativeSizeEstimate(F);
396e8d8bef9SDimitry Andric   }
397e8d8bef9SDimitry Andric   return Ret;
398e8d8bef9SDimitry Andric }
399e8d8bef9SDimitry Andric 
400e8d8bef9SDimitry Andric std::unique_ptr<InlineAdvisor> llvm::getDevelopmentModeAdvisor(
401e8d8bef9SDimitry Andric     Module &M, ModuleAnalysisManager &MAM,
402e8d8bef9SDimitry Andric     std::function<bool(CallBase &)> GetDefaultAdvice) {
403e8d8bef9SDimitry Andric   auto &Ctx = M.getContext();
404e8d8bef9SDimitry Andric   std::unique_ptr<MLModelRunner> Runner;
405e8d8bef9SDimitry Andric   if (TFModelUnderTrainingPath.empty())
4060eae32dcSDimitry Andric     Runner.reset(new NoInferenceModelRunner(Ctx, getInputFeatures()));
40704eeddc0SDimitry Andric   else
40804eeddc0SDimitry Andric     Runner = ModelUnderTrainingRunner::createAndEnsureValid(
40904eeddc0SDimitry Andric         Ctx, TFModelUnderTrainingPath, DecisionName, getInputFeatures(),
41004eeddc0SDimitry Andric         TFOutputSpecOverride);
41104eeddc0SDimitry Andric   if (!Runner)
412e8d8bef9SDimitry Andric     return nullptr;
413e8d8bef9SDimitry Andric   std::unique_ptr<TrainingLogger> Logger;
414e8d8bef9SDimitry Andric   if (!TrainingLog.empty())
41504eeddc0SDimitry Andric     Logger = std::make_unique<TrainingLogger>(
41604eeddc0SDimitry Andric         TrainingLog, dyn_cast<ModelUnderTrainingRunner>(Runner.get()));
417e8d8bef9SDimitry Andric 
418e8d8bef9SDimitry Andric   return std::make_unique<DevelopmentModeMLInlineAdvisor>(
41904eeddc0SDimitry Andric       M, MAM, std::move(Runner), GetDefaultAdvice, std::move(Logger));
420e8d8bef9SDimitry Andric }
421bdd1243dSDimitry Andric #endif // defined(LLVM_HAVE_TFLITE)
422