1e8d8bef9SDimitry Andric //===- LTO.cpp ------------------------------------------------------------===// 2e8d8bef9SDimitry Andric // 3e8d8bef9SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4e8d8bef9SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5e8d8bef9SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6e8d8bef9SDimitry Andric // 7e8d8bef9SDimitry Andric //===----------------------------------------------------------------------===// 8e8d8bef9SDimitry Andric 9e8d8bef9SDimitry Andric #include "LTO.h" 10e8d8bef9SDimitry Andric #include "Config.h" 11e8d8bef9SDimitry Andric #include "Driver.h" 12e8d8bef9SDimitry Andric #include "InputFiles.h" 13fe6060f1SDimitry Andric #include "Symbols.h" 14fe6060f1SDimitry Andric #include "Target.h" 15e8d8bef9SDimitry Andric 16fe6060f1SDimitry Andric #include "lld/Common/Args.h" 17e8d8bef9SDimitry Andric #include "lld/Common/ErrorHandler.h" 18e8d8bef9SDimitry Andric #include "lld/Common/Strings.h" 19e8d8bef9SDimitry Andric #include "lld/Common/TargetOptionsCommandFlags.h" 20fe6060f1SDimitry Andric #include "llvm/LTO/Config.h" 21e8d8bef9SDimitry Andric #include "llvm/LTO/LTO.h" 22*349cc55cSDimitry Andric #include "llvm/Support/Caching.h" 23e8d8bef9SDimitry Andric #include "llvm/Support/FileSystem.h" 24e8d8bef9SDimitry Andric #include "llvm/Support/Path.h" 25e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h" 26e8d8bef9SDimitry Andric #include "llvm/Transforms/ObjCARC.h" 27e8d8bef9SDimitry Andric 28e8d8bef9SDimitry Andric using namespace lld; 29e8d8bef9SDimitry Andric using namespace lld::macho; 30e8d8bef9SDimitry Andric using namespace llvm; 31fe6060f1SDimitry Andric using namespace llvm::MachO; 32e8d8bef9SDimitry Andric using namespace llvm::sys; 33e8d8bef9SDimitry Andric 34e8d8bef9SDimitry Andric static lto::Config createConfig() { 35e8d8bef9SDimitry Andric lto::Config c; 36e8d8bef9SDimitry Andric c.Options = initTargetOptionsFromCodeGenFlags(); 37e8d8bef9SDimitry Andric c.CodeModel = getCodeModelFromCMModel(); 38e8d8bef9SDimitry Andric c.CPU = getCPUStr(); 39e8d8bef9SDimitry Andric c.MAttrs = getMAttrs(); 40*349cc55cSDimitry Andric c.DiagHandler = diagnosticHandler; 41e8d8bef9SDimitry Andric c.UseNewPM = config->ltoNewPassManager; 42e8d8bef9SDimitry Andric c.PreCodeGenPassesHook = [](legacy::PassManager &pm) { 43e8d8bef9SDimitry Andric pm.add(createObjCARCContractPass()); 44e8d8bef9SDimitry Andric }; 45fe6060f1SDimitry Andric c.TimeTraceEnabled = config->timeTraceEnabled; 46fe6060f1SDimitry Andric c.TimeTraceGranularity = config->timeTraceGranularity; 47fe6060f1SDimitry Andric c.OptLevel = config->ltoo; 48fe6060f1SDimitry Andric c.CGOptLevel = args::getCGOptLevel(config->ltoo); 49fe6060f1SDimitry Andric if (config->saveTemps) 50fe6060f1SDimitry Andric checkError(c.addSaveTemps(config->outputFile.str() + ".", 51fe6060f1SDimitry Andric /*UseInputModulePath=*/true)); 52e8d8bef9SDimitry Andric return c; 53e8d8bef9SDimitry Andric } 54e8d8bef9SDimitry Andric 55e8d8bef9SDimitry Andric BitcodeCompiler::BitcodeCompiler() { 56fe6060f1SDimitry Andric lto::ThinBackend backend = lto::createInProcessThinBackend( 57fe6060f1SDimitry Andric heavyweight_hardware_concurrency(config->thinLTOJobs)); 58e8d8bef9SDimitry Andric ltoObj = std::make_unique<lto::LTO>(createConfig(), backend); 59e8d8bef9SDimitry Andric } 60e8d8bef9SDimitry Andric 61e8d8bef9SDimitry Andric void BitcodeCompiler::add(BitcodeFile &f) { 62e8d8bef9SDimitry Andric ArrayRef<lto::InputFile::Symbol> objSyms = f.obj->symbols(); 63e8d8bef9SDimitry Andric std::vector<lto::SymbolResolution> resols; 64e8d8bef9SDimitry Andric resols.reserve(objSyms.size()); 65e8d8bef9SDimitry Andric 66e8d8bef9SDimitry Andric // Provide a resolution to the LTO API for each symbol. 67fe6060f1SDimitry Andric auto symIt = f.symbols.begin(); 68e8d8bef9SDimitry Andric for (const lto::InputFile::Symbol &objSym : objSyms) { 69e8d8bef9SDimitry Andric resols.emplace_back(); 70e8d8bef9SDimitry Andric lto::SymbolResolution &r = resols.back(); 71fe6060f1SDimitry Andric Symbol *sym = *symIt++; 72e8d8bef9SDimitry Andric 73e8d8bef9SDimitry Andric // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile 74e8d8bef9SDimitry Andric // reports two symbols for module ASM defined. Without this check, lld 75e8d8bef9SDimitry Andric // flags an undefined in IR with a definition in ASM as prevailing. 76e8d8bef9SDimitry Andric // Once IRObjectFile is fixed to report only one symbol this hack can 77e8d8bef9SDimitry Andric // be removed. 78fe6060f1SDimitry Andric r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f; 79fe6060f1SDimitry Andric 80fe6060f1SDimitry Andric // FIXME: What about other output types? And we can probably be less 81fe6060f1SDimitry Andric // restrictive with -flat_namespace, but it's an infrequent use case. 82fe6060f1SDimitry Andric // FIXME: Honor config->exportDynamic. 83fe6060f1SDimitry Andric r.VisibleToRegularObj = config->outputType != MH_EXECUTE || 84fe6060f1SDimitry Andric config->namespaceKind == NamespaceKind::flat || 85fe6060f1SDimitry Andric sym->isUsedInRegularObj; 86fe6060f1SDimitry Andric 87fe6060f1SDimitry Andric // Un-define the symbol so that we don't get duplicate symbol errors when we 88fe6060f1SDimitry Andric // load the ObjFile emitted by LTO compilation. 89fe6060f1SDimitry Andric if (r.Prevailing) 90fe6060f1SDimitry Andric replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(), 91fe6060f1SDimitry Andric RefState::Strong); 92e8d8bef9SDimitry Andric 93e8d8bef9SDimitry Andric // TODO: set the other resolution configs properly 94e8d8bef9SDimitry Andric } 95e8d8bef9SDimitry Andric checkError(ltoObj->add(std::move(f.obj), resols)); 96e8d8bef9SDimitry Andric } 97e8d8bef9SDimitry Andric 98e8d8bef9SDimitry Andric // Merge all the bitcode files we have seen, codegen the result 99e8d8bef9SDimitry Andric // and return the resulting ObjectFile(s). 100e8d8bef9SDimitry Andric std::vector<ObjFile *> BitcodeCompiler::compile() { 101e8d8bef9SDimitry Andric unsigned maxTasks = ltoObj->getMaxTasks(); 102e8d8bef9SDimitry Andric buf.resize(maxTasks); 103fe6060f1SDimitry Andric files.resize(maxTasks); 104e8d8bef9SDimitry Andric 105fe6060f1SDimitry Andric // The -cache_path_lto option specifies the path to a directory in which 106fe6060f1SDimitry Andric // to cache native object files for ThinLTO incremental builds. If a path was 107fe6060f1SDimitry Andric // specified, configure LTO to use it as the cache directory. 108*349cc55cSDimitry Andric FileCache cache; 109fe6060f1SDimitry Andric if (!config->thinLTOCacheDir.empty()) 110*349cc55cSDimitry Andric cache = 111*349cc55cSDimitry Andric check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir, 112fe6060f1SDimitry Andric [&](size_t task, std::unique_ptr<MemoryBuffer> mb) { 113fe6060f1SDimitry Andric files[task] = std::move(mb); 114fe6060f1SDimitry Andric })); 115fe6060f1SDimitry Andric 116fe6060f1SDimitry Andric checkError(ltoObj->run( 117fe6060f1SDimitry Andric [&](size_t task) { 118*349cc55cSDimitry Andric return std::make_unique<CachedFileStream>( 119e8d8bef9SDimitry Andric std::make_unique<raw_svector_ostream>(buf[task])); 120fe6060f1SDimitry Andric }, 121fe6060f1SDimitry Andric cache)); 122fe6060f1SDimitry Andric 123fe6060f1SDimitry Andric if (!config->thinLTOCacheDir.empty()) 124fe6060f1SDimitry Andric pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy); 125e8d8bef9SDimitry Andric 126e8d8bef9SDimitry Andric if (config->saveTemps) { 127e8d8bef9SDimitry Andric if (!buf[0].empty()) 128e8d8bef9SDimitry Andric saveBuffer(buf[0], config->outputFile + ".lto.o"); 129e8d8bef9SDimitry Andric for (unsigned i = 1; i != maxTasks; ++i) 130e8d8bef9SDimitry Andric saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o"); 131e8d8bef9SDimitry Andric } 132e8d8bef9SDimitry Andric 133e8d8bef9SDimitry Andric if (!config->ltoObjPath.empty()) 134e8d8bef9SDimitry Andric fs::create_directories(config->ltoObjPath); 135e8d8bef9SDimitry Andric 136e8d8bef9SDimitry Andric std::vector<ObjFile *> ret; 137e8d8bef9SDimitry Andric for (unsigned i = 0; i != maxTasks; ++i) { 138fe6060f1SDimitry Andric if (buf[i].empty()) 139e8d8bef9SDimitry Andric continue; 140e8d8bef9SDimitry Andric SmallString<261> filePath("/tmp/lto.tmp"); 141e8d8bef9SDimitry Andric uint32_t modTime = 0; 142e8d8bef9SDimitry Andric if (!config->ltoObjPath.empty()) { 143e8d8bef9SDimitry Andric filePath = config->ltoObjPath; 144e8d8bef9SDimitry Andric path::append(filePath, Twine(i) + "." + 145fe6060f1SDimitry Andric getArchitectureName(config->arch()) + 146fe6060f1SDimitry Andric ".lto.o"); 147e8d8bef9SDimitry Andric saveBuffer(buf[i], filePath); 148e8d8bef9SDimitry Andric modTime = getModTime(filePath); 149e8d8bef9SDimitry Andric } 150e8d8bef9SDimitry Andric ret.push_back(make<ObjFile>( 151e8d8bef9SDimitry Andric MemoryBufferRef(buf[i], saver.save(filePath.str())), modTime, "")); 152e8d8bef9SDimitry Andric } 153fe6060f1SDimitry Andric for (std::unique_ptr<MemoryBuffer> &file : files) 154fe6060f1SDimitry Andric if (file) 155fe6060f1SDimitry Andric ret.push_back(make<ObjFile>(*file, 0, "")); 156e8d8bef9SDimitry Andric return ret; 157e8d8bef9SDimitry Andric } 158