[llvm] [JITLink] Make duplicate symbol detection O(1) (PR #223919)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 15 23:11:26 PDT 2026
https://github.com/vincent-163 created https://github.com/llvm/llvm-project/pull/223919
### Summary
`addDefinedSymbol()` and `addAbsoluteSymbol()` check for duplicate definitions
by linearly scanning all absolute symbols, or every symbol of every section in
the graph, respectively. That makes adding N symbols to a `LinkGraph` O(N^2),
which is a serious problem for graphs with tens of thousands of symbols: linking
a 30k-symbol object with `llvm-jitlink` spends most of its link time in these
two assertions.
Symbol names in a `LinkGraph` are interned in the graph's `SymbolStringPool`, so
equal names are represented by equal (non-owning) `SymbolStringPtr`s and can be
looked up with a hash. This tracks the names of the non-local symbols added to
the graph in two `DenseSet`s and uses them for the duplicate checks instead of
scanning the symbol lists.
### Details
* The sets are only maintained when assertions are enabled, since the checks
that use them are asserts (they are `#ifndef NDEBUG` members, so release
builds are unaffected).
* Removing a symbol from the graph (`removeDefinedSymbol()`,
`removeAbsoluteSymbol()`, `removeSection()`) erases its name from the
corresponding set, so a name can be reused after its symbol is removed
without tripping the check.
* Local symbols keep the old behaviour of not being checked (and not being
tracked).
### Testing
* New unit test `LinkGraphTest.DuplicateSymbolDetection` exercises the
duplicate-definition asserts for defined and absolute symbols, checks that
local symbols may still share a name, and that removing a symbol releases
its name for reuse.
* Full `JITLinkTests` suite passes (59/59).
### Notes
This changes nothing about which links succeed or fail: the asserts fire on
exactly the same programs as before, they just run in constant time. The
`DenseSet` lookups use the pointer-based hashing of `NonOwningSymbolStringPtr`,
which is safe here because the name's pooled entry is kept alive by the symbol
itself for as long as the name is tracked.
>From 2a045cac4d9bdc70a242f84f9530aa2a9a5c9417 Mon Sep 17 00:00:00 2001
From: vincent163 <vincent.163 at outlook.com>
Date: Wed, 16 Sep 2026 12:06:25 +0800
Subject: [PATCH] [JITLink] Make duplicate symbol detection O(1)
addDefinedSymbol() and addAbsoluteSymbol() checked for duplicate
definitions by linearly scanning all absolute symbols, or every symbol of
every section in the graph, respectively. That makes adding N symbols to
a graph O(N^2), which is a serious problem for graphs with tens of
thousands of symbols: linking a 30000 symbol object with llvm-jitlink
spends most of the link time in these two assertions.
Symbol names in a LinkGraph are interned in the graph's
SymbolStringPool, so equal names are represented by equal (non-owning)
SymbolStringPtrs and can be looked up with a hash. Track the names of the
non-local symbols added to the graph in two DenseSets and use them for the
duplicate checks instead of scanning the symbol lists.
A name is dropped from the sets when its symbol leaves the graph, so that
removing a symbol releases its name for reuse, and the sets are only
maintained when assertions are enabled, since the checks that use them are
asserts.
---
.../llvm/ExecutionEngine/JITLink/JITLink.h | 43 +++++++++++++---
.../JITLink/LinkGraphTests.cpp | 49 +++++++++++++++++++
2 files changed, 84 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
index f6794636df1f8b..8eab13c564fa69 100644
--- a/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
+++ b/llvm/include/llvm/ExecutionEngine/JITLink/JITLink.h
@@ -1313,10 +1313,9 @@ class LinkGraph {
orc::ExecutorAddr Address,
orc::ExecutorAddrDiff Size, Linkage L, Scope S,
bool IsLive) {
- assert((S == Scope::Local || llvm::none_of(AbsoluteSymbols,
- [&](const Symbol *Sym) {
- return Sym->getName() == Name;
- })) &&
+ assert((S == Scope::Local ||
+ AbsoluteSymbolNames.insert(orc::NonOwningSymbolStringPtr(Name))
+ .second) &&
"Duplicate absolute symbol");
auto &Sym = Symbol::constructAbsolute(Allocator, createAddressable(Address),
std::move(Name), Size, L, S, IsLive);
@@ -1353,10 +1352,9 @@ class LinkGraph {
orc::SymbolStringPtr Name,
orc::ExecutorAddrDiff Size, Linkage L, Scope S,
bool IsCallable, bool IsLive) {
- assert((S == Scope::Local || llvm::none_of(defined_symbols(),
- [&](const Symbol *Sym) {
- return Sym->getName() == Name;
- })) &&
+ assert((S == Scope::Local ||
+ DefinedSymbolNames.insert(orc::NonOwningSymbolStringPtr(Name))
+ .second) &&
"Duplicate defined symbol");
auto &Sym =
Symbol::constructNamedDef(Allocator, Content, Offset, std::move(Name),
@@ -1621,6 +1619,9 @@ class LinkGraph {
assert(AbsoluteSymbols.count(&Sym) &&
"Symbol is not in the absolute symbols set");
AbsoluteSymbols.erase(&Sym);
+#ifndef NDEBUG
+ forgetSymbolName(AbsoluteSymbolNames, Sym);
+#endif
Addressable &Base = *Sym.Base;
assert(llvm::none_of(external_symbols(),
[&](Symbol *AS) { return AS->Base == &Base; }) &&
@@ -1632,6 +1633,9 @@ class LinkGraph {
/// Removes defined symbols. Does not remove the underlying block.
void removeDefinedSymbol(Symbol &Sym) {
assert(Sym.isDefined() && "Sym is not a defined symbol");
+#ifndef NDEBUG
+ forgetSymbolName(DefinedSymbolNames, Sym);
+#endif
Sym.getSection().removeSymbol(Sym);
destroySymbol(Sym);
}
@@ -1654,6 +1658,10 @@ class LinkGraph {
assert(Sections.count(Sec.getName()) && "Section not found");
assert(Sections.find(Sec.getName())->second.get() == &Sec &&
"Section map entry invalid");
+#ifndef NDEBUG
+ for (Symbol *Sym : Sec.symbols())
+ forgetSymbolName(DefinedSymbolNames, *Sym);
+#endif
Sections.erase(Sec.getName());
}
@@ -1683,6 +1691,25 @@ class LinkGraph {
DenseMap<StringRef, std::unique_ptr<Section>> Sections;
ExternalSymbolMap ExternalSymbols;
AbsoluteSymbolSet AbsoluteSymbols;
+#ifndef NDEBUG
+ // Names of the non-local symbols that have been added to this graph. These
+ // are used to detect duplicate definitions, and are only maintained in
+ // assertions-enabled builds (where the duplicate-definition checks are
+ // active). Interning guarantees that equal names are represented by equal
+ // SymbolStringPtrs, so looking a name up here is O(1), whereas scanning the
+ // graph's symbols for each newly added symbol is quadratic in the number of
+ // symbols in the graph.
+ DenseSet<orc::NonOwningSymbolStringPtr> AbsoluteSymbolNames;
+ DenseSet<orc::NonOwningSymbolStringPtr> DefinedSymbolNames;
+
+ // Stop tracking the name of a symbol that is about to leave the graph, so
+ // that the entry does not outlive the symbol's pooled name.
+ static void forgetSymbolName(DenseSet<orc::NonOwningSymbolStringPtr> &Names,
+ const Symbol &Sym) {
+ if (Sym.hasName() && Sym.getScope() != Scope::Local)
+ Names.erase(orc::NonOwningSymbolStringPtr(Sym.getName()));
+ }
+#endif
orc::shared::AllocActions AAs;
};
diff --git a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
index c7608734935282..b4180f4325ef34 100644
--- a/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
+++ b/llvm/unittests/ExecutionEngine/JITLink/LinkGraphTests.cpp
@@ -907,3 +907,52 @@ TEST(LinkGraphTest, BasicLayoutHonorsNoAlloc) {
EXPECT_EQ(SegInfo.Alignment, 8U);
EXPECT_EQ(SegInfo.ContentSize, 8U);
}
+
+TEST(LinkGraphTest, DuplicateSymbolDetection) {
+ // Check that duplicate definitions of non-local symbols are diagnosed, and
+ // that same-named local symbols are still permitted.
+ LinkGraph G("foo", std::make_shared<orc::SymbolStringPool>(),
+ Triple("x86_64-apple-darwin"), SubtargetFeatures(),
+ getGenericEdgeKindName);
+
+ auto &Sec =
+ G.createSection("__data", orc::MemProt::Read | orc::MemProt::Write);
+ auto &B = G.createContentBlock(Sec, BlockContent, orc::ExecutorAddr(0x1000),
+ 8, 0);
+
+ G.addDefinedSymbol(B, 0, "defined", 4, Linkage::Strong, Scope::Default,
+ false, false);
+ G.addAbsoluteSymbol("absolute", orc::ExecutorAddr(0x2000), 4,
+ Linkage::Strong, Scope::Default, false);
+
+ // Local symbols are permitted to share a name with a non-local symbol.
+ G.addDefinedSymbol(B, 4, "defined", 4, Linkage::Strong, Scope::Local, false,
+ false);
+ G.addAbsoluteSymbol("absolute", orc::ExecutorAddr(0x2004), 4,
+ Linkage::Strong, Scope::Local, false);
+
+ // Removing a symbol releases its name, so it can be used again without
+ // tripping the duplicate checks.
+ Symbol &ToRemove = G.addDefinedSymbol(B, 8, "reused", 4, Linkage::Strong,
+ Scope::Default, false, false);
+ G.removeDefinedSymbol(ToRemove);
+ G.addDefinedSymbol(B, 8, "reused", 4, Linkage::Strong, Scope::Default, false,
+ false);
+
+ Symbol &AbsToRemove =
+ G.addAbsoluteSymbol("reused_absolute", orc::ExecutorAddr(0x3000), 4,
+ Linkage::Strong, Scope::Default, false);
+ G.removeAbsoluteSymbol(AbsToRemove);
+ G.addAbsoluteSymbol("reused_absolute", orc::ExecutorAddr(0x3000), 4,
+ Linkage::Strong, Scope::Default, false);
+
+#ifndef NDEBUG
+ EXPECT_DEATH(G.addDefinedSymbol(B, 0, "defined", 4, Linkage::Strong,
+ Scope::Default, false, false),
+ "Duplicate defined symbol");
+
+ EXPECT_DEATH(G.addAbsoluteSymbol("absolute", orc::ExecutorAddr(0x2000), 4,
+ Linkage::Strong, Scope::Default, false),
+ "Duplicate absolute symbol");
+#endif
+}
More information about the llvm-commits
mailing list