llvm-split.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #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. using namespace llvm;
  25. static cl::opt<std::string>
  26. InputFilename(cl::Positional, cl::desc("<input bitcode file>"),
  27. cl::init("-"), cl::value_desc("filename"));
  28. static cl::opt<std::string>
  29. OutputFilename("o", cl::desc("Override output filename"),
  30. cl::value_desc("filename"));
  31. static cl::opt<unsigned> NumOutputs("j", cl::Prefix, cl::init(2),
  32. cl::desc("Number of output files"));
  33. static cl::opt<bool>
  34. PreserveLocals("preserve-locals", cl::Prefix, cl::init(false),
  35. cl::desc("Split without externalizing locals"));
  36. int main(int argc, char **argv) {
  37. LLVMContext Context;
  38. SMDiagnostic Err;
  39. cl::ParseCommandLineOptions(argc, argv, "LLVM module splitter\n");
  40. std::unique_ptr<Module> M = parseIRFile(InputFilename, Err, Context);
  41. if (!M) {
  42. Err.print(argv[0], errs());
  43. return 1;
  44. }
  45. unsigned I = 0;
  46. SplitModule(std::move(M), NumOutputs, [&](std::unique_ptr<Module> MPart) {
  47. std::error_code EC;
  48. std::unique_ptr<ToolOutputFile> Out(
  49. new ToolOutputFile(OutputFilename + utostr(I++), EC, sys::fs::F_None));
  50. if (EC) {
  51. errs() << EC.message() << '\n';
  52. exit(1);
  53. }
  54. verifyModule(*MPart);
  55. WriteBitcodeToFile(MPart.get(), Out->os());
  56. // Declare success.
  57. Out->keep();
  58. }, PreserveLocals);
  59. return 0;
  60. }