1317025Sdim//===- LTO.cpp ------------------------------------------------------------===//
2317025Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317025Sdim//
7317025Sdim//===----------------------------------------------------------------------===//
8317025Sdim
9317025Sdim#include "LTO.h"
10317025Sdim#include "Config.h"
11317025Sdim#include "InputFiles.h"
12317025Sdim#include "Symbols.h"
13353358Sdim#include "lld/Common/Args.h"
14327952Sdim#include "lld/Common/ErrorHandler.h"
15341825Sdim#include "lld/Common/Strings.h"
16327952Sdim#include "lld/Common/TargetOptionsCommandFlags.h"
17317025Sdim#include "llvm/ADT/STLExtras.h"
18317025Sdim#include "llvm/ADT/SmallString.h"
19317025Sdim#include "llvm/ADT/StringRef.h"
20317025Sdim#include "llvm/ADT/Twine.h"
21353358Sdim#include "llvm/Bitcode/BitcodeWriter.h"
22317025Sdim#include "llvm/IR/DiagnosticPrinter.h"
23327952Sdim#include "llvm/LTO/Caching.h"
24317025Sdim#include "llvm/LTO/Config.h"
25317025Sdim#include "llvm/LTO/LTO.h"
26317025Sdim#include "llvm/Object/SymbolicFile.h"
27317025Sdim#include "llvm/Support/CodeGen.h"
28317025Sdim#include "llvm/Support/Error.h"
29317025Sdim#include "llvm/Support/FileSystem.h"
30317025Sdim#include "llvm/Support/MemoryBuffer.h"
31317025Sdim#include "llvm/Support/raw_ostream.h"
32317025Sdim#include <algorithm>
33317025Sdim#include <cstddef>
34317025Sdim#include <memory>
35317025Sdim#include <string>
36317025Sdim#include <system_error>
37317025Sdim#include <vector>
38317025Sdim
39317025Sdimusing namespace llvm;
40317025Sdimusing namespace llvm::object;
41317025Sdim
42360784Sdimnamespace lld {
43360784Sdimnamespace coff {
44317025Sdim
45353358Sdim// Creates an empty file to and returns a raw_fd_ostream to write to it.
46353358Sdimstatic std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
47353358Sdim  std::error_code ec;
48353358Sdim  auto ret =
49360784Sdim      std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
50353358Sdim  if (ec) {
51353358Sdim    error("cannot open " + file + ": " + ec.message());
52353358Sdim    return nullptr;
53353358Sdim  }
54353358Sdim  return ret;
55353358Sdim}
56317025Sdim
57353358Sdimstatic std::string getThinLTOOutputFile(StringRef path) {
58353358Sdim  return lto::getThinLTOOutputFile(path,
59353358Sdim                                   config->thinLTOPrefixReplace.first,
60353358Sdim                                   config->thinLTOPrefixReplace.second);
61353358Sdim}
62353358Sdim
63353358Sdimstatic lto::Config createConfig() {
64353358Sdim  lto::Config c;
65353358Sdim  c.Options = initTargetOptionsFromCodeGenFlags();
66353358Sdim
67341825Sdim  // Always emit a section per function/datum with LTO. LLVM LTO should get most
68341825Sdim  // of the benefit of linker GC, but there are still opportunities for ICF.
69353358Sdim  c.Options.FunctionSections = true;
70353358Sdim  c.Options.DataSections = true;
71317025Sdim
72327952Sdim  // Use static reloc model on 32-bit x86 because it usually results in more
73327952Sdim  // compact code, and because there are also known code generation bugs when
74327952Sdim  // using the PIC model (see PR34306).
75353358Sdim  if (config->machine == COFF::IMAGE_FILE_MACHINE_I386)
76353358Sdim    c.RelocModel = Reloc::Static;
77327952Sdim  else
78353358Sdim    c.RelocModel = Reloc::PIC_;
79353358Sdim  c.DisableVerify = true;
80353358Sdim  c.DiagHandler = diagnosticHandler;
81353358Sdim  c.OptLevel = config->ltoo;
82353358Sdim  c.CPU = getCPUStr();
83353358Sdim  c.MAttrs = getMAttrs();
84353358Sdim  c.CGOptLevel = args::getCGOptLevel(config->ltoo);
85344779Sdim
86353358Sdim  if (config->saveTemps)
87353358Sdim    checkError(c.addSaveTemps(std::string(config->outputFile) + ".",
88341825Sdim                              /*UseInputModulePath*/ true));
89353358Sdim  return c;
90317025Sdim}
91317025Sdim
92353358SdimBitcodeCompiler::BitcodeCompiler() {
93353358Sdim  // Initialize indexFile.
94353358Sdim  if (!config->thinLTOIndexOnlyArg.empty())
95353358Sdim    indexFile = openFile(config->thinLTOIndexOnlyArg);
96317025Sdim
97353358Sdim  // Initialize ltoObj.
98353358Sdim  lto::ThinBackend backend;
99353358Sdim  if (config->thinLTOIndexOnly) {
100353358Sdim    auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
101353358Sdim    backend = lto::createWriteIndexesThinBackend(
102353358Sdim        config->thinLTOPrefixReplace.first, config->thinLTOPrefixReplace.second,
103353358Sdim        config->thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite);
104353358Sdim  } else if (config->thinLTOJobs != 0) {
105353358Sdim    backend = lto::createInProcessThinBackend(config->thinLTOJobs);
106353358Sdim  }
107353358Sdim
108360784Sdim  ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,
109353358Sdim                                       config->ltoPartitions);
110353358Sdim}
111353358Sdim
112317025SdimBitcodeCompiler::~BitcodeCompiler() = default;
113317025Sdim
114353358Sdimstatic void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); }
115317025Sdim
116353358Sdimvoid BitcodeCompiler::add(BitcodeFile &f) {
117353358Sdim  lto::InputFile &obj = *f.obj;
118353358Sdim  unsigned symNum = 0;
119353358Sdim  std::vector<Symbol *> symBodies = f.getSymbols();
120353358Sdim  std::vector<lto::SymbolResolution> resols(symBodies.size());
121317025Sdim
122353358Sdim  if (config->thinLTOIndexOnly)
123353358Sdim    thinIndices.insert(obj.getName());
124353358Sdim
125317025Sdim  // Provide a resolution to the LTO API for each symbol.
126353358Sdim  for (const lto::InputFile::Symbol &objSym : obj.symbols()) {
127353358Sdim    Symbol *sym = symBodies[symNum];
128353358Sdim    lto::SymbolResolution &r = resols[symNum];
129353358Sdim    ++symNum;
130317025Sdim
131317025Sdim    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
132317025Sdim    // reports two symbols for module ASM defined. Without this check, lld
133317025Sdim    // flags an undefined in IR with a definition in ASM as prevailing.
134317025Sdim    // Once IRObjectFile is fixed to report only one symbol this hack can
135317025Sdim    // be removed.
136353358Sdim    r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
137353358Sdim    r.VisibleToRegularObj = sym->isUsedInRegularObj;
138353358Sdim    if (r.Prevailing)
139353358Sdim      undefine(sym);
140317025Sdim  }
141353358Sdim  checkError(ltoObj->add(std::move(f.obj), resols));
142317025Sdim}
143317025Sdim
144317025Sdim// Merge all the bitcode files we have seen, codegen the result
145317025Sdim// and return the resulting objects.
146317025Sdimstd::vector<StringRef> BitcodeCompiler::compile() {
147353358Sdim  unsigned maxTasks = ltoObj->getMaxTasks();
148353358Sdim  buf.resize(maxTasks);
149353358Sdim  files.resize(maxTasks);
150317025Sdim
151327952Sdim  // The /lldltocache option specifies the path to a directory in which to cache
152327952Sdim  // native object files for ThinLTO incremental builds. If a path was
153327952Sdim  // specified, configure LTO to use it as the cache directory.
154353358Sdim  lto::NativeObjectCache cache;
155353358Sdim  if (!config->ltoCache.empty())
156353358Sdim    cache = check(lto::localCache(
157353358Sdim        config->ltoCache, [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
158353358Sdim          files[task] = std::move(mb);
159341825Sdim        }));
160317025Sdim
161353358Sdim  checkError(ltoObj->run(
162353358Sdim      [&](size_t task) {
163360784Sdim        return std::make_unique<lto::NativeObjectStream>(
164360784Sdim            std::make_unique<raw_svector_ostream>(buf[task]));
165327952Sdim      },
166353358Sdim      cache));
167327952Sdim
168353358Sdim  // Emit empty index files for non-indexed files
169353358Sdim  for (StringRef s : thinIndices) {
170353358Sdim    std::string path = getThinLTOOutputFile(s);
171353358Sdim    openFile(path + ".thinlto.bc");
172353358Sdim    if (config->thinLTOEmitImportsFiles)
173353358Sdim      openFile(path + ".imports");
174353358Sdim  }
175327952Sdim
176353358Sdim  // ThinLTO with index only option is required to generate only the index
177353358Sdim  // files. After that, we exit from linker and ThinLTO backend runs in a
178353358Sdim  // distributed environment.
179353358Sdim  if (config->thinLTOIndexOnly) {
180360784Sdim    if (!config->ltoObjPath.empty())
181360784Sdim      saveBuffer(buf[0], config->ltoObjPath);
182353358Sdim    if (indexFile)
183353358Sdim      indexFile->close();
184353358Sdim    return {};
185353358Sdim  }
186353358Sdim
187353358Sdim  if (!config->ltoCache.empty())
188353358Sdim    pruneCache(config->ltoCache, config->ltoCachePolicy);
189353358Sdim
190353358Sdim  std::vector<StringRef> ret;
191353358Sdim  for (unsigned i = 0; i != maxTasks; ++i) {
192353358Sdim    if (buf[i].empty())
193317025Sdim      continue;
194353358Sdim    if (config->saveTemps) {
195353358Sdim      if (i == 0)
196353358Sdim        saveBuffer(buf[i], config->outputFile + ".lto.obj");
197317025Sdim      else
198353358Sdim        saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.obj");
199317025Sdim    }
200353358Sdim    ret.emplace_back(buf[i].data(), buf[i].size());
201317025Sdim  }
202327952Sdim
203353358Sdim  for (std::unique_ptr<MemoryBuffer> &file : files)
204353358Sdim    if (file)
205353358Sdim      ret.push_back(file->getBuffer());
206327952Sdim
207353358Sdim  return ret;
208317025Sdim}
209360784Sdim
210360784Sdim} // namespace coff
211360784Sdim} // namespace lld
212