[llvm] Simplify hot-path size computations in BumpPtrAllocator. (PR #101467)
Owen Anderson via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 1 03:37:59 PDT 2024
https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/101467
>From 3484f9d1d17bceb5ccca7cca216c5f52043fe61a Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Wed, 31 Jul 2024 23:43:03 -1000
Subject: [PATCH 1/2] Simplify hot-path size computations in BumpPtrAllocator.
(#101312)
~0.1% instruction count improvements
https://llvm-compile-time-tracker.com/compare.php?from=07d2709a17860a202d91781769a88837e4fb5f2a&to=d5cc47831ecd9f0a2b164b16da67f74b94e9aafc&stat=instructions:u
---
llvm/include/llvm/Support/Allocator.h | 16 +++++++++-------
1 file changed, 9 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index e05f0ec0e8704..27f9fa1139541 100644
--- a/llvm/include/llvm/Support/Allocator.h
+++ b/llvm/include/llvm/Support/Allocator.h
@@ -149,8 +149,7 @@ 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
@@ -158,19 +157,22 @@ class BumpPtrAllocatorImpl
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);
+ __msan_allocated_memory(reinterpret_cast<char *>(AlignedPtr), Size);
// Similarly, tell ASan about this space.
__asan_unpoison_memory_region(AlignedPtr, Size);
- return AlignedPtr;
+ return reinterpret_cast<char *>(reinterpret_cast<char *>(AlignedPtr));
}
return AllocateSlow(Size, SizeToAllocate, Alignment);
>From 8d8c7e70be3948e27e855c5b74049e930c0a488f Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Thu, 1 Aug 2024 00:37:36 -1000
Subject: [PATCH 2/2] Fix copy-paste-o
---
llvm/include/llvm/Support/Allocator.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index 27f9fa1139541..a6396c43e4b16 100644
--- a/llvm/include/llvm/Support/Allocator.h
+++ b/llvm/include/llvm/Support/Allocator.h
@@ -171,8 +171,8 @@ class BumpPtrAllocatorImpl
// will point to the allocation of the entire slab.
__msan_allocated_memory(reinterpret_cast<char *>(AlignedPtr), Size);
// Similarly, tell ASan about this space.
- __asan_unpoison_memory_region(AlignedPtr, Size);
- return reinterpret_cast<char *>(reinterpret_cast<char *>(AlignedPtr));
+ __asan_unpoison_memory_region(reinterpret_cast<char *>(AlignedPtr), Size);
+ return reinterpret_cast<char *>(AlignedPtr));
}
return AllocateSlow(Size, SizeToAllocate, Alignment);
More information about the llvm-commits
mailing list