annotate lld/MachO/LTO.cpp @ 207:2e18cbf3894f

LLVM12
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Tue, 08 Jun 2021 06:07:14 +0900
parents
children 5f17cb93ff66
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
207
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 //===- LTO.cpp ------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 //
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 #include "LTO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 #include "Config.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 #include "Driver.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12 #include "InputFiles.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 #include "Symbols.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 #include "Target.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16 #include "lld/Common/ErrorHandler.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 #include "lld/Common/Strings.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 #include "lld/Common/TargetOptionsCommandFlags.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19 #include "llvm/LTO/LTO.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 #include "llvm/Support/FileSystem.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 #include "llvm/Support/Path.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 #include "llvm/Support/raw_ostream.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 #include "llvm/Transforms/ObjCARC.h"
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 using namespace lld;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 using namespace lld::macho;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 using namespace llvm;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 using namespace llvm::MachO;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 using namespace llvm::sys;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 static lto::Config createConfig() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 lto::Config c;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33 c.Options = initTargetOptionsFromCodeGenFlags();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 c.CodeModel = getCodeModelFromCMModel();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 c.CPU = getCPUStr();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 c.MAttrs = getMAttrs();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 c.UseNewPM = config->ltoNewPassManager;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 c.PreCodeGenPassesHook = [](legacy::PassManager &pm) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 pm.add(createObjCARCContractPass());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 };
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41 c.TimeTraceEnabled = config->timeTraceEnabled;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 c.TimeTraceGranularity = config->timeTraceGranularity;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 if (config->saveTemps)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44 checkError(c.addSaveTemps(config->outputFile.str() + ".",
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 /*UseInputModulePath=*/true));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 return c;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
48
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
49 BitcodeCompiler::BitcodeCompiler() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
50 lto::ThinBackend backend = lto::createInProcessThinBackend(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
51 heavyweight_hardware_concurrency(config->thinLTOJobs));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
52 ltoObj = std::make_unique<lto::LTO>(createConfig(), backend);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
53 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
54
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
55 void BitcodeCompiler::add(BitcodeFile &f) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
56 ArrayRef<lto::InputFile::Symbol> objSyms = f.obj->symbols();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
57 std::vector<lto::SymbolResolution> resols;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
58 resols.reserve(objSyms.size());
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
59
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 // Provide a resolution to the LTO API for each symbol.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 auto symIt = f.symbols.begin();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
62 for (const lto::InputFile::Symbol &objSym : objSyms) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
63 resols.emplace_back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
64 lto::SymbolResolution &r = resols.back();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
65 Symbol *sym = *symIt++;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
66
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
67 // Ideally we shouldn't check for SF_Undefined but currently IRObjectFile
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
68 // reports two symbols for module ASM defined. Without this check, lld
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
69 // flags an undefined in IR with a definition in ASM as prevailing.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
70 // Once IRObjectFile is fixed to report only one symbol this hack can
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
71 // be removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
72 r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
73
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
74 // FIXME: What about other output types? And we can probably be less
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
75 // restrictive with -flat_namespace, but it's an infrequent use case.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
76 r.VisibleToRegularObj = config->outputType != MH_EXECUTE ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
77 config->namespaceKind == NamespaceKind::flat ||
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
78 sym->isUsedInRegularObj;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
79
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
80 // Un-define the symbol so that we don't get duplicate symbol errors when we
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
81 // load the ObjFile emitted by LTO compilation.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
82 if (r.Prevailing)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
83 replaceSymbol<Undefined>(sym, sym->getName(), sym->getFile(),
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
84 RefState::Strong);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
85
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
86 // TODO: set the other resolution configs properly
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
87 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
88 checkError(ltoObj->add(std::move(f.obj), resols));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
89 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
90
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
91 // Merge all the bitcode files we have seen, codegen the result
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
92 // and return the resulting ObjectFile(s).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
93 std::vector<ObjFile *> BitcodeCompiler::compile() {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
94 unsigned maxTasks = ltoObj->getMaxTasks();
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
95 buf.resize(maxTasks);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
96
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
97 checkError(ltoObj->run([&](size_t task) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
98 return std::make_unique<lto::NativeObjectStream>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
99 std::make_unique<raw_svector_ostream>(buf[task]));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
100 }));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
101
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
102 if (config->saveTemps) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
103 if (!buf[0].empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
104 saveBuffer(buf[0], config->outputFile + ".lto.o");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
105 for (unsigned i = 1; i != maxTasks; ++i)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
106 saveBuffer(buf[i], config->outputFile + Twine(i) + ".lto.o");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
107 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
108
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
109 if (!config->ltoObjPath.empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
110 fs::create_directories(config->ltoObjPath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
111
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
112 std::vector<ObjFile *> ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
113 for (unsigned i = 0; i != maxTasks; ++i) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
114 if (buf[i].empty())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
115 continue;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
116 SmallString<261> filePath("/tmp/lto.tmp");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
117 uint32_t modTime = 0;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
118 if (!config->ltoObjPath.empty()) {
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
119 filePath = config->ltoObjPath;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
120 path::append(filePath, Twine(i) + "." +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
121 getArchitectureName(config->arch()) +
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
122 ".lto.o");
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
123 saveBuffer(buf[i], filePath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
124 modTime = getModTime(filePath);
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
125 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
126 ret.push_back(make<ObjFile>(
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
127 MemoryBufferRef(buf[i], saver.save(filePath.str())), modTime, ""));
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
128 }
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
129
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
130 return ret;
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
diff changeset
131 }