LLVM 24.0.0git
Proxy.h
Go to the documentation of this file.
1//===------- Proxy.h - Protocol-agnostic executor call APIs -----*- 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// Protocol-agnostic interfaces for invoking executor-side operations. These
10// abstract over how a call reaches the executor, so clients can be written
11// once and used whether the operation is provided by a full ORC runtime or by
12// LLVM's own ORC-runtime-lite.
13//
14// This header provides only the core Proxy machinery. A Proxy's dispatch
15// function is supplied by a spec for some concrete protocol -- see
16// SPSProxySpec.h for the Simple Packed Serialization implementation. Named
17// proxies for specific operation families live alongside the utilities that use
18// them (e.g. CallProxies.h, EPCGenericMemoryAccess.h).
19//
20//===----------------------------------------------------------------------===//
21
22#ifndef LLVM_EXECUTIONENGINE_ORC_PROXY_H
23#define LLVM_EXECUTIONENGINE_ORC_PROXY_H
24
28#include "llvm/Support/Error.h"
30
31#include <future>
32#include <type_traits>
33
34namespace llvm::orc {
35
36class ProxyBase {
37public:
38 ProxyBase() = default;
39 ProxyBase(ExecutorAddr CalleeAddr) : CalleeAddr(CalleeAddr) {}
40
41 /// Returns the address of the callee in the executor.
42 const ExecutorAddr &calleeAddr() const { return CalleeAddr; }
43
44 /// Evaluates to true if the callee is non-null.
45 explicit operator bool() const { return !!CalleeAddr; }
46
47private:
48 ExecutorAddr CalleeAddr;
49};
50
51template <typename FnT> class Proxy;
52
53namespace detail {
54
55/// Maps a proxy's callee return type to the type delivered to the client, so a
56/// dispatch failure can always be reported alongside the result:
57///
58/// void -> Error
59/// Error -> Error
60/// T -> Expected<T>
61/// Expected<T> -> Expected<T>
62template <typename T> struct ProxyErrorRet {
64};
65template <> struct ProxyErrorRet<void> {
66 using type = Error;
67};
68template <> struct ProxyErrorRet<Error> {
69 using type = Error;
70};
71template <typename T> struct ProxyErrorRet<Expected<T>> {
73};
74
75/// Maps a proxy's client-facing return type to the std::promise value type used
76/// by the blocking call operator (working around MSVC's std::promise).
77template <typename T> struct ProxyRetPromise;
78template <> struct ProxyRetPromise<Error> {
79 using type = std::promise<MSVCPError>;
80};
81template <typename T> struct ProxyRetPromise<Expected<T>> {
82 using type = std::promise<MSVCPExpected<T>>;
83};
84
85} // namespace detail
86
87/// Protocol-agnostic interface for invoking an executor-side operation with the
88/// signature RetT(ArgTs...).
89///
90/// Two call operators are provided: an asynchronous form that delivers the
91/// result to an OnComplete continuation, and a synchronous form that blocks
92/// until the result is available.
93///
94/// A Proxy abstracts over how the operation is dispatched to the executor. Its
95/// dispatch function is supplied by a spec (e.g. sps::ProxySpec).
96template <typename RetT, typename... ArgTs>
97class Proxy<RetT(ArgTs...)> : public ProxyBase {
98public:
99 using FnType = RetT(ArgTs...);
100
101 /// The result type produced by the executor-side function itself.
102 using CalleeRetT = RetT;
103
104 /// The result type delivered to the client: Error when the callee returns
105 /// void or Error, otherwise Expected<T> (with Expected<T> callees flattened
106 /// rather than nested), so that dispatch failures can be reported alongside
107 /// the result.
109
110 using DispatchFn = void (*)(unique_function<void(ErrorRetT)> OnComplete,
111 ExecutionSession &ES, ExecutorAddr Callee,
112 const ArgTs &...Args);
113
114 Proxy() = default;
115 Proxy(DispatchFn Dispatch, ExecutorAddr CalleeAddr)
116 : ProxyBase(CalleeAddr), Dispatch(Dispatch) {}
117
118 /// Asynchronously invoke the operation with the given Args, delivering its
119 /// result (or an error) to OnComplete.
120 void operator()(unique_function<void(ErrorRetT)> OnComplete,
121 ExecutionSession &ES, const ArgTs &...Args) const {
122 assert(Dispatch && "Proxy's Dispatch member is not set");
123 Dispatch(std::move(OnComplete), ES, calleeAddr(), Args...);
124 }
125
126 /// Invoke the operation with the given Args, blocking until its result (or an
127 /// error) is available.
128 ErrorRetT operator()(ExecutionSession &ES, const ArgTs &...Args) const {
130 auto F = P.get_future();
131 this->operator()(
132 [P = std::move(P)](ErrorRetT R) mutable { P.set_value(std::move(R)); },
133 ES, Args...);
134 return F.get();
135 }
136
137private:
138 DispatchFn Dispatch = nullptr;
139};
140
141} // namespace llvm::orc
142
143#endif // LLVM_EXECUTIONENGINE_ORC_PROXY_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file provides a collection of function (or more generally, callable) type erasure utilities supp...
#define F(x, y, z)
Definition MD5.cpp:54
#define T
#define P(N)
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
An ExecutionSession represents a running JIT program.
Definition Core.h:1111
Represents an address in the executor process.
const ExecutorAddr & calleeAddr() const
Returns the address of the callee in the executor.
Definition Proxy.h:42
ProxyBase(ExecutorAddr CalleeAddr)
Definition Proxy.h:39
void(*)(unique_function< void(ErrorRetT)> OnComplete, ExecutionSession &ES, ExecutorAddr Callee, const ArgTs &...Args) DispatchFn
Definition Proxy.h:110
ErrorRetT operator()(ExecutionSession &ES, const ArgTs &...Args) const
Invoke the operation with the given Args, blocking until its result (or an error) is available.
Definition Proxy.h:128
Proxy(DispatchFn Dispatch, ExecutorAddr CalleeAddr)
Definition Proxy.h:115
void operator()(unique_function< void(ErrorRetT)> OnComplete, ExecutionSession &ES, const ArgTs &...Args) const
Asynchronously invoke the operation with the given Args, delivering its result (or an error) to OnCom...
Definition Proxy.h:120
RetT CalleeRetT
The result type produced by the executor-side function itself.
Definition Proxy.h:102
typename detail::ProxyErrorRet< RetT >::type ErrorRetT
The result type delivered to the client: Error when the callee returns void or Error,...
Definition Proxy.h:108
unique_function is a type-erasing functor similar to std::function.
Maps a proxy's callee return type to the type delivered to the client, so a dispatch failure can alwa...
Definition Proxy.h:62
std::promise< MSVCPError > type
Definition Proxy.h:79
std::promise< MSVCPExpected< T > > type
Definition Proxy.h:82
Maps a proxy's client-facing return type to the std::promise value type used by the blocking call ope...
Definition Proxy.h:77