From 7cfde51c4f362a529497a83e035a3bb73915a884 Mon Sep 17 00:00:00 2001 From: Teresa Johnson Date: Thu, 28 Jan 2016 15:08:09 +0000 Subject: [PATCH] Improve efficiency of handling unmapped subprogram metadata The stripNullSubprograms function is very inefficient because it walks all subprograms in all compile units in the dest module any time a new module is linked in. For LTO in particular this will get increasingly expensive as more modules are linked. This patch improves the efficiency in several ways. The first is that no scanning is necessary when there were no unneeded subprograms identified in the first place. The second is that only the newly-linked module's compile unit metadata should be examined. Fixes PR26346. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@259049 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/IRMover.cpp | 53 ++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/lib/Linker/IRMover.cpp b/lib/Linker/IRMover.cpp index 6dbc183f782..bc2cc66541f 100644 --- a/lib/Linker/IRMover.cpp +++ b/lib/Linker/IRMover.cpp @@ -507,8 +507,9 @@ class IRLinker { SmallPtrSet &Visited); /// The value mapper leaves nulls in the list of subprograms for any - /// in the UnneededSubprograms map. Strip those out after metadata linking. - void stripNullSubprograms(); + /// in the UnneededSubprograms map. Strip those out of the mapped + /// compile unit. + void stripNullSubprograms(DICompileUnit *CU); public: IRLinker(Module &DstM, IRMover::IdentifiedStructTypeSet &Set, Module &SrcM, @@ -1266,28 +1267,24 @@ void IRLinker::findNeededSubprograms() { } } -// Squash null subprograms from compile unit subprogram lists. -void IRLinker::stripNullSubprograms() { - NamedMDNode *CompileUnits = DstM.getNamedMetadata("llvm.dbg.cu"); - if (!CompileUnits) +// Squash null subprograms from the given compile unit's subprogram list. +void IRLinker::stripNullSubprograms(DICompileUnit *CU) { + // There won't be any nulls if we didn't have any subprograms marked + // as unneeded. + if (UnneededSubprograms.empty()) return; - for (unsigned I = 0, E = CompileUnits->getNumOperands(); I != E; ++I) { - auto *CU = cast(CompileUnits->getOperand(I)); - assert(CU && "Expected valid compile unit"); - - SmallVector NewSPs; - NewSPs.reserve(CU->getSubprograms().size()); - bool FoundNull = false; - for (DISubprogram *SP : CU->getSubprograms()) { - if (!SP) { - FoundNull = true; - continue; - } - NewSPs.push_back(SP); + SmallVector NewSPs; + NewSPs.reserve(CU->getSubprograms().size()); + bool FoundNull = false; + for (DISubprogram *SP : CU->getSubprograms()) { + if (!SP) { + FoundNull = true; + continue; } - if (FoundNull) - CU->replaceSubprograms(MDTuple::get(CU->getContext(), NewSPs)); + NewSPs.push_back(SP); } + if (FoundNull) + CU->replaceSubprograms(MDTuple::get(CU->getContext(), NewSPs)); } /// Insert all of the named MDNodes in Src into the Dest module. @@ -1300,12 +1297,18 @@ void IRLinker::linkNamedMDNodes() { continue; NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName()); // Add Src elements into Dest node. - for (const MDNode *op : NMD.operands()) - DestNMD->addOperand(MapMetadata( + for (const MDNode *op : NMD.operands()) { + MDNode *DestMD = MapMetadata( op, ValueMap, ValueMapperFlags | RF_NullMapMissingGlobalValues, - &TypeMap, &GValMaterializer)); + &TypeMap, &GValMaterializer); + // For each newly mapped compile unit remove any null subprograms, + // which occur when findNeededSubprograms identified any as unneeded + // in the dest module. + if (auto *CU = dyn_cast(DestMD)) + stripNullSubprograms(CU); + DestNMD->addOperand(DestMD); + } } - stripNullSubprograms(); } /// Merge the linker flags in Src into the Dest module.