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