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