xref: /freebsd/contrib/llvm-project/lld/MachO/LTO.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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"
1704eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.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"
22349cc55cSDimitry 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();
37*81ad6265SDimitry Andric   c.Options.EmitAddrsig = config->icfLevel == ICFLevel::safe;
38e8d8bef9SDimitry Andric   c.CodeModel = getCodeModelFromCMModel();
39e8d8bef9SDimitry Andric   c.CPU = getCPUStr();
40e8d8bef9SDimitry Andric   c.MAttrs = getMAttrs();
41349cc55cSDimitry Andric   c.DiagHandler = diagnosticHandler;
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.
67*81ad6265SDimitry Andric   bool exportDynamic =
68*81ad6265SDimitry Andric       config->outputType != MH_EXECUTE || config->exportDynamic;
69fe6060f1SDimitry Andric   auto symIt = f.symbols.begin();
70e8d8bef9SDimitry Andric   for (const lto::InputFile::Symbol &objSym : objSyms) {
71e8d8bef9SDimitry Andric     resols.emplace_back();
72e8d8bef9SDimitry Andric     lto::SymbolResolution &r = resols.back();
73fe6060f1SDimitry Andric     Symbol *sym = *symIt++;
74e8d8bef9SDimitry Andric 
75e8d8bef9SDimitry Andric     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
76e8d8bef9SDimitry Andric     // reports two symbols for module ASM defined. Without this check, lld
77e8d8bef9SDimitry Andric     // flags an undefined in IR with a definition in ASM as prevailing.
78e8d8bef9SDimitry Andric     // Once IRObjectFile is fixed to report only one symbol this hack can
79e8d8bef9SDimitry Andric     // be removed.
80fe6060f1SDimitry Andric     r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
81fe6060f1SDimitry Andric 
82*81ad6265SDimitry Andric     if (const auto *defined = dyn_cast<Defined>(sym)) {
83*81ad6265SDimitry Andric       r.ExportDynamic =
84*81ad6265SDimitry Andric           defined->isExternal() && !defined->privateExtern && exportDynamic;
85*81ad6265SDimitry Andric       r.FinalDefinitionInLinkageUnit =
86*81ad6265SDimitry Andric           !defined->isExternalWeakDef() && !defined->interposable;
87*81ad6265SDimitry Andric     } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) {
88*81ad6265SDimitry Andric       r.ExportDynamic = !common->privateExtern && exportDynamic;
89*81ad6265SDimitry Andric       r.FinalDefinitionInLinkageUnit = true;
90*81ad6265SDimitry Andric     }
91*81ad6265SDimitry Andric 
92*81ad6265SDimitry Andric     r.VisibleToRegularObj =
93*81ad6265SDimitry Andric         sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic);
94fe6060f1SDimitry Andric 
95fe6060f1SDimitry Andric     // Un-define the symbol so that we don't get duplicate symbol errors when we
96fe6060f1SDimitry Andric     // load the ObjFile emitted by LTO compilation.
97fe6060f1SDimitry Andric     if (r.Prevailing)
98fe6060f1SDimitry Andric       replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(),
99fe6060f1SDimitry Andric                                RefState::Strong);
100e8d8bef9SDimitry Andric 
101e8d8bef9SDimitry Andric     // TODO: set the other resolution configs properly
102e8d8bef9SDimitry Andric   }
103e8d8bef9SDimitry Andric   checkError(ltoObj->add(std::move(f.obj), resols));
104e8d8bef9SDimitry Andric }
105e8d8bef9SDimitry Andric 
106e8d8bef9SDimitry Andric // Merge all the bitcode files we have seen, codegen the result
107e8d8bef9SDimitry Andric // and return the resulting ObjectFile(s).
108e8d8bef9SDimitry Andric std::vector<ObjFile *> BitcodeCompiler::compile() {
109e8d8bef9SDimitry Andric   unsigned maxTasks = ltoObj->getMaxTasks();
110e8d8bef9SDimitry Andric   buf.resize(maxTasks);
111fe6060f1SDimitry Andric   files.resize(maxTasks);
112e8d8bef9SDimitry Andric 
113fe6060f1SDimitry Andric   // The -cache_path_lto option specifies the path to a directory in which
114fe6060f1SDimitry Andric   // to cache native object files for ThinLTO incremental builds. If a path was
115fe6060f1SDimitry Andric   // specified, configure LTO to use it as the cache directory.
116349cc55cSDimitry Andric   FileCache cache;
117fe6060f1SDimitry Andric   if (!config->thinLTOCacheDir.empty())
118349cc55cSDimitry Andric     cache =
119349cc55cSDimitry Andric         check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
120fe6060f1SDimitry Andric                          [&](size_t task, std::unique_ptr<MemoryBuffer> mb) {
121fe6060f1SDimitry Andric                            files[task] = std::move(mb);
122fe6060f1SDimitry Andric                          }));
123fe6060f1SDimitry Andric 
124fe6060f1SDimitry Andric   checkError(ltoObj->run(
125fe6060f1SDimitry Andric       [&](size_t task) {
126349cc55cSDimitry Andric         return std::make_unique<CachedFileStream>(
127e8d8bef9SDimitry Andric             std::make_unique<raw_svector_ostream>(buf[task]));
128fe6060f1SDimitry Andric       },
129fe6060f1SDimitry Andric       cache));
130fe6060f1SDimitry Andric 
131fe6060f1SDimitry Andric   if (!config->thinLTOCacheDir.empty())
132fe6060f1SDimitry Andric     pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy);
133e8d8bef9SDimitry Andric 
134e8d8bef9SDimitry Andric   if (config->saveTemps) {
135e8d8bef9SDimitry Andric     if (!buf[0].empty())
136e8d8bef9SDimitry Andric       saveBuffer(buf[0], config->outputFile + ".lto.o");
137e8d8bef9SDimitry Andric     for (unsigned i = 1; i != maxTasks; ++i)
138e8d8bef9SDimitry Andric       saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
139e8d8bef9SDimitry Andric   }
140e8d8bef9SDimitry Andric 
141e8d8bef9SDimitry Andric   if (!config->ltoObjPath.empty())
142e8d8bef9SDimitry Andric     fs::create_directories(config->ltoObjPath);
143e8d8bef9SDimitry Andric 
144e8d8bef9SDimitry Andric   std::vector<ObjFile *> ret;
145e8d8bef9SDimitry Andric   for (unsigned i = 0; i != maxTasks; ++i) {
146fe6060f1SDimitry Andric     if (buf[i].empty())
147e8d8bef9SDimitry Andric       continue;
148e8d8bef9SDimitry Andric     SmallString<261> filePath("/tmp/lto.tmp");
149e8d8bef9SDimitry Andric     uint32_t modTime = 0;
150e8d8bef9SDimitry Andric     if (!config->ltoObjPath.empty()) {
151e8d8bef9SDimitry Andric       filePath = config->ltoObjPath;
152e8d8bef9SDimitry Andric       path::append(filePath, Twine(i) + "." +
153fe6060f1SDimitry Andric                                  getArchitectureName(config->arch()) +
154fe6060f1SDimitry Andric                                  ".lto.o");
155e8d8bef9SDimitry Andric       saveBuffer(buf[i], filePath);
156e8d8bef9SDimitry Andric       modTime = getModTime(filePath);
157e8d8bef9SDimitry Andric     }
158e8d8bef9SDimitry Andric     ret.push_back(make<ObjFile>(
15904eeddc0SDimitry Andric         MemoryBufferRef(buf[i], saver().save(filePath.str())), modTime, ""));
160e8d8bef9SDimitry Andric   }
161fe6060f1SDimitry Andric   for (std::unique_ptr<MemoryBuffer> &file : files)
162fe6060f1SDimitry Andric     if (file)
163fe6060f1SDimitry Andric       ret.push_back(make<ObjFile>(*file, 0, ""));
164e8d8bef9SDimitry Andric   return ret;
165e8d8bef9SDimitry Andric }
166