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 "Driver.h" 12 #include "InputFiles.h" 13 #include "Symbols.h" 14 15 #include "lld/Common/ErrorHandler.h" 16 #include "lld/Common/Strings.h" 17 #include "lld/Common/TargetOptionsCommandFlags.h" 18 #include "llvm/LTO/LTO.h" 19 #include "llvm/Support/FileSystem.h" 20 #include "llvm/Support/Path.h" 21 #include "llvm/Support/raw_ostream.h" 22 #include "llvm/Transforms/ObjCARC.h" 23 24 using namespace lld; 25 using namespace lld::macho; 26 using namespace llvm; 27 using namespace llvm::sys; 28 29 static lto::Config createConfig() { 30 lto::Config c; 31 c.Options = initTargetOptionsFromCodeGenFlags(); 32 c.CodeModel = getCodeModelFromCMModel(); 33 c.CPU = getCPUStr(); 34 c.MAttrs = getMAttrs(); 35 c.UseNewPM = config->ltoNewPassManager; 36 c.PreCodeGenPassesHook = [](legacy::PassManager &pm) { 37 pm.add(createObjCARCContractPass()); 38 }; 39 c.TimeTraceEnabled = config->timeTraceEnabled; 40 c.TimeTraceGranularity = config->timeTraceGranularity; 41 return c; 42 } 43 44 BitcodeCompiler::BitcodeCompiler() { 45 lto::ThinBackend backend = 46 lto::createInProcessThinBackend(heavyweight_hardware_concurrency()); 47 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend); 48 } 49 50 void BitcodeCompiler::add(BitcodeFile &f) { 51 ArrayRef<lto::InputFile::Symbol> objSyms = f.obj->symbols(); 52 std::vector<lto::SymbolResolution> resols; 53 resols.reserve(objSyms.size()); 54 55 // Provide a resolution to the LTO API for each symbol. 56 auto symIt = f.symbols.begin(); 57 for (const lto::InputFile::Symbol &objSym : objSyms) { 58 resols.emplace_back(); 59 lto::SymbolResolution &r = resols.back(); 60 Symbol *sym = *symIt++; 61 62 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 63 // reports two symbols for module ASM defined. Without this check, lld 64 // flags an undefined in IR with a definition in ASM as prevailing. 65 // Once IRObjectFile is fixed to report only one symbol this hack can 66 // be removed. 67 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f; 68 69 // Un-define the symbol so that we don't get duplicate symbol errors when we 70 // load the ObjFile emitted by LTO compilation. 71 if (r.Prevailing) 72 replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(), 73 RefState::Strong); 74 75 // TODO: set the other resolution configs properly 76 r.VisibleToRegularObj = true; 77 } 78 checkError(ltoObj->add(std::move(f.obj), resols)); 79 } 80 81 // Merge all the bitcode files we have seen, codegen the result 82 // and return the resulting ObjectFile(s). 83 std::vector<ObjFile *> BitcodeCompiler::compile() { 84 unsigned maxTasks = ltoObj->getMaxTasks(); 85 buf.resize(maxTasks); 86 87 checkError(ltoObj->run([&](size_t task) { 88 return std::make_unique<lto::NativeObjectStream>( 89 std::make_unique<raw_svector_ostream>(buf[task])); 90 })); 91 92 if (config->saveTemps) { 93 if (!buf[0].empty()) 94 saveBuffer(buf[0], config->outputFile + ".lto.o"); 95 for (unsigned i = 1; i != maxTasks; ++i) 96 saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o"); 97 } 98 99 if (!config->ltoObjPath.empty()) 100 fs::create_directories(config->ltoObjPath); 101 102 std::vector<ObjFile *> ret; 103 for (unsigned i = 0; i != maxTasks; ++i) { 104 if (buf[i].empty()) 105 continue; 106 SmallString<261> filePath("/tmp/lto.tmp"); 107 uint32_t modTime = 0; 108 if (!config->ltoObjPath.empty()) { 109 filePath = config->ltoObjPath; 110 path::append(filePath, Twine(i) + "." + 111 getArchitectureName(config->target.Arch) + 112 ".lto.o"); 113 saveBuffer(buf[i], filePath); 114 modTime = getModTime(filePath); 115 } 116 ret.push_back(make<ObjFile>( 117 MemoryBufferRef(buf[i], saver.save(filePath.str())), modTime, "")); 118 } 119 120 return ret; 121 } 122