blob: 06c3d0406cfa31942cca2a5b206e2f735760f0ec [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
Teresa Johnson9ba95f92016-08-11 14:58:12 +000038static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
39
Mehdi Amini458f8052016-08-19 23:54:40 +000040static cl::opt<bool>
41 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
42 cl::desc("Write out individual index and "
43 "import files for the "
44 "distributed backend case"));
45
46static cl::opt<int> Threads("-thinlto-threads",
47 cl::init(thread::hardware_concurrency()));
48
Teresa Johnson9ba95f92016-08-11 14:58:12 +000049static cl::list<std::string> SymbolResolutions(
50 "r",
51 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
52 "where \"resolution\" is a sequence (which may be empty) of the\n"
53 "following characters:\n"
54 " p - prevailing: the linker has chosen this definition of the\n"
55 " symbol\n"
56 " l - local: the definition of this symbol is unpreemptable at\n"
57 " runtime and is known to be in this linkage unit\n"
58 " x - externally visible: the definition of this symbol is\n"
59 " visible outside of the LTO unit\n"
60 "A resolution for each symbol must be specified."),
61 cl::ZeroOrMore);
62
63static void check(Error E, std::string Msg) {
64 if (!E)
65 return;
66 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
67 errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n';
68 });
69 exit(1);
70}
71
72template <typename T> static T check(Expected<T> E, std::string Msg) {
73 if (E)
74 return std::move(*E);
75 check(E.takeError(), Msg);
76 return T();
77}
78
79static void check(std::error_code EC, std::string Msg) {
80 check(errorCodeToError(EC), Msg);
81}
82
83template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
84 if (E)
85 return std::move(*E);
86 check(E.getError(), Msg);
87 return T();
88}
89
Mehdi Amini970800e2016-08-17 06:23:09 +000090namespace {
91// Define the LTOOutput handling
92class LTOOutput : public lto::NativeObjectOutput {
Chandler Carruth5f6d73b2016-08-17 07:48:34 +000093 std::string Path;
Mehdi Amini970800e2016-08-17 06:23:09 +000094
95public:
Chandler Carruth5f6d73b2016-08-17 07:48:34 +000096 LTOOutput(std::string Path) : Path(std::move(Path)) {}
Mehdi Amini970800e2016-08-17 06:23:09 +000097 std::unique_ptr<raw_pwrite_stream> getStream() override {
98 std::error_code EC;
99 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
100 check(EC, Path);
101 return std::move(S);
102 }
103};
104}
105
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000106int main(int argc, char **argv) {
107 InitializeAllTargets();
108 InitializeAllTargetMCs();
109 InitializeAllAsmPrinters();
110 InitializeAllAsmParsers();
111
112 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
113
114 std::map<std::pair<std::string, std::string>, SymbolResolution>
115 CommandLineResolutions;
116 for (std::string R : SymbolResolutions) {
117 StringRef Rest = R;
118 StringRef FileName, SymbolName;
119 std::tie(FileName, Rest) = Rest.split(',');
120 if (Rest.empty()) {
121 llvm::errs() << "invalid resolution: " << R << '\n';
122 return 1;
123 }
124 std::tie(SymbolName, Rest) = Rest.split(',');
125 SymbolResolution Res;
126 for (char C : Rest) {
127 if (C == 'p')
128 Res.Prevailing = true;
129 else if (C == 'l')
130 Res.FinalDefinitionInLinkageUnit = true;
131 else if (C == 'x')
132 Res.VisibleToRegularObj = true;
133 else
134 llvm::errs() << "invalid character " << C << " in resolution: " << R
135 << '\n';
136 }
137 CommandLineResolutions[{FileName, SymbolName}] = Res;
138 }
139
140 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
141
142 Config Conf;
143 Conf.DiagHandler = [](const DiagnosticInfo &) {
144 exit(1);
145 };
146
147 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000148 check(Conf.addSaveTemps(OutputFilename + "."),
149 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000150
Mehdi Amini458f8052016-08-19 23:54:40 +0000151 ThinBackend Backend;
152 if (ThinLTODistributedIndexes)
153 Backend = createWriteIndexesThinBackend("", "", true, "");
154 else
155 Backend = createInProcessThinBackend(Threads);
156 LTO Lto(std::move(Conf), std::move(Backend));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000157
158 bool HasErrors = false;
159 for (std::string F : InputFilenames) {
160 std::unique_ptr<MemoryBuffer> MB = check(MemoryBuffer::getFile(F), F);
161 std::unique_ptr<InputFile> Input =
162 check(InputFile::create(MB->getMemBufferRef()), F);
163
164 std::vector<SymbolResolution> Res;
165 for (const InputFile::Symbol &Sym : Input->symbols()) {
166 auto I = CommandLineResolutions.find({F, Sym.getName()});
167 if (I == CommandLineResolutions.end()) {
168 llvm::errs() << argv[0] << ": missing symbol resolution for " << F
169 << ',' << Sym.getName() << '\n';
170 HasErrors = true;
171 } else {
172 Res.push_back(I->second);
173 CommandLineResolutions.erase(I);
174 }
175 }
176
177 if (HasErrors)
178 continue;
179
180 MBs.push_back(std::move(MB));
181 check(Lto.add(std::move(Input), Res), F);
182 }
183
184 if (!CommandLineResolutions.empty()) {
185 HasErrors = true;
186 for (auto UnusedRes : CommandLineResolutions)
187 llvm::errs() << argv[0] << ": unused symbol resolution for "
188 << UnusedRes.first.first << ',' << UnusedRes.first.second
189 << '\n';
190 }
191 if (HasErrors)
192 return 1;
193
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000194 auto AddOutput =
195 [&](size_t Task) -> std::unique_ptr<lto::NativeObjectOutput> {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000196 std::string Path = OutputFilename + "." + utostr(Task);
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000197 if (CacheDir.empty())
198 return llvm::make_unique<LTOOutput>(std::move(Path));
199
200 return llvm::make_unique<CacheObjectOutput>(
Teresa Johnson26a46282016-08-26 23:29:14 +0000201 CacheDir, [Path](std::string EntryPath) {
202 // Load the entry from the cache now.
203 auto ReloadedBufferOrErr = MemoryBuffer::getFile(EntryPath);
204 if (auto EC = ReloadedBufferOrErr.getError())
205 report_fatal_error(Twine("Can't reload cached file '") + EntryPath +
206 "': " + EC.message() + "\n");
207
208 *LTOOutput(Path).getStream() << (*ReloadedBufferOrErr)->getBuffer();
Mehdi Aminiadc0e262016-08-23 21:30:12 +0000209 });
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000210 };
211
Mehdi Amini970800e2016-08-17 06:23:09 +0000212 check(Lto.run(AddOutput), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000213}