llvm-split.cpp 2.3 KB

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