blob: b2a83213bb8090d4d903488319ae27ccaa99d4a3 [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
19#include "llvm/LTO/LTO.h"
20#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/TargetSelect.h"
22
23using namespace llvm;
24using namespace lto;
25using namespace object;
26
27static cl::list<std::string> InputFilenames(cl::Positional, cl::OneOrMore,
28 cl::desc("<input bitcode files>"));
29
30static cl::opt<std::string> OutputFilename("o", cl::Required,
31 cl::desc("Output filename"),
32 cl::value_desc("filename"));
33
34static cl::opt<bool> SaveTemps("save-temps", cl::desc("Save temporary files"));
35
Mehdi Amini458f8052016-08-19 23:54:40 +000036static cl::opt<bool>
37 ThinLTODistributedIndexes("thinlto-distributed-indexes", cl::init(false),
38 cl::desc("Write out individual index and "
39 "import files for the "
40 "distributed backend case"));
41
42static cl::opt<int> Threads("-thinlto-threads",
43 cl::init(thread::hardware_concurrency()));
44
Teresa Johnson9ba95f92016-08-11 14:58:12 +000045static cl::list<std::string> SymbolResolutions(
46 "r",
47 cl::desc("Specify a symbol resolution: filename,symbolname,resolution\n"
48 "where \"resolution\" is a sequence (which may be empty) of the\n"
49 "following characters:\n"
50 " p - prevailing: the linker has chosen this definition of the\n"
51 " symbol\n"
52 " l - local: the definition of this symbol is unpreemptable at\n"
53 " runtime and is known to be in this linkage unit\n"
54 " x - externally visible: the definition of this symbol is\n"
55 " visible outside of the LTO unit\n"
56 "A resolution for each symbol must be specified."),
57 cl::ZeroOrMore);
58
59static void check(Error E, std::string Msg) {
60 if (!E)
61 return;
62 handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
63 errs() << "llvm-lto: " << Msg << ": " << EIB.message().c_str() << '\n';
64 });
65 exit(1);
66}
67
68template <typename T> static T check(Expected<T> E, std::string Msg) {
69 if (E)
70 return std::move(*E);
71 check(E.takeError(), Msg);
72 return T();
73}
74
75static void check(std::error_code EC, std::string Msg) {
76 check(errorCodeToError(EC), Msg);
77}
78
79template <typename T> static T check(ErrorOr<T> E, std::string Msg) {
80 if (E)
81 return std::move(*E);
82 check(E.getError(), Msg);
83 return T();
84}
85
Mehdi Amini970800e2016-08-17 06:23:09 +000086namespace {
87// Define the LTOOutput handling
88class LTOOutput : public lto::NativeObjectOutput {
Chandler Carruth5f6d73b2016-08-17 07:48:34 +000089 std::string Path;
Mehdi Amini970800e2016-08-17 06:23:09 +000090
91public:
Chandler Carruth5f6d73b2016-08-17 07:48:34 +000092 LTOOutput(std::string Path) : Path(std::move(Path)) {}
Mehdi Amini970800e2016-08-17 06:23:09 +000093 std::unique_ptr<raw_pwrite_stream> getStream() override {
94 std::error_code EC;
95 auto S = llvm::make_unique<raw_fd_ostream>(Path, EC, sys::fs::F_None);
96 check(EC, Path);
97 return std::move(S);
98 }
99};
100}
101
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000102int main(int argc, char **argv) {
103 InitializeAllTargets();
104 InitializeAllTargetMCs();
105 InitializeAllAsmPrinters();
106 InitializeAllAsmParsers();
107
108 cl::ParseCommandLineOptions(argc, argv, "Resolution-based LTO test harness");
109
110 std::map<std::pair<std::string, std::string>, SymbolResolution>
111 CommandLineResolutions;
112 for (std::string R : SymbolResolutions) {
113 StringRef Rest = R;
114 StringRef FileName, SymbolName;
115 std::tie(FileName, Rest) = Rest.split(',');
116 if (Rest.empty()) {
117 llvm::errs() << "invalid resolution: " << R << '\n';
118 return 1;
119 }
120 std::tie(SymbolName, Rest) = Rest.split(',');
121 SymbolResolution Res;
122 for (char C : Rest) {
123 if (C == 'p')
124 Res.Prevailing = true;
125 else if (C == 'l')
126 Res.FinalDefinitionInLinkageUnit = true;
127 else if (C == 'x')
128 Res.VisibleToRegularObj = true;
129 else
130 llvm::errs() << "invalid character " << C << " in resolution: " << R
131 << '\n';
132 }
133 CommandLineResolutions[{FileName, SymbolName}] = Res;
134 }
135
136 std::vector<std::unique_ptr<MemoryBuffer>> MBs;
137
138 Config Conf;
139 Conf.DiagHandler = [](const DiagnosticInfo &) {
140 exit(1);
141 };
142
143 if (SaveTemps)
Mehdi Aminieccffad2016-08-18 00:12:33 +0000144 check(Conf.addSaveTemps(OutputFilename + "."),
145 "Config::addSaveTemps failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +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
Mehdi Amini970800e2016-08-17 06:23:09 +0000190 auto AddOutput = [&](size_t Task) {
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000191 std::string Path = OutputFilename + "." + utostr(Task);
Chandler Carruth5f6d73b2016-08-17 07:48:34 +0000192 return llvm::make_unique<LTOOutput>(std::move(Path));
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000193 };
194
Mehdi Amini970800e2016-08-17 06:23:09 +0000195 check(Lto.run(AddOutput), "LTO::run failed");
Teresa Johnson9ba95f92016-08-11 14:58:12 +0000196}