1 //===- LTO.cpp ------------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
13 #include "InputFiles.h"
15 #include "lld/Core/TargetOptionsCommandFlags.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/ADT/SmallString.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/DiagnosticPrinter.h"
21 #include "llvm/LTO/Config.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/Object/SymbolicFile.h"
24 #include "llvm/Support/CodeGen.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/raw_ostream.h"
33 #include <system_error>
37 using namespace llvm::object;
40 using namespace lld::coff;
42 static void diagnosticHandler(const DiagnosticInfo &DI) {
43 SmallString<128> ErrStorage;
44 raw_svector_ostream OS(ErrStorage);
45 DiagnosticPrinterRawOStream DP(OS);
50 static void checkError(Error E) {
51 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) -> Error {
53 return Error::success();
57 static void saveBuffer(StringRef Buffer, const Twine &Path) {
59 raw_fd_ostream OS(Path.str(), EC, sys::fs::OpenFlags::F_None);
61 error("cannot create " + Path + ": " + EC.message());
65 static std::unique_ptr<lto::LTO> createLTO() {
67 Conf.Options = InitTargetOptionsFromCodeGenFlags();
68 Conf.RelocModel = Reloc::PIC_;
69 Conf.DisableVerify = true;
70 Conf.DiagHandler = diagnosticHandler;
71 Conf.OptLevel = Config->LTOOptLevel;
72 if (Config->SaveTemps)
73 checkError(Conf.addSaveTemps(std::string(Config->OutputFile) + ".",
74 /*UseInputModulePath*/ true));
75 lto::ThinBackend Backend;
76 if (Config->LTOJobs != 0)
77 Backend = lto::createInProcessThinBackend(Config->LTOJobs);
78 return llvm::make_unique<lto::LTO>(std::move(Conf), Backend,
79 Config->LTOPartitions);
82 BitcodeCompiler::BitcodeCompiler() : LTOObj(createLTO()) {}
84 BitcodeCompiler::~BitcodeCompiler() = default;
86 static void undefine(Symbol *S) {
87 replaceBody<Undefined>(S, S->body()->getName());
90 void BitcodeCompiler::add(BitcodeFile &F) {
91 lto::InputFile &Obj = *F.Obj;
93 std::vector<SymbolBody *> SymBodies = F.getSymbols();
94 std::vector<lto::SymbolResolution> Resols(SymBodies.size());
96 // Provide a resolution to the LTO API for each symbol.
97 for (const lto::InputFile::Symbol &ObjSym : Obj.symbols()) {
98 SymbolBody *B = SymBodies[SymNum];
99 Symbol *Sym = B->symbol();
100 lto::SymbolResolution &R = Resols[SymNum];
103 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
104 // reports two symbols for module ASM defined. Without this check, lld
105 // flags an undefined in IR with a definition in ASM as prevailing.
106 // Once IRObjectFile is fixed to report only one symbol this hack can
108 R.Prevailing = !ObjSym.isUndefined() && B->getFile() == &F;
109 R.VisibleToRegularObj = Sym->IsUsedInRegularObj;
113 checkError(LTOObj->add(std::move(F.Obj), Resols));
116 // Merge all the bitcode files we have seen, codegen the result
117 // and return the resulting objects.
118 std::vector<StringRef> BitcodeCompiler::compile() {
119 unsigned MaxTasks = LTOObj->getMaxTasks();
120 Buff.resize(MaxTasks);
122 checkError(LTOObj->run([&](size_t Task) {
123 return llvm::make_unique<lto::NativeObjectStream>(
124 llvm::make_unique<raw_svector_ostream>(Buff[Task]));
127 std::vector<StringRef> Ret;
128 for (unsigned I = 0; I != MaxTasks; ++I) {
131 if (Config->SaveTemps) {
133 saveBuffer(Buff[I], Config->OutputFile + ".lto.obj");
135 saveBuffer(Buff[I], Config->OutputFile + Twine(I) + ".lto.obj");
137 Ret.emplace_back(Buff[I].data(), Buff[I].size());