blob: 01756d2c764a267adcefc1192697b523a2731705 [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
Peter Collingbournea5b71642016-11-30 23:19:05 +0000112 // FIXME: Workaround PR30396 which means that a symbol can appear
113 // more than once if it is defined in module-level assembly and
114 // has a GV declaration. We allow (file, symbol) pairs to have multiple
115 // resolutions and apply them in the order observed.
116 std::map<std::pair<std::string, std::string>, std::list<SymbolResolution>>
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000117 CommandLineResolutions;
118 for (std::string R : SymbolResolutions) {
119 StringRef Rest = R;
120 StringRef FileName, SymbolName;
121 std::tie(FileName, Rest) = Rest.split(',');
122 if (Rest.empty()) {
123 llvm::errs() << "invalid resolution: " << R << '\n';
124 return 1;
125 }
126 std::tie(SymbolName, Rest) = Rest.split(',');
127 SymbolResolution Res;
128 for (char C : Rest) {
129 if (C == 'p')
130 Res.Prevailing = true;
131 else if (C == 'l')
132 Res.FinalDefinitionInLinkageUnit = true;
133 else if (C == 'x')
134 Res.VisibleToRegularObj = true;
135 else
136 llvm::errs() << "invalid character " << C << " in resolution: " << R
137 << '\n';
138 }
Peter Collingbournea5b71642016-11-30 23:19:05 +0000139 CommandLineResolutions[{FileName, SymbolName}].push_back(Res);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000140 }
141
142 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
143
144 Config Conf;
145 Conf.DiagHandler = [](const DiagnosticInfo &) {
146 exit(1);
147 };
148
149 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000150 check(Conf.addSaveTemps(OutputFilename + "."),
151 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000152
Davide Italianoec9612d2016-09-07 17:46:16 +0000153 // Run a custom pipeline, if asked for.
154 Conf.OptPipeline = OptPipeline;
Davide Italiano14e9e8a2016-09-16 21:03:21 +0000155 Conf.AAPipeline = AAPipeline;
Davide Italianoec9612d2016-09-07 17:46:16 +0000156
Teresa Johnson002af9b2016-10-31 22:12:21 +0000157 Conf.OptLevel = OptLevel - '0';
158
Mehdi Amini458f8052016-08-19 23:54:40 +0000159 ThinBackend Backend;
160 if (ThinLTODistributedIndexes)
161 Backend = createWriteIndexesThinBackend("", "", true, "");
162 else
163 Backend = createInProcessThinBackend(Threads);
164 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000165
166 bool HasErrors = false;
167 for (std::string F : InputFilenames) {
168 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
169 std::unique_ptr<InputFile> Input =
170 check(InputFile::create(MB->getMemBufferRef()), F);
171
172 std::vector<SymbolResolution> Res;
173 for (const InputFile::Symbol &Sym : Input->symbols()) {
174 auto I = CommandLineResolutions.find({F, Sym.getName()});
175 if (I == CommandLineResolutions.end()) {
176 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
177 << ',' << Sym.getName() << '\n';
178 HasErrors = true;
179 } else {
Peter Collingbournea5b71642016-11-30 23:19:05 +0000180 Res.push_back(I->second.front());
181 I->second.pop_front();
182 if (I->second.empty())
183 CommandLineResolutions.erase(I);
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000184 }
185 }
186
187 if (HasErrors)
188 continue;
189
190 MBs.push_back(std::move(MB));
191 check(Lto.add(std::move(Input), Res), F);
192 }
193
194 if (!CommandLineResolutions.empty()) {
195 HasErrors = true;
196 for (auto UnusedRes : CommandLineResolutions)
197 llvm::errs() << argv[0] << ": unused symbol resolution for "
198 << UnusedRes.first.first << ',' << UnusedRes.first.second
199 << '\n';
200 }
201 if (HasErrors)
202 return 1;
203
Peter Collingbourne80186a52016-09-23 21:33:43 +0000204 auto AddStream =
205 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000206 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000207
Peter Collingbourne80186a52016-09-23 21:33:43 +0000208 std::error_code EC;
209 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
210 check(EC, Path);
211 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000212 };
213
Peter Collingbourne80186a52016-09-23 21:33:43 +0000214 auto AddFile = [&](size_t Task, StringRef Path) {
215 auto ReloadedBufferOrErr = MemoryBuffer::getFile(Path);
216 if (auto EC = ReloadedBufferOrErr.getError())
217 report_fatal_error(Twine("Can't reload cached file '") + Path + "': " +
218 EC.message() + "\n");
219
220 *AddStream(Task)->OS << (*ReloadedBufferOrErr)->getBuffer();
221 };
222
223 NativeObjectCache Cache;
224 if (!CacheDir.empty())
225 Cache = localCache(CacheDir, AddFile);
226
227 check(Lto.run(AddStream, Cache), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000228}