[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / llvm / tools / llvm-split / llvm-split.cpp
blobc6e20e0373c71781806b174833d1cc4c241fecfd
1 //===-- llvm-split: command line tool for testing module splitter ---------===//
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 // This program can be used to test the llvm::SplitModule function.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ADT/StringExtras.h"
14 #include "llvm/Bitcode/BitcodeWriter.h"
15 #include "llvm/IR/LLVMContext.h"
16 #include "llvm/IR/Verifier.h"
17 #include "llvm/IRReader/IRReader.h"
18 #include "llvm/Support/CommandLine.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "llvm/Support/ToolOutputFile.h"
22 #include "llvm/Support/raw_ostream.h"
23 #include "llvm/Support/WithColor.h"
24 #include "llvm/Transforms/Utils/SplitModule.h"
26 using namespace llvm;
28 static cl::OptionCategory SplitCategory("Split Options");
30 static cl::opt<std::string> InputFilename(cl::Positional,
31 cl::desc("<input bitcode file>"),
32 cl::init("-"),
33 cl::value_desc("filename"),
34 cl::cat(SplitCategory));
36 static cl::opt<std::string> OutputFilename("o",
37 cl::desc("Override output filename"),
38 cl::value_desc("filename"),
39 cl::cat(SplitCategory));
41 static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
42 cl::desc("Number of output files"),
43 cl::cat(SplitCategory));
45 static cl::opt<bool>
46 PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
47 cl::desc("Split without externalizing locals"),
48 cl::cat(SplitCategory));
50 int main(int argc, char **argv) {
51 LLVMContext Context;
52 SMDiagnostic Err;
53 cl::HideUnrelatedOptions({&SplitCategory, &getColorCategory()});
54 cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
56 std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
58 if (!M) {
59 Err.print(argv[0], errs());
60 return 1;
63 unsigned I = 0;
64 SplitModule(
65 *M, NumOutputs,
66 [&](std::unique_ptr<Module> MPart) {
67 std::error_code EC;
68 std::unique_ptr<ToolOutputFile> Out(new ToolOutputFile(
69 OutputFilename + utostr(I++), EC, sys::fs::OF_None));
70 if (EC) {
71 errs() << EC.message() << '\n';
72 exit(1);
75 if (verifyModule(*MPart, &errs())) {
76 errs() << "Broken module!\n";
77 exit(1);
80 WriteBitcodeToFile(*MPart, Out->os());
82 // Declare success.
83 Out->keep();
85 PreserveLocals);
87 return 0;