xref: /freebsd/contrib/llvm-project/lld/MachO/LTO.cpp (revision 1631382cf2820245cc72965498ff174bb548dd63)
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 #include "Symbols.h"
14 #include "Target.h"
15 
16 #include "lld/Common/Args.h"
17 #include "lld/Common/CommonLinkerContext.h"
18 #include "lld/Common/Strings.h"
19 #include "lld/Common/TargetOptionsCommandFlags.h"
20 #include "llvm/Bitcode/BitcodeWriter.h"
21 #include "llvm/LTO/Config.h"
22 #include "llvm/LTO/LTO.h"
23 #include "llvm/Support/Caching.h"
24 #include "llvm/Support/FileSystem.h"
25 #include "llvm/Support/Path.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Transforms/ObjCARC.h"
28 
29 using namespace lld;
30 using namespace lld::macho;
31 using namespace llvm;
32 using namespace llvm::MachO;
33 using namespace llvm::sys;
34 
35 // Creates an empty file to store a list of object files for final
36 // linking of distributed ThinLTO.
37 static std::unique_ptr<raw_fd_ostream> openFile(StringRef file) {
38   std::error_code ec;
39   auto ret =
40       std::make_unique<raw_fd_ostream>(file, ec, sys::fs::OpenFlags::OF_None);
41   if (ec) {
42     error("cannot open " + file + ": " + ec.message());
43     return nullptr;
44   }
45   return ret;
46 }
47 
48 static std::string getThinLTOOutputFile(StringRef modulePath) {
49   return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
50                                    config->thinLTOPrefixReplaceNew);
51 }
52 
53 static lto::Config createConfig() {
54   lto::Config c;
55   c.Options = initTargetOptionsFromCodeGenFlags();
56   c.Options.EmitAddrsig = config->icfLevel == ICFLevel::safe;
57   for (StringRef C : config->mllvmOpts)
58     c.MllvmArgs.emplace_back(C.str());
59   c.CodeModel = getCodeModelFromCMModel();
60   c.CPU = getCPUStr();
61   c.MAttrs = getMAttrs();
62   c.DiagHandler = diagnosticHandler;
63   c.PreCodeGenPassesHook = [](legacy::PassManager &pm) {
64     pm.add(createObjCARCContractPass());
65   };
66 
67   c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
68 
69   c.TimeTraceEnabled = config->timeTraceEnabled;
70   c.TimeTraceGranularity = config->timeTraceGranularity;
71   c.DebugPassManager = config->ltoDebugPassManager;
72   c.CSIRProfile = std::string(config->csProfilePath);
73   c.RunCSIRInstr = config->csProfileGenerate;
74   c.OptLevel = config->ltoo;
75   c.CGOptLevel = config->ltoCgo;
76   if (config->saveTemps)
77     checkError(c.addSaveTemps(config->outputFile.str() + ".",
78                               /*UseInputModulePath=*/true));
79   return c;
80 }
81 
82 // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails,
83 // or `originalPath` is not set, saves `buffer` to `path`.
84 static void saveOrHardlinkBuffer(StringRef buffer, const Twine &path,
85                                  std::optional<StringRef> originalPath) {
86   if (originalPath) {
87     auto err = fs::create_hard_link(*originalPath, path);
88     if (!err)
89       return;
90   }
91   saveBuffer(buffer, path);
92 }
93 
94 BitcodeCompiler::BitcodeCompiler() {
95   // Initialize indexFile.
96   if (!config->thinLTOIndexOnlyArg.empty())
97     indexFile = openFile(config->thinLTOIndexOnlyArg);
98 
99   // Initialize ltoObj.
100   lto::ThinBackend backend;
101   auto onIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
102   if (config->thinLTOIndexOnly) {
103     backend = lto::createWriteIndexesThinBackend(
104         std::string(config->thinLTOPrefixReplaceOld),
105         std::string(config->thinLTOPrefixReplaceNew),
106         std::string(config->thinLTOPrefixReplaceNativeObject),
107         config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
108   } else {
109     backend = lto::createInProcessThinBackend(
110         llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
111         onIndexWrite, config->thinLTOEmitIndexFiles,
112         config->thinLTOEmitImportsFiles);
113   }
114 
115   ltoObj = std::make_unique<lto::LTO>(createConfig(), backend);
116 }
117 
118 void BitcodeCompiler::add(BitcodeFile &f) {
119   lto::InputFile &obj = *f.obj;
120 
121   if (config->thinLTOEmitIndexFiles)
122     thinIndices.insert(obj.getName());
123 
124   ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
125   std::vector<lto::SymbolResolution> resols;
126   resols.reserve(objSyms.size());
127 
128   // Provide a resolution to the LTO API for each symbol.
129   bool exportDynamic =
130       config->outputType != MH_EXECUTE || config->exportDynamic;
131   auto symIt = f.symbols.begin();
132   for (const lto::InputFile::Symbol &objSym : objSyms) {
133     resols.emplace_back();
134     lto::SymbolResolution &r = resols.back();
135     Symbol *sym = *symIt++;
136 
137     // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
138     // reports two symbols for module ASM defined. Without this check, lld
139     // flags an undefined in IR with a definition in ASM as prevailing.
140     // Once IRObjectFile is fixed to report only one symbol this hack can
141     // be removed.
142     r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
143 
144     if (const auto *defined = dyn_cast<Defined>(sym)) {
145       r.ExportDynamic =
146           defined->isExternal() && !defined->privateExtern && exportDynamic;
147       r.FinalDefinitionInLinkageUnit =
148           !defined->isExternalWeakDef() && !defined->interposable;
149     } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) {
150       r.ExportDynamic = !common->privateExtern && exportDynamic;
151       r.FinalDefinitionInLinkageUnit = true;
152     }
153 
154     r.VisibleToRegularObj =
155         sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic);
156 
157     // Un-define the symbol so that we don't get duplicate symbol errors when we
158     // load the ObjFile emitted by LTO compilation.
159     if (r.Prevailing)
160       replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(),
161                                RefState::Strong, /*wasBitcodeSymbol=*/true);
162 
163     // TODO: set the other resolution configs properly
164   }
165   checkError(ltoObj->add(std::move(f.obj), resols));
166   hasFiles = true;
167 }
168 
169 // If LazyObjFile has not been added to link, emit empty index files.
170 // This is needed because this is what GNU gold plugin does and we have a
171 // distributed build system that depends on that behavior.
172 static void thinLTOCreateEmptyIndexFiles() {
173   DenseSet<StringRef> linkedBitCodeFiles;
174   for (InputFile *file : inputFiles)
175     if (auto *f = dyn_cast<BitcodeFile>(file))
176       if (!f->lazy)
177         linkedBitCodeFiles.insert(f->getName());
178 
179   for (InputFile *file : inputFiles) {
180     if (auto *f = dyn_cast<BitcodeFile>(file)) {
181       if (!f->lazy)
182         continue;
183       if (linkedBitCodeFiles.contains(f->getName()))
184         continue;
185       std::string path =
186           replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
187       std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
188       if (!os)
189         continue;
190 
191       ModuleSummaryIndex m(/*HaveGVs=*/false);
192       m.setSkipModuleByDistributedBackend();
193       writeIndexToFile(m, *os);
194       if (config->thinLTOEmitImportsFiles)
195         openFile(path + ".imports");
196     }
197   }
198 }
199 
200 // Merge all the bitcode files we have seen, codegen the result
201 // and return the resulting ObjectFile(s).
202 std::vector<ObjFile *> BitcodeCompiler::compile() {
203   unsigned maxTasks = ltoObj->getMaxTasks();
204   buf.resize(maxTasks);
205   files.resize(maxTasks);
206 
207   // The -cache_path_lto option specifies the path to a directory in which
208   // to cache native object files for ThinLTO incremental builds. If a path was
209   // specified, configure LTO to use it as the cache directory.
210   FileCache cache;
211   if (!config->thinLTOCacheDir.empty())
212     cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
213                              [&](size_t task, const Twine &moduleName,
214                                  std::unique_ptr<MemoryBuffer> mb) {
215                                files[task] = std::move(mb);
216                              }));
217 
218   if (hasFiles)
219     checkError(ltoObj->run(
220         [&](size_t task, const Twine &moduleName) {
221           return std::make_unique<CachedFileStream>(
222               std::make_unique<raw_svector_ostream>(buf[task]));
223         },
224         cache));
225 
226   // Emit empty index files for non-indexed files
227   for (StringRef s : thinIndices) {
228     std::string path = getThinLTOOutputFile(s);
229     openFile(path + ".thinlto.bc");
230     if (config->thinLTOEmitImportsFiles)
231       openFile(path + ".imports");
232   }
233 
234   if (config->thinLTOEmitIndexFiles)
235     thinLTOCreateEmptyIndexFiles();
236 
237   // In ThinLTO mode, Clang passes a temporary directory in -object_path_lto,
238   // while the argument is a single file in FullLTO mode.
239   bool objPathIsDir = true;
240   if (!config->ltoObjPath.empty()) {
241     if (std::error_code ec = fs::create_directories(config->ltoObjPath))
242       fatal("cannot create LTO object path " + config->ltoObjPath + ": " +
243             ec.message());
244 
245     if (!fs::is_directory(config->ltoObjPath)) {
246       objPathIsDir = false;
247       unsigned objCount =
248           count_if(buf, [](const SmallString<0> &b) { return !b.empty(); });
249       if (objCount > 1)
250         fatal("-object_path_lto must specify a directory when using ThinLTO");
251     }
252   }
253 
254   auto outputFilePath = [objPathIsDir](int i) {
255     SmallString<261> filePath("/tmp/lto.tmp");
256     if (!config->ltoObjPath.empty()) {
257       filePath = config->ltoObjPath;
258       if (objPathIsDir)
259         path::append(filePath, Twine(i) + "." +
260                                    getArchitectureName(config->arch()) +
261                                    ".lto.o");
262     }
263     return filePath;
264   };
265 
266   // ThinLTO with index only option is required to generate only the index
267   // files. After that, we exit from linker and ThinLTO backend runs in a
268   // distributed environment.
269   if (config->thinLTOIndexOnly) {
270     if (!config->ltoObjPath.empty())
271       saveBuffer(buf[0], outputFilePath(0));
272     if (indexFile)
273       indexFile->close();
274     return {};
275   }
276 
277   if (!config->thinLTOCacheDir.empty())
278     pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
279 
280   std::vector<ObjFile *> ret;
281   for (unsigned i = 0; i < maxTasks; ++i) {
282     // Get the native object contents either from the cache or from memory.  Do
283     // not use the cached MemoryBuffer directly to ensure dsymutil does not
284     // race with the cache pruner.
285     StringRef objBuf;
286     std::optional<StringRef> cachePath;
287     if (files[i]) {
288       objBuf = files[i]->getBuffer();
289       cachePath = files[i]->getBufferIdentifier();
290     } else {
291       objBuf = buf[i];
292     }
293     if (objBuf.empty())
294       continue;
295 
296     // FIXME: should `saveTemps` and `ltoObjPath` use the same file name?
297     if (config->saveTemps)
298       saveBuffer(objBuf,
299                  config->outputFile + ((i == 0) ? "" : Twine(i)) + ".lto.o");
300 
301     auto filePath = outputFilePath(i);
302     uint32_t modTime = 0;
303     if (!config->ltoObjPath.empty()) {
304       saveOrHardlinkBuffer(objBuf, filePath, cachePath);
305       modTime = getModTime(filePath);
306     }
307     ret.push_back(make<ObjFile>(
308         MemoryBufferRef(objBuf, saver().save(filePath.str())), modTime, ""));
309   }
310 
311   return ret;
312 }
313