Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADT] Fix alignment check in unique_function constructor #99403

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions llvm/include/llvm/ADT/FunctionExtras.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ using EnableIfCallable = std::enable_if_t<std::disjunction<
template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
protected:
static constexpr size_t InlineStorageSize = sizeof(void *) * 3;
static constexpr size_t InlineStorageAlign = alignof(void *);

template <typename T, class = void>
struct IsSizeLessThanThresholdT : std::false_type {};
Expand Down Expand Up @@ -161,7 +162,8 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
// provide three pointers worth of storage here.
// This is mutable as an inlined `const unique_function<void() const>` may
// still modify its own mutable members.
alignas(void *) mutable std::byte InlineStorage[InlineStorageSize];
alignas(InlineStorageAlign) mutable std::byte
InlineStorage[InlineStorageSize];
} StorageUnion;

// A compressed pointer to either our dispatching callback or our table of
Expand Down Expand Up @@ -262,7 +264,7 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
bool IsInlineStorage = true;
void *CallableAddr = getInlineStorage();
if (sizeof(CallableT) > InlineStorageSize ||
alignof(CallableT) > alignof(decltype(StorageUnion.InlineStorage))) {
alignof(CallableT) > InlineStorageAlign) {
IsInlineStorage = false;
// Allocate out-of-line storage. FIXME: Use an explicit alignment
// parameter in C++17 mode.
Expand Down
19 changes: 19 additions & 0 deletions llvm/unittests/ADT/FunctionExtrasTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,23 @@ class Incomplete {};
Incomplete incompleteFunction() { return {}; }
const Incomplete incompleteFunctionConst() { return {}; }

// Check that we can store a pointer-sized payload inline in the unique_function.
TEST(UniqueFunctionTest, InlineStorageWorks) {
// We do assume a couple of implementation details of the unique_function here:
// - It can store certain small-enough payload inline
// - Inline storage size is at least >= sizeof(void*)
void *ptr;
unique_function<void(void *)> UniqueFunctionWithInlineStorage{
[ptr](void *self) {
auto mid = reinterpret_cast<uintptr_t>(&ptr);
auto beg = reinterpret_cast<uintptr_t>(self);
auto end = reinterpret_cast<uintptr_t>(self) +
sizeof(unique_function<void(void *)>);
// Make sure the address of the captured pointer lies somewhere within
// the unique_function object.
EXPECT_TRUE(mid >= beg && mid < end);
}};
UniqueFunctionWithInlineStorage(&UniqueFunctionWithInlineStorage);
}

} // anonymous namespace
Loading