LLVM 24.0.0git
MLGOUtils.h
Go to the documentation of this file.
1//===- MLGOUtils.h - Utilities for MLGO Release Mode ------------*- C++ -*-===//
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 provides helper functions for creating MLModelRunners and checking
11/// model validity in release mode.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_ANALYSIS_UTILS_MLGOUTILS_H
16#define LLVM_ANALYSIS_UTILS_MLGOUTILS_H
17
18#include "llvm/ADT/StringRef.h"
23#include "llvm/IR/LLVMContext.h"
25#include <memory>
26#include <string>
27#include <vector>
28
29namespace llvm {
30
31/// Helper to check if a release-mode ML advisor has a valid model to execute.
32/// Overload for cl::opt<EnumType>.
33template <class CompiledModelType, class EnumType, bool ExternalStorage,
34 class ParserClass>
38 EnumType DefaultModelVal = EnumType::Default) {
41 SelectedModel != DefaultModelVal;
42}
43
44/// Helper to check if a release-mode ML advisor has a valid model to execute.
45/// Overload for plain EnumType.
46template <class CompiledModelType, class EnumType>
48 EnumType SelectedModel,
49 EnumType DefaultModelVal = EnumType::Default) {
52 SelectedModel != DefaultModelVal;
53}
54
55/// Helper to construct the appropriate MLModelRunner in release mode:
56/// 1. InteractiveModelRunner if an interactive channel is specified.
57/// 2. EmitCModelRunner if MLIR lowering is enabled.
58/// 3. ReleaseModeModelRunner<CompiledModelType> otherwise.
59template <class CompiledModelType, bool HaveMLIRLowering, class CreateEmitCFunc>
60std::unique_ptr<MLModelRunner> createReleaseModeModelRunner(
61 LLVMContext &Ctx, const std::vector<TensorSpec> &InputFeatures,
63 const TensorSpec &InteractiveDecisionSpec,
64 CreateEmitCFunc &&CreateEmitCModelRunner,
66 if (!InteractiveChannelBaseName.empty()) {
67 return std::make_unique<InteractiveModelRunner>(
68 Ctx, InputFeatures, InteractiveDecisionSpec,
71 }
72 if constexpr (HaveMLIRLowering) {
73 return CreateEmitCModelRunner(Ctx, InputFeatures);
74 } else {
75 return std::make_unique<ReleaseModeModelRunner<CompiledModelType>>(
77 }
78}
79
80} // namespace llvm
81
82#endif // LLVM_ANALYSIS_UTILS_MLGOUTILS_H
static LVOptions Options
Definition LVOptions.cpp:25
NoopSavedModelImpl CompiledModelType
static cl::opt< std::string > InteractiveChannelBaseName("inliner-interactive-channel-base", cl::Hidden, cl::desc("Base file path for the interactive mode. The incoming filename should " "have the name <inliner-interactive-channel-base>.in, while the " "outgoing name should be <inliner-interactive-channel-base>.out"))
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
This is an optimization pass for GlobalISel generic memory operations.
bool isEmbeddedModelEvaluatorValid()
bool isReleaseModelValid(StringRef InteractiveChannelBaseName, const cl::opt< EnumType, ExternalStorage, ParserClass > &SelectedModel, EnumType DefaultModelVal=EnumType::Default)
Helper to check if a release-mode ML advisor has a valid model to execute.
Definition MLGOUtils.h:35
LLVM_ABI const char *const DecisionName
static const std::vector< TensorSpec > InputFeatures
std::unique_ptr< MLModelRunner > createReleaseModeModelRunner(LLVMContext &Ctx, const std::vector< TensorSpec > &InputFeatures, StringRef DecisionName, const std::string &InteractiveChannelBaseName, const TensorSpec &InteractiveDecisionSpec, CreateEmitCFunc &&CreateEmitCModelRunner, const EmbeddedModelRunnerOptions &Options={})
Helper to construct the appropriate MLModelRunner in release mode:
Definition MLGOUtils.h:60
ReleaseModeModelRunner - production mode implementation of the MLModelRunner.