1//===-- llvm-cgdata.cpp - LLVM CodeGen Data Tool --------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// llvm-cgdata parses raw codegen data embedded in compiled binary files, and
10// merges them into a single .cgdata file. It can also inspect and maninuplate
11// a .cgdata file. This .cgdata can contain various codegen data like outlining
12// information, and it can be used to optimize the code in the subsequent build.
13//
14//===----------------------------------------------------------------------===//
15#include "llvm/ADT/StringRef.h"
16#include "llvm/CGData/CodeGenDataReader.h"
17#include "llvm/CGData/CodeGenDataWriter.h"
18#include "llvm/IR/LLVMContext.h"
19#include "llvm/Object/Archive.h"
20#include "llvm/Object/Binary.h"
21#include "llvm/Option/ArgList.h"
22#include "llvm/Option/Option.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/Driver.h"
25#include "llvm/Support/LibraryOptionsContext.h"
26#include "llvm/Support/VirtualFileSystem.h"
27#include "llvm/Support/WithColor.h"
28#include "llvm/Support/raw_ostream.h"
29
30using namespace llvm;
31using namespace llvm::object;
32
33enum CGDataFormat {
34 Invalid,
35 Text,
36 Binary,
37};
38
39enum CGDataAction {
40 Convert,
41 Merge,
42 Show,
43};
44
45// Command-line option boilerplate.
46namespace {
47enum ID {
48 OPT_INVALID = 0, // This is not an option ID.
49#define OPTION(...) LLVM_MAKE_OPT_ID(__VA_ARGS__),
50#include "Opts.inc"
51#undef OPTION
52};
53
54using namespace llvm::opt;
55#define OPTTABLE_CODE
56#include "Opts.inc"
57
58class CGDataOptTable : public opt::OptTable {
59public:
60 CGDataOptTable() : OptTable(optionTables()) {}
61};
62} // end anonymous namespace
63
64// Options
65static StringRef ToolName;
66static std::string OutputFilename = "-";
67static std::string Filename;
68static bool ShowCGDataVersion;
69static bool SkipTrim;
70static bool LazyLoading;
71static CGDataAction Action;
72static std::optional<CGDataFormat> OutputFormat;
73static std::vector<std::string> InputFilenames;
74
75static void exitWithError(Twine Message, StringRef Whence = "",
76 StringRef Hint = "") {
77 WithColor::error();
78 if (!Whence.empty())
79 errs() << Whence << ": ";
80 errs() << Message << "\n";
81 if (!Hint.empty())
82 WithColor::note() << Hint << "\n";
83 ::exit(status: 1);
84}
85
86static void exitWithError(Error E, StringRef Whence = "") {
87 if (E.isA<CGDataError>()) {
88 handleAllErrors(E: std::move(E), Handlers: [&](const CGDataError &IPE) {
89 exitWithError(Message: IPE.message(), Whence);
90 });
91 return;
92 }
93
94 exitWithError(Message: toString(E: std::move(E)), Whence);
95}
96
97static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
98 exitWithError(Message: EC.message(), Whence);
99}
100
101static int convert_main(int argc, const char *argv[]) {
102 std::error_code EC;
103 raw_fd_ostream OS(OutputFilename, EC,
104 OutputFormat == CGDataFormat::Text
105 ? sys::fs::OF_TextWithCRLF
106 : sys::fs::OF_None);
107 if (EC)
108 exitWithErrorCode(EC, Whence: OutputFilename);
109
110 auto FS = vfs::getRealFileSystem();
111 auto ReaderOrErr = CodeGenDataReader::create(LibraryOptions: cl::LibraryOptionsContext(),
112 Path: Filename, FS&: *FS, LazyLoading);
113 if (Error E = ReaderOrErr.takeError())
114 exitWithError(E: std::move(E), Whence: Filename);
115
116 CodeGenDataWriter Writer;
117 auto Reader = ReaderOrErr->get();
118 if (Reader->hasOutlinedHashTree()) {
119 OutlinedHashTreeRecord Record(Reader->releaseOutlinedHashTree());
120 Writer.addRecord(Record);
121 }
122 if (Reader->hasStableFunctionMap()) {
123 StableFunctionMapRecord Record(Reader->releaseStableFunctionMap());
124 Writer.addRecord(Record);
125 }
126
127 if (OutputFormat == CGDataFormat::Text) {
128 if (Error E = Writer.writeText(OS))
129 exitWithError(E: std::move(E));
130 } else {
131 if (Error E = Writer.write(OS))
132 exitWithError(E: std::move(E));
133 }
134
135 return 0;
136}
137
138static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
139 OutlinedHashTreeRecord &GlobalOutlineRecord,
140 StableFunctionMapRecord &GlobalFunctionMapRecord);
141
142static bool handleArchive(StringRef Filename, Archive &Arch,
143 OutlinedHashTreeRecord &GlobalOutlineRecord,
144 StableFunctionMapRecord &GlobalFunctionMapRecord) {
145 bool Result = true;
146 Error Err = Error::success();
147 for (const auto &Child : Arch.children(Err)) {
148 auto BuffOrErr = Child.getMemoryBufferRef();
149 if (Error E = BuffOrErr.takeError())
150 exitWithError(E: std::move(E), Whence: Filename);
151 auto NameOrErr = Child.getName();
152 if (Error E = NameOrErr.takeError())
153 exitWithError(E: std::move(E), Whence: Filename);
154 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
155 Result &= handleBuffer(Filename: Name, Buffer: BuffOrErr.get(), GlobalOutlineRecord,
156 GlobalFunctionMapRecord);
157 }
158 if (Err)
159 exitWithError(E: std::move(Err), Whence: Filename);
160 return Result;
161}
162
163static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
164 OutlinedHashTreeRecord &GlobalOutlineRecord,
165 StableFunctionMapRecord &GlobalFunctionMapRecord) {
166 Expected<std::unique_ptr<object::Binary>> BinOrErr =
167 object::createBinary(Source: Buffer);
168 if (Error E = BinOrErr.takeError())
169 exitWithError(E: std::move(E), Whence: Filename);
170
171 bool Result = true;
172 if (auto *Obj = dyn_cast<ObjectFile>(Val: BinOrErr->get())) {
173 if (Error E = CodeGenDataReader::mergeFromObjectFile(
174 Obj, GlobalOutlineRecord, GlobalFunctionMapRecord))
175 exitWithError(E: std::move(E), Whence: Filename);
176 } else if (auto *Arch = dyn_cast<Archive>(Val: BinOrErr->get())) {
177 Result &= handleArchive(Filename, Arch&: *Arch, GlobalOutlineRecord,
178 GlobalFunctionMapRecord);
179 } else {
180 // TODO: Support for the MachO universal binary format.
181 errs() << "Error: unsupported binary file: " << Filename << "\n";
182 Result = false;
183 }
184
185 return Result;
186}
187
188static bool handleFile(StringRef Filename,
189 OutlinedHashTreeRecord &GlobalOutlineRecord,
190 StableFunctionMapRecord &GlobalFunctionMapRecord) {
191 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
192 MemoryBuffer::getFileOrSTDIN(Filename);
193 if (std::error_code EC = BuffOrErr.getError())
194 exitWithErrorCode(EC, Whence: Filename);
195 return handleBuffer(Filename, Buffer: *BuffOrErr.get(), GlobalOutlineRecord,
196 GlobalFunctionMapRecord);
197}
198
199static int merge_main(int argc, const char *argv[]) {
200 bool Result = true;
201 OutlinedHashTreeRecord GlobalOutlineRecord;
202 StableFunctionMapRecord GlobalFunctionMapRecord;
203 for (auto &Filename : InputFilenames)
204 Result &=
205 handleFile(Filename, GlobalOutlineRecord, GlobalFunctionMapRecord);
206
207 if (!Result)
208 exitWithError(Message: "failed to merge codegen data files.");
209
210 GlobalFunctionMapRecord.finalize(LibraryOptions: cl::LibraryOptionsContext(), SkipTrim);
211
212 CodeGenDataWriter Writer;
213 if (!GlobalOutlineRecord.empty())
214 Writer.addRecord(Record&: GlobalOutlineRecord);
215 if (!GlobalFunctionMapRecord.empty())
216 Writer.addRecord(Record&: GlobalFunctionMapRecord);
217
218 std::error_code EC;
219 raw_fd_ostream OS(OutputFilename, EC,
220 OutputFormat == CGDataFormat::Text
221 ? sys::fs::OF_TextWithCRLF
222 : sys::fs::OF_None);
223 if (EC)
224 exitWithErrorCode(EC, Whence: OutputFilename);
225
226 if (OutputFormat == CGDataFormat::Text) {
227 if (Error E = Writer.writeText(OS))
228 exitWithError(E: std::move(E));
229 } else {
230 if (Error E = Writer.write(OS))
231 exitWithError(E: std::move(E));
232 }
233
234 return 0;
235}
236
237static int show_main(int argc, const char *argv[]) {
238 std::error_code EC;
239 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
240 if (EC)
241 exitWithErrorCode(EC, Whence: OutputFilename);
242
243 auto FS = vfs::getRealFileSystem();
244 auto ReaderOrErr = CodeGenDataReader::create(LibraryOptions: cl::LibraryOptionsContext(),
245 Path: Filename, FS&: *FS, LazyLoading);
246 if (Error E = ReaderOrErr.takeError())
247 exitWithError(E: std::move(E), Whence: Filename);
248
249 auto Reader = ReaderOrErr->get();
250 if (ShowCGDataVersion)
251 OS << "Version: " << Reader->getVersion() << "\n";
252
253 if (Reader->hasOutlinedHashTree()) {
254 auto Tree = Reader->releaseOutlinedHashTree();
255 OS << "Outlined hash tree:\n";
256 OS << " Total Node Count: " << Tree->size() << "\n";
257 OS << " Terminal Node Count: " << Tree->size(/*GetTerminalCountOnly=*/true)
258 << "\n";
259 OS << " Depth: " << Tree->depth() << "\n";
260 }
261 if (Reader->hasStableFunctionMap()) {
262 auto Map = Reader->releaseStableFunctionMap();
263 OS << "Stable function map:\n";
264 OS << " Unique hash Count: " << Map->size() << "\n";
265 OS << " Total function Count: "
266 << Map->size(Type: StableFunctionMap::TotalFunctionCount) << "\n";
267 OS << " Mergeable function Count: "
268 << Map->size(Type: StableFunctionMap::MergeableFunctionCount) << "\n";
269 }
270
271 return 0;
272}
273
274static void parseArgs(int argc, char **argv) {
275 CGDataOptTable Tbl;
276 ToolName = argv[0];
277 llvm::BumpPtrAllocator A;
278 llvm::StringSaver Saver{A};
279 llvm::opt::InputArgList Args =
280 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
281 llvm::errs() << Msg << '\n';
282 std::exit(status: 1);
283 });
284
285 if (Args.hasArg(Ids: OPT_help)) {
286 Tbl.printHelp(
287 OS&: llvm::outs(),
288 Usage: "llvm-cgdata <action> [options] (<binary files>|<.cgdata file>)",
289 Title: ToolName.str().c_str());
290 std::exit(status: 0);
291 }
292 if (Args.hasArg(Ids: OPT_version)) {
293 cl::PrintVersionMessage();
294 std::exit(status: 0);
295 }
296
297 ShowCGDataVersion = Args.hasArg(Ids: OPT_cgdata_version);
298 SkipTrim = Args.hasArg(Ids: OPT_skip_trim);
299
300 if (opt::Arg *A = Args.getLastArg(Ids: OPT_format)) {
301 StringRef OF = A->getValue();
302 OutputFormat = StringSwitch<CGDataFormat>(OF)
303 .Case(S: "text", Value: CGDataFormat::Text)
304 .Case(S: "binary", Value: CGDataFormat::Binary)
305 .Default(Value: CGDataFormat::Invalid);
306 if (OutputFormat == CGDataFormat::Invalid)
307 exitWithError(Message: "unsupported format '" + OF + "'");
308 }
309
310 InputFilenames = Args.getAllArgValues(Id: OPT_INPUT);
311 if (InputFilenames.empty())
312 exitWithError(Message: "No input file is specified.");
313 Filename = InputFilenames[0];
314
315 if (Args.hasArg(Ids: OPT_output)) {
316 OutputFilename = Args.getLastArgValue(Id: OPT_output);
317 for (auto &Filename : InputFilenames)
318 if (Filename == OutputFilename)
319 exitWithError(
320 Message: "Input file name cannot be the same as the output file name!\n");
321 }
322
323 opt::Arg *ActionArg = nullptr;
324 for (opt::Arg *Arg : Args.filtered(Ids: OPT_action_group)) {
325 if (ActionArg)
326 exitWithError(Message: "Only one action is allowed.");
327 ActionArg = Arg;
328 }
329 if (!ActionArg)
330 exitWithError(Message: "One action is required.");
331
332 switch (ActionArg->getOption().getID()) {
333 case OPT_show:
334 if (InputFilenames.size() != 1)
335 exitWithError(Message: "only one input file is allowed.");
336 Action = CGDataAction::Show;
337 break;
338 case OPT_convert:
339 // The default output format is text for convert.
340 if (!OutputFormat)
341 OutputFormat = CGDataFormat::Text;
342 if (InputFilenames.size() != 1)
343 exitWithError(Message: "only one input file is allowed.");
344 Action = CGDataAction::Convert;
345 break;
346 case OPT_merge:
347 // The default output format is binary for merge.
348 if (!OutputFormat)
349 OutputFormat = CGDataFormat::Binary;
350 Action = CGDataAction::Merge;
351 break;
352 default:
353 llvm_unreachable("unrecognized action");
354 }
355
356 LazyLoading = Args.hasArg(Ids: OPT_indexed_codegen_data_lazy_loading);
357}
358
359int llvm_cgdata_main(int argc, char **argvNonConst, const llvm::ToolContext &) {
360 const char **argv = const_cast<const char **>(argvNonConst);
361 parseArgs(argc, argv: argvNonConst);
362
363 switch (Action) {
364 case CGDataAction::Convert:
365 return convert_main(argc, argv);
366 case CGDataAction::Merge:
367 return merge_main(argc, argv);
368 case CGDataAction::Show:
369 return show_main(argc, argv);
370 }
371
372 llvm_unreachable("unrecognized action");
373}
374