blob: 042f874596fb1edbe8384e6f4f22318ca0d9b51e [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
29static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
30 cl::desc("<input bitcode files>"));
31
32static cl::opt<std::string> OutputFilename("o", cl::Required,
33 cl::desc("Output filename"),
34 cl::value_desc("filename"));
35
Mehdi Aminiadc0e262016-08-23 21:30:12 +000036static cl::opt<std::string> CacheDir("cache-dir", cl::desc("Cache Directory"),
37 cl::value_desc("directory"));
38
Davide Italianoec9612d2016-09-07 17:46:16 +000039static cl::opt<std::string> OptPipeline("opt-pipeline",
40 cl::desc("Optimizer Pipeline"),
41 cl::value_desc("pipeline"));
42
Davide Italiano14e9e8a2016-09-16 21:03:21 +000043static cl::opt<std::string> AAPipeline("aa-pipeline",
44 cl::desc("Alias Analysis Pipeline"),
45 cl::value_desc("aapipeline"));
46
Teresa Johnson9ba95f92016-08-11 14:58:12 +000047static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
48
Mehdi Amini458f8052016-08-19 23:54:40 +000049static cl::opt<bool>
50 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
51 cl::desc("Write out individual index and "
52 "import files for the "
53 "distributed backend case"));
54
55static cl::opt<int> Threads("-thinlto-threads",
Teresa Johnsonec544c52016-10-19 17:35:01 +000056 cl::init(llvm::heavyweight_hardware_concurrency()));
Mehdi Amini458f8052016-08-19 23:54:40 +000057
Teresa Johnson9ba95f92016-08-11 14:58:12 +000058static cl::list<std::string> SymbolResolutions(
59 "r",
60 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
61 "where \"resolution\" is a sequence (which may be empty) of the\n"
62 "following characters:\n"
63 " p - prevailing: the linker has chosen this definition of the\n"
64 " symbol\n"
65 " l - local: the definition of this symbol is unpreemptable at\n"
66 " runtime and is known to be in this linkage unit\n"
67 " x - externally visible: the definition of this symbol is\n"
68 " visible outside of the LTO unit\n"
69 "A resolution for each symbol must be specified."),
70 cl::ZeroOrMore);
71
72static void check(Error E, std::string Msg) {
73 if (!E)
74 return;
75 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
76 errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n';
77 });
78 exit(1);
79}
80
81template <typename T> static T check(Expected<T> E, std::string Msg) {
82 if (E)
83 return std::move(*E);
84 check(E.takeError(), Msg);
85 return T();
86}
87
88static void check(std::error_code EC, std::string Msg) {
89 check(errorCodeToError(EC), Msg);
90}
91
92template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
93 if (E)
94 return std::move(*E);
95 check(E.getError(), Msg);
96 return T();
97}
98
99int main(int argc, char **argv) {
100 InitializeAllTargets();
101 InitializeAllTargetMCs();
102 InitializeAllAsmPrinters();
103 InitializeAllAsmParsers();
104
105 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
106
107 std::map<std::pair<std::string, std::string>, SymbolResolution>
108 CommandLineResolutions;
109 for (std::string R : SymbolResolutions) {
110 StringRef Rest = R;
111 StringRef FileName, SymbolName;
112 std::tie(FileName, Rest) = Rest.split(',');
113 if (Rest.empty()) {
114 llvm::errs() << "invalid resolution: " << R << '\n';
115 return 1;
116 }
117 std::tie(SymbolName, Rest) = Rest.split(',');
118 SymbolResolution Res;
119 for (char C : Rest) {
120 if (C == 'p')
121 Res.Prevailing = true;
122 else if (C == 'l')
123 Res.FinalDefinitionInLinkageUnit = true;
124 else if (C == 'x')
125 Res.VisibleToRegularObj = true;
126 else
127 llvm::errs() << "invalid character " << C << " in resolution: " << R
128 << '\n';
129 }
130 CommandLineResolutions[{FileName, SymbolName}] = Res;
131 }
132
133 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
134
135 Config Conf;
136 Conf.DiagHandler = [](const DiagnosticInfo &) {
137 exit(1);
138 };
139
140 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000141 check(Conf.addSaveTemps(OutputFilename + "."),
142 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000143
Davide Italianoec9612d2016-09-07 17:46:16 +0000144 // Run a custom pipeline, if asked for.
145 Conf.OptPipeline = OptPipeline;
Davide Italiano14e9e8a2016-09-16 21:03:21 +0000146 Conf.AAPipeline = AAPipeline;
Davide Italianoec9612d2016-09-07 17:46:16 +0000147
Mehdi Amini458f8052016-08-19 23:54:40 +0000148 ThinBackend Backend;
149 if (ThinLTODistributedIndexes)
150 Backend = createWriteIndexesThinBackend("", "", true, "");
151 else
152 Backend = createInProcessThinBackend(Threads);
153 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000154
155 bool HasErrors = false;
156 for (std::string F : InputFilenames) {
157 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
158 std::unique_ptr<InputFile> Input =
159 check(InputFile::create(MB->getMemBufferRef()), F);
160
161 std::vector<SymbolResolution> Res;
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000162 // FIXME: Workaround PR30396 which means that a symbol can appear
163 // more than once if it is defined in module-level assembly and
164 // has a GV declaration. Keep track of the resolutions found in this
165 // file and remove them from the CommandLineResolutions map afterwards,
166 // so that we don't flag the second one as missing.
167 std::map<std::string, SymbolResolution> CurrentFileSymResolutions;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000168 for (const InputFile::Symbol &Sym : Input->symbols()) {
169 auto I = CommandLineResolutions.find({F, Sym.getName()});
170 if (I == CommandLineResolutions.end()) {
171 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
172 << ',' << Sym.getName() << '\n';
173 HasErrors = true;
174 } else {
175 Res.push_back(I->second);
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000176 CurrentFileSymResolutions[Sym.getName()] = I->second;
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000177 }
178 }
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000179 for (auto I : CurrentFileSymResolutions) {
Teresa Johnson2f159a22016-10-12 20:06:02 +0000180#ifndef NDEBUG
181 auto NumErased =
182#endif
183 CommandLineResolutions.erase({F, I.first});
Teresa Johnson4b9b3792016-10-12 18:39:29 +0000184 assert(NumErased > 0);
185 }
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000186
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}