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/// The operation is identified by an ExecutorAddr (the address of the
39/// executor-side function to invoke) and takes ArgTs... as arguments. Two call
40/// operators are provided: an asynchronous form that delivers the result to an
41/// OnComplete continuation, and a synchronous form that blocks until the result
42/// is available.
43///
44/// A Caller abstracts over how the operation is dispatched to the executor.
45/// Concrete implementations (e.g. rt::sps::Caller) supply the dispatch
46/// mechanism.
47template <typename RetT, typename... ArgTs> class Caller<RetT(ArgTs...)> {
48public:
49 using FnType = RetT(ArgTs...);
50
51 /// The result type produced by the executor-side function itself.
52 using CalleeRetT = RetT;
53
54 /// The result type delivered to callers: Expected<RetT>, or Error when RetT
55 /// is void, so that dispatch failures can be reported alongside the result.
56 using ErrorRetT =
57 std::conditional_t<std::is_void_v<RetT>, Error, Expected<RetT>>;
58
59 virtual ~Caller() = default;
60
61 /// Asynchronously invoke the executor-side function at FnAddr with the given
62 /// Args, delivering its result (or an error) to OnComplete.
63 virtual void operator()(unique_function<void(ErrorRetT)> OnComplete,
64 ExecutorAddr FnAddr, ArgTs... Args) = 0;
65
66 /// Invoke the executor-side function at FnAddr with the given Args, blocking
67 /// until its result (or an error) is available.
68 ErrorRetT operator()(ExecutorAddr FnAddr, ArgTs &&...Args) {
69 using PromiseValT = std::conditional_t<std::is_void_v<RetT>, MSVCPError,
71 std::promise<PromiseValT> P;
72 auto F = P.get_future();
73 this->operator()(
74 [P = std::move(P)](ErrorRetT R) mutable { P.set_value(std::move(R)); },
75 FnAddr, std::forward<ArgTs>(Args)...);
76 return F.get();
77 }
78};
79
80/// Runtime-agnostic interface for running a main-like function
81/// (int(int argc, char *argv[])) in the executor.
82///
83/// The function to run is given by its ExecutorAddr, its arguments as an
84/// argument vector, and its int64_t result is returned.
85class MainCaller : public Caller<int64_t(ArrayRef<std::string>)> {};
86
87/// Runtime-agnostic interface for running a void() function in the executor.
88///
89/// The function to run is given by its ExecutorAddr.
90///
91/// WARNING: This Caller is experimental and may be removed.
92class VoidVoidCaller : public Caller<void()> {};
93
94/// Runtime-agnostic interface for running an int32_t() function in the
95/// executor.
96///
97/// The function to run is given by its ExecutorAddr.
98///
99/// WARNING: This Caller is experimental and may be removed.
100class Int32VoidCaller : public Caller<int32_t()> {};
101
102/// Runtime-agnostic interface for running an int32_t(int32_t) function in the
103/// executor.
104///
105/// The function to run is given by its ExecutorAddr.
106///
107/// WARNING: This Caller is experimental and may be removed.
108class Int32Int32Caller : public Caller<int32_t(int32_t)> {};
109
110} // namespace llvm::orc::rt
111
112#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
Represents an address in the executor process.
RetT CalleeRetT
The result type produced by the executor-side function itself.
Definition Calls.h:52
virtual void operator()(unique_function< void(ErrorRetT)> OnComplete, ExecutorAddr FnAddr, ArgTs... Args)=0
Asynchronously invoke the executor-side function at FnAddr with the given Args, delivering its result...
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:56
ErrorRetT operator()(ExecutorAddr FnAddr, ArgTs &&...Args)
Invoke the executor-side function at FnAddr with the given Args, blocking until its result (or an err...
Definition Calls.h:68
Runtime-agnostic interface for running an int32_t(int32_t) function in the executor.
Definition Calls.h:108
Runtime-agnostic interface for running an int32_t() function in the executor.
Definition Calls.h:100
Runtime-agnostic interface for running a main-like function (int(int argc, char *argv[])) in the exec...
Definition Calls.h:85
Runtime-agnostic interface for running a void() function in the executor.
Definition Calls.h:92
unique_function is a type-erasing functor similar to std::function.