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