Path: blob/main/contrib/llvm-project/llvm/tools/bugpoint/bugpoint.cpp
35230 views
//===- bugpoint.cpp - The LLVM Bugpoint utility ---------------------------===//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 program is an automated compiler debugger tool. It is used to narrow9// down miscompilations and crash problems to a specific pass in the compiler,10// and the specific Module or Function input that is causing the problem.11//12//===----------------------------------------------------------------------===//1314#include "BugDriver.h"15#include "ToolRunner.h"16#include "llvm/Config/llvm-config.h"17#include "llvm/IR/LLVMContext.h"18#include "llvm/IR/LegacyPassManager.h"19#include "llvm/IR/LegacyPassNameParser.h"20#include "llvm/InitializePasses.h"21#include "llvm/LinkAllIR.h"22#include "llvm/LinkAllPasses.h"23#include "llvm/Passes/PassPlugin.h"24#include "llvm/Support/CommandLine.h"25#include "llvm/Support/InitLLVM.h"26#include "llvm/Support/PluginLoader.h"27#include "llvm/Support/PrettyStackTrace.h"28#include "llvm/Support/Process.h"29#include "llvm/Support/TargetSelect.h"30#include "llvm/Support/Valgrind.h"31#include "llvm/Transforms/IPO/AlwaysInliner.h"3233// Enable this macro to debug bugpoint itself.34//#define DEBUG_BUGPOINT 13536using namespace llvm;3738static cl::opt<bool>39FindBugs("find-bugs", cl::desc("Run many different optimization sequences "40"on program to find bugs"),41cl::init(false));4243static cl::list<std::string>44InputFilenames(cl::Positional, cl::OneOrMore,45cl::desc("<input llvm ll/bc files>"));4647static cl::opt<unsigned> TimeoutValue(48"timeout", cl::init(300), cl::value_desc("seconds"),49cl::desc("Number of seconds program is allowed to run before it "50"is killed (default is 300s), 0 disables timeout"));5152static cl::opt<int> MemoryLimit(53"mlimit", cl::init(-1), cl::value_desc("MBytes"),54cl::desc("Maximum amount of memory to use. 0 disables check. Defaults to "55"400MB (800MB under valgrind, 0 with sanitizers)."));5657static cl::opt<bool>58UseValgrind("enable-valgrind",59cl::desc("Run optimizations through valgrind"));6061// The AnalysesList is automatically populated with registered Passes by the62// PassNameParser.63//64static cl::list<const PassInfo *, bool, PassNameParser>65PassList(cl::desc("Passes available:"));6667static cl::opt<std::string>68OverrideTriple("mtriple", cl::desc("Override target triple for module"));6970/// BugpointIsInterrupted - Set to true when the user presses ctrl-c.71bool llvm::BugpointIsInterrupted = false;7273#ifndef DEBUG_BUGPOINT74static void BugpointInterruptFunction() { BugpointIsInterrupted = true; }75#endif7677// Hack to capture a pass list.78namespace {79class AddToDriver : public legacy::FunctionPassManager {80BugDriver &D;8182public:83AddToDriver(BugDriver &_D) : FunctionPassManager(nullptr), D(_D) {}8485void add(Pass *P) override {86const void *ID = P->getPassID();87const PassInfo *PI = PassRegistry::getPassRegistry()->getPassInfo(ID);88D.addPass(std::string(PI->getPassArgument()));89}90};91}9293#define HANDLE_EXTENSION(Ext) \94llvm::PassPluginLibraryInfo get##Ext##PluginInfo();95#include "llvm/Support/Extension.def"9697int main(int argc, char **argv) {98#ifndef DEBUG_BUGPOINT99InitLLVM X(argc, argv);100#endif101102// Initialize passes103PassRegistry &Registry = *PassRegistry::getPassRegistry();104initializeCore(Registry);105initializeScalarOpts(Registry);106initializeVectorization(Registry);107initializeIPO(Registry);108initializeAnalysis(Registry);109initializeTransformUtils(Registry);110initializeInstCombine(Registry);111initializeTarget(Registry);112113if (std::getenv("bar") == (char*) -1) {114InitializeAllTargets();115InitializeAllTargetMCs();116InitializeAllAsmPrinters();117InitializeAllAsmParsers();118}119120cl::ParseCommandLineOptions(argc, argv,121"LLVM automatic testcase reducer. See\nhttp://"122"llvm.org/cmds/bugpoint.html"123" for more information.\n");124#ifndef DEBUG_BUGPOINT125sys::SetInterruptFunction(BugpointInterruptFunction);126#endif127128LLVMContext Context;129// If we have an override, set it and then track the triple we want Modules130// to use.131if (!OverrideTriple.empty()) {132TargetTriple.setTriple(Triple::normalize(OverrideTriple));133outs() << "Override triple set to '" << TargetTriple.getTriple() << "'\n";134}135136if (MemoryLimit < 0) {137// Set the default MemoryLimit. Be sure to update the flag's description if138// you change this.139if (sys::RunningOnValgrind() || UseValgrind)140MemoryLimit = 800;141else142MemoryLimit = 400;143#if (LLVM_ADDRESS_SANITIZER_BUILD || LLVM_MEMORY_SANITIZER_BUILD || \144LLVM_THREAD_SANITIZER_BUILD)145// Starting from kernel 4.9 memory allocated with mmap is counted against146// RLIMIT_DATA. Sanitizers need to allocate tens of terabytes for shadow.147MemoryLimit = 0;148#endif149}150151BugDriver D(argv[0], FindBugs, TimeoutValue, MemoryLimit, UseValgrind,152Context);153if (D.addSources(InputFilenames))154return 1;155156AddToDriver PM(D);157158for (const PassInfo *PI : PassList)159D.addPass(std::string(PI->getPassArgument()));160161// Bugpoint has the ability of generating a plethora of core files, so to162// avoid filling up the disk, we prevent it163#ifndef DEBUG_BUGPOINT164sys::Process::PreventCoreFiles();165#endif166167// Needed to pull in symbols from statically linked extensions, including static168// registration. It is unused otherwise because bugpoint has no support for169// NewPM.170#define HANDLE_EXTENSION(Ext) \171(void)get##Ext##PluginInfo();172#include "llvm/Support/Extension.def"173174if (Error E = D.run()) {175errs() << toString(std::move(E));176return 1;177}178return 0;179}180181182