LLVM 24.0.0git
Calls.h
Go to the documentation of this file.
1//===------- Calls.h - Runtime-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// Runtime-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. Concrete implementations live in subdirectories
13// (e.g. RTBridge/SPS).
14//
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_EXECUTIONENGINE_ORC_RTBRIDGE_CALLS_H
18#define LLVM_EXECUTIONENGINE_ORC_RTBRIDGE_CALLS_H
19
20#include "llvm/ADT/ArrayRef.h"
23#include "llvm/Support/Error.h"
25
26#include <cstdint>
27#include <future>
28#include <string>
29#include <type_traits>
30
31namespace llvm::orc::rt {
32
33template <typename FnT> class Caller;
34
35/// Runtime-agnostic interface for invoking an executor-side operation with the
36/// signature RetT(ArgTs...).
37///
38/// Two call operators are provided: an asynchronous form that delivers the
39/// result to an OnComplete continuation, and a synchronous form that blocks
40/// until the result is available.
41///
42/// A Caller abstracts over how the operation is dispatched to the executor.
43/// Concrete implementations (e.g. rt::sps::Caller) supply the dispatch
44/// mechanism.
45template <typename RetT, typename... ArgTs> class Caller<RetT(ArgTs...)> {
46public:
47 using FnType = RetT(ArgTs...);
48
49 /// The result type produced by the executor-side function itself.
50 using CalleeRetT = RetT;
51
52 /// The result type delivered to callers: Expected<RetT>, or Error when RetT
53 /// is void, so that dispatch failures can be reported alongside the result.
54 using ErrorRetT =
55 std::conditional_t<std::is_void_v<RetT>, Error, Expected<RetT>>;
56
57 virtual ~Caller() = default;
58
59 /// Asynchronously invoke the operation with the given Args, delivering its
60 /// result (or an error) to OnComplete.
61 virtual void operator()(unique_function<void(ErrorRetT)> OnComplete,
62 ArgTs... Args) = 0;
63
64 /// Invoke the operation with the given Args, blocking until its result (or an
65 /// error) is available.
66 ErrorRetT operator()(ArgTs... Args) {
67 using PromiseValT = std::conditional_t<std::is_void_v<RetT>, MSVCPError,
69 std::promise<PromiseValT> P;
70 auto F = P.get_future();
71 this->operator()(
72 [P = std::move(P)](ErrorRetT R) mutable { P.set_value(std::move(R)); },
73 std::move(Args)...);
74 return F.get();
75 }
76};
77
78/// Runtime-agnostic interface for running a main-like function
79/// (int(int argc, char *argv[])) in the executor.
80///
81/// The function to run is given by its ExecutorAddr, its arguments as an
82/// argument vector, and its int64_t result is returned.
83class MainCaller : public Caller<int64_t(ExecutorAddr, ArrayRef<std::string>)> {
84};
85
86/// Runtime-agnostic interface for running a void() function in the executor.
87///
88/// The function to run is given by its ExecutorAddr.
89///
90/// WARNING: This Caller is experimental and may be removed.
91class VoidVoidCaller : public Caller<void(ExecutorAddr)> {};
92
93/// Runtime-agnostic interface for running an int32_t() function in the
94/// executor.
95///
96/// The function to run is given by its ExecutorAddr.
97///
98/// WARNING: This Caller is experimental and may be removed.
99class Int32VoidCaller : public Caller<int32_t(ExecutorAddr)> {};
100
101/// Runtime-agnostic interface for running an int32_t(int32_t) function in the
102/// executor.
103///
104/// The function to run is given by its ExecutorAddr.
105///
106/// WARNING: This Caller is experimental and may be removed.
107class Int32Int32Caller : public Caller<int32_t(ExecutorAddr, int32_t)> {};
108
109} // namespace llvm::orc::rt
110
111#endif // LLVM_EXECUTIONENGINE_ORC_RTBRIDGE_CALLS_H
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 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
virtual void operator()(unique_function< void(ErrorRetT)> OnComplete, ArgTs... Args)=0
Asynchronously invoke the operation with the given Args, delivering its result (or an error) to OnCom...
ErrorRetT operator()(ArgTs... Args)
Invoke the operation with the given Args, blocking until its result (or an error) is available.
Definition Calls.h:66
RetT CalleeRetT
The result type produced by the executor-side function itself.
Definition Calls.h:50
std::conditional_t< std::is_void_v< RetT >, Error, Expected< RetT > > ErrorRetT
The result type delivered to callers: Expected<RetT>, or Error when RetT is void, so that dispatch fa...
Definition Calls.h:54
Runtime-agnostic interface for running an int32_t(int32_t) function in the executor.
Definition Calls.h:107
Runtime-agnostic interface for running an int32_t() function in the executor.
Definition Calls.h:99
Runtime-agnostic interface for running a main-like function (int(int argc, char *argv[])) in the exec...
Definition Calls.h:83
Runtime-agnostic interface for running a void() function in the executor.
Definition Calls.h:91
unique_function is a type-erasing functor similar to std::function.