Skip to content

Simplify hot-path size computations in BumpPtrAllocator. #101312

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

Merged
merged 3 commits into from
Aug 1, 2024
Merged
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
14 changes: 8 additions & 6 deletions llvm/include/llvm/Support/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,28 +149,30 @@ class BumpPtrAllocatorImpl
// Keep track of how many bytes we've allocated.
BytesAllocated += Size;

size_t Adjustment = offsetToAlignedAddr(CurPtr, Alignment);
assert(Adjustment + Size >= Size && "Adjustment + Size must not overflow");
uintptr_t AlignedPtr = alignAddr(CurPtr, Alignment);

size_t SizeToAllocate = Size;
#if LLVM_ADDRESS_SANITIZER_BUILD
// Add trailing bytes as a "red zone" under ASan.
SizeToAllocate += RedZoneSize;
#endif

uintptr_t AllocEndPtr = AlignedPtr + SizeToAllocate;
assert(AllocEndPtr >= uintptr_t(CurPtr) &&
"Alignment + Size must not overflow");

// Check if we have enough space.
if (LLVM_LIKELY(Adjustment + SizeToAllocate <= size_t(End - CurPtr)
if (LLVM_LIKELY(AllocEndPtr <= uintptr_t(End)
// We can't return nullptr even for a zero-sized allocation!
&& CurPtr != nullptr)) {
char *AlignedPtr = CurPtr + Adjustment;
CurPtr = AlignedPtr + SizeToAllocate;
CurPtr = reinterpret_cast<char *>(AllocEndPtr);
// Update the allocation point of this memory block in MemorySanitizer.
// Without this, MemorySanitizer messages for values originated from here
// will point to the allocation of the entire slab.
__msan_allocated_memory(AlignedPtr, Size);
// Similarly, tell ASan about this space.
__asan_unpoison_memory_region(AlignedPtr, Size);
return AlignedPtr;
return reinterpret_cast<char *>(AlignedPtr);
}

return AllocateSlow(Size, SizeToAllocate, Alignment);
Expand Down
Loading