[llvm] Simplify hot-path size computations in BumpPtrAllocator. (PR #101312)

Owen Anderson via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 23:53:03 PDT 2024


https://github.com/resistor updated https://github.com/llvm/llvm-project/pull/101312

>From 186d4f68b2a8a70c20d77961e1ba22b5cfa68b90 Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Tue, 30 Jul 2024 21:50:15 -1000
Subject: [PATCH 1/3] Simplify hot-path size computations in BumpPtrAllocator.

---
 llvm/include/llvm/Support/Allocator.h | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index e05f0ec0e8704..26ea8d6f47215 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");
+    char *AlignedPtr = reinterpret_cast<char *>(alignAddr(CurPtr, Alignment));
 
     size_t SizeToAllocate = Size;
 #if LLVM_ADDRESS_SANITIZER_BUILD
@@ -158,12 +157,13 @@ class BumpPtrAllocatorImpl
     SizeToAllocate += RedZoneSize;
 #endif
 
+    char *AllocEndPtr = AlignedPtr + SizeToAllocate;
+
     // Check if we have enough space.
-    if (LLVM_LIKELY(Adjustment + SizeToAllocate <= size_t(End - CurPtr)
+    if (LLVM_LIKELY(AllocEndPtr <= End
                     // We can't return nullptr even for a zero-sized allocation!
                     && CurPtr != nullptr)) {
-      char *AlignedPtr = CurPtr + Adjustment;
-      CurPtr = AlignedPtr + SizeToAllocate;
+      CurPtr = 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.

>From 2f776a12f3b72d41f383a1aa13ae518f73721891 Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Wed, 31 Jul 2024 20:45:01 -1000
Subject: [PATCH 2/3] Perform address computations in uintptr_t, and restore an
 assertion for overflow.

---
 llvm/include/llvm/Support/Allocator.h | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index 26ea8d6f47215..394a8ba9825fc 100644
--- a/llvm/include/llvm/Support/Allocator.h
+++ b/llvm/include/llvm/Support/Allocator.h
@@ -149,7 +149,7 @@ class BumpPtrAllocatorImpl
     // Keep track of how many bytes we've allocated.
     BytesAllocated += Size;
 
-    char *AlignedPtr = reinterpret_cast<char *>(alignAddr(CurPtr, Alignment));
+    uintptr_t AlignedPtr = alignAddr(CurPtr, Alignment);
 
     size_t SizeToAllocate = Size;
 #if LLVM_ADDRESS_SANITIZER_BUILD
@@ -157,20 +157,21 @@ class BumpPtrAllocatorImpl
     SizeToAllocate += RedZoneSize;
 #endif
 
-    char *AllocEndPtr = AlignedPtr + SizeToAllocate;
+    uintptr_t AllocEndPtr = AlignedPtr + SizeToAllocate;
+    assert(AllocEndPtr >= uintptr_t(CurPtr) && "Alignment + Size must not overflow");
 
     // Check if we have enough space.
-    if (LLVM_LIKELY(AllocEndPtr <= End
+    if (LLVM_LIKELY(AllocEndPtr <= uintptr_t(End)
                     // We can't return nullptr even for a zero-sized allocation!
                     && CurPtr != nullptr)) {
-      CurPtr = AllocEndPtr;
+      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);

>From 393f3f870d9bcf413ffac116492919f03fc668bf Mon Sep 17 00:00:00 2001
From: Owen Anderson <resistor at mac.com>
Date: Wed, 31 Jul 2024 20:52:41 -1000
Subject: [PATCH 3/3] Fix formatting.

---
 llvm/include/llvm/Support/Allocator.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h
index 394a8ba9825fc..6be7c320a26f8 100644
--- a/llvm/include/llvm/Support/Allocator.h
+++ b/llvm/include/llvm/Support/Allocator.h
@@ -158,7 +158,8 @@ class BumpPtrAllocatorImpl
 #endif
 
     uintptr_t AllocEndPtr = AlignedPtr + SizeToAllocate;
-    assert(AllocEndPtr >= uintptr_t(CurPtr) && "Alignment + Size must not overflow");
+    assert(AllocEndPtr >= uintptr_t(CurPtr) &&
+           "Alignment + Size must not overflow");
 
     // Check if we have enough space.
     if (LLVM_LIKELY(AllocEndPtr <= uintptr_t(End)



More information about the llvm-commits mailing list