1 //===- FunctionExtras.h - Function type erasure utilities -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file provides a collection of function (or more generally, callable)
11 /// type erasure utilities supplementing those provided by the standard library
14 /// It provides `unique_function`, which works like `std::function` but supports
15 /// move-only callable objects.
18 /// - Add a `function` that provides const, volatile, and ref-qualified support,
19 /// which doesn't work with `std::function`.
20 /// - Provide support for specifying multiple signatures to type erase callable
21 /// objects with an overload set, such as those produced by generic lambdas.
22 /// - Expand to include a copyable utility that directly replaces std::function
23 /// but brings the above improvements.
25 /// Note that LLVM's utilities are greatly simplified by not supporting
28 /// If the standard library ever begins to provide comparable facilities we can
29 /// consider switching to those.
31 //===----------------------------------------------------------------------===//
33 #ifndef LLVM_ADT_FUNCTION_EXTRAS_H
34 #define LLVM_ADT_FUNCTION_EXTRAS_H
36 #include "llvm/ADT/PointerIntPair.h"
37 #include "llvm/ADT/PointerUnion.h"
38 #include "llvm/Support/type_traits.h"
43 template <typename FunctionT> class unique_function;
45 template <typename ReturnT, typename... ParamTs>
46 class unique_function<ReturnT(ParamTs...)> {
47 static constexpr size_t InlineStorageSize = sizeof(void *) * 3;
49 // MSVC has a bug and ICEs if we give it a particular dependent value
50 // expression as part of the `std::conditional` below. To work around this,
51 // we build that into a template struct's constexpr bool.
52 template <typename T> struct IsSizeLessThanThresholdT {
53 static constexpr bool value = sizeof(T) <= (2 * sizeof(void *));
56 // Provide a type function to map parameters that won't observe extra copies
57 // or moves and which are small enough to likely pass in register to values
58 // and all other types to l-value reference types. We use this to compute the
59 // types used in our erased call utility to minimize copies and moves unless
60 // doing so would force things unnecessarily into memory.
62 // The heuristic used is related to common ABI register passing conventions.
63 // It doesn't have to be exact though, and in one way it is more strict
64 // because we want to still be able to observe either moves *or* copies.
66 using AdjustedParamT = typename std::conditional<
67 !std::is_reference<T>::value &&
68 llvm::is_trivially_copy_constructible<T>::value &&
69 llvm::is_trivially_move_constructible<T>::value &&
70 IsSizeLessThanThresholdT<T>::value,
73 // The type of the erased function pointer we use as a callback to dispatch to
74 // the stored callable when it is trivial to move and destroy.
75 using CallPtrT = ReturnT (*)(void *CallableAddr,
76 AdjustedParamT<ParamTs>... Params);
77 using MovePtrT = void (*)(void *LHSCallableAddr, void *RHSCallableAddr);
78 using DestroyPtrT = void (*)(void *CallableAddr);
80 /// A struct to hold a single trivial callback with sufficient alignment for
82 struct alignas(8) TrivialCallback {
86 /// A struct we use to aggregate three callbacks when we need full set of
88 struct alignas(8) NonTrivialCallbacks {
91 DestroyPtrT DestroyPtr;
94 // Create a pointer union between either a pointer to a static trivial call
95 // pointer in a struct or a pointer to a static struct of the call, move, and
97 using CallbackPointerUnionT =
98 PointerUnion<TrivialCallback *, NonTrivialCallbacks *>;
100 // The main storage buffer. This will either have a pointer to out-of-line
101 // storage or an inline buffer storing the callable.
102 union StorageUnionT {
103 // For out-of-line storage we keep a pointer to the underlying storage and
104 // the size. This is enough to deallocate the memory.
105 struct OutOfLineStorageT {
111 sizeof(OutOfLineStorageT) <= InlineStorageSize,
112 "Should always use all of the out-of-line storage for inline storage!");
114 // For in-line storage, we just provide an aligned character buffer. We
115 // provide three pointers worth of storage here.
116 typename std::aligned_storage<InlineStorageSize, alignof(void *)>::type
120 // A compressed pointer to either our dispatching callback or our table of
121 // dispatching callbacks and the flag for whether the callable itself is
122 // stored inline or not.
123 PointerIntPair<CallbackPointerUnionT, 1, bool> CallbackAndInlineFlag;
125 bool isInlineStorage() const { return CallbackAndInlineFlag.getInt(); }
127 bool isTrivialCallback() const {
128 return CallbackAndInlineFlag.getPointer().template is<TrivialCallback *>();
131 CallPtrT getTrivialCallback() const {
132 return CallbackAndInlineFlag.getPointer().template get<TrivialCallback *>()->CallPtr;
135 NonTrivialCallbacks *getNonTrivialCallbacks() const {
136 return CallbackAndInlineFlag.getPointer()
137 .template get<NonTrivialCallbacks *>();
140 void *getInlineStorage() { return &StorageUnion.InlineStorage; }
142 void *getOutOfLineStorage() {
143 return StorageUnion.OutOfLineStorage.StoragePtr;
145 size_t getOutOfLineStorageSize() const {
146 return StorageUnion.OutOfLineStorage.Size;
148 size_t getOutOfLineStorageAlignment() const {
149 return StorageUnion.OutOfLineStorage.Alignment;
152 void setOutOfLineStorage(void *Ptr, size_t Size, size_t Alignment) {
153 StorageUnion.OutOfLineStorage = {Ptr, Size, Alignment};
156 template <typename CallableT>
157 static ReturnT CallImpl(void *CallableAddr, AdjustedParamT<ParamTs>... Params) {
158 return (*reinterpret_cast<CallableT *>(CallableAddr))(
159 std::forward<ParamTs>(Params)...);
162 template <typename CallableT>
163 static void MoveImpl(void *LHSCallableAddr, void *RHSCallableAddr) noexcept {
164 new (LHSCallableAddr)
165 CallableT(std::move(*reinterpret_cast<CallableT *>(RHSCallableAddr)));
168 template <typename CallableT>
169 static void DestroyImpl(void *CallableAddr) noexcept {
170 reinterpret_cast<CallableT *>(CallableAddr)->~CallableT();
174 unique_function() = default;
175 unique_function(std::nullptr_t /*null_callable*/) {}
178 if (!CallbackAndInlineFlag.getPointer())
181 // Cache this value so we don't re-check it after type-erased operations.
182 bool IsInlineStorage = isInlineStorage();
184 if (!isTrivialCallback())
185 getNonTrivialCallbacks()->DestroyPtr(
186 IsInlineStorage ? getInlineStorage() : getOutOfLineStorage());
188 if (!IsInlineStorage)
189 deallocate_buffer(getOutOfLineStorage(), getOutOfLineStorageSize(),
190 getOutOfLineStorageAlignment());
193 unique_function(unique_function &&RHS) noexcept {
194 // Copy the callback and inline flag.
195 CallbackAndInlineFlag = RHS.CallbackAndInlineFlag;
197 // If the RHS is empty, just copying the above is sufficient.
201 if (!isInlineStorage()) {
202 // The out-of-line case is easiest to move.
203 StorageUnion.OutOfLineStorage = RHS.StorageUnion.OutOfLineStorage;
204 } else if (isTrivialCallback()) {
205 // Move is trivial, just memcpy the bytes across.
206 memcpy(getInlineStorage(), RHS.getInlineStorage(), InlineStorageSize);
208 // Non-trivial move, so dispatch to a type-erased implementation.
209 getNonTrivialCallbacks()->MovePtr(getInlineStorage(),
210 RHS.getInlineStorage());
213 // Clear the old callback and inline flag to get back to as-if-null.
214 RHS.CallbackAndInlineFlag = {};
217 // In debug builds, we also scribble across the rest of the storage.
218 memset(RHS.getInlineStorage(), 0xAD, InlineStorageSize);
222 unique_function &operator=(unique_function &&RHS) noexcept {
226 // Because we don't try to provide any exception safety guarantees we can
227 // implement move assignment very simply by first destroying the current
228 // object and then move-constructing over top of it.
229 this->~unique_function();
230 new (this) unique_function(std::move(RHS));
234 template <typename CallableT> unique_function(CallableT Callable) {
235 bool IsInlineStorage = true;
236 void *CallableAddr = getInlineStorage();
237 if (sizeof(CallableT) > InlineStorageSize ||
238 alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) {
239 IsInlineStorage = false;
240 // Allocate out-of-line storage. FIXME: Use an explicit alignment
241 // parameter in C++17 mode.
242 auto Size = sizeof(CallableT);
243 auto Alignment = alignof(CallableT);
244 CallableAddr = allocate_buffer(Size, Alignment);
245 setOutOfLineStorage(CallableAddr, Size, Alignment);
248 // Now move into the storage.
249 new (CallableAddr) CallableT(std::move(Callable));
251 // See if we can create a trivial callback. We need the callable to be
252 // trivially moved and trivially destroyed so that we don't have to store
253 // type erased callbacks for those operations.
255 // FIXME: We should use constexpr if here and below to avoid instantiating
256 // the non-trivial static objects when unnecessary. While the linker should
257 // remove them, it is still wasteful.
258 if (llvm::is_trivially_move_constructible<CallableT>::value &&
259 std::is_trivially_destructible<CallableT>::value) {
260 // We need to create a nicely aligned object. We use a static variable
261 // for this because it is a trivial struct.
262 static TrivialCallback Callback = { &CallImpl<CallableT> };
264 CallbackAndInlineFlag = {&Callback, IsInlineStorage};
268 // Otherwise, we need to point at an object that contains all the different
269 // type erased behaviors needed. Create a static instance of the struct type
270 // here and then use a pointer to that.
271 static NonTrivialCallbacks Callbacks = {
272 &CallImpl<CallableT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
274 CallbackAndInlineFlag = {&Callbacks, IsInlineStorage};
277 ReturnT operator()(ParamTs... Params) {
279 isInlineStorage() ? getInlineStorage() : getOutOfLineStorage();
281 return (isTrivialCallback()
282 ? getTrivialCallback()
283 : getNonTrivialCallbacks()->CallPtr)(CallableAddr, Params...);
286 explicit operator bool() const {
287 return (bool)CallbackAndInlineFlag.getPointer();
291 } // end namespace llvm
293 #endif // LLVM_ADT_FUNCTION_H