blob: a3cc6ef14d1178680e0da9efe484b215e15e5d1a [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;
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000161 // FIXME: Workaround PR30396 which means that a symbol can appear
162 // more than once if it is defined in module-level assembly and
163 // has a GV declaration. Keep track of the resolutions found in this
164 // file and remove them from the CommandLineResolutions map afterwards,
165 // so that we don't flag the second one as missing.
166 std::map<std::string, SymbolResolution> CurrentFileSymResolutions;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000167 for (const InputFile::Symbol &Sym : Input->symbols()) {
168 auto I = CommandLineResolutions.find({F, Sym.getName()});
169 if (I == CommandLineResolutions.end()) {
170 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
171 << ',' << Sym.getName() << '\n';
172 HasErrors = true;
173 } else {
174 Res.push_back(I->second);
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000175 CurrentFileSymResolutions[Sym.getName()] = I->second;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000176 }
177 }
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000178 for (auto I : CurrentFileSymResolutions) {
Teresa Johnson2f159a22016-10-12 20:06:02 +0000179#ifndef NDEBUG
180 auto NumErased =
181#endif
182 CommandLineResolutions.erase({F, I.first});
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000183 assert(NumErased > 0);
184 }
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000185
186 if (HasErrors)
187 continue;
188
189 MBs.push_back(std::move(MB));
190 check(Lto.add(std::move(Input), Res), F);
191 }
192
193 if (!CommandLineResolutions.empty()) {
194 HasErrors = true;
195 for (auto UnusedRes : CommandLineResolutions)
196 llvm::errs() << argv[0] << ": unused symbol resolution for "
197 << UnusedRes.first.first << ',' << UnusedRes.first.second
198 << '\n';
199 }
200 if (HasErrors)
201 return 1;
202
Peter Collingbourne80186a52016-09-23 21:33:43 +0000203 auto AddStream =
204 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectStream> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000205 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000206
Peter Collingbourne80186a52016-09-23 21:33:43 +0000207 std::error_code EC;
208 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
209 check(EC, Path);
210 return llvm::make_unique<lto::NativeObjectStream>(std::move(S));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000211 };
212
Peter Collingbourne80186a52016-09-23 21:33:43 +0000213 auto AddFile = [&](size_t Task, StringRef Path) {
214 auto ReloadedBufferOrErr = MemoryBuffer::getFile(Path);
215 if (auto EC = ReloadedBufferOrErr.getError())
216 report_fatal_error(Twine("Can't reload cached file '") + Path + "': " +
217 EC.message() + "\n");
218
219 *AddStream(Task)->OS << (*ReloadedBufferOrErr)->getBuffer();
220 };
221
222 NativeObjectCache Cache;
223 if (!CacheDir.empty())
224 Cache = localCache(CacheDir, AddFile);
225
226 check(Lto.run(AddStream, Cache), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000227}