brintos

brintos / llvm-project-archived public Read only

0
0
Text · 10.2 KiB · b1efb28 Raw
272 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9#include "LTO.h"10#include "COFFLinkerContext.h"11#include "Config.h"12#include "InputFiles.h"13#include "Symbols.h"14#include "lld/Common/Args.h"15#include "lld/Common/CommonLinkerContext.h"16#include "lld/Common/Filesystem.h"17#include "lld/Common/Strings.h"18#include "lld/Common/TargetOptionsCommandFlags.h"19#include "llvm/ADT/STLExtras.h"20#include "llvm/ADT/StringRef.h"21#include "llvm/ADT/Twine.h"22#include "llvm/Bitcode/BitcodeWriter.h"23#include "llvm/IR/DiagnosticPrinter.h"24#include "llvm/LTO/Config.h"25#include "llvm/LTO/LTO.h"26#include "llvm/Support/Caching.h"27#include "llvm/Support/CodeGen.h"28#include "llvm/Support/MemoryBuffer.h"29#include "llvm/Support/TimeProfiler.h"30#include "llvm/Support/raw_ostream.h"31#include <cstddef>32#include <memory>33#include <string>34#include <vector>35 36using namespace llvm;37using namespace llvm::object;38using namespace lld;39using namespace lld::coff;40 41std::string BitcodeCompiler::getThinLTOOutputFile(StringRef path) {42  return lto::getThinLTOOutputFile(path, ctx.config.thinLTOPrefixReplaceOld,43                                   ctx.config.thinLTOPrefixReplaceNew);44}45 46lto::Config BitcodeCompiler::createConfig() {47  lto::Config c;48  c.Options = initTargetOptionsFromCodeGenFlags();49  c.Options.EmitAddrsig = true;50  for (StringRef C : ctx.config.mllvmOpts)51    c.MllvmArgs.emplace_back(C.str());52 53  // Always emit a section per function/datum with LTO. LLVM LTO should get most54  // of the benefit of linker GC, but there are still opportunities for ICF.55  c.Options.FunctionSections = true;56  c.Options.DataSections = true;57 58  // Use static reloc model on 32-bit x86 because it usually results in more59  // compact code, and because there are also known code generation bugs when60  // using the PIC model (see PR34306).61  if (ctx.config.machine == COFF::IMAGE_FILE_MACHINE_I386)62    c.RelocModel = Reloc::Static;63  else64    c.RelocModel = Reloc::PIC_;65#ifndef NDEBUG66  c.DisableVerify = false;67#else68  c.DisableVerify = true;69#endif70  c.DiagHandler = diagnosticHandler;71  c.DwoDir = ctx.config.dwoDir.str();72  c.OptLevel = ctx.config.ltoo;73  c.CPU = getCPUStr();74  c.MAttrs = getMAttrs();75  std::optional<CodeGenOptLevel> optLevelOrNone = CodeGenOpt::getLevel(76      ctx.config.ltoCgo.value_or(args::getCGOptLevel(ctx.config.ltoo)));77  assert(optLevelOrNone && "Invalid optimization level!");78  c.CGOptLevel = *optLevelOrNone;79  c.AlwaysEmitRegularLTOObj = !ctx.config.ltoObjPath.empty();80  c.DebugPassManager = ctx.config.ltoDebugPassManager;81  c.CSIRProfile = std::string(ctx.config.ltoCSProfileFile);82  c.RunCSIRInstr = ctx.config.ltoCSProfileGenerate;83  c.PGOWarnMismatch = ctx.config.ltoPGOWarnMismatch;84  c.SampleProfile = ctx.config.ltoSampleProfileName;85  c.TimeTraceEnabled = ctx.config.timeTraceEnabled;86  c.TimeTraceGranularity = ctx.config.timeTraceGranularity;87 88  if (ctx.config.emit == EmitKind::LLVM) {89    c.PreCodeGenModuleHook = [this](size_t task, const Module &m) {90      if (std::unique_ptr<raw_fd_ostream> os =91              openLTOOutputFile(ctx.config.outputFile))92        WriteBitcodeToFile(m, *os, false);93      return false;94    };95  } else if (ctx.config.emit == EmitKind::ASM) {96    c.CGFileType = CodeGenFileType::AssemblyFile;97    c.Options.MCOptions.AsmVerbose = true;98  }99 100  if (!ctx.config.saveTempsArgs.empty())101    checkError(c.addSaveTemps(std::string(ctx.config.outputFile) + ".",102                              /*UseInputModulePath*/ true,103                              ctx.config.saveTempsArgs));104  return c;105}106 107BitcodeCompiler::BitcodeCompiler(COFFLinkerContext &c) : ctx(c) {108  // Initialize indexFile.109  if (!ctx.config.thinLTOIndexOnlyArg.empty())110    indexFile = openFile(ctx.config.thinLTOIndexOnlyArg);111 112  // Initialize ltoObj.113  lto::ThinBackend backend;114  if (!ctx.config.dtltoDistributor.empty()) {115    backend = lto::createOutOfProcessThinBackend(116        llvm::hardware_concurrency(ctx.config.thinLTOJobs),117        /*OnWrite=*/nullptr,118        /*ShouldEmitIndexFiles=*/false,119        /*ShouldEmitImportFiles=*/false, ctx.config.outputFile,120        ctx.config.dtltoDistributor, ctx.config.dtltoDistributorArgs,121        ctx.config.dtltoCompiler, ctx.config.dtltoCompilerPrependArgs,122        ctx.config.dtltoCompilerArgs, !ctx.config.saveTempsArgs.empty());123  } else if (ctx.config.thinLTOIndexOnly) {124    auto OnIndexWrite = [&](StringRef S) { thinIndices.erase(S); };125    backend = lto::createWriteIndexesThinBackend(126        llvm::hardware_concurrency(ctx.config.thinLTOJobs),127        std::string(ctx.config.thinLTOPrefixReplaceOld),128        std::string(ctx.config.thinLTOPrefixReplaceNew),129        std::string(ctx.config.thinLTOPrefixReplaceNativeObject),130        ctx.config.thinLTOEmitImportsFiles, indexFile.get(), OnIndexWrite);131  } else {132    backend = lto::createInProcessThinBackend(133        llvm::heavyweight_hardware_concurrency(ctx.config.thinLTOJobs));134  }135 136  ltoObj = std::make_unique<lto::LTO>(createConfig(), backend,137                                      ctx.config.ltoPartitions);138}139 140BitcodeCompiler::~BitcodeCompiler() = default;141 142static void undefine(Symbol *s) { replaceSymbol<Undefined>(s, s->getName()); }143 144void BitcodeCompiler::add(BitcodeFile &f) {145  lto::InputFile &obj = *f.obj;146  unsigned symNum = 0;147  std::vector<Symbol *> symBodies = f.getSymbols();148  std::vector<lto::SymbolResolution> resols(symBodies.size());149 150  if (ctx.config.thinLTOIndexOnly)151    thinIndices.insert(obj.getName());152 153  // Provide a resolution to the LTO API for each symbol.154  for (const lto::InputFile::Symbol &objSym : obj.symbols()) {155    Symbol *sym = symBodies[symNum];156    lto::SymbolResolution &r = resols[symNum];157    ++symNum;158 159    // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile160    // reports two symbols for module ASM defined. Without this check, lld161    // flags an undefined in IR with a definition in ASM as prevailing.162    // Once IRObjectFile is fixed to report only one symbol this hack can163    // be removed.164    r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;165    r.VisibleToRegularObj = sym->isUsedInRegularObj;166    if (r.Prevailing)167      undefine(sym);168 169    // We tell LTO to not apply interprocedural optimization for wrapped170    // (with -wrap) symbols because otherwise LTO would inline them while171    // their values are still not final.172    r.LinkerRedefined = !sym->canInline;173  }174  checkError(ltoObj->add(std::move(f.obj), resols));175}176 177// Merge all the bitcode files we have seen, codegen the result178// and return the resulting objects.179std::vector<InputFile *> BitcodeCompiler::compile() {180  llvm::TimeTraceScope timeScope("Bitcode compile");181  unsigned maxTasks = ltoObj->getMaxTasks();182  buf.resize(maxTasks);183  files.resize(maxTasks);184  file_names.resize(maxTasks);185 186  // The /lldltocache option specifies the path to a directory in which to cache187  // native object files for ThinLTO incremental builds. If a path was188  // specified, configure LTO to use it as the cache directory.189  FileCache cache;190  if (!ctx.config.ltoCache.empty())191    cache = check(localCache("ThinLTO", "Thin", ctx.config.ltoCache,192                             [&](size_t task, const Twine &moduleName,193                                 std::unique_ptr<MemoryBuffer> mb) {194                               files[task] = std::move(mb);195                               file_names[task] = moduleName.str();196                             }));197 198  checkError(ltoObj->run(199      [&](size_t task, const Twine &moduleName) {200        buf[task].first = moduleName.str();201        return std::make_unique<CachedFileStream>(202            std::make_unique<raw_svector_ostream>(buf[task].second));203      },204      cache));205 206  // Emit empty index files for non-indexed files207  for (StringRef s : thinIndices) {208    std::string path = getThinLTOOutputFile(s);209    openFile(path + ".thinlto.bc");210    if (ctx.config.thinLTOEmitImportsFiles)211      openFile(path + ".imports");212  }213 214  // ThinLTO with index only option is required to generate only the index215  // files. After that, we exit from linker and ThinLTO backend runs in a216  // distributed environment.217  if (ctx.config.thinLTOIndexOnly) {218    if (!ctx.config.ltoObjPath.empty())219      saveBuffer(buf[0].second, ctx.config.ltoObjPath);220    if (indexFile)221      indexFile->close();222    return {};223  }224 225  if (!ctx.config.ltoCache.empty())226    pruneCache(ctx.config.ltoCache, ctx.config.ltoCachePolicy, files);227 228  std::vector<InputFile *> ret;229  bool emitASM = ctx.config.emit == EmitKind::ASM;230  const char *Ext = emitASM ? ".s" : ".obj";231  for (unsigned i = 0; i != maxTasks; ++i) {232    StringRef bitcodeFilePath;233    // Get the native object contents either from the cache or from memory.  Do234    // not use the cached MemoryBuffer directly, or the PDB will not be235    // deterministic.236    StringRef objBuf;237    if (files[i]) {238      objBuf = files[i]->getBuffer();239      bitcodeFilePath = file_names[i];240    } else {241      objBuf = buf[i].second;242      bitcodeFilePath = buf[i].first;243    }244    if (objBuf.empty())245      continue;246 247    // If the input bitcode file is path/to/a.obj, then the corresponding lto248    // object file name will look something like: path/to/main.exe.lto.a.obj.249    StringRef ltoObjName;250    if (bitcodeFilePath == "ld-temp.o") {251      ltoObjName =252          saver().save(Twine(ctx.config.outputFile) + ".lto" +253                       (i == 0 ? Twine("") : Twine('.') + Twine(i)) + Ext);254    } else {255      StringRef directory = sys::path::parent_path(bitcodeFilePath);256      StringRef baseName = sys::path::stem(bitcodeFilePath);257      StringRef outputFileBaseName = sys::path::filename(ctx.config.outputFile);258      SmallString<64> path;259      sys::path::append(path, directory,260                        outputFileBaseName + ".lto." + baseName + Ext);261      sys::path::remove_dots(path, true);262      ltoObjName = saver().save(path.str());263    }264    if (llvm::is_contained(ctx.config.saveTempsArgs, "prelink") || emitASM)265      saveBuffer(buf[i].second, ltoObjName);266    if (!emitASM)267      ret.push_back(ObjFile::create(ctx, MemoryBufferRef(objBuf, ltoObjName)));268  }269 270  return ret;271}272