Skip to content

Commit 0697bcb

Browse files
committed
[Support] Split MallocAllocator out of Allocator.h
StringMap.h is very popular (4K uses), and it doesn't need to see BumpPtrAllocator, which is relatively expensive according to ClangBuildAnalyzer. StringMap only needs MallocAllocator, so split that into AllocatorBase.h and use it instead. Here is the change in header uses: $ diff -u thedeps-before.txt thedeps-after.txt | \ grep '^[-+] ' | sort | uniq -c | sort -nr 3993 + ../llvm/include/llvm/Support/AllocatorBase.h 758 - ../llvm/include/llvm/Support/Allocator.h 270 - ../llvm/include/llvm/Support/Alignment.h 13 - ../llvm/include/llvm/Support/Host.h 6 - ../llvm/include/llvm/ADT/StringMap.h 4 - ../llvm/include/llvm/Support/SwapByteOrder.h 4 - ../llvm/include/llvm/Support/MathExtras.h 4 - ../llvm/include/llvm/Support/AlignOf.h 4 - ../llvm/include/llvm/ADT/SmallVector.h 1 - ../llvm/include/llvm/Support/PointerLikeTypeTraits.h Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D73392
1 parent 3da4774 commit 0697bcb

File tree

16 files changed

+118
-84
lines changed

16 files changed

+118
-84
lines changed

llvm/include/llvm/ADT/ScopedHashTable.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
#include "llvm/ADT/DenseMap.h"
3434
#include "llvm/ADT/DenseMapInfo.h"
35-
#include "llvm/Support/Allocator.h"
35+
#include "llvm/Support/AllocatorBase.h"
3636
#include <cassert>
3737
#include <new>
3838

llvm/include/llvm/ADT/SparseSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "llvm/ADT/STLExtras.h"
2323
#include "llvm/ADT/SmallVector.h"
24-
#include "llvm/Support/Allocator.h"
24+
#include "llvm/Support/AllocatorBase.h"
2525
#include <cassert>
2626
#include <cstdint>
2727
#include <cstdlib>

llvm/include/llvm/ADT/StringMap.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/ADT/iterator.h"
1818
#include "llvm/ADT/iterator_range.h"
19-
#include "llvm/Support/Allocator.h"
19+
#include "llvm/Support/AllocatorBase.h"
2020
#include "llvm/Support/PointerLikeTypeTraits.h"
21-
#include "llvm/Support/ErrorHandling.h"
2221
#include <algorithm>
2322
#include <cassert>
2423
#include <cstdint>

llvm/include/llvm/ADT/StringSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "llvm/ADT/StringMap.h"
1717
#include "llvm/ADT/StringRef.h"
18-
#include "llvm/Support/Allocator.h"
18+
#include "llvm/Support/AllocatorBase.h"
1919
#include <cassert>
2020
#include <initializer_list>
2121
#include <utility>

llvm/include/llvm/BinaryFormat/MachO.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#include "llvm/Support/Compiler.h"
1717
#include "llvm/Support/DataTypes.h"
18-
#include "llvm/Support/Host.h"
18+
#include "llvm/Support/SwapByteOrder.h"
1919

2020
namespace llvm {
2121
namespace MachO {

llvm/include/llvm/DebugInfo/CodeView/ContinuationRecordBuilder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
1919
#include "llvm/DebugInfo/CodeView/TypeRecordMapping.h"
2020
#include "llvm/DebugInfo/CodeView/TypeVisitorCallbacks.h"
21-
#include "llvm/Support/Allocator.h"
2221
#include "llvm/Support/BinaryByteStream.h"
2322
#include "llvm/Support/BinaryStreamWriter.h"
2423
#include "llvm/Support/Error.h"

llvm/include/llvm/DebugInfo/PDB/Native/SymbolCache.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/DebugInfo/CodeView/TypeIndex.h"
1515
#include "llvm/DebugInfo/CodeView/TypeRecord.h"
1616
#include "llvm/DebugInfo/PDB/Native/NativeRawSymbol.h"
17-
#include "llvm/Support/Allocator.h"
1817

1918
#include <memory>
2019
#include <vector>

llvm/include/llvm/Object/IRSymtab.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "llvm/ADT/iterator_range.h"
2929
#include "llvm/IR/GlobalValue.h"
3030
#include "llvm/Object/SymbolicFile.h"
31+
#include "llvm/Support/Allocator.h"
3132
#include "llvm/Support/Endian.h"
3233
#include "llvm/Support/Error.h"
3334
#include <cassert>

llvm/include/llvm/ProfileData/SampleProf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/IR/Function.h"
2323
#include "llvm/IR/GlobalValue.h"
2424
#include "llvm/IR/Module.h"
25+
#include "llvm/Support/Allocator.h"
2526
#include "llvm/Support/Debug.h"
2627
#include "llvm/Support/ErrorOr.h"
2728
#include "llvm/Support/MathExtras.h"

llvm/include/llvm/Remarks/RemarkStringTable.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/ADT/StringMap.h"
2020
#include "llvm/ADT/StringRef.h"
2121
#include "llvm/Remarks/Remark.h"
22+
#include "llvm/Support/Allocator.h"
2223
#include <vector>
2324

2425
namespace llvm {

llvm/include/llvm/Support/Allocator.h

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/ADT/Optional.h"
2424
#include "llvm/ADT/SmallVector.h"
2525
#include "llvm/Support/Alignment.h"
26+
#include "llvm/Support/AllocatorBase.h"
2627
#include "llvm/Support/Compiler.h"
2728
#include "llvm/Support/ErrorHandling.h"
2829
#include "llvm/Support/MathExtras.h"
@@ -38,81 +39,6 @@
3839

3940
namespace llvm {
4041

41-
/// CRTP base class providing obvious overloads for the core \c
42-
/// Allocate() methods of LLVM-style allocators.
43-
///
44-
/// This base class both documents the full public interface exposed by all
45-
/// LLVM-style allocators, and redirects all of the overloads to a single core
46-
/// set of methods which the derived class must define.
47-
template <typename DerivedT> class AllocatorBase {
48-
public:
49-
/// Allocate \a Size bytes of \a Alignment aligned memory. This method
50-
/// must be implemented by \c DerivedT.
51-
void *Allocate(size_t Size, size_t Alignment) {
52-
#ifdef __clang__
53-
static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>(
54-
&AllocatorBase::Allocate) !=
55-
static_cast<void *(DerivedT::*)(size_t, size_t)>(
56-
&DerivedT::Allocate),
57-
"Class derives from AllocatorBase without implementing the "
58-
"core Allocate(size_t, size_t) overload!");
59-
#endif
60-
return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
61-
}
62-
63-
/// Deallocate \a Ptr to \a Size bytes of memory allocated by this
64-
/// allocator.
65-
void Deallocate(const void *Ptr, size_t Size) {
66-
#ifdef __clang__
67-
static_assert(static_cast<void (AllocatorBase::*)(const void *, size_t)>(
68-
&AllocatorBase::Deallocate) !=
69-
static_cast<void (DerivedT::*)(const void *, size_t)>(
70-
&DerivedT::Deallocate),
71-
"Class derives from AllocatorBase without implementing the "
72-
"core Deallocate(void *) overload!");
73-
#endif
74-
return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size);
75-
}
76-
77-
// The rest of these methods are helpers that redirect to one of the above
78-
// core methods.
79-
80-
/// Allocate space for a sequence of objects without constructing them.
81-
template <typename T> T *Allocate(size_t Num = 1) {
82-
return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
83-
}
84-
85-
/// Deallocate space for a sequence of objects without constructing them.
86-
template <typename T>
87-
typename std::enable_if<
88-
!std::is_same<typename std::remove_cv<T>::type, void>::value, void>::type
89-
Deallocate(T *Ptr, size_t Num = 1) {
90-
Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
91-
}
92-
};
93-
94-
class MallocAllocator : public AllocatorBase<MallocAllocator> {
95-
public:
96-
void Reset() {}
97-
98-
LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
99-
size_t /*Alignment*/) {
100-
return safe_malloc(Size);
101-
}
102-
103-
// Pull in base class overloads.
104-
using AllocatorBase<MallocAllocator>::Allocate;
105-
106-
void Deallocate(const void *Ptr, size_t /*Size*/) {
107-
free(const_cast<void *>(Ptr));
108-
}
109-
110-
// Pull in base class overloads.
111-
using AllocatorBase<MallocAllocator>::Deallocate;
112-
113-
void PrintStats() const {}
114-
};
115-
11642
namespace detail {
11743

11844
// We call out to an external function to actually print the message as the
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
//===- AllocatorBase.h - Simple memory allocation abstraction -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
/// \file
9+
///
10+
/// This file defines the MallocAllocator and BumpPtrAllocator interfaces. Both
11+
/// of these conform to an LLVM "Allocator" concept which consists of an
12+
/// Allocate method accepting a size and alignment, and a Deallocate accepting
13+
/// a pointer and size. Further, the LLVM "Allocator" concept has overloads of
14+
/// Allocate and Deallocate for setting size and alignment based on the final
15+
/// type. These overloads are typically provided by a base class template \c
16+
/// AllocatorBase.
17+
///
18+
//===----------------------------------------------------------------------===//
19+
20+
#ifndef LLVM_SUPPORT_ALLOCATORBASE_H
21+
#define LLVM_SUPPORT_ALLOCATORBASE_H
22+
23+
#include "llvm/Support/Compiler.h"
24+
#include "llvm/Support/MemAlloc.h"
25+
26+
namespace llvm {
27+
28+
/// CRTP base class providing obvious overloads for the core \c
29+
/// Allocate() methods of LLVM-style allocators.
30+
///
31+
/// This base class both documents the full public interface exposed by all
32+
/// LLVM-style allocators, and redirects all of the overloads to a single core
33+
/// set of methods which the derived class must define.
34+
template <typename DerivedT> class AllocatorBase {
35+
public:
36+
/// Allocate \a Size bytes of \a Alignment aligned memory. This method
37+
/// must be implemented by \c DerivedT.
38+
void *Allocate(size_t Size, size_t Alignment) {
39+
#ifdef __clang__
40+
static_assert(static_cast<void *(AllocatorBase::*)(size_t, size_t)>(
41+
&AllocatorBase::Allocate) !=
42+
static_cast<void *(DerivedT::*)(size_t, size_t)>(
43+
&DerivedT::Allocate),
44+
"Class derives from AllocatorBase without implementing the "
45+
"core Allocate(size_t, size_t) overload!");
46+
#endif
47+
return static_cast<DerivedT *>(this)->Allocate(Size, Alignment);
48+
}
49+
50+
/// Deallocate \a Ptr to \a Size bytes of memory allocated by this
51+
/// allocator.
52+
void Deallocate(const void *Ptr, size_t Size) {
53+
#ifdef __clang__
54+
static_assert(static_cast<void (AllocatorBase::*)(const void *, size_t)>(
55+
&AllocatorBase::Deallocate) !=
56+
static_cast<void (DerivedT::*)(const void *, size_t)>(
57+
&DerivedT::Deallocate),
58+
"Class derives from AllocatorBase without implementing the "
59+
"core Deallocate(void *) overload!");
60+
#endif
61+
return static_cast<DerivedT *>(this)->Deallocate(Ptr, Size);
62+
}
63+
64+
// The rest of these methods are helpers that redirect to one of the above
65+
// core methods.
66+
67+
/// Allocate space for a sequence of objects without constructing them.
68+
template <typename T> T *Allocate(size_t Num = 1) {
69+
return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
70+
}
71+
72+
/// Deallocate space for a sequence of objects without constructing them.
73+
template <typename T>
74+
typename std::enable_if<
75+
!std::is_same<typename std::remove_cv<T>::type, void>::value, void>::type
76+
Deallocate(T *Ptr, size_t Num = 1) {
77+
Deallocate(static_cast<const void *>(Ptr), Num * sizeof(T));
78+
}
79+
};
80+
81+
class MallocAllocator : public AllocatorBase<MallocAllocator> {
82+
public:
83+
void Reset() {}
84+
85+
LLVM_ATTRIBUTE_RETURNS_NONNULL void *Allocate(size_t Size,
86+
size_t /*Alignment*/) {
87+
return safe_malloc(Size);
88+
}
89+
90+
// Pull in base class overloads.
91+
using AllocatorBase<MallocAllocator>::Allocate;
92+
93+
void Deallocate(const void *Ptr, size_t /*Size*/) {
94+
free(const_cast<void *>(Ptr));
95+
}
96+
97+
// Pull in base class overloads.
98+
using AllocatorBase<MallocAllocator>::Deallocate;
99+
100+
void PrintStats() const {}
101+
};
102+
103+
} // namespace llvm
104+
105+
#endif // LLVM_SUPPORT_ALLOCATORBASE_H

llvm/include/llvm/Support/BinaryStreamReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "llvm/ADT/ArrayRef.h"
1313
#include "llvm/ADT/STLExtras.h"
14+
#include "llvm/Support/Alignment.h"
1415
#include "llvm/Support/BinaryStreamArray.h"
1516
#include "llvm/Support/BinaryStreamRef.h"
1617
#include "llvm/Support/ConvertUTF.h"

llvm/include/llvm/Support/Process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#define LLVM_SUPPORT_PROCESS_H
2626

2727
#include "llvm/ADT/Optional.h"
28-
#include "llvm/Support/Allocator.h"
28+
#include "llvm/Support/AllocatorBase.h"
2929
#include "llvm/Support/Chrono.h"
3030
#include "llvm/Support/DataTypes.h"
3131
#include "llvm/Support/Error.h"

llvm/lib/IR/SafepointIRVerifier.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
#include "llvm/IR/Statepoint.h"
4646
#include "llvm/IR/Value.h"
4747
#include "llvm/InitializePasses.h"
48+
#include "llvm/Support/Allocator.h"
4849
#include "llvm/Support/CommandLine.h"
4950
#include "llvm/Support/Debug.h"
5051
#include "llvm/Support/raw_ostream.h"

llvm/unittests/Support/BinaryStreamTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9+
#include "llvm/Support/Allocator.h"
910
#include "llvm/Support/BinaryByteStream.h"
1011
#include "llvm/Support/BinaryItemStream.h"
1112
#include "llvm/Support/BinaryStreamArray.h"

0 commit comments

Comments
 (0)