From 8f3933c2b74edf999a97450c015bd9712c15e96e Mon Sep 17 00:00:00 2001 From: Elliot <35050275+apache-hb@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 1529b803debe5..336b8b9968088 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 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 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(ConstructorArgs)...); - - static_assert(Size < AllocUnit); - addNode(AllocUnit); - Head->Used = Size; - return new (Head->Buf) T(std::forward(ConstructorArgs)...); + uint8_t *Data = allocAlignedBuffer(sizeof(T), alignof(T)); + return new (Data) T(std::forward(ConstructorArgs)...); } private: From f970420852ff1370909871961bb9f6ea497f07b9 Mon Sep 17 00:00:00 2001 From: Elliot <35050275+apache-hb@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 336b8b9968088..6ce03ebb0dea3 100644 --- a/llvm/include/llvm/Demangle/MicrosoftDemangle.h +++ b/llvm/include/llvm/Demangle/MicrosoftDemangle.h @@ -92,6 +92,7 @@ class ArenaAllocator { } template T *alloc(Args &&... ConstructorArgs) { + static_assert(sizeof(T) < AllocUnit); uint8_t *Data = allocAlignedBuffer(sizeof(T), alignof(T)); return new (Data) T(std::forward(ConstructorArgs)...); }