llvm-split.cpp 2.9 KB

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