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