FindBugs.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//
  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 file defines an interface that allows bugpoint to choose different
  10. // combinations of optimizations to run on the selected input. Bugpoint will
  11. // run these optimizations and record the success/failure of each. This way
  12. // we can hopefully spot bugs in the optimizations.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "BugDriver.h"
  16. #include "llvm/Support/FileSystem.h"
  17. #include "llvm/Support/raw_ostream.h"
  18. #include <random>
  19. using namespace llvm;
  20. Error
  21. BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {
  22. setPassesToRun(AllPasses);
  23. outs() << "Starting bug finding procedure...\n\n";
  24. // Creating a reference output if necessary
  25. if (Error E = initializeExecutionEnvironment())
  26. return E;
  27. outs() << "\n";
  28. if (ReferenceOutputFile.empty()) {
  29. outs() << "Generating reference output from raw program: \n";
  30. if (Error E = createReferenceFile(*Program))
  31. return E;
  32. }
  33. std::mt19937 randomness(std::random_device{}());
  34. unsigned num = 1;
  35. while (true) {
  36. //
  37. // Step 1: Randomize the order of the optimizer passes.
  38. //
  39. llvm::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);
  40. //
  41. // Step 2: Run optimizer passes on the program and check for success.
  42. //
  43. outs() << "Running selected passes on program to test for crash: ";
  44. for (int i = 0, e = PassesToRun.size(); i != e; i++) {
  45. outs() << "-" << PassesToRun[i] << " ";
  46. }
  47. std::string Filename;
  48. if (runPasses(*Program, PassesToRun, Filename, false)) {
  49. outs() << "\n";
  50. outs() << "Optimizer passes caused failure!\n\n";
  51. return debugOptimizerCrash();
  52. } else {
  53. outs() << "Combination " << num << " optimized successfully!\n";
  54. }
  55. //
  56. // Step 3: Compile the optimized code.
  57. //
  58. outs() << "Running the code generator to test for a crash: ";
  59. if (Error E = compileProgram(*Program)) {
  60. outs() << "\n*** compileProgram threw an exception: ";
  61. outs() << toString(std::move(E));
  62. return debugCodeGeneratorCrash();
  63. }
  64. outs() << '\n';
  65. //
  66. // Step 4: Run the program and compare its output to the reference
  67. // output (created above).
  68. //
  69. outs() << "*** Checking if passes caused miscompliation:\n";
  70. Expected<bool> Diff = diffProgram(*Program, Filename, "", false);
  71. if (Error E = Diff.takeError()) {
  72. errs() << toString(std::move(E));
  73. return debugCodeGeneratorCrash();
  74. }
  75. if (*Diff) {
  76. outs() << "\n*** diffProgram returned true!\n";
  77. Error E = debugMiscompilation();
  78. if (!E)
  79. return Error::success();
  80. }
  81. outs() << "\n*** diff'd output matches!\n";
  82. sys::fs::remove(Filename);
  83. outs() << "\n\n";
  84. num++;
  85. } // end while
  86. // Unreachable.
  87. }