Skip to content

Commit e96ff45

Browse files
authored
[clang][bytecode] Lazily create DynamicAllocator (#155831)
Due to all the tracking via map(s) and a BumpPtrAllocator, the creating and destroying the DynamicAllocator is rather expensive. Try to do it lazily and only create it when first calling InterpState::getAllocator().
1 parent bde2abd commit e96ff45

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clang/lib/AST/ByteCode/InterpState.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ InterpState::~InterpState() {
5959
void InterpState::cleanup() {
6060
// As a last resort, make sure all pointers still pointing to a dead block
6161
// don't point to it anymore.
62-
Alloc.cleanup();
62+
if (Alloc)
63+
Alloc->cleanup();
6364
}
6465

6566
Frame *InterpState::getCurrentFrame() { return Current; }
@@ -99,10 +100,13 @@ void InterpState::deallocate(Block *B) {
99100
}
100101

101102
bool InterpState::maybeDiagnoseDanglingAllocations() {
102-
bool NoAllocationsLeft = !Alloc.hasAllocations();
103+
if (!Alloc)
104+
return true;
105+
106+
bool NoAllocationsLeft = !Alloc->hasAllocations();
103107

104108
if (!checkingPotentialConstantExpression()) {
105-
for (const auto &[Source, Site] : Alloc.allocation_sites()) {
109+
for (const auto &[Source, Site] : Alloc->allocation_sites()) {
106110
assert(!Site.empty());
107111

108112
CCEDiag(Source->getExprLoc(), diag::note_constexpr_memory_leak)

clang/lib/AST/ByteCode/InterpState.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ class InterpState final : public State, public SourceMapper {
115115

116116
void setEvalLocation(SourceLocation SL) { this->EvalLocation = SL; }
117117

118-
DynamicAllocator &getAllocator() { return Alloc; }
118+
DynamicAllocator &getAllocator() {
119+
if (!Alloc) {
120+
Alloc = std::make_unique<DynamicAllocator>();
121+
}
122+
123+
return *Alloc.get();
124+
}
119125

120126
/// Diagnose any dynamic allocations that haven't been freed yet.
121127
/// Will return \c false if there were any allocations to diagnose,
@@ -161,7 +167,7 @@ class InterpState final : public State, public SourceMapper {
161167
/// Reference to the offset-source mapping.
162168
SourceMapper *M;
163169
/// Allocator used for dynamic allocations performed via the program.
164-
DynamicAllocator Alloc;
170+
std::unique_ptr<DynamicAllocator> Alloc;
165171

166172
public:
167173
/// Reference to the module containing all bytecode.

0 commit comments

Comments
 (0)