LLVM 24.0.0git
EmitCModelRunner.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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/// \file
10/// This file implements a model runner wrapping an EmitC compiled ML model.
11///
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_ANALYSIS_EMITCMODELRUNNER_H
15#define LLVM_ANALYSIS_EMITCMODELRUNNER_H
16
19
20#include <type_traits>
21
22namespace llvm {
23
24template <class TGen> class EmitCModelRunner final : public MLModelRunner {
25public:
26 template <class FType>
27 EmitCModelRunner(LLVMContext &Ctx, const FType &InputSpec)
28 : MLModelRunner(Ctx, MLModelRunner::Kind::Release, InputSpec.size()) {
29 for (auto [I, Spec] : llvm::enumerate(InputSpec))
30 populateTensor(I, Spec);
31 }
32
33 ~EmitCModelRunner() override = default;
34
35 static bool classof(const MLModelRunner *R) {
36 return R->getKind() == MLModelRunner::Kind::Release;
37 }
38
39protected:
40 void *evaluateUntyped() override { return evaluateImpl(); }
41
42private:
43 void populateTensor(size_t Pos, const TensorSpec &Spec) {
44 void *Buffer = nullptr;
45 auto It = CompiledModel.reflectionMap.find(Spec.name());
46 if (It != CompiledModel.reflectionMap.end())
47 Buffer = static_cast<void *>(It->second);
48 setUpBufferForTensor(Pos, Spec, Buffer);
49 }
50
51 using ResultType = decltype(std::declval<TGen>()());
52 static_assert(!std::is_void_v<ResultType>,
53 "EmitCModelRunner models must return a non-void result.");
54
55 void *evaluateImpl() {
56 Result = CompiledModel();
57 return &Result;
58 }
59
60 ResultType Result = {};
61 TGen CompiledModel = {};
62};
63
64} // namespace llvm
65
66#endif // LLVM_ANALYSIS_EMITCMODELRUNNER_H
#define I(x, y, z)
Definition MD5.cpp:57
void * evaluateUntyped() override
~EmitCModelRunner() override=default
EmitCModelRunner(LLVMContext &Ctx, const FType &InputSpec)
static bool classof(const MLModelRunner *R)
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
void setUpBufferForTensor(size_t Index, const TensorSpec &Spec, void *Buffer)
MLModelRunner(const MLModelRunner &)=delete
LLVMContext & Ctx
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1669
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2554