Skip to content

Commit dab999d

Browse files
committed
[llvm link] Destroy ConstantArrays in LLVMContext if they are not used.
ConstantArrays constructed during linking can cause quadratic memory explosion. An example is the ConstantArrays constructed when linking in GlobalVariables with appending linkage. Releasing all unused constants can cause a 20% LTO compile-time slowdown for a large application. So this commit releases unused ConstantArrays only. rdar://19040716. It reduces memory footprint from 20+G to 6+G. llvm-svn: 226592
1 parent 3a70d07 commit dab999d

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

llvm/include/llvm/IR/Module.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,15 @@ class Module {
630630
named_metadata_end());
631631
}
632632

633+
/// Destroy ConstantArrays in LLVMContext if they are not used.
634+
/// ConstantArrays constructed during linking can cause quadratic memory
635+
/// explosion. Releasing all unused constants can cause a 20% LTO compile-time
636+
/// slowdown for a large application.
637+
///
638+
/// NOTE: Constants are currently owned by LLVMContext. This can then only
639+
/// be called where all uses of the LLVMContext are understood.
640+
void dropTriviallyDeadConstantArrays();
641+
633642
/// @}
634643
/// @name Utility functions for printing and dumping Module objects
635644
/// @{

llvm/lib/IR/LLVMContextImpl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,28 @@ LLVMContextImpl::~LLVMContextImpl() {
163163
MDStringCache.clear();
164164
}
165165

166+
void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
167+
bool Changed;
168+
do {
169+
Changed = false;
170+
171+
for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
172+
I != E; ) {
173+
auto *C = I->first;
174+
I++;
175+
if (C->use_empty()) {
176+
Changed = true;
177+
C->destroyConstant();
178+
}
179+
}
180+
181+
} while (Changed);
182+
}
183+
184+
void Module::dropTriviallyDeadConstantArrays() {
185+
Context.pImpl->dropTriviallyDeadConstantArrays();
186+
}
187+
166188
namespace llvm {
167189
/// \brief Make MDOperand transparent for hashing.
168190
///

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ class LLVMContextImpl {
474474

475475
LLVMContextImpl(LLVMContext &C);
476476
~LLVMContextImpl();
477+
478+
/// Destroy the ConstantArrays if they are not used.
479+
void dropTriviallyDeadConstantArrays();
477480
};
478481

479482
}

llvm/lib/Linker/LinkModules.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,9 @@ void Linker::deleteModule() {
17211721
bool Linker::linkInModule(Module *Src) {
17221722
ModuleLinker TheLinker(Composite, IdentifiedStructTypes, Src,
17231723
DiagnosticHandler);
1724-
return TheLinker.run();
1724+
bool RetCode = TheLinker.run();
1725+
Composite->dropTriviallyDeadConstantArrays();
1726+
return RetCode;
17251727
}
17261728

17271729
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)