llvm-project
167 строк · 5.7 Кб
1//===- LTO.cpp ------------------------------------------------------------===//
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#include "LTO.h"
10#include "Config.h"
11#include "InputFiles.h"
12#include "Symbols.h"
13#include "lld/Common/Args.h"
14#include "lld/Common/ErrorHandler.h"
15#include "lld/Common/Strings.h"
16#include "lld/Common/TargetOptionsCommandFlags.h"
17#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallString.h"
19#include "llvm/ADT/StringRef.h"
20#include "llvm/ADT/Twine.h"
21#include "llvm/IR/DiagnosticPrinter.h"
22#include "llvm/LTO/Config.h"
23#include "llvm/LTO/LTO.h"
24#include "llvm/Object/SymbolicFile.h"
25#include "llvm/Support/Caching.h"
26#include "llvm/Support/CodeGen.h"
27#include "llvm/Support/Error.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
30#include "llvm/Support/raw_ostream.h"
31#include <algorithm>
32#include <cstddef>
33#include <memory>
34#include <string>
35#include <system_error>
36#include <vector>
37
38using namespace llvm;
39
40namespace lld::wasm {
41static std::unique_ptr<lto::LTO> createLTO() {
42lto::Config c;
43c.Options = initTargetOptionsFromCodeGenFlags();
44
45// Always emit a section per function/data with LTO.
46c.Options.FunctionSections = true;
47c.Options.DataSections = true;
48
49c.DisableVerify = config->disableVerify;
50c.DiagHandler = diagnosticHandler;
51c.OptLevel = config->ltoo;
52c.MAttrs = getMAttrs();
53c.CGOptLevel = config->ltoCgo;
54c.DebugPassManager = config->ltoDebugPassManager;
55
56if (config->relocatable)
57c.RelocModel = std::nullopt;
58else if (ctx.isPic)
59c.RelocModel = Reloc::PIC_;
60else
61c.RelocModel = Reloc::Static;
62
63if (config->saveTemps)
64checkError(c.addSaveTemps(config->outputFile.str() + ".",
65/*UseInputModulePath*/ true));
66lto::ThinBackend backend = lto::createInProcessThinBackend(
67llvm::heavyweight_hardware_concurrency(config->thinLTOJobs));
68return std::make_unique<lto::LTO>(std::move(c), backend,
69config->ltoPartitions);
70}
71
72BitcodeCompiler::BitcodeCompiler() : ltoObj(createLTO()) {}
73
74BitcodeCompiler::~BitcodeCompiler() = default;
75
76static void undefine(Symbol *s) {
77if (auto f = dyn_cast<DefinedFunction>(s))
78replaceSymbol<UndefinedFunction>(f, f->getName(), std::nullopt,
79std::nullopt, 0, f->getFile(),
80f->signature);
81else if (isa<DefinedData>(s))
82replaceSymbol<UndefinedData>(s, s->getName(), 0, s->getFile());
83else
84llvm_unreachable("unexpected symbol kind");
85}
86
87void BitcodeCompiler::add(BitcodeFile &f) {
88lto::InputFile &obj = *f.obj;
89unsigned symNum = 0;
90ArrayRef<Symbol *> syms = f.getSymbols();
91std::vector<lto::SymbolResolution> resols(syms.size());
92
93// Provide a resolution to the LTO API for each symbol.
94for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
95Symbol *sym = syms[symNum];
96lto::SymbolResolution &r = resols[symNum];
97++symNum;
98
99// Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
100// reports two symbols for module ASM defined. Without this check, lld
101// flags an undefined in IR with a definition in ASM as prevailing.
102// Once IRObjectFile is fixed to report only one symbol this hack can
103// be removed.
104r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
105r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
106sym->isNoStrip() ||
107(r.Prevailing && sym->isExported());
108if (r.Prevailing)
109undefine(sym);
110
111// We tell LTO to not apply interprocedural optimization for wrapped
112// (with --wrap) symbols because otherwise LTO would inline them while
113// their values are still not final.
114r.LinkerRedefined = !sym->canInline;
115}
116checkError(ltoObj->add(std::move(f.obj), resols));
117}
118
119// Merge all the bitcode files we have seen, codegen the result
120// and return the resulting objects.
121std::vector<StringRef> BitcodeCompiler::compile() {
122unsigned maxTasks = ltoObj->getMaxTasks();
123buf.resize(maxTasks);
124files.resize(maxTasks);
125
126// The --thinlto-cache-dir option specifies the path to a directory in which
127// to cache native object files for ThinLTO incremental builds. If a path was
128// specified, configure LTO to use it as the cache directory.
129FileCache cache;
130if (!config->thinLTOCacheDir.empty())
131cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
132[&](size_t task, const Twine &moduleName,
133std::unique_ptr<MemoryBuffer> mb) {
134files[task] = std::move(mb);
135}));
136
137checkError(ltoObj->run(
138[&](size_t task, const Twine &moduleName) {
139return std::make_unique<CachedFileStream>(
140std::make_unique<raw_svector_ostream>(buf[task]));
141},
142cache));
143
144if (!config->thinLTOCacheDir.empty())
145pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
146
147std::vector<StringRef> ret;
148for (unsigned i = 0; i != maxTasks; ++i) {
149if (buf[i].empty())
150continue;
151if (config->saveTemps) {
152if (i == 0)
153saveBuffer(buf[i], config->outputFile + ".lto.o");
154else
155saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
156}
157ret.emplace_back(buf[i].data(), buf[i].size());
158}
159
160for (std::unique_ptr<MemoryBuffer> &file : files)
161if (file)
162ret.push_back(file->getBuffer());
163
164return ret;
165}
166
167} // namespace lld::wasm
168