[llvm] Reduce binary size of the Microsoft demangler (PR #89454)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Apr 19 13:59:51 PDT 2024
https://github.com/apache-hb created https://github.com/llvm/llvm-project/pull/89454
This pulls the body of `llvm::ms_demangle::ArenaAllocator::allocArray` and `llvm::ms_demangle::ArenaAllocator::alloc` into a common `allocAlignedBuffer` function. In my testing this reduces the overall binary size of `LLVMDemangle.lib` by 6kb and has a negligible impact on performance.
The `addNode(std::max(AllocUnit, Size));` being pulled into the common function retains the same behaviour as before as `Size` will always be `< AllocUnit` when allocating a single object.
>From 8f3933c2b74edf999a97450c015bd9712c15e96e Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Mon, 15 Apr 2024 03:47:04 -0400
Subject: [PATCH 1/2] Reduce binary size of the microsoft demangler
---
.../include/llvm/Demangle/MicrosoftDemangle.h | 55 ++++++++-----------
1 file changed, 22 insertions(+), 33 deletions(-)
diff --git a/llvm/include/llvm/Demangle/MicrosoftDemangle.h b/llvm/include/llvm/Demangle/MicrosoftDemangle.h
index 1529b803debe5a..336b8b9968088c 100644
--- a/llvm/include/llvm/Demangle/MicrosoftDemangle.h
+++ b/llvm/include/llvm/Demangle/MicrosoftDemangle.h
@@ -41,6 +41,24 @@ class ArenaAllocator {
NewHead->Used = 0;
}
+ uint8_t *allocAlignedBuffer(size_t Size, size_t Align) {
+ assert(Head && Head->Buf);
+
+ size_t P = (size_t)Head->Buf + Head->Used;
+ uintptr_t AlignedP =
+ (((size_t)P + Align - 1) & ~(size_t)(Align - 1));
+ uint8_t *PP = (uint8_t *)AlignedP;
+ size_t Adjustment = AlignedP - P;
+
+ Head->Used += Size + Adjustment;
+ if (Head->Used <= Head->Capacity)
+ return PP;
+
+ addNode(std::max(AllocUnit, Size));
+ Head->Used = Size;
+ return Head->Buf;
+ }
+
public:
ArenaAllocator() { addNode(AllocUnit); }
@@ -69,42 +87,13 @@ class ArenaAllocator {
}
template <typename T, typename... Args> T *allocArray(size_t Count) {
- size_t Size = Count * sizeof(T);
- assert(Head && Head->Buf);
-
- size_t P = (size_t)Head->Buf + Head->Used;
- uintptr_t AlignedP =
- (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
- uint8_t *PP = (uint8_t *)AlignedP;
- size_t Adjustment = AlignedP - P;
-
- Head->Used += Size + Adjustment;
- if (Head->Used <= Head->Capacity)
- return new (PP) T[Count]();
-
- addNode(std::max(AllocUnit, Size));
- Head->Used = Size;
- return new (Head->Buf) T[Count]();
+ uint8_t *Data = allocAlignedBuffer(Count * sizeof(T), alignof(T));
+ return new (Data) T[Count]();
}
template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
- constexpr size_t Size = sizeof(T);
- assert(Head && Head->Buf);
-
- size_t P = (size_t)Head->Buf + Head->Used;
- uintptr_t AlignedP =
- (((size_t)P + alignof(T) - 1) & ~(size_t)(alignof(T) - 1));
- uint8_t *PP = (uint8_t *)AlignedP;
- size_t Adjustment = AlignedP - P;
-
- Head->Used += Size + Adjustment;
- if (Head->Used <= Head->Capacity)
- return new (PP) T(std::forward<Args>(ConstructorArgs)...);
-
- static_assert(Size < AllocUnit);
- addNode(AllocUnit);
- Head->Used = Size;
- return new (Head->Buf) T(std::forward<Args>(ConstructorArgs)...);
+ uint8_t *Data = allocAlignedBuffer(sizeof(T), alignof(T));
+ return new (Data) T(std::forward<Args>(ConstructorArgs)...);
}
private:
>From f970420852ff1370909871961bb9f6ea497f07b9 Mon Sep 17 00:00:00 2001
From: Elliot <35050275+apache-hb at users.noreply.github.com>
Date: Fri, 19 Apr 2024 16:56:08 -0400
Subject: [PATCH 2/2] Update MicrosoftDemangle.h
---
llvm/include/llvm/Demangle/MicrosoftDemangle.h | 1 +
1 file changed, 1 insertion(+)
diff --git a/llvm/include/llvm/Demangle/MicrosoftDemangle.h b/llvm/include/llvm/Demangle/MicrosoftDemangle.h
index 336b8b9968088c..6ce03ebb0dea3f 100644
--- a/llvm/include/llvm/Demangle/MicrosoftDemangle.h
+++ b/llvm/include/llvm/Demangle/MicrosoftDemangle.h
@@ -92,6 +92,7 @@ class ArenaAllocator {
}
template <typename T, typename... Args> T *alloc(Args &&... ConstructorArgs) {
+ static_assert(sizeof(T) < AllocUnit);
uint8_t *Data = allocAlignedBuffer(sizeof(T), alignof(T));
return new (Data) T(std::forward<Args>(ConstructorArgs)...);
}
More information about the llvm-commits
mailing list