Skip to content

Commit

Permalink
[LLD] Remove global state in lld/COFF
Browse files Browse the repository at this point in the history
Remove globals from the lldCOFF library, by moving globals into a context class.
This patch mostly moves the config object into COFFLinkerContext.

See https://lists.llvm.org/pipermail/llvm-dev/2021-June/151184.html for
context about removing globals from LLD.

Reviewed By: aganea

Differential Revision: https://reviews.llvm.org/D110450
  • Loading branch information
amykhuang authored and aganea committed Jan 8, 2023
1 parent ad41d1e commit 7370ff6
Show file tree
Hide file tree
Showing 31 changed files with 1,038 additions and 828 deletions.
14 changes: 10 additions & 4 deletions lld/COFF/COFFLinkerContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
//===----------------------------------------------------------------------===//

#include "COFFLinkerContext.h"
#include "Symbols.h"
#include "lld/Common/Memory.h"
#include "llvm/BinaryFormat/COFF.h"
#include "llvm/DebugInfo/CodeView/TypeHashing.h"
#include "llvm/Demangle/Demangle.h"

namespace lld::coff {

COFFLinkerContext::COFFLinkerContext()
: symtab(*this), rootTimer("Total Linking Time"),
: driver(*this), symtab(*this), rootTimer("Total Linking Time"),
inputFileTimer("Input File Reading", rootTimer),
ltoTimer("LTO", rootTimer), gcTimer("GC", rootTimer),
icfTimer("ICF", rootTimer), codeLayoutTimer("Code Layout", rootTimer),
Expand All @@ -33,6 +35,10 @@ COFFLinkerContext::COFFLinkerContext()
symbolMergingTimer("Symbol Merging", addObjectsTimer),
publicsLayoutTimer("Publics Stream Layout", totalPdbLinkTimer),
tpiStreamLayoutTimer("TPI Stream Layout", totalPdbLinkTimer),
diskCommitTimer("Commit to Disk", totalPdbLinkTimer) {}

diskCommitTimer("Commit to Disk", totalPdbLinkTimer) {
FakeSection ltoTextSection(llvm::COFF::IMAGE_SCN_MEM_EXECUTE);
FakeSection ltoDataSection(llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA);
ltoTextSectionChunk = make<FakeSectionChunk>(&ltoTextSection.section);
ltoDataSectionChunk = make<FakeSectionChunk>(&ltoDataSection.section);
}
} // namespace lld::coff
17 changes: 13 additions & 4 deletions lld/COFF/COFFLinkerContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
//
//===----------------------------------------------------------------------===//

#ifndef LLD_COFF_COFFLinkerContext_H
#define LLD_COFF_COFFLinkerContext_H
#ifndef LLD_COFF_COFFLINKERCONTEXT_H
#define LLD_COFF_COFFLINKERCONTEXT_H

#include "Chunks.h"
#include "Config.h"
#include "DebugTypes.h"
#include "Driver.h"
#include "InputFiles.h"
#include "SymbolTable.h"
#include "Writer.h"
Expand All @@ -27,9 +28,9 @@ class COFFLinkerContext : public CommonLinkerContext {
COFFLinkerContext &operator=(const COFFLinkerContext &) = delete;
~COFFLinkerContext() = default;

void addTpiSource(TpiSource *tpi) { tpiSourceList.push_back(tpi); }

LinkerDriver driver;
SymbolTable symtab;
COFFOptTable optTable;

std::vector<ObjFile *> objFileInstances;
std::map<std::string, PDBInputFile *> pdbInputFileInstances;
Expand All @@ -41,6 +42,8 @@ class COFFLinkerContext : public CommonLinkerContext {
/// All sources of type information in the program.
std::vector<TpiSource *> tpiSourceList;

void addTpiSource(TpiSource *tpi) { tpiSourceList.push_back(tpi); }

std::map<llvm::codeview::GUID, TpiSource *> typeServerSourceMappings;
std::map<uint32_t, TpiSource *> precompSourceMappings;

Expand All @@ -52,6 +55,10 @@ class COFFLinkerContext : public CommonLinkerContext {
return c->osidx == 0 ? nullptr : outputSections[c->osidx - 1];
}

// Fake sections for parsing bitcode files.
FakeSectionChunk *ltoTextSectionChunk;
FakeSectionChunk *ltoDataSectionChunk;

// All timers used in the COFF linker.
Timer rootTimer;
Timer inputFileTimer;
Expand All @@ -77,6 +84,8 @@ class COFFLinkerContext : public CommonLinkerContext {
Timer publicsLayoutTimer;
Timer tpiStreamLayoutTimer;
Timer diskCommitTimer;

Configuration config;
};

} // namespace lld::coff
Expand Down
14 changes: 8 additions & 6 deletions lld/COFF/CallGraphSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ class CallGraphSort {
private:
std::vector<Cluster> clusters;
std::vector<const SectionChunk *> sections;

const COFFLinkerContext &ctx;
};

// Maximum amount the combined cluster density can be worse than the original
Expand All @@ -71,8 +73,8 @@ using SectionPair = std::pair<const SectionChunk *, const SectionChunk *>;
// Take the edge list in Config->CallGraphProfile, resolve symbol names to
// Symbols, and generate a graph between InputSections with the provided
// weights.
CallGraphSort::CallGraphSort(const COFFLinkerContext &ctx) {
MapVector<SectionPair, uint64_t> &profile = config->callGraphProfile;
CallGraphSort::CallGraphSort(const COFFLinkerContext &ctx) : ctx(ctx) {
const MapVector<SectionPair, uint64_t> &profile = ctx.config.callGraphProfile;
DenseMap<const SectionChunk *, int> secToCluster;

auto getOrCreateNode = [&](const SectionChunk *isec) -> int {
Expand All @@ -85,7 +87,7 @@ CallGraphSort::CallGraphSort(const COFFLinkerContext &ctx) {
};

// Create the graph.
for (std::pair<SectionPair, uint64_t> &c : profile) {
for (const std::pair<SectionPair, uint64_t> &c : profile) {
const auto *fromSec = cast<SectionChunk>(c.first.first->repl);
const auto *toSec = cast<SectionChunk>(c.first.second->repl);
uint64_t weight = c.second;
Expand Down Expand Up @@ -205,11 +207,11 @@ DenseMap<const SectionChunk *, int> CallGraphSort::run() {
break;
}
}
if (!config->printSymbolOrder.empty()) {
if (!ctx.config.printSymbolOrder.empty()) {
std::error_code ec;
raw_fd_ostream os(config->printSymbolOrder, ec, sys::fs::OF_None);
raw_fd_ostream os(ctx.config.printSymbolOrder, ec, sys::fs::OF_None);
if (ec) {
error("cannot open " + config->printSymbolOrder + ": " + ec.message());
error("cannot open " + ctx.config.printSymbolOrder + ": " + ec.message());
return orderMap;
}
// Print the symbols ordered by C3, in the order of increasing curOrder
Expand Down
Loading

0 comments on commit 7370ff6

Please sign in to comment.