LLVM 23.0.0git
OptBisect.cpp
Go to the documentation of this file.
1//===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
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/// \file
10/// This file implements support for a bisecting optimizations based on a
11/// command line option.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/IR/OptBisect.h"
17#include "llvm/Pass.h"
21#include <cassert>
22#include <cstdlib>
23
24using namespace llvm;
25
27 static OptBisect OptBisector;
28 return OptBisector;
29}
30
32 "opt-bisect-limit", cl::Hidden, cl::init(-1), cl::Optional,
33 cl::cb<void, int>([](int Limit) {
34 if (Limit == -1)
35 // -1 means run all passes.
36 getOptBisector().setIntervals({{1, std::numeric_limits<int>::max()}});
37 else if (Limit == 0)
38 // 0 means run no passes.
39 getOptBisector().setIntervals({{0, 0}});
40 else if (Limit > 0)
41 // Convert limit to interval 1-Limit.
42 getOptBisector().setIntervals({{1, Limit}});
43 else
45 ("Invalid limit for -opt-bisect-limit: " + llvm::utostr(Limit))
46 .c_str());
47 }),
49 "Maximum optimization to perform (equivalent to -opt-bisect=1-N)"));
50
52 "opt-bisect", cl::Hidden, cl::Optional,
53 cl::cb<void, const std::string &>([](const std::string &IntervalStr) {
54 if (IntervalStr == "-1") {
55 // -1 means run all passes.
56 getOptBisector().setIntervals({{1, std::numeric_limits<int>::max()}});
57 return;
58 }
59
60 auto Intervals =
62 if (!Intervals) {
63 handleAllErrors(Intervals.takeError(), [&](const StringError &E) {
64 errs() << "Error: Invalid interval specification for -opt-bisect: "
65 << IntervalStr << " (" << E.getMessage() << ")\n";
66 });
67 exit(1);
68 }
69 getOptBisector().setIntervals(std::move(*Intervals));
70 }),
71 cl::desc("Run optimization passes only for the specified intervals. "
72 "Format: '1-10,20-30,45' runs passes 1-10, 20-30, and 45, where "
73 "index 1 is the first pass. Supply '0' to run no passes and -1 to "
74 "run all passes."));
75
77 "opt-bisect-verbose",
79 "Show verbose output when opt-bisect-limit and/or opt-disable are set"),
81
84 cl::cb<void, std::string>([](const std::string &Pass) {
86 }),
87 cl::desc("Optimization pass(es) to disable (comma-separated list)"));
88
89static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc,
90 bool Running) {
91 StringRef Status = Running ? "" : "NOT ";
92 errs() << "BISECT: " << Status << "running pass (" << PassNum << ") " << Name
93 << " on " << TargetDesc << '\n';
94}
95
97 StringRef IRDescription) const {
99
100 int CurBisectNum = ++LastBisectNum;
101
102 // Check if current pass number falls within any of the specified intervals.
103 // Since the bisector may be enabled by opt-disable, we also need to check if
104 // the BisectIntervals are empty.
105 bool ShouldRun =
106 BisectIntervals.empty() ||
107 IntegerInclusiveIntervalUtils::contains(BisectIntervals, CurBisectNum);
108
109 // Also check if the pass is disabled via -opt-disable.
110 ShouldRun = ShouldRun && !DisabledPasses.contains(PassName);
111
113 printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
114 return ShouldRun;
115}
116
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static cl::opt< bool > OptBisectVerbose("opt-bisect-verbose", cl::desc("Show verbose output when opt-bisect-limit and/or opt-disable are set"), cl::Hidden, cl::init(true), cl::Optional)
static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc, bool Running)
Definition OptBisect.cpp:89
static cl::list< std::string > OptDisablePasses("opt-disable", cl::Hidden, cl::CommaSeparated, cl::Optional, cl::cb< void, std::string >([](const std::string &Pass) { getOptBisector().setDisabled(Pass);}), cl::desc("Optimization pass(es) to disable (comma-separated list)"))
static cl::opt< std::string > OptBisectIntervals("opt-bisect", cl::Hidden, cl::Optional, cl::cb< void, const std::string & >([](const std::string &IntervalStr) { if(IntervalStr=="-1") { getOptBisector().setIntervals({{1, std::numeric_limits< int >::max()}});return;} auto Intervals=IntegerInclusiveIntervalUtils::parseIntervals(IntervalStr);if(!Intervals) { handleAllErrors(Intervals.takeError(), [&](const StringError &E) { errs()<< "Error: Invalid interval specification for -opt-bisect: "<< IntervalStr<< " ("<< E.getMessage()<< ")\n";});exit(1);} getOptBisector().setIntervals(std::move(*Intervals));}), cl::desc("Run optimization passes only for the specified intervals. " "Format: '1-10,20-30,45' runs passes 1-10, 20-30, and 45, where " "index 1 is the first pass. Supply '0' to run no passes and -1 to " "run all passes."))
static OptBisect & getOptBisector()
Definition OptBisect.cpp:26
static cl::opt< int > OptBisectLimit("opt-bisect-limit", cl::Hidden, cl::init(-1), cl::Optional, cl::cb< void, int >([](int Limit) { if(Limit==-1) getOptBisector().setIntervals({{1, std::numeric_limits< int >::max()}});else if(Limit==0) getOptBisector().setIntervals({{0, 0}});else if(Limit > 0) getOptBisector().setIntervals({{1, Limit}});else llvm_unreachable(("Invalid limit for -opt-bisect-limit: "+llvm::utostr(Limit)) .c_str());}), cl::desc("Maximum optimization to perform (equivalent to -opt-bisect=1-N)"))
This file declares the interface for bisecting optimizations.
This file contains some functions that are useful when dealing with strings.
static const char PassName[]
This class implements a mechanism to disable passes and individual optimizations at compile time base...
Definition OptBisect.h:46
void setDisabled(StringRef Pass)
Parses the command line argument to extract the names of the passes to be disabled.
Definition OptBisect.h:88
bool shouldRunPass(StringRef PassName, StringRef IRDescription) const override
Checks the bisect intervals to determine if the specified pass should run.
Definition OptBisect.cpp:96
void setIntervals(IntegerInclusiveIntervalUtils::IntervalList Intervals)
Set intervals directly from an IntervalList.
Definition OptBisect.h:76
bool isEnabled() const override
isEnabled() should return true before calling shouldRunPass().
Definition OptBisect.h:71
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition OptBisect.h:26
Pass interface - Implemented by all 'passes'.
Definition Pass.h:99
This class wraps a string in an Error.
Definition Error.h:1282
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI bool contains(ArrayRef< IntegerInclusiveInterval > Intervals, int64_t Value)
Check if a value is contained in any of the intervals.
LLVM_ABI Expected< IntervalList > parseIntervals(StringRef IntervalStr, char Separator=',')
Parse a interval specification string like "1-10,20-30,45" or "1-10:20-30:45".
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition Error.h:990
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
LLVM_ABI OptPassGate & getGlobalPassGate()
Singleton instance of the OptPassGate class, so multiple pass managers don't need to coordinate their...
std::string utostr(uint64_t X, bool isNeg=false)
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.