blob: 2b72615a46e6cda0c5617892942759e6530b365e [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"
23
24using namespace llvm;
25using namespace lto;
26using namespace object;
27
28static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
29 cl::desc("<input bitcode files>"));
30
31static cl::opt<std::string> OutputFilename("o", cl::Required,
32 cl::desc("Output filename"),
33 cl::value_desc("filename"));
34
Mehdi Aminiadc0e262016-08-23 21:30:12 +000035static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
36 cl::value_desc("directory"));
37
Davide Italianoec9612d2016-09-07 17:46:16 +000038static cl::opt<std::string> OptPipeline("opt-pipeline",
39 cl::desc("Optimizer Pipeline"),
40 cl::value_desc("pipeline"));
41
Davide Italiano14e9e8a2016-09-16 21:03:21 +000042static cl::opt<std::string> AAPipeline("aa-pipeline",
43 cl::desc("Alias Analysis Pipeline"),
44 cl::value_desc("aapipeline"));
45
Teresa Johnson9ba95f92016-08-11 14:58:12 +000046static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
47
Mehdi Amini458f8052016-08-19 23:54:40 +000048static cl::opt<bool>
49 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
50 cl::desc("Write out individual index and "
51 "import files for the "
52 "distributed backend case"));
53
54static cl::opt<int> Threads("-thinlto-threads",
55 cl::init(thread::hardware_concurrency()));
56
Teresa Johnson9ba95f92016-08-11 14:58:12 +000057static cl::list<std::string> SymbolResolutions(
58 "r",
59 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
60 "where \"resolution\" is a sequence (which may be empty) of the\n"
61 "following characters:\n"
62 " p - prevailing: the linker has chosen this definition of the\n"
63 " symbol\n"
64 " l - local: the definition of this symbol is unpreemptable at\n"
65 " runtime and is known to be in this linkage unit\n"
66 " x - externally visible: the definition of this symbol is\n"
67 " visible outside of the LTO unit\n"
68 "A resolution for each symbol must be specified."),
69 cl::ZeroOrMore);
70
71static void check(Error E, std::string Msg) {
72 if (!E)
73 return;
74 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
75 errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n';
76 });
77 exit(1);
78}
79
80template <typename T> static T check(Expected<T> E, std::string Msg) {
81 if (E)
82 return std::move(*E);
83 check(E.takeError(), Msg);
84 return T();
85}
86
87static void check(std::error_code EC, std::string Msg) {
88 check(errorCodeToError(EC), Msg);
89}
90
91template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
92 if (E)
93 return std::move(*E);
94 check(E.getError(), Msg);
95 return T();
96}
97
98int main(int argc, char **argv) {
99 InitializeAllTargets();
100 InitializeAllTargetMCs();
101 InitializeAllAsmPrinters();
102 InitializeAllAsmParsers();
103
104 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
105
106 std::map<std::pair<std::string, std::string>, SymbolResolution>
107 CommandLineResolutions;
108 for (std::string R : SymbolResolutions) {
109 StringRef Rest = R;
110 StringRef FileName, SymbolName;
111 std::tie(FileName, Rest) = Rest.split(',');
112 if (Rest.empty()) {
113 llvm::errs() << "invalid resolution: " << R << '\n';
114 return 1;
115 }
116 std::tie(SymbolName, Rest) = Rest.split(',');
117 SymbolResolution Res;
118 for (char C : Rest) {
119 if (C == 'p')
120 Res.Prevailing = true;
121 else if (C == 'l')
122 Res.FinalDefinitionInLinkageUnit = true;
123 else if (C == 'x')
124 Res.VisibleToRegularObj = true;
125 else
126 llvm::errs() << "invalid character " << C << " in resolution: " << R
127 << '\n';
128 }
129 CommandLineResolutions[{FileName, SymbolName}] = Res;
130 }
131
132 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
133
134 Config Conf;
135 Conf.DiagHandler = [](const DiagnosticInfo &) {
136 exit(1);
137 };
138
139 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000140 check(Conf.addSaveTemps(OutputFilename + "."),
141 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000142
Davide Italianoec9612d2016-09-07 17:46:16 +0000143 // Run a custom pipeline, if asked for.
144 Conf.OptPipeline = OptPipeline;
Davide Italiano14e9e8a2016-09-16 21:03:21 +0000145 Conf.AAPipeline = AAPipeline;
Davide Italianoec9612d2016-09-07 17:46:16 +0000146
Mehdi Amini458f8052016-08-19 23:54:40 +0000147 ThinBackend Backend;
148 if (ThinLTODistributedIndexes)
149 Backend = createWriteIndexesThinBackend("", "", true, "");
150 else
151 Backend = createInProcessThinBackend(Threads);
152 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000153
154 bool HasErrors = false;
155 for (std::string F : InputFilenames) {
156 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
157 std::unique_ptr<InputFile> Input =
158 check(InputFile::create(MB->getMemBufferRef()), F);
159
160 std::vector<SymbolResolution> Res;
161 for (const InputFile::Symbol &Sym : Input->symbols()) {
162 auto I = CommandLineResolutions.find({F, Sym.getName()});
163 if (I == CommandLineResolutions.end()) {
164 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
165 << ',' << Sym.getName() << '\n';
166 HasErrors = true;
167 } else {
168 Res.push_back(I->second);
169 CommandLineResolutions.erase(I);
170 }
171 }
172
173 if (HasErrors)
174 continue;
175
176 MBs.push_back(std::move(MB));
177 check(Lto.add(std::move(Input), Res), F);
178 }
179
180 if (!CommandLineResolutions.empty()) {
181 HasErrors = true;
182 for (auto UnusedRes : CommandLineResolutions)
183 llvm::errs() << argv[0] << ": unused symbol resolution for "
184 << UnusedRes.first.first << ',' << UnusedRes.first.second
185 << '\n';
186 }
187 if (HasErrors)
188 return 1;
189
Peter Collingbourne80186a52016-09-23 21:33:43 +0000190 auto AddStream =
191 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000192 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000193
Peter Collingbourne80186a52016-09-23 21:33:43 +0000194 std::error_code EC;
195 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
196 check(EC, Path);
197 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000198 };
199
Peter Collingbourne80186a52016-09-23 21:33:43 +0000200 auto AddFile = [&](size_t Task, StringRef Path) {
201 auto ReloadedBufferOrErr = MemoryBuffer::getFile(Path);
202 if (auto EC = ReloadedBufferOrErr.getError())
203 report_fatal_error(Twine("Can't reload cached file '") + Path + "': " +
204 EC.message() + "\n");
205
206 *AddStream(Task)->OS << (*ReloadedBufferOrErr)->getBuffer();
207 };
208
209 NativeObjectCache Cache;
210 if (!CacheDir.empty())
211 Cache = localCache(CacheDir, AddFile);
212
213 check(Lto.run(AddStream, Cache), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000214}