Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 1 | //===-- llvm-split: command line tool for testing module splitter ---------===// |
| 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 can be used to test the llvm::SplitModule function. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/ADT/StringExtras.h" |
Teresa Johnson | a547919 | 2016-11-11 05:34:58 +0000 | [diff] [blame] | 15 | #include "llvm/Bitcode/BitcodeWriter.h" |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 16 | #include "llvm/IR/LLVMContext.h" |
Evgeniy Stepanov | 2b6ba77f | 2016-03-28 21:37:02 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Verifier.h" |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 18 | #include "llvm/IRReader/IRReader.h" |
| 19 | #include "llvm/Support/CommandLine.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | #include "llvm/Support/SourceMgr.h" |
| 22 | #include "llvm/Support/ToolOutputFile.h" |
| 23 | #include "llvm/Support/raw_ostream.h" |
| 24 | #include "llvm/Transforms/Utils/SplitModule.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | |
| 28 | static cl::opt<std::string> |
| 29 | InputFilename(cl::Positional, cl::desc("<input bitcode file>"), |
| 30 | cl::init("-"), cl::value_desc("filename")); |
| 31 | |
| 32 | static cl::opt<std::string> |
| 33 | OutputFilename("o", cl::desc("Override output filename"), |
| 34 | cl::value_desc("filename")); |
| 35 | |
| 36 | static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2), |
| 37 | cl::desc("Number of output files")); |
| 38 | |
Sergei Larin | 72b0e15 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 39 | static cl::opt<bool> |
| 40 | PreserveLocals("preserve-locals", cl::Prefix, cl::init(false), |
| 41 | cl::desc("Split without externalizing locals")); |
| 42 | |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 43 | int main(int argc, char **argv) { |
Mehdi Amini | 8be7707 | 2016-04-14 21:59:01 +0000 | [diff] [blame] | 44 | LLVMContext Context; |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 45 | SMDiagnostic Err; |
| 46 | cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n"); |
| 47 | |
| 48 | std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context); |
| 49 | |
| 50 | if (!M) { |
| 51 | Err.print(argv[0], errs()); |
| 52 | return 1; |
| 53 | } |
| 54 | |
| 55 | unsigned I = 0; |
| 56 | SplitModule(std::move(M), NumOutputs, [&](std::unique_ptr<Module> MPart) { |
| 57 | std::error_code EC; |
Reid Kleckner | 97ca964 | 2017-09-23 01:03:17 +0000 | [diff] [blame] | 58 | std::unique_ptr<ToolOutputFile> Out( |
| 59 | new ToolOutputFile(OutputFilename + utostr(I++), EC, sys::fs::F_None)); |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 60 | if (EC) { |
| 61 | errs() << EC.message() << '\n'; |
| 62 | exit(1); |
| 63 | } |
| 64 | |
Evgeniy Stepanov | 2b6ba77f | 2016-03-28 21:37:02 +0000 | [diff] [blame] | 65 | verifyModule(*MPart); |
Rafael Espindola | 06d6207 | 2018-02-14 19:11:32 +0000 | [diff] [blame] | 66 | WriteBitcodeToFile(*MPart, Out->os()); |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 67 | |
| 68 | // Declare success. |
| 69 | Out->keep(); |
Sergei Larin | 72b0e15 | 2016-01-18 21:07:13 +0000 | [diff] [blame] | 70 | }, PreserveLocals); |
Peter Collingbourne | c1e784c | 2015-08-21 02:48:20 +0000 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |