blob: b48f6c1993e1d4dfd5d3c98e0f4e6d13799fd54c [file] [log] [blame]
Teresa Johnson9ba95f92016-08-11 14:58:12 +00001//===-- llvm-lto2: test harness for the resolution-based LTO interface ----===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This program takes in a list of bitcode files, links them and performs
11// link-time optimization according to the provided symbol resolutions using the
12// resolution-based LTO interface, and outputs one or more object files.
13//
14// This program is intended to eventually replace llvm-lto which uses the legacy
15// LTO interface.
16//
17//===----------------------------------------------------------------------===//
18
Mehdi Aminiadc0e262016-08-23 21:30:12 +000019#include "llvm/LTO/Caching.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000020#include "llvm/LTO/LTO.h"
21#include "llvm/Support/CommandLine.h"
22#include "llvm/Support/TargetSelect.h"
Teresa Johnsonec544c52016-10-19 17:35:01 +000023#include "llvm/Support/Threading.h"
Teresa Johnson9ba95f92016-08-11 14:58:12 +000024
25using namespace llvm;
26using namespace lto;
27using namespace object;
28
Teresa Johnson002af9b2016-10-31 22:12:21 +000029static cl::opt<char>
30 OptLevel("O", cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
31 "(default = '-O2')"),
32 cl::Prefix, cl::ZeroOrMore, cl::init('2'));
33
Teresa Johnson9ba95f92016-08-11 14:58:12 +000034static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
35 cl::desc("<input bitcode files>"));
36
37static cl::opt<std::string> OutputFilename("o", cl::Required,
38 cl::desc("Output filename"),
39 cl::value_desc("filename"));
40
Mehdi Aminiadc0e262016-08-23 21:30:12 +000041static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
42 cl::value_desc("directory"));
43
Davide Italianoec9612d2016-09-07 17:46:16 +000044static cl::opt<std::string> OptPipeline("opt-pipeline",
45 cl::desc("Optimizer Pipeline"),
46 cl::value_desc("pipeline"));
47
Davide Italiano14e9e8a2016-09-16 21:03:21 +000048static cl::opt<std::string> AAPipeline("aa-pipeline",
49 cl::desc("Alias Analysis Pipeline"),
50 cl::value_desc("aapipeline"));
51
Teresa Johnson9ba95f92016-08-11 14:58:12 +000052static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
53
Mehdi Amini458f8052016-08-19 23:54:40 +000054static cl::opt<bool>
55 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
56 cl::desc("Write out individual index and "
57 "import files for the "
58 "distributed backend case"));
59
60static cl::opt<int> Threads("-thinlto-threads",
Teresa Johnsonec544c52016-10-19 17:35:01 +000061 cl::init(llvm::heavyweight_hardware_concurrency()));
Mehdi Amini458f8052016-08-19 23:54:40 +000062
Teresa Johnson9ba95f92016-08-11 14:58:12 +000063static cl::list<std::string> SymbolResolutions(
64 "r",
65 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
66 "where \"resolution\" is a sequence (which may be empty) of the\n"
67 "following characters:\n"
68 " p - prevailing: the linker has chosen this definition of the\n"
69 " symbol\n"
70 " l - local: the definition of this symbol is unpreemptable at\n"
71 " runtime and is known to be in this linkage unit\n"
72 " x - externally visible: the definition of this symbol is\n"
73 " visible outside of the LTO unit\n"
74 "A resolution for each symbol must be specified."),
75 cl::ZeroOrMore);
76
77static void check(Error E, std::string Msg) {
78 if (!E)
79 return;
80 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
81 errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n';
82 });
83 exit(1);
84}
85
86template <typename T> static T check(Expected<T> E, std::string Msg) {
87 if (E)
88 return std::move(*E);
89 check(E.takeError(), Msg);
90 return T();
91}
92
93static void check(std::error_code EC, std::string Msg) {
94 check(errorCodeToError(EC), Msg);
95}
96
97template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
98 if (E)
99 return std::move(*E);
100 check(E.getError(), Msg);
101 return T();
102}
103
104int main(int argc, char **argv) {
105 InitializeAllTargets();
106 InitializeAllTargetMCs();
107 InitializeAllAsmPrinters();
108 InitializeAllAsmParsers();
109
110 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
111
112 std::map<std::pair<std::string, std::string>, SymbolResolution>
113 CommandLineResolutions;
114 for (std::string R : SymbolResolutions) {
115 StringRef Rest = R;
116 StringRef FileName, SymbolName;
117 std::tie(FileName, Rest) = Rest.split(',');
118 if (Rest.empty()) {
119 llvm::errs() << "invalid resolution: " << R << '\n';
120 return 1;
121 }
122 std::tie(SymbolName, Rest) = Rest.split(',');
123 SymbolResolution Res;
124 for (char C : Rest) {
125 if (C == 'p')
126 Res.Prevailing = true;
127 else if (C == 'l')
128 Res.FinalDefinitionInLinkageUnit = true;
129 else if (C == 'x')
130 Res.VisibleToRegularObj = true;
131 else
132 llvm::errs() << "invalid character " << C << " in resolution: " << R
133 << '\n';
134 }
135 CommandLineResolutions[{FileName, SymbolName}] = Res;
136 }
137
138 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
139
140 Config Conf;
141 Conf.DiagHandler = [](const DiagnosticInfo &) {
142 exit(1);
143 };
144
145 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000146 check(Conf.addSaveTemps(OutputFilename + "."),
147 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000148
Davide Italianoec9612d2016-09-07 17:46:16 +0000149 // Run a custom pipeline, if asked for.
150 Conf.OptPipeline = OptPipeline;
Davide Italiano14e9e8a2016-09-16 21:03:21 +0000151 Conf.AAPipeline = AAPipeline;
Davide Italianoec9612d2016-09-07 17:46:16 +0000152
Teresa Johnson002af9b2016-10-31 22:12:21 +0000153 Conf.OptLevel = OptLevel - '0';
154
Mehdi Amini458f8052016-08-19 23:54:40 +0000155 ThinBackend Backend;
156 if (ThinLTODistributedIndexes)
157 Backend = createWriteIndexesThinBackend("", "", true, "");
158 else
159 Backend = createInProcessThinBackend(Threads);
160 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000161
162 bool HasErrors = false;
163 for (std::string F : InputFilenames) {
164 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
165 std::unique_ptr<InputFile> Input =
166 check(InputFile::create(MB->getMemBufferRef()), F);
167
168 std::vector<SymbolResolution> Res;
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000169 // FIXME: Workaround PR30396 which means that a symbol can appear
170 // more than once if it is defined in module-level assembly and
171 // has a GV declaration. Keep track of the resolutions found in this
172 // file and remove them from the CommandLineResolutions map afterwards,
173 // so that we don't flag the second one as missing.
174 std::map<std::string, SymbolResolution> CurrentFileSymResolutions;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000175 for (const InputFile::Symbol &Sym : Input->symbols()) {
176 auto I = CommandLineResolutions.find({F, Sym.getName()});
177 if (I == CommandLineResolutions.end()) {
178 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
179 << ',' << Sym.getName() << '\n';
180 HasErrors = true;
181 } else {
182 Res.push_back(I->second);
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000183 CurrentFileSymResolutions[Sym.getName()] = I->second;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000184 }
185 }
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000186 for (auto I : CurrentFileSymResolutions) {
Teresa Johnson2f159a22016-10-12 20:06:02 +0000187#ifndef NDEBUG
188 auto NumErased =
189#endif
190 CommandLineResolutions.erase({F, I.first});
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000191 assert(NumErased > 0);
192 }
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000193
194 if (HasErrors)
195 continue;
196
197 MBs.push_back(std::move(MB));
198 check(Lto.add(std::move(Input), Res), F);
199 }
200
201 if (!CommandLineResolutions.empty()) {
202 HasErrors = true;
203 for (auto UnusedRes : CommandLineResolutions)
204 llvm::errs() << argv[0] << ": unused symbol resolution for "
205 << UnusedRes.first.first << ',' << UnusedRes.first.second
206 << '\n';
207 }
208 if (HasErrors)
209 return 1;
210
Peter Collingbourne80186a52016-09-23 21:33:43 +0000211 auto AddStream =
212 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000213 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000214
Peter Collingbourne80186a52016-09-23 21:33:43 +0000215 std::error_code EC;
216 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
217 check(EC, Path);
218 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000219 };
220
Peter Collingbourne80186a52016-09-23 21:33:43 +0000221 auto AddFile = [&](size_t Task, StringRef Path) {
222 auto ReloadedBufferOrErr = MemoryBuffer::getFile(Path);
223 if (auto EC = ReloadedBufferOrErr.getError())
224 report_fatal_error(Twine("Can't reload cached file '") + Path + "': " +
225 EC.message() + "\n");
226
227 *AddStream(Task)->OS << (*ReloadedBufferOrErr)->getBuffer();
228 };
229
230 NativeObjectCache Cache;
231 if (!CacheDir.empty())
232 Cache = localCache(CacheDir, AddFile);
233
234 check(Lto.run(AddStream, Cache), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000235}