From 91a6c88349fb0149d9c6c2f41f407753d1208c84 Mon Sep 17 00:00:00 2001 From: Johannes Doerfert Date: Thu, 16 Apr 2020 18:23:01 -0500 Subject: [PATCH] [Attributor] Use a pointer value type for the AAMap This reduces memory consumption and the need to copy complex data structures repeatedly. No functional change is intended. --- Single run of the Attributor module and then CGSCC pass (oldPM) for SPASS/clause.c (~10k LLVM-IR loc): Before: ``` calls to allocation functions: 613353 (376521/s) temporary memory allocations: 83636 (51341/s) peak heap memory consumption: 75.64MB peak RSS (including heaptrack overhead): 162.97MB total memory leaked: 269.04KB ``` After: ``` calls to allocation functions: 616575 (349929/s) temporary memory allocations: 83650 (47474/s) peak heap memory consumption: 72.15MB peak RSS (including heaptrack overhead): 159.81MB total memory leaked: 269.04KB ``` Difference: ``` calls to allocation functions: 3222 (24225/s) temporary memory allocations: 14 (105/s) peak heap memory consumption: -3.49MB peak RSS (including heaptrack overhead): 0B total memory leaked: 0B --- --- llvm/include/llvm/Transforms/IPO/Attributor.h | 38 ++++++++++--------- llvm/lib/Transforms/IPO/Attributor.cpp | 5 +++ 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/llvm/include/llvm/Transforms/IPO/Attributor.h b/llvm/include/llvm/Transforms/IPO/Attributor.h index f242b93297990..de5827c610d05 100644 --- a/llvm/include/llvm/Transforms/IPO/Attributor.h +++ b/llvm/include/llvm/Transforms/IPO/Attributor.h @@ -803,10 +803,13 @@ struct Attributor { // Put the attribute in the lookup map structure and the container we use to // keep track of all attributes. const IRPosition &IRP = AA.getIRPosition(); - auto &KindToAbstractAttributeMap = AAMap[IRP]; - assert(!KindToAbstractAttributeMap.count(&AAType::ID) && - "Attribute already in map!"); - KindToAbstractAttributeMap[&AAType::ID] = &AA; + Kind2AAMapTy *&Kind2AA = AAMap[IRP]; + if (!Kind2AA) + Kind2AA = new (Allocator) Kind2AAMapTy(); + + assert(!(*Kind2AA)[&AAType::ID] && "Attribute already in map!"); + (*Kind2AA)[&AAType::ID] = &AA; + AllAbstractAttributes.push_back(&AA); return AA; } @@ -1185,18 +1188,19 @@ struct Attributor { // Lookup the abstract attribute of type AAType. If found, return it after // registering a dependence of QueryingAA on the one returned attribute. - auto KindToAbstractAttributeMapIt = AAMap.find(IRP); - if ( KindToAbstractAttributeMapIt == AAMap.end()) + Kind2AAMapTy *Kind2AA = AAMap.lookup(IRP); + if (!Kind2AA) return nullptr; - if (AAType *AA = static_cast( - KindToAbstractAttributeMapIt->second.lookup(&AAType::ID))) { - // Do not register a dependence on an attribute with an invalid state. - if (TrackDependence && AA->getState().isValidState()) - recordDependence(*AA, const_cast(*QueryingAA), - DepClass); - return AA; - } - return nullptr; + + AAType *AA = static_cast((*Kind2AA)[&AAType::ID]); + if (!AA) + return nullptr; + + // Do not register a dependence on an attribute with an invalid state. + if (TrackDependence && AA->getState().isValidState()) + recordDependence(*AA, const_cast(*QueryingAA), + DepClass); + return AA; } /// Apply all requested function signature rewrites @@ -1215,9 +1219,9 @@ struct Attributor { /// on the outer level, and the addresses of the static member (AAType::ID) on /// the inner level. ///{ - using KindToAbstractAttributeMap = + using Kind2AAMapTy = SmallDenseMap; - DenseMap AAMap; + DenseMap AAMap; ///} /// A map from abstract attributes to the ones that queried them through calls diff --git a/llvm/lib/Transforms/IPO/Attributor.cpp b/llvm/lib/Transforms/IPO/Attributor.cpp index 2d92423106fa8..1b7fceb19718a 100644 --- a/llvm/lib/Transforms/IPO/Attributor.cpp +++ b/llvm/lib/Transforms/IPO/Attributor.cpp @@ -487,6 +487,11 @@ Attributor::~Attributor() { for (AbstractAttribute *AA : AllAbstractAttributes) AA->~AbstractAttribute(); + // The Kind2AAMap objects are allocated via a BumpPtrAllocator, we call + // the destructor manually. + for (auto &It : AAMap) + It.getSecond()->~Kind2AAMapTy(); + for (auto &It : ArgumentReplacementMap) DeleteContainerPointers(It.second); }