Path: blob/main/contrib/llvm-project/llvm/tools/bugpoint/FindBugs.cpp
35230 views
//===-- FindBugs.cpp - Run Many Different Optimizations -------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file defines an interface that allows bugpoint to choose different9// combinations of optimizations to run on the selected input. Bugpoint will10// run these optimizations and record the success/failure of each. This way11// we can hopefully spot bugs in the optimizations.12//13//===----------------------------------------------------------------------===//1415#include "BugDriver.h"16#include "llvm/Support/FileSystem.h"17#include "llvm/Support/raw_ostream.h"18#include <random>19using namespace llvm;2021Error22BugDriver::runManyPasses(const std::vector<std::string> &AllPasses) {23setPassesToRun(AllPasses);24outs() << "Starting bug finding procedure...\n\n";2526// Creating a reference output if necessary27if (Error E = initializeExecutionEnvironment())28return E;2930outs() << "\n";31if (ReferenceOutputFile.empty()) {32outs() << "Generating reference output from raw program: \n";33if (Error E = createReferenceFile(*Program))34return E;35}3637std::mt19937 randomness(std::random_device{}());38unsigned num = 1;39while (true) {40//41// Step 1: Randomize the order of the optimizer passes.42//43llvm::shuffle(PassesToRun.begin(), PassesToRun.end(), randomness);4445//46// Step 2: Run optimizer passes on the program and check for success.47//48outs() << "Running selected passes on program to test for crash: ";49for (int i = 0, e = PassesToRun.size(); i != e; i++) {50outs() << "-" << PassesToRun[i] << " ";51}5253std::string Filename;54if (runPasses(*Program, PassesToRun, Filename, false)) {55outs() << "\n";56outs() << "Optimizer passes caused failure!\n\n";57return debugOptimizerCrash();58} else {59outs() << "Combination " << num << " optimized successfully!\n";60}6162//63// Step 3: Compile the optimized code.64//65outs() << "Running the code generator to test for a crash: ";66if (Error E = compileProgram(*Program)) {67outs() << "\n*** compileProgram threw an exception: ";68outs() << toString(std::move(E));69return debugCodeGeneratorCrash();70}71outs() << '\n';7273//74// Step 4: Run the program and compare its output to the reference75// output (created above).76//77outs() << "*** Checking if passes caused miscompliation:\n";78Expected<bool> Diff = diffProgram(*Program, Filename, "", false);79if (Error E = Diff.takeError()) {80errs() << toString(std::move(E));81return debugCodeGeneratorCrash();82}83if (*Diff) {84outs() << "\n*** diffProgram returned true!\n";85Error E = debugMiscompilation();86if (!E)87return Error::success();88}89outs() << "\n*** diff'd output matches!\n";9091sys::fs::remove(Filename);9293outs() << "\n\n";94num++;95} // end while9697// Unreachable.98}99100101