blob: e5f0b3a1ad55f2553f9acf6908f7b491de18b22b [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
Mehdi Amini970800e2016-08-17 06:23:09 +000098namespace {
99// Define the LTOOutput handling
100class LTOOutput : public lto::NativeObjectOutput {
Chandler Carruth5f6d73b2016-08-17 07:48:34 +0000101 std::string Path;
Mehdi Amini970800e2016-08-17 06:23:09 +0000102
103public:
Chandler Carruth5f6d73b2016-08-17 07:48:34 +0000104 LTOOutput(std::string Path) : Path(std::move(Path)) {}
Mehdi Amini970800e2016-08-17 06:23:09 +0000105 std::unique_ptr<raw_pwrite_stream> getStream() override {
106 std::error_code EC;
107 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
108 check(EC, Path);
109 return std::move(S);
110 }
111};
112}
113
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000114int main(int argc, char **argv) {
115 InitializeAllTargets();
116 InitializeAllTargetMCs();
117 InitializeAllAsmPrinters();
118 InitializeAllAsmParsers();
119
120 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
121
122 std::map<std::pair<std::string, std::string>, SymbolResolution>
123 CommandLineResolutions;
124 for (std::string R : SymbolResolutions) {
125 StringRef Rest = R;
126 StringRef FileName, SymbolName;
127 std::tie(FileName, Rest) = Rest.split(',');
128 if (Rest.empty()) {
129 llvm::errs() << "invalid resolution: " << R << '\n';
130 return 1;
131 }
132 std::tie(SymbolName, Rest) = Rest.split(',');
133 SymbolResolution Res;
134 for (char C : Rest) {
135 if (C == 'p')
136 Res.Prevailing = true;
137 else if (C == 'l')
138 Res.FinalDefinitionInLinkageUnit = true;
139 else if (C == 'x')
140 Res.VisibleToRegularObj = true;
141 else
142 llvm::errs() << "invalid character " << C << " in resolution: " << R
143 << '\n';
144 }
145 CommandLineResolutions[{FileName, SymbolName}] = Res;
146 }
147
148 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
149
150 Config Conf;
151 Conf.DiagHandler = [](const DiagnosticInfo &) {
152 exit(1);
153 };
154
155 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000156 check(Conf.addSaveTemps(OutputFilename + "."),
157 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000158
Davide Italianoec9612d2016-09-07 17:46:16 +0000159 // Run a custom pipeline, if asked for.
160 Conf.OptPipeline = OptPipeline;
Davide Italiano14e9e8a2016-09-16 21:03:21 +0000161 Conf.AAPipeline = AAPipeline;
Davide Italianoec9612d2016-09-07 17:46:16 +0000162
Mehdi Amini458f8052016-08-19 23:54:40 +0000163 ThinBackend Backend;
164 if (ThinLTODistributedIndexes)
165 Backend = createWriteIndexesThinBackend("", "", true, "");
166 else
167 Backend = createInProcessThinBackend(Threads);
168 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000169
170 bool HasErrors = false;
171 for (std::string F : InputFilenames) {
172 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
173 std::unique_ptr<InputFile> Input =
174 check(InputFile::create(MB->getMemBufferRef()), F);
175
176 std::vector<SymbolResolution> Res;
177 for (const InputFile::Symbol &Sym : Input->symbols()) {
178 auto I = CommandLineResolutions.find({F, Sym.getName()});
179 if (I == CommandLineResolutions.end()) {
180 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
181 << ',' << Sym.getName() << '\n';
182 HasErrors = true;
183 } else {
184 Res.push_back(I->second);
185 CommandLineResolutions.erase(I);
186 }
187 }
188
189 if (HasErrors)
190 continue;
191
192 MBs.push_back(std::move(MB));
193 check(Lto.add(std::move(Input), Res), F);
194 }
195
196 if (!CommandLineResolutions.empty()) {
197 HasErrors = true;
198 for (auto UnusedRes : CommandLineResolutions)
199 llvm::errs() << argv[0] << ": unused symbol resolution for "
200 << UnusedRes.first.first << ',' << UnusedRes.first.second
201 << '\n';
202 }
203 if (HasErrors)
204 return 1;
205
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000206 auto AddOutput =
207 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectOutput> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000208 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000209 if (CacheDir.empty())
210 return llvm::make_unique<LTOOutput>(std::move(Path));
211
212 return llvm::make_unique<CacheObjectOutput>(
Teresa Johnson26a46282016-08-26 23:29:14 +0000213 CacheDir, [Path](std::string EntryPath) {
214 // Load the entry from the cache now.
215 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
216 if (auto EC = ReloadedBufferOrErr.getError())
217 report_fatal_error(Twine("Can't reload cached file '") + EntryPath +
218 "': " + EC.message() + "\n");
219
220 *LTOOutput(Path).getStream() << (*ReloadedBufferOrErr)->getBuffer();
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000221 });
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000222 };
223
Mehdi Amini970800e2016-08-17 06:23:09 +0000224 check(Lto.run(AddOutput), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000225}