Skip to content

Commit 071e823

Browse files
aengelkeAlexisPerry
authored andcommitted
[MC] Eliminate two symbol-related hash maps (llvm#95464)
Previously, a symbol insertion requires (at least) three hash table operations: - Lookup/create entry in Symbols (main symbol table) - Lookup NextUniqueID to deduplicate identical temporary labels - Add entry to UsedNames, which is also used to serve as storage for the symbol name in the MCSymbol. All three lookups are done with the same name, so combining these into a single table reduces the number of lookups to one. Thus, a pointer to a symbol table entry can be passed to createSymbol to avoid a duplicate lookup of the same name. The new symbol table entry value is placed in a separate header to avoid including MCContext in MCSymbol or vice versa.
1 parent 7ef6fe8 commit 071e823

File tree

15 files changed

+147
-101
lines changed

15 files changed

+147
-101
lines changed

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,9 @@ Error LowerAnnotations::runOnFunctions(BinaryContext &BC) {
636636
Error CleanMCState::runOnFunctions(BinaryContext &BC) {
637637
MCContext &Ctx = *BC.Ctx;
638638
for (const auto &SymMapEntry : Ctx.getSymbols()) {
639-
const MCSymbol *S = SymMapEntry.second;
639+
const MCSymbol *S = SymMapEntry.getValue().Symbol;
640+
if (!S)
641+
continue;
640642
if (S->isDefined()) {
641643
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: Symbol \"" << S->getName()
642644
<< "\" is already defined\n");

llvm/include/llvm/MC/MCContext.h

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/MC/MCDwarf.h"
2222
#include "llvm/MC/MCPseudoProbe.h"
2323
#include "llvm/MC/MCSection.h"
24+
#include "llvm/MC/MCSymbolTableEntry.h"
2425
#include "llvm/MC/SectionKind.h"
2526
#include "llvm/Support/Allocator.h"
2627
#include "llvm/Support/Compiler.h"
@@ -81,7 +82,7 @@ struct WasmSignature;
8182
///
8283
class MCContext {
8384
public:
84-
using SymbolTable = StringMap<MCSymbol *, BumpPtrAllocator &>;
85+
using SymbolTable = StringMap<MCSymbolTableValue, BumpPtrAllocator &>;
8586
using DiagHandlerTy =
8687
std::function<void(const SMDiagnostic &, bool, const SourceMgr &,
8788
std::vector<const MDNode *> &)>;
@@ -147,7 +148,7 @@ class MCContext {
147148

148149
SpecificBumpPtrAllocator<wasm::WasmSignature> WasmSignatureAllocator;
149150

150-
/// Bindings of names to symbols.
151+
/// Bindings of names to symbol table values.
151152
SymbolTable Symbols;
152153

153154
/// A mapping from a local label number and an instance count to a symbol.
@@ -158,18 +159,8 @@ class MCContext {
158159
/// We have three labels represented by the pairs (1, 0), (2, 0) and (1, 1)
159160
DenseMap<std::pair<unsigned, unsigned>, MCSymbol *> LocalSymbols;
160161

161-
/// Keeps tracks of names that were used both for used declared and
162-
/// artificial symbols. The value is "true" if the name has been used for a
163-
/// non-section symbol (there can be at most one of those, plus an unlimited
164-
/// number of section symbols with the same name).
165-
StringMap<bool, BumpPtrAllocator &> UsedNames;
166-
167162
/// Keeps track of labels that are used in inline assembly.
168-
SymbolTable InlineAsmUsedLabelNames;
169-
170-
/// The next ID to dole out to an unnamed assembler temporary symbol with
171-
/// a given prefix.
172-
StringMap<unsigned> NextID;
163+
StringMap<MCSymbol *, BumpPtrAllocator &> InlineAsmUsedLabelNames;
173164

174165
/// Instances of directional local labels.
175166
DenseMap<unsigned, MCLabel *> Instances;
@@ -348,10 +339,11 @@ class MCContext {
348339

349340
MCDataFragment *allocInitialFragment(MCSection &Sec);
350341

351-
MCSymbol *createSymbolImpl(const StringMapEntry<bool> *Name,
352-
bool IsTemporary);
353-
MCSymbol *createSymbol(StringRef Name, bool AlwaysAddSuffix,
354-
bool IsTemporary);
342+
MCSymbolTableEntry &getSymbolTableEntry(StringRef Name);
343+
344+
MCSymbol *createSymbolImpl(const MCSymbolTableEntry *Name, bool IsTemporary);
345+
MCSymbol *createRenamableSymbol(const Twine &Name, bool AlwaysAddSuffix,
346+
bool IsTemporary);
355347

356348
MCSymbol *getOrCreateDirectionalLocalSymbol(unsigned LocalLabelVal,
357349
unsigned Instance);
@@ -362,7 +354,7 @@ class MCContext {
362354
unsigned UniqueID,
363355
const MCSymbolELF *LinkedToSym);
364356

365-
MCSymbolXCOFF *createXCOFFSymbolImpl(const StringMapEntry<bool> *Name,
357+
MCSymbolXCOFF *createXCOFFSymbolImpl(const MCSymbolTableEntry *Name,
366358
bool IsTemporary);
367359

368360
/// Map of currently defined macros.

llvm/include/llvm/MC/MCSymbol.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/MC/MCExpr.h"
1919
#include "llvm/MC/MCFragment.h"
20+
#include "llvm/MC/MCSymbolTableEntry.h"
2021
#include "llvm/Support/ErrorHandling.h"
2122
#include "llvm/Support/MathExtras.h"
2223
#include <cassert>
@@ -156,11 +157,11 @@ class MCSymbol {
156157
/// system, the name is a pointer so isn't going to satisfy the 8 byte
157158
/// alignment of uint64_t. Account for that here.
158159
using NameEntryStorageTy = union {
159-
const StringMapEntry<bool> *NameEntry;
160+
const MCSymbolTableEntry *NameEntry;
160161
uint64_t AlignmentPadding;
161162
};
162163

163-
MCSymbol(SymbolKind Kind, const StringMapEntry<bool> *Name, bool isTemporary)
164+
MCSymbol(SymbolKind Kind, const MCSymbolTableEntry *Name, bool isTemporary)
164165
: IsTemporary(isTemporary), IsRedefinable(false), IsUsed(false),
165166
IsRegistered(false), IsExternal(false), IsPrivateExtern(false),
166167
IsWeakExternal(false), Kind(Kind), IsUsedInReloc(false),
@@ -173,8 +174,7 @@ class MCSymbol {
173174

174175
// Provide custom new/delete as we will only allocate space for a name
175176
// if we need one.
176-
void *operator new(size_t s, const StringMapEntry<bool> *Name,
177-
MCContext &Ctx);
177+
void *operator new(size_t s, const MCSymbolTableEntry *Name, MCContext &Ctx);
178178

179179
private:
180180
void operator delete(void *);
@@ -188,12 +188,12 @@ class MCSymbol {
188188
}
189189

190190
/// Get a reference to the name field. Requires that we have a name
191-
const StringMapEntry<bool> *&getNameEntryPtr() {
191+
const MCSymbolTableEntry *&getNameEntryPtr() {
192192
assert(HasName && "Name is required");
193193
NameEntryStorageTy *Name = reinterpret_cast<NameEntryStorageTy *>(this);
194194
return (*(Name - 1)).NameEntry;
195195
}
196-
const StringMapEntry<bool> *&getNameEntryPtr() const {
196+
const MCSymbolTableEntry *&getNameEntryPtr() const {
197197
return const_cast<MCSymbol*>(this)->getNameEntryPtr();
198198
}
199199

llvm/include/llvm/MC/MCSymbolCOFF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include "llvm/BinaryFormat/COFF.h"
1313
#include "llvm/MC/MCSymbol.h"
14+
#include "llvm/MC/MCSymbolTableEntry.h"
1415
#include <cstdint>
1516

1617
namespace llvm {
@@ -29,7 +30,7 @@ class MCSymbolCOFF : public MCSymbol {
2930
};
3031

3132
public:
32-
MCSymbolCOFF(const StringMapEntry<bool> *Name, bool isTemporary)
33+
MCSymbolCOFF(const MCSymbolTableEntry *Name, bool isTemporary)
3334
: MCSymbol(SymbolKindCOFF, Name, isTemporary) {}
3435

3536
uint16_t getType() const {

llvm/include/llvm/MC/MCSymbolELF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#define LLVM_MC_MCSYMBOLELF_H
1010

1111
#include "llvm/MC/MCSymbol.h"
12+
#include "llvm/MC/MCSymbolTableEntry.h"
1213

1314
namespace llvm {
1415
class MCSymbolELF : public MCSymbol {
@@ -17,7 +18,7 @@ class MCSymbolELF : public MCSymbol {
1718
const MCExpr *SymbolSize = nullptr;
1819

1920
public:
20-
MCSymbolELF(const StringMapEntry<bool> *Name, bool isTemporary)
21+
MCSymbolELF(const MCSymbolTableEntry *Name, bool isTemporary)
2122
: MCSymbol(SymbolKindELF, Name, isTemporary) {}
2223
void setSize(const MCExpr *SS) { SymbolSize = SS; }
2324

llvm/include/llvm/MC/MCSymbolGOFF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
#define LLVM_MC_MCSYMBOLGOFF_H
1515

1616
#include "llvm/MC/MCSymbol.h"
17+
#include "llvm/MC/MCSymbolTableEntry.h"
1718

1819
namespace llvm {
1920

2021
class MCSymbolGOFF : public MCSymbol {
2122
public:
22-
MCSymbolGOFF(const StringMapEntry<bool> *Name, bool IsTemporary)
23+
MCSymbolGOFF(const MCSymbolTableEntry *Name, bool IsTemporary)
2324
: MCSymbol(SymbolKindGOFF, Name, IsTemporary) {}
2425
static bool classof(const MCSymbol *S) { return S->isGOFF(); }
2526
};

llvm/include/llvm/MC/MCSymbolMachO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "llvm/ADT/Twine.h"
1212
#include "llvm/MC/MCSymbol.h"
13+
#include "llvm/MC/MCSymbolTableEntry.h"
1314

1415
namespace llvm {
1516
class MCSymbolMachO : public MCSymbol {
@@ -42,7 +43,7 @@ class MCSymbolMachO : public MCSymbol {
4243
};
4344

4445
public:
45-
MCSymbolMachO(const StringMapEntry<bool> *Name, bool isTemporary)
46+
MCSymbolMachO(const MCSymbolTableEntry *Name, bool isTemporary)
4647
: MCSymbol(SymbolKindMachO, Name, isTemporary) {}
4748

4849
// Reference type methods.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===-- llvm/MC/MCSymbolTableEntry.h - Symbol table entry -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_MC_MCSYMBOLTABLEENTRY_H
10+
#define LLVM_MC_MCSYMBOLTABLEENTRY_H
11+
12+
#include "llvm/ADT/StringMapEntry.h"
13+
14+
namespace llvm {
15+
16+
class MCSymbol;
17+
18+
/// The value for an entry in the symbol table of an MCContext.
19+
///
20+
/// This is in a separate file, because MCSymbol uses MCSymbolTableEntry (see
21+
/// below) to reuse the name that is stored in the symbol table.
22+
struct MCSymbolTableValue {
23+
/// The symbol associated with the name, if any.
24+
MCSymbol *Symbol = nullptr;
25+
26+
/// The next ID to dole out to an unnamed assembler temporary symbol with
27+
/// the prefix (symbol table key).
28+
unsigned NextUniqueID = 0;
29+
30+
/// Whether the name associated with this value is used for a symbol. This is
31+
/// not necessarily true: sometimes, we use a symbol table value without an
32+
/// associated symbol for accessing NextUniqueID when a suffix is added to a
33+
/// name. However, Used might be true even if Symbol is nullptr: temporary
34+
/// named symbols are not added to the symbol table.
35+
bool Used = false;
36+
};
37+
38+
/// MCContext stores MCSymbolTableValue in a string map (see MCSymbol::operator
39+
/// new). To avoid redundant storage of the name, MCSymbol stores a pointer (8
40+
/// bytes -- half the size of a StringRef) to the entry to access it.
41+
using MCSymbolTableEntry = StringMapEntry<MCSymbolTableValue>;
42+
43+
} // end namespace llvm
44+
45+
#endif

llvm/include/llvm/MC/MCSymbolWasm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "llvm/BinaryFormat/Wasm.h"
1212
#include "llvm/MC/MCSymbol.h"
13+
#include "llvm/MC/MCSymbolTableEntry.h"
1314

1415
namespace llvm {
1516

@@ -33,7 +34,7 @@ class MCSymbolWasm : public MCSymbol {
3334
const MCExpr *SymbolSize = nullptr;
3435

3536
public:
36-
MCSymbolWasm(const StringMapEntry<bool> *Name, bool isTemporary)
37+
MCSymbolWasm(const MCSymbolTableEntry *Name, bool isTemporary)
3738
: MCSymbol(SymbolKindWasm, Name, isTemporary) {}
3839
static bool classof(const MCSymbol *S) { return S->isWasm(); }
3940

llvm/include/llvm/MC/MCSymbolXCOFF.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/ADT/StringRef.h"
1212
#include "llvm/BinaryFormat/XCOFF.h"
1313
#include "llvm/MC/MCSymbol.h"
14+
#include "llvm/MC/MCSymbolTableEntry.h"
1415

1516
namespace llvm {
1617

@@ -21,7 +22,7 @@ class MCSymbolXCOFF : public MCSymbol {
2122
enum XCOFFSymbolFlags : uint16_t { SF_EHInfo = 0x0001 };
2223

2324
public:
24-
MCSymbolXCOFF(const StringMapEntry<bool> *Name, bool isTemporary)
25+
MCSymbolXCOFF(const MCSymbolTableEntry *Name, bool isTemporary)
2526
: MCSymbol(SymbolKindXCOFF, Name, isTemporary) {}
2627

2728
static bool classof(const MCSymbol *S) { return S->isXCOFF(); }

0 commit comments

Comments
 (0)