[LoopStrengthReduce] Teach LoopStrengthReduce to preserve MemorySSA is available.
[llvm-project.git] / lld / wasm / LTO.cpp
blob615b4c9dc944c03118ba42a18782b783b9cf159e
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 //===----------------------------------------------------------------------===//
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/Caching.h"
23 #include "llvm/LTO/Config.h"
24 #include "llvm/LTO/LTO.h"
25 #include "llvm/Object/SymbolicFile.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>
38 using namespace llvm;
40 namespace lld {
41 namespace wasm {
42 static std::unique_ptr<lto::LTO> createLTO() {
43 lto::Config c;
44 c.Options = initTargetOptionsFromCodeGenFlags();
46 // Always emit a section per function/data with LTO.
47 c.Options.FunctionSections = true;
48 c.Options.DataSections = true;
50 c.DisableVerify = config->disableVerify;
51 c.DiagHandler = diagnosticHandler;
52 c.OptLevel = config->ltoo;
53 c.MAttrs = getMAttrs();
54 c.CGOptLevel = args::getCGOptLevel(config->ltoo);
56 if (config->relocatable)
57 c.RelocModel = None;
58 else if (config->isPic)
59 c.RelocModel = Reloc::PIC_;
60 else
61 c.RelocModel = Reloc::Static;
63 if (config->saveTemps)
64 checkError(c.addSaveTemps(config->outputFile.str() + ".",
65 /*UseInputModulePath*/ true));
67 lto::ThinBackend backend;
68 if (config->thinLTOJobs != -1U)
69 backend = lto::createInProcessThinBackend(config->thinLTOJobs);
70 return std::make_unique<lto::LTO>(std::move(c), backend,
71 config->ltoPartitions);
74 BitcodeCompiler::BitcodeCompiler() : ltoObj(createLTO()) {}
76 BitcodeCompiler::~BitcodeCompiler() = default;
78 static void undefine(Symbol *s) {
79 if (auto f = dyn_cast<DefinedFunction>(s))
80 replaceSymbol<UndefinedFunction>(f, f->getName(), "", "", 0, f->getFile(),
81 f->signature);
82 else if (isa<DefinedData>(s))
83 replaceSymbol<UndefinedData>(s, s->getName(), 0, s->getFile());
84 else
85 llvm_unreachable("unexpected symbol kind");
88 void BitcodeCompiler::add(BitcodeFile &f) {
89 lto::InputFile &obj = *f.obj;
90 unsigned symNum = 0;
91 ArrayRef<Symbol *> syms = f.getSymbols();
92 std::vector<lto::SymbolResolution> resols(syms.size());
94 // Provide a resolution to the LTO API for each symbol.
95 for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
96 Symbol *sym = syms[symNum];
97 lto::SymbolResolution &r = resols[symNum];
98 ++symNum;
100 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
101 // reports two symbols for module ASM defined. Without this check, lld
102 // flags an undefined in IR with a definition in ASM as prevailing.
103 // Once IRObjectFile is fixed to report only one symbol this hack can
104 // be removed.
105 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
106 r.VisibleToRegularObj = config->relocatable || sym->isUsedInRegularObj ||
107 sym->isNoStrip() ||
108 (r.Prevailing && sym->isExported());
109 if (r.Prevailing)
110 undefine(sym);
112 // We tell LTO to not apply interprocedural optimization for wrapped
113 // (with --wrap) symbols because otherwise LTO would inline them while
114 // their values are still not final.
115 r.LinkerRedefined = !sym->canInline;
117 checkError(ltoObj->add(std::move(f.obj), resols));
120 // Merge all the bitcode files we have seen, codegen the result
121 // and return the resulting objects.
122 std::vector<StringRef> BitcodeCompiler::compile() {
123 unsigned maxTasks = ltoObj->getMaxTasks();
124 buf.resize(maxTasks);
125 files.resize(maxTasks);
127 // The --thinlto-cache-dir option specifies the path to a directory in which
128 // to cache native object files for ThinLTO incremental builds. If a path was
129 // specified, configure LTO to use it as the cache directory.
130 lto::NativeObjectCache cache;
131 if (!config->thinLTOCacheDir.empty())
132 cache = check(
133 lto::localCache(config->thinLTOCacheDir,
134 [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
135 files[task] = std::move(mb);
136 }));
138 checkError(ltoObj->run(
139 [&](size_t task) {
140 return std::make_unique<lto::NativeObjectStream>(
141 std::make_unique<raw_svector_ostream>(buf[task]));
143 cache));
145 if (!config->thinLTOCacheDir.empty())
146 pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy);
148 std::vector<StringRef> ret;
149 for (unsigned i = 0; i != maxTasks; ++i) {
150 if (buf[i].empty())
151 continue;
152 if (config->saveTemps) {
153 if (i == 0)
154 saveBuffer(buf[i], config->outputFile + ".lto.o");
155 else
156 saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
158 ret.emplace_back(buf[i].data(), buf[i].size());
161 for (std::unique_ptr<MemoryBuffer> &file : files)
162 if (file)
163 ret.push_back(file->getBuffer());
165 return ret;
168 } // namespace wasm
169 } // namespace lld