Revert "[libc] Use best-fit binary trie to make malloc logarithmic" (#117065)
[llvm-project.git] / lld / MachO / LTO.cpp
blob28f5290edb58e30f5f7eef37aea664aac9ddef8e
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 //===----------------------------------------------------------------------===//
9 #include "LTO.h"
10 #include "Config.h"
11 #include "Driver.h"
12 #include "InputFiles.h"
13 #include "Symbols.h"
14 #include "Target.h"
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"
30 using namespace lld;
31 using namespace lld::macho;
32 using namespace llvm;
33 using namespace llvm::MachO;
34 using namespace llvm::sys;
36 static std::string getThinLTOOutputFile(StringRef modulePath) {
37 return lto::getThinLTOOutputFile(modulePath, config->thinLTOPrefixReplaceOld,
38 config->thinLTOPrefixReplaceNew);
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(C.str());
47 c.CodeModel = getCodeModelFromCMModel();
48 c.CPU = getCPUStr();
49 c.MAttrs = getMAttrs();
50 c.DiagHandler = diagnosticHandler;
52 c.AlwaysEmitRegularLTOObj = !config->ltoObjPath.empty();
54 c.TimeTraceEnabled = config->timeTraceEnabled;
55 c.TimeTraceGranularity = config->timeTraceGranularity;
56 c.DebugPassManager = config->ltoDebugPassManager;
57 c.CSIRProfile = std::string(config->csProfilePath);
58 c.RunCSIRInstr = config->csProfileGenerate;
59 c.PGOWarnMismatch = config->pgoWarnMismatch;
60 c.OptLevel = config->ltoo;
61 c.CGOptLevel = config->ltoCgo;
62 if (config->saveTemps)
63 checkError(c.addSaveTemps(config->outputFile.str() + ".",
64 /*UseInputModulePath=*/true));
65 return c;
68 // If `originalPath` exists, hardlinks `path` to `originalPath`. If that fails,
69 // or `originalPath` is not set, saves `buffer` to `path`.
70 static void saveOrHardlinkBuffer(StringRef buffer, const Twine &path,
71 std::optional<StringRef> originalPath) {
72 if (originalPath) {
73 auto err = fs::create_hard_link(*originalPath, path);
74 if (!err)
75 return;
77 saveBuffer(buffer, path);
80 BitcodeCompiler::BitcodeCompiler() {
81 // Initialize indexFile.
82 if (!config->thinLTOIndexOnlyArg.empty())
83 indexFile = openFile(config->thinLTOIndexOnlyArg);
85 // Initialize ltoObj.
86 lto::ThinBackend backend;
87 auto onIndexWrite = [&](StringRef S) { thinIndices.erase(S); };
88 if (config->thinLTOIndexOnly) {
89 backend = lto::createWriteIndexesThinBackend(
90 llvm::hardware_concurrency(config->thinLTOJobs),
91 std::string(config->thinLTOPrefixReplaceOld),
92 std::string(config->thinLTOPrefixReplaceNew),
93 std::string(config->thinLTOPrefixReplaceNativeObject),
94 config->thinLTOEmitImportsFiles, indexFile.get(), onIndexWrite);
95 } else {
96 backend = lto::createInProcessThinBackend(
97 llvm::heavyweight_hardware_concurrency(config->thinLTOJobs),
98 onIndexWrite, config->thinLTOEmitIndexFiles,
99 config->thinLTOEmitImportsFiles);
102 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend);
105 void BitcodeCompiler::add(BitcodeFile &f) {
106 lto::InputFile &obj = *f.obj;
108 if (config->thinLTOEmitIndexFiles)
109 thinIndices.insert(obj.getName());
111 ArrayRef<lto::InputFile::Symbol> objSyms = obj.symbols();
112 std::vector<lto::SymbolResolution> resols;
113 resols.reserve(objSyms.size());
115 // Provide a resolution to the LTO API for each symbol.
116 bool exportDynamic =
117 config->outputType != MH_EXECUTE || config->exportDynamic;
118 auto symIt = f.symbols.begin();
119 for (const lto::InputFile::Symbol &objSym : objSyms) {
120 resols.emplace_back();
121 lto::SymbolResolution &r = resols.back();
122 Symbol *sym = *symIt++;
124 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
125 // reports two symbols for module ASM defined. Without this check, lld
126 // flags an undefined in IR with a definition in ASM as prevailing.
127 // Once IRObjectFile is fixed to report only one symbol this hack can
128 // be removed.
129 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
131 if (const auto *defined = dyn_cast<Defined>(sym)) {
132 r.ExportDynamic =
133 defined->isExternal() && !defined->privateExtern && exportDynamic;
134 r.FinalDefinitionInLinkageUnit =
135 !defined->isExternalWeakDef() && !defined->interposable;
136 } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) {
137 r.ExportDynamic = !common->privateExtern && exportDynamic;
138 r.FinalDefinitionInLinkageUnit = true;
141 r.VisibleToRegularObj =
142 sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic);
144 // Un-define the symbol so that we don't get duplicate symbol errors when we
145 // load the ObjFile emitted by LTO compilation.
146 if (r.Prevailing)
147 replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(),
148 RefState::Strong, /*wasBitcodeSymbol=*/true);
150 // TODO: set the other resolution configs properly
152 checkError(ltoObj->add(std::move(f.obj), resols));
153 hasFiles = true;
156 // If LazyObjFile has not been added to link, emit empty index files.
157 // This is needed because this is what GNU gold plugin does and we have a
158 // distributed build system that depends on that behavior.
159 static void thinLTOCreateEmptyIndexFiles() {
160 DenseSet<StringRef> linkedBitCodeFiles;
161 for (InputFile *file : inputFiles)
162 if (auto *f = dyn_cast<BitcodeFile>(file))
163 if (!f->lazy)
164 linkedBitCodeFiles.insert(f->getName());
166 for (InputFile *file : inputFiles) {
167 if (auto *f = dyn_cast<BitcodeFile>(file)) {
168 if (!f->lazy)
169 continue;
170 if (linkedBitCodeFiles.contains(f->getName()))
171 continue;
172 std::string path =
173 replaceThinLTOSuffix(getThinLTOOutputFile(f->obj->getName()));
174 std::unique_ptr<raw_fd_ostream> os = openFile(path + ".thinlto.bc");
175 if (!os)
176 continue;
178 ModuleSummaryIndex m(/*HaveGVs=*/false);
179 m.setSkipModuleByDistributedBackend();
180 writeIndexToFile(m, *os);
181 if (config->thinLTOEmitImportsFiles)
182 openFile(path + ".imports");
187 // Merge all the bitcode files we have seen, codegen the result
188 // and return the resulting ObjectFile(s).
189 std::vector<ObjFile *> BitcodeCompiler::compile() {
190 unsigned maxTasks = ltoObj->getMaxTasks();
191 buf.resize(maxTasks);
192 files.resize(maxTasks);
194 // The -cache_path_lto option specifies the path to a directory in which
195 // to cache native object files for ThinLTO incremental builds. If a path was
196 // specified, configure LTO to use it as the cache directory.
197 FileCache cache;
198 if (!config->thinLTOCacheDir.empty())
199 cache = check(localCache("ThinLTO", "Thin", config->thinLTOCacheDir,
200 [&](size_t task, const Twine &moduleName,
201 std::unique_ptr<MemoryBuffer> mb) {
202 files[task] = std::move(mb);
203 }));
205 if (hasFiles)
206 checkError(ltoObj->run(
207 [&](size_t task, const Twine &moduleName) {
208 return std::make_unique<CachedFileStream>(
209 std::make_unique<raw_svector_ostream>(buf[task]));
211 cache));
213 // Emit empty index files for non-indexed files
214 for (StringRef s : thinIndices) {
215 std::string path = getThinLTOOutputFile(s);
216 openFile(path + ".thinlto.bc");
217 if (config->thinLTOEmitImportsFiles)
218 openFile(path + ".imports");
221 if (config->thinLTOEmitIndexFiles)
222 thinLTOCreateEmptyIndexFiles();
224 // In ThinLTO mode, Clang passes a temporary directory in -object_path_lto,
225 // while the argument is a single file in FullLTO mode.
226 bool objPathIsDir = true;
227 if (!config->ltoObjPath.empty()) {
228 if (std::error_code ec = fs::create_directories(config->ltoObjPath))
229 fatal("cannot create LTO object path " + config->ltoObjPath + ": " +
230 ec.message());
232 if (!fs::is_directory(config->ltoObjPath)) {
233 objPathIsDir = false;
234 unsigned objCount =
235 count_if(buf, [](const SmallString<0> &b) { return !b.empty(); });
236 if (objCount > 1)
237 fatal("-object_path_lto must specify a directory when using ThinLTO");
241 auto outputFilePath = [objPathIsDir](int i) {
242 SmallString<261> filePath("/tmp/lto.tmp");
243 if (!config->ltoObjPath.empty()) {
244 filePath = config->ltoObjPath;
245 if (objPathIsDir)
246 path::append(filePath, Twine(i) + "." +
247 getArchitectureName(config->arch()) +
248 ".lto.o");
250 return filePath;
253 // ThinLTO with index only option is required to generate only the index
254 // files. After that, we exit from linker and ThinLTO backend runs in a
255 // distributed environment.
256 if (config->thinLTOIndexOnly) {
257 if (!config->ltoObjPath.empty())
258 saveBuffer(buf[0], outputFilePath(0));
259 if (indexFile)
260 indexFile->close();
261 return {};
264 if (!config->thinLTOCacheDir.empty())
265 pruneCache(config->thinLTOCacheDir, config->thinLTOCachePolicy, files);
267 std::vector<ObjFile *> ret;
268 for (unsigned i = 0; i < maxTasks; ++i) {
269 // Get the native object contents either from the cache or from memory. Do
270 // not use the cached MemoryBuffer directly to ensure dsymutil does not
271 // race with the cache pruner.
272 StringRef objBuf;
273 std::optional<StringRef> cachePath;
274 if (files[i]) {
275 objBuf = files[i]->getBuffer();
276 cachePath = files[i]->getBufferIdentifier();
277 } else {
278 objBuf = buf[i];
280 if (objBuf.empty())
281 continue;
283 // FIXME: should `saveTemps` and `ltoObjPath` use the same file name?
284 if (config->saveTemps)
285 saveBuffer(objBuf,
286 config->outputFile + ((i == 0) ? "" : Twine(i)) + ".lto.o");
288 auto filePath = outputFilePath(i);
289 uint32_t modTime = 0;
290 if (!config->ltoObjPath.empty()) {
291 saveOrHardlinkBuffer(objBuf, filePath, cachePath);
292 modTime = getModTime(filePath);
294 ret.push_back(make<ObjFile>(
295 MemoryBufferRef(objBuf, saver().save(filePath.str())), modTime,
296 /*archiveName=*/"", /*lazy=*/false,
297 /*forceHidden=*/false, /*compatArch=*/true, /*builtFromBitcode=*/true));
300 return ret;