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/Path.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 CGDataAction Action;
71static std::optional<CGDataFormat> OutputFormat;
72static std::vector<std::string> InputFilenames;
73
74static void exitWithError(Twine Message, StringRef Whence = "",
75 StringRef Hint = "") {
76 WithColor::error();
77 if (!Whence.empty())
78 errs() << Whence << ": ";
79 errs() << Message << "\n";
80 if (!Hint.empty())
81 WithColor::note() << Hint << "\n";
82 ::exit(status: 1);
83}
84
85static void exitWithError(Error E, StringRef Whence = "") {
86 if (E.isA<CGDataError>()) {
87 handleAllErrors(E: std::move(E), Handlers: [&](const CGDataError &IPE) {
88 exitWithError(Message: IPE.message(), Whence);
89 });
90 return;
91 }
92
93 exitWithError(Message: toString(E: std::move(E)), Whence);
94}
95
96static void exitWithErrorCode(std::error_code EC, StringRef Whence = "") {
97 exitWithError(Message: EC.message(), Whence);
98}
99
100static int convert_main(int argc, const char *argv[]) {
101 std::error_code EC;
102 raw_fd_ostream OS(OutputFilename, EC,
103 OutputFormat == CGDataFormat::Text
104 ? sys::fs::OF_TextWithCRLF
105 : sys::fs::OF_None);
106 if (EC)
107 exitWithErrorCode(EC, Whence: OutputFilename);
108
109 auto FS = vfs::getRealFileSystem();
110 auto ReaderOrErr = CodeGenDataReader::create(Path: Filename, FS&: *FS);
111 if (Error E = ReaderOrErr.takeError())
112 exitWithError(E: std::move(E), Whence: Filename);
113
114 CodeGenDataWriter Writer;
115 auto Reader = ReaderOrErr->get();
116 if (Reader->hasOutlinedHashTree()) {
117 OutlinedHashTreeRecord Record(Reader->releaseOutlinedHashTree());
118 Writer.addRecord(Record);
119 }
120 if (Reader->hasStableFunctionMap()) {
121 StableFunctionMapRecord Record(Reader->releaseStableFunctionMap());
122 Writer.addRecord(Record);
123 }
124
125 if (OutputFormat == CGDataFormat::Text) {
126 if (Error E = Writer.writeText(OS))
127 exitWithError(E: std::move(E));
128 } else {
129 if (Error E = Writer.write(OS))
130 exitWithError(E: std::move(E));
131 }
132
133 return 0;
134}
135
136static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
137 OutlinedHashTreeRecord &GlobalOutlineRecord,
138 StableFunctionMapRecord &GlobalFunctionMapRecord);
139
140static bool handleArchive(StringRef Filename, Archive &Arch,
141 OutlinedHashTreeRecord &GlobalOutlineRecord,
142 StableFunctionMapRecord &GlobalFunctionMapRecord) {
143 bool Result = true;
144 Error Err = Error::success();
145 for (const auto &Child : Arch.children(Err)) {
146 auto BuffOrErr = Child.getMemoryBufferRef();
147 if (Error E = BuffOrErr.takeError())
148 exitWithError(E: std::move(E), Whence: Filename);
149 auto NameOrErr = Child.getName();
150 if (Error E = NameOrErr.takeError())
151 exitWithError(E: std::move(E), Whence: Filename);
152 std::string Name = (Filename + "(" + NameOrErr.get() + ")").str();
153 Result &= handleBuffer(Filename: Name, Buffer: BuffOrErr.get(), GlobalOutlineRecord,
154 GlobalFunctionMapRecord);
155 }
156 if (Err)
157 exitWithError(E: std::move(Err), Whence: Filename);
158 return Result;
159}
160
161static bool handleBuffer(StringRef Filename, MemoryBufferRef Buffer,
162 OutlinedHashTreeRecord &GlobalOutlineRecord,
163 StableFunctionMapRecord &GlobalFunctionMapRecord) {
164 Expected<std::unique_ptr<object::Binary>> BinOrErr =
165 object::createBinary(Source: Buffer);
166 if (Error E = BinOrErr.takeError())
167 exitWithError(E: std::move(E), Whence: Filename);
168
169 bool Result = true;
170 if (auto *Obj = dyn_cast<ObjectFile>(Val: BinOrErr->get())) {
171 if (Error E = CodeGenDataReader::mergeFromObjectFile(
172 Obj, GlobalOutlineRecord, GlobalFunctionMapRecord))
173 exitWithError(E: std::move(E), Whence: Filename);
174 } else if (auto *Arch = dyn_cast<Archive>(Val: BinOrErr->get())) {
175 Result &= handleArchive(Filename, Arch&: *Arch, GlobalOutlineRecord,
176 GlobalFunctionMapRecord);
177 } else {
178 // TODO: Support for the MachO universal binary format.
179 errs() << "Error: unsupported binary file: " << Filename << "\n";
180 Result = false;
181 }
182
183 return Result;
184}
185
186static bool handleFile(StringRef Filename,
187 OutlinedHashTreeRecord &GlobalOutlineRecord,
188 StableFunctionMapRecord &GlobalFunctionMapRecord) {
189 ErrorOr<std::unique_ptr<MemoryBuffer>> BuffOrErr =
190 MemoryBuffer::getFileOrSTDIN(Filename);
191 if (std::error_code EC = BuffOrErr.getError())
192 exitWithErrorCode(EC, Whence: Filename);
193 return handleBuffer(Filename, Buffer: *BuffOrErr.get(), GlobalOutlineRecord,
194 GlobalFunctionMapRecord);
195}
196
197static int merge_main(int argc, const char *argv[]) {
198 bool Result = true;
199 OutlinedHashTreeRecord GlobalOutlineRecord;
200 StableFunctionMapRecord GlobalFunctionMapRecord;
201 for (auto &Filename : InputFilenames)
202 Result &=
203 handleFile(Filename, GlobalOutlineRecord, GlobalFunctionMapRecord);
204
205 if (!Result)
206 exitWithError(Message: "failed to merge codegen data files.");
207
208 GlobalFunctionMapRecord.finalize(SkipTrim);
209
210 CodeGenDataWriter Writer;
211 if (!GlobalOutlineRecord.empty())
212 Writer.addRecord(Record&: GlobalOutlineRecord);
213 if (!GlobalFunctionMapRecord.empty())
214 Writer.addRecord(Record&: GlobalFunctionMapRecord);
215
216 std::error_code EC;
217 raw_fd_ostream OS(OutputFilename, EC,
218 OutputFormat == CGDataFormat::Text
219 ? sys::fs::OF_TextWithCRLF
220 : sys::fs::OF_None);
221 if (EC)
222 exitWithErrorCode(EC, Whence: OutputFilename);
223
224 if (OutputFormat == CGDataFormat::Text) {
225 if (Error E = Writer.writeText(OS))
226 exitWithError(E: std::move(E));
227 } else {
228 if (Error E = Writer.write(OS))
229 exitWithError(E: std::move(E));
230 }
231
232 return 0;
233}
234
235static int show_main(int argc, const char *argv[]) {
236 std::error_code EC;
237 raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::OF_TextWithCRLF);
238 if (EC)
239 exitWithErrorCode(EC, Whence: OutputFilename);
240
241 auto FS = vfs::getRealFileSystem();
242 auto ReaderOrErr = CodeGenDataReader::create(Path: Filename, FS&: *FS);
243 if (Error E = ReaderOrErr.takeError())
244 exitWithError(E: std::move(E), Whence: Filename);
245
246 auto Reader = ReaderOrErr->get();
247 if (ShowCGDataVersion)
248 OS << "Version: " << Reader->getVersion() << "\n";
249
250 if (Reader->hasOutlinedHashTree()) {
251 auto Tree = Reader->releaseOutlinedHashTree();
252 OS << "Outlined hash tree:\n";
253 OS << " Total Node Count: " << Tree->size() << "\n";
254 OS << " Terminal Node Count: " << Tree->size(/*GetTerminalCountOnly=*/true)
255 << "\n";
256 OS << " Depth: " << Tree->depth() << "\n";
257 }
258 if (Reader->hasStableFunctionMap()) {
259 auto Map = Reader->releaseStableFunctionMap();
260 OS << "Stable function map:\n";
261 OS << " Unique hash Count: " << Map->size() << "\n";
262 OS << " Total function Count: "
263 << Map->size(Type: StableFunctionMap::TotalFunctionCount) << "\n";
264 OS << " Mergeable function Count: "
265 << Map->size(Type: StableFunctionMap::MergeableFunctionCount) << "\n";
266 }
267
268 return 0;
269}
270
271static void parseArgs(int argc, char **argv) {
272 CGDataOptTable Tbl;
273 ToolName = argv[0];
274 llvm::BumpPtrAllocator A;
275 llvm::StringSaver Saver{A};
276 llvm::opt::InputArgList Args =
277 Tbl.parseArgs(Argc: argc, Argv: argv, Unknown: OPT_UNKNOWN, Saver, ErrorFn: [&](StringRef Msg) {
278 llvm::errs() << Msg << '\n';
279 std::exit(status: 1);
280 });
281
282 if (Args.hasArg(Ids: OPT_help)) {
283 Tbl.printHelp(
284 OS&: llvm::outs(),
285 Usage: "llvm-cgdata <action> [options] (<binary files>|<.cgdata file>)",
286 Title: ToolName.str().c_str());
287 std::exit(status: 0);
288 }
289 if (Args.hasArg(Ids: OPT_version)) {
290 cl::PrintVersionMessage();
291 std::exit(status: 0);
292 }
293
294 ShowCGDataVersion = Args.hasArg(Ids: OPT_cgdata_version);
295 SkipTrim = Args.hasArg(Ids: OPT_skip_trim);
296
297 if (opt::Arg *A = Args.getLastArg(Ids: OPT_format)) {
298 StringRef OF = A->getValue();
299 OutputFormat = StringSwitch<CGDataFormat>(OF)
300 .Case(S: "text", Value: CGDataFormat::Text)
301 .Case(S: "binary", Value: CGDataFormat::Binary)
302 .Default(Value: CGDataFormat::Invalid);
303 if (OutputFormat == CGDataFormat::Invalid)
304 exitWithError(Message: "unsupported format '" + OF + "'");
305 }
306
307 InputFilenames = Args.getAllArgValues(Id: OPT_INPUT);
308 if (InputFilenames.empty())
309 exitWithError(Message: "No input file is specified.");
310 Filename = InputFilenames[0];
311
312 if (Args.hasArg(Ids: OPT_output)) {
313 OutputFilename = Args.getLastArgValue(Id: OPT_output);
314 for (auto &Filename : InputFilenames)
315 if (Filename == OutputFilename)
316 exitWithError(
317 Message: "Input file name cannot be the same as the output file name!\n");
318 }
319
320 opt::Arg *ActionArg = nullptr;
321 for (opt::Arg *Arg : Args.filtered(Ids: OPT_action_group)) {
322 if (ActionArg)
323 exitWithError(Message: "Only one action is allowed.");
324 ActionArg = Arg;
325 }
326 if (!ActionArg)
327 exitWithError(Message: "One action is required.");
328
329 switch (ActionArg->getOption().getID()) {
330 case OPT_show:
331 if (InputFilenames.size() != 1)
332 exitWithError(Message: "only one input file is allowed.");
333 Action = CGDataAction::Show;
334 break;
335 case OPT_convert:
336 // The default output format is text for convert.
337 if (!OutputFormat)
338 OutputFormat = CGDataFormat::Text;
339 if (InputFilenames.size() != 1)
340 exitWithError(Message: "only one input file is allowed.");
341 Action = CGDataAction::Convert;
342 break;
343 case OPT_merge:
344 // The default output format is binary for merge.
345 if (!OutputFormat)
346 OutputFormat = CGDataFormat::Binary;
347 Action = CGDataAction::Merge;
348 break;
349 default:
350 llvm_unreachable("unrecognized action");
351 }
352
353 IndexedCodeGenDataLazyLoading =
354 Args.hasArg(Ids: OPT_indexed_codegen_data_lazy_loading);
355}
356
357int llvm_cgdata_main(int argc, char **argvNonConst, const llvm::ToolContext &) {
358 const char **argv = const_cast<const char **>(argvNonConst);
359 parseArgs(argc, argv: argvNonConst);
360
361 switch (Action) {
362 case CGDataAction::Convert:
363 return convert_main(argc, argv);
364 case CGDataAction::Merge:
365 return merge_main(argc, argv);
366 case CGDataAction::Show:
367 return show_main(argc, argv);
368 }
369
370 llvm_unreachable("unrecognized action");
371}
372