LLVM  14.0.0git
ReleaseModeModelRunner.cpp
Go to the documentation of this file.
1 //===- ReleaseModeModelRunner.cpp - Fast, precompiled model runner -------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a model runner wrapping an AOT compiled ML model.
10 // Only inference is supported.
11 //
12 //===----------------------------------------------------------------------===//
13 #include "llvm/Config/config.h"
14 #if defined(LLVM_HAVE_TF_AOT)
15 
18 
19 // codegen-ed file
20 #include "InlinerSizeModel.h" // NOLINT
21 
22 #include <memory>
23 #include <vector>
24 
25 using namespace llvm;
26 namespace {
27 
28 const char FeedPrefix[] = "feed_";
29 const char FetchPrefix[] = "fetch_";
30 
31 /// MLModelRunner - production mode implementation. It uses a AOT-compiled
32 /// SavedModel for efficient execution.
33 class ReleaseModeModelRunner final : public MLModelRunner {
34 public:
35  ReleaseModeModelRunner(LLVMContext &Ctx);
36  virtual ~ReleaseModeModelRunner() = default;
37 
38  bool run() override;
39 
40  void setFeature(FeatureIndex Index, int64_t Value) override;
41  int64_t getFeature(int Index) const override;
42 
43 private:
44  std::vector<int32_t> FeatureIndices;
45  int32_t ResultIndex = -1;
46  std::unique_ptr<llvm::InlinerSizeModel> CompiledModel;
47 };
48 } // namespace
49 
50 ReleaseModeModelRunner::ReleaseModeModelRunner(LLVMContext &Ctx)
51  : MLModelRunner(Ctx),
52  CompiledModel(std::make_unique<llvm::InlinerSizeModel>()) {
53  assert(CompiledModel && "The CompiledModel should be valid");
54 
55  FeatureIndices.resize(NumberOfFeatures);
56 
57  for (size_t I = 0; I < NumberOfFeatures; ++I) {
58  const int Index =
59  CompiledModel->LookupArgIndex(FeedPrefix + FeatureNameMap[I]);
60  assert(Index >= 0 && "Cannot find Feature in inlining model");
61  FeatureIndices[I] = Index;
62  }
63 
64  ResultIndex =
65  CompiledModel->LookupResultIndex(std::string(FetchPrefix) + DecisionName);
66  assert(ResultIndex >= 0 && "Cannot find DecisionName in inlining model");
67 }
68 
69 int64_t ReleaseModeModelRunner::getFeature(int Index) const {
70  return *static_cast<int64_t *>(
71  CompiledModel->arg_data(FeatureIndices[Index]));
72 }
73 
74 void ReleaseModeModelRunner::setFeature(FeatureIndex Index, int64_t Value) {
75  *static_cast<int64_t *>(CompiledModel->arg_data(
76  FeatureIndices[static_cast<size_t>(Index)])) = Value;
77 }
78 
79 bool ReleaseModeModelRunner::run() {
80  CompiledModel->Run();
81  return static_cast<bool>(
82  *static_cast<int64_t *>(CompiledModel->result_data(ResultIndex)));
83 }
84 
85 std::unique_ptr<InlineAdvisor>
86 llvm::getReleaseModeAdvisor(Module &M, ModuleAnalysisManager &MAM) {
87  auto AOTRunner = std::make_unique<ReleaseModeModelRunner>(M.getContext());
88  return std::make_unique<MLInlineAdvisor>(M, MAM, std::move(AOTRunner));
89 }
90 #endif // defined(LLVM_HAVE_TF_AOT)
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
MAM
ModuleAnalysisManager MAM
Definition: PassBuilderBindings.cpp:61
llvm::FeatureNameMap
const std::array< std::string, NumberOfFeatures > FeatureNameMap
Index
uint32_t Index
Definition: ELFObjHandler.cpp:84
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:59
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::FeatureIndex
FeatureIndex
Definition: InlineModelFeatureMaps.h:107
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
llvm::DecisionName
const char *const DecisionName
std
Definition: BitVector.h:838
MLInlineAdvisor.h
llvm::AnalysisManager
A container for analyses that lazily runs them and caches their results.
Definition: InstructionSimplify.h:44
InlineModelFeatureMaps.h
llvm::NumberOfFeatures
constexpr size_t NumberOfFeatures
Definition: InlineModelFeatureMaps.h:127
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::MLModelRunner
MLModelRunner interface: abstraction of a mechanism for evaluating a tensorflow "saved model".
Definition: MLModelRunner.h:21