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.
10 //
11 //===----------------------------------------------------------------------===//
12 
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/Transforms/Utils/SplitModule.h"
24 
25 using namespace llvm;
26 
27 static cl::OptionCategory SplitCategory("Split Options");
28 
29 static cl::opt<std::string> InputFilename(cl::Positional,
30                                           cl::desc("<input bitcode file>"),
31                                           cl::init("-"),
32                                           cl::value_desc("filename"),
33                                           cl::cat(SplitCategory));
34 
35 static cl::opt<std::string> OutputFilename("o",
36                                            cl::desc("Override output filename"),
37                                            cl::value_desc("filename"),
38                                            cl::cat(SplitCategory));
39 
40 static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
41                                     cl::desc("Number of output files"),
42                                     cl::cat(SplitCategory));
43 
44 static cl::opt<bool>
45     PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
46                    cl::desc("Split without externalizing locals"),
47                    cl::cat(SplitCategory));
48 
49 int main(int argc, char **argv) {
50   LLVMContext Context;
51   SMDiagnostic Err;
52   cl::HideUnrelatedOptions({&SplitCategory, &getColorCategory()});
53   cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
54 
55   std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
56 
57   if (!M) {
58     Err.print(argv[0], errs());
59     return 1;
60   }
61 
62   unsigned I = 0;
63   SplitModule(
64       *M, NumOutputs,
65       [&](std::unique_ptr<Module> MPart) {
66         std::error_code EC;
67         std::unique_ptr<ToolOutputFile> Out(new ToolOutputFile(
68             OutputFilename + utostr(I++), EC, sys::fs::OF_None));
69         if (EC) {
70           errs() << EC.message() << '\n';
71           exit(1);
72         }
73 
74         if (verifyModule(*MPart, &errs())) {
75           errs() << "Broken module!\n";
76           exit(1);
77         }
78 
79         WriteBitcodeToFile(*MPart, Out->os());
80 
81         // Declare success.
82         Out->keep();
83       },
84       PreserveLocals);
85 
86   return 0;
87 }
88