llvm-reduce.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //===- llvm-reduce.cpp - The LLVM Delta Reduction utility -----------------===//
  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 tries to reduce an IR test case for a given interesting-ness
  10. // test. It runs multiple delta debugging passes in order to minimize the input
  11. // file. It's worth noting that this is a part of the bugpoint redesign
  12. // proposal, and thus a *temporary* tool that will eventually be integrated
  13. // into the bugpoint tool itself.
  14. //
  15. //===----------------------------------------------------------------------===//
  16. #include "DeltaManager.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/IR/LLVMContext.h"
  19. #include "llvm/IR/Verifier.h"
  20. #include "llvm/IRReader/IRReader.h"
  21. #include "llvm/Support/CommandLine.h"
  22. #include "llvm/Support/InitLLVM.h"
  23. #include "llvm/Support/SourceMgr.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include <system_error>
  26. #include <vector>
  27. using namespace llvm;
  28. static cl::OptionCategory Options("llvm-reduce options");
  29. static cl::opt<bool> Help("h", cl::desc("Alias for -help"), cl::Hidden,
  30. cl::cat(Options));
  31. static cl::opt<bool> Version("v", cl::desc("Alias for -version"), cl::Hidden,
  32. cl::cat(Options));
  33. static cl::opt<std::string> InputFilename(cl::Positional, cl::Required,
  34. cl::desc("<input llvm ll/bc file>"),
  35. cl::cat(Options));
  36. static cl::opt<std::string>
  37. TestFilename("test", cl::Required,
  38. cl::desc("Name of the interesting-ness test to be run"),
  39. cl::cat(Options));
  40. static cl::list<std::string>
  41. TestArguments("test-arg", cl::ZeroOrMore,
  42. cl::desc("Arguments passed onto the interesting-ness test"),
  43. cl::cat(Options));
  44. static cl::opt<std::string>
  45. OutputFilename("output",
  46. cl::desc("Specify the output file. default: reduced.ll"));
  47. static cl::alias OutputFileAlias("o", cl::desc("Alias for -output"),
  48. cl::aliasopt(OutputFilename),
  49. cl::cat(Options));
  50. static cl::opt<bool>
  51. ReplaceInput("in-place",
  52. cl::desc("WARNING: This option will replace your input file "
  53. "with the reduced version!"),
  54. cl::cat(Options));
  55. // Parses IR into a Module and verifies it
  56. static std::unique_ptr<Module> parseInputFile(StringRef Filename,
  57. LLVMContext &Ctxt) {
  58. SMDiagnostic Err;
  59. std::unique_ptr<Module> Result = parseIRFile(Filename, Err, Ctxt);
  60. if (!Result) {
  61. Err.print("llvm-reduce", errs());
  62. return Result;
  63. }
  64. if (verifyModule(*Result, &errs())) {
  65. errs() << "Error: " << Filename << " - input module is broken!\n";
  66. return std::unique_ptr<Module>();
  67. }
  68. return Result;
  69. }
  70. void writeOutput(Module *M, StringRef Message) {
  71. if (ReplaceInput) // In-place
  72. OutputFilename = InputFilename.c_str();
  73. else if (OutputFilename.empty() || OutputFilename == "-")
  74. OutputFilename = "reduced.ll";
  75. std::error_code EC;
  76. raw_fd_ostream Out(OutputFilename, EC);
  77. if (EC) {
  78. errs() << "Error opening output file: " << EC.message() << "!\n";
  79. exit(1);
  80. }
  81. M->print(Out, /*AnnotationWriter=*/nullptr);
  82. errs() << Message << OutputFilename << "\n";
  83. }
  84. int main(int argc, char **argv) {
  85. InitLLVM X(argc, argv);
  86. cl::ParseCommandLineOptions(argc, argv, "LLVM automatic testcase reducer.\n");
  87. LLVMContext Context;
  88. std::unique_ptr<Module> OriginalProgram =
  89. parseInputFile(InputFilename, Context);
  90. // Initialize test environment
  91. TestRunner Tester(TestFilename, TestArguments);
  92. Tester.setProgram(std::move(OriginalProgram));
  93. // Try to reduce code
  94. runDeltaPasses(Tester);
  95. if (!Tester.getProgram()) {
  96. errs() << "\nCouldnt reduce input :/\n";
  97. } else {
  98. // Print reduced file to STDOUT
  99. if (OutputFilename == "-")
  100. Tester.getProgram()->print(outs(), nullptr);
  101. else
  102. writeOutput(Tester.getProgram(), "\nDone reducing! Reduced testcase: ");
  103. }
  104. return 0;
  105. }