Skip to content

Commit 3386273

Browse files
authored
IRMover: Proper fix of performance regression of llvm#146020 (llvm#157218)
In llvm#157045 I didn't realize that IRMover object is one for entire LTO, but IRLinker is created per module. We need one chache for combined module. I timed "IRLinker::linkNamedMDNodes" code on one of our internal binaries, not very large, but above average. Before llvm#146020: 0.4859s After llvm#146020: 624.4686s After llvm#157045: 322.3493s After this patch: 0.5574s
1 parent cf44f19 commit 3386273

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

llvm/include/llvm/Linker/IRMover.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_LINKER_IRMOVER_H
1111

1212
#include "llvm/ADT/ArrayRef.h"
13+
#include "llvm/ADT/DenseMap.h"
1314
#include "llvm/ADT/DenseSet.h"
1415
#include "llvm/ADT/FunctionExtras.h"
1516
#include "llvm/Support/Compiler.h"
@@ -19,6 +20,8 @@ namespace llvm {
1920
class Error;
2021
class GlobalValue;
2122
class Metadata;
23+
class MDNode;
24+
class NamedMDNode;
2225
class Module;
2326
class StructType;
2427
class TrackingMDRef;
@@ -67,6 +70,8 @@ class IRMover {
6770
using LazyCallback =
6871
llvm::unique_function<void(GlobalValue &GV, ValueAdder Add)>;
6972

73+
using NamedMDNodesT = DenseMap<const NamedMDNode *, DenseSet<const MDNode *>>;
74+
7075
/// Move in the provide values in \p ValuesToLink from \p Src.
7176
///
7277
/// - \p AddLazyFor is a call back that the IRMover will call when a global
@@ -86,6 +91,7 @@ class IRMover {
8691
Module &Composite;
8792
IdentifiedStructTypeSet IdentifiedStructTypes;
8893
MDMapT SharedMDs; ///< A Metadata map to use for all calls to \a move().
94+
NamedMDNodesT NamedMDNodes; ///< Cache for IRMover::linkNamedMDNodes().
8995
};
9096

9197
} // End llvm namespace

llvm/lib/Linker/IRMover.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class IRLinker {
293293
std::unique_ptr<Module> SrcM;
294294

295295
// Lookup table to optimize IRMover::linkNamedMDNodes().
296-
DenseMap<StringRef, DenseSet<MDNode *>> NamedMDNodes;
296+
IRMover::NamedMDNodesT &NamedMDNodes;
297297

298298
/// See IRMover::move().
299299
IRMover::LazyCallback AddLazyFor;
@@ -440,10 +440,12 @@ class IRLinker {
440440
IRLinker(Module &DstM, MDMapT &SharedMDs,
441441
IRMover::IdentifiedStructTypeSet &Set, std::unique_ptr<Module> SrcM,
442442
ArrayRef<GlobalValue *> ValuesToLink,
443-
IRMover::LazyCallback AddLazyFor, bool IsPerformingImport)
444-
: DstM(DstM), SrcM(std::move(SrcM)), AddLazyFor(std::move(AddLazyFor)),
445-
TypeMap(Set), GValMaterializer(*this), LValMaterializer(*this),
446-
SharedMDs(SharedMDs), IsPerformingImport(IsPerformingImport),
443+
IRMover::LazyCallback AddLazyFor, bool IsPerformingImport,
444+
IRMover::NamedMDNodesT &NamedMDNodes)
445+
: DstM(DstM), SrcM(std::move(SrcM)), NamedMDNodes(NamedMDNodes),
446+
AddLazyFor(std::move(AddLazyFor)), TypeMap(Set),
447+
GValMaterializer(*this), LValMaterializer(*this), SharedMDs(SharedMDs),
448+
IsPerformingImport(IsPerformingImport),
447449
Mapper(ValueMap, RF_ReuseAndMutateDistinctMDs | RF_IgnoreMissingLocals,
448450
&TypeMap, &GValMaterializer),
449451
IndirectSymbolMCID(Mapper.registerAlternateMappingContext(
@@ -1138,7 +1140,7 @@ void IRLinker::linkNamedMDNodes() {
11381140

11391141
NamedMDNode *DestNMD = DstM.getOrInsertNamedMetadata(NMD.getName());
11401142

1141-
auto &Inserted = NamedMDNodes[DestNMD->getName()];
1143+
auto &Inserted = NamedMDNodes[DestNMD];
11421144
if (Inserted.empty()) {
11431145
// Must be the first module, copy everything from DestNMD.
11441146
Inserted.insert(DestNMD->operands().begin(), DestNMD->operands().end());
@@ -1683,6 +1685,6 @@ Error IRMover::move(std::unique_ptr<Module> Src,
16831685
LazyCallback AddLazyFor, bool IsPerformingImport) {
16841686
IRLinker TheIRLinker(Composite, SharedMDs, IdentifiedStructTypes,
16851687
std::move(Src), ValuesToLink, std::move(AddLazyFor),
1686-
IsPerformingImport);
1688+
IsPerformingImport, NamedMDNodes);
16871689
return TheIRLinker.run();
16881690
}

0 commit comments

Comments
 (0)