LLVM 22.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 static OptDisable OptDisabler;
33 return OptDisabler;
34}
35
37 "opt-bisect-limit", cl::Hidden, cl::init(-1), cl::Optional,
38 cl::cb<void, int>([](int Limit) {
39 if (Limit == -1)
40 // -1 means run all passes.
41 getOptBisector().setIntervals({{1, std::numeric_limits<int>::max()}});
42 else if (Limit == 0)
43 // 0 means run no passes.
44 getOptBisector().setIntervals({{0, 0}});
45 else if (Limit > 0)
46 // Convert limit to interval 1-Limit.
47 getOptBisector().setIntervals({{1, Limit}});
48 else
50 ("Invalid limit for -opt-bisect-limit: " + llvm::utostr(Limit))
51 .c_str());
52 }),
54 "Maximum optimization to perform (equivalent to -opt-bisect=1-N)"));
55
57 "opt-bisect", cl::Hidden, cl::Optional,
58 cl::cb<void, const std::string &>([](const std::string &IntervalStr) {
59 if (IntervalStr == "-1") {
60 // -1 means run all passes.
61 getOptBisector().setIntervals({{1, std::numeric_limits<int>::max()}});
62 return;
63 }
64
65 auto Intervals =
67 if (!Intervals) {
68 handleAllErrors(Intervals.takeError(), [&](const StringError &E) {
69 errs() << "Error: Invalid interval specification for -opt-bisect: "
70 << IntervalStr << " (" << E.getMessage() << ")\n";
71 });
72 exit(1);
73 }
74 getOptBisector().setIntervals(std::move(*Intervals));
75 }),
76 cl::desc("Run optimization passes only for the specified intervals. "
77 "Format: '1-10,20-30,45' runs passes 1-10, 20-30, and 45, where "
78 "index 1 is the first pass. Supply '0' to run no passes and -1 to "
79 "run all passes."));
80
82 "opt-bisect-verbose",
83 cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden,
84 cl::init(true), cl::Optional);
85
88 cl::cb<void, std::string>([](const std::string &Pass) {
90 }),
91 cl::desc("Optimization pass(es) to disable (comma-separated list)"));
92
93static cl::opt<bool>
94 OptDisableVerbose("opt-disable-enable-verbosity",
95 cl::desc("Show verbose output when opt-disable is set"),
97
98static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc,
99 bool Running) {
100 StringRef Status = Running ? "" : "NOT ";
101 errs() << "BISECT: " << Status << "running pass (" << PassNum << ") " << Name
102 << " on " << TargetDesc << '\n';
103}
104
106 StringRef IRDescription) const {
107 assert(isEnabled());
108
109 int CurBisectNum = ++LastBisectNum;
110
111 // Check if current pass number falls within any of the specified intervals.
112 bool ShouldRun =
113 IntegerInclusiveIntervalUtils::contains(BisectIntervals, CurBisectNum);
114
116 printPassMessage(PassName, CurBisectNum, IRDescription, ShouldRun);
117 return ShouldRun;
118}
119
120static void printDisablePassMessage(const StringRef &Name, StringRef TargetDesc,
121 bool Running) {
122 StringRef Status = Running ? "" : "NOT ";
123 dbgs() << "OptDisable: " << Status << "running pass " << Name << " on "
124 << TargetDesc << "\n";
125}
126
127void OptDisable::setDisabled(StringRef Pass) { DisabledPasses.insert(Pass); }
128
130 StringRef IRDescription) const {
131 assert(isEnabled());
132
133 const bool ShouldRun = !DisabledPasses.contains(PassName);
135 printDisablePassMessage(PassName, IRDescription, ShouldRun);
136 return ShouldRun;
137}
138
140 if (getOptDisabler().isEnabled())
141 return getOptDisabler();
142 return getOptBisector();
143}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void printPassMessage(StringRef Name, int PassNum, StringRef TargetDesc, bool Running)
Definition OptBisect.cpp:98
static cl::opt< bool > OptBisectVerbose("opt-bisect-verbose", cl::desc("Show verbose output when opt-bisect-limit is set"), cl::Hidden, cl::init(true), cl::Optional)
static cl::opt< bool > OptDisableVerbose("opt-disable-enable-verbosity", cl::desc("Show verbose output when opt-disable is set"), cl::Hidden, cl::init(false), cl::Optional)
static void printDisablePassMessage(const StringRef &Name, StringRef TargetDesc, bool Running)
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)"))
static OptDisable & getOptDisabler()
Definition OptBisect.cpp:31
static cl::list< std::string > OptDisablePasses("opt-disable", cl::Hidden, cl::CommaSeparated, cl::Optional, cl::cb< void, std::string >([](const std::string &Pass) { getOptDisabler().setDisabled(Pass);}), cl::desc("Optimization pass(es) to disable (comma-separated list)"))
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:45
bool shouldRunPass(StringRef PassName, StringRef IRDescription) const override
Checks the bisect intervals to determine if the specified pass should run.
void setIntervals(IntegerInclusiveIntervalUtils::IntervalList Intervals)
Set intervals directly from an IntervalList.
Definition OptBisect.h:73
bool isEnabled() const override
isEnabled() should return true before calling shouldRunPass().
Definition OptBisect.h:70
This class implements a mechanism to disable passes and individual optimizations at compile time base...
Definition OptBisect.h:92
bool shouldRunPass(StringRef PassName, StringRef IRDescription) const override
Checks the pass name to determine if the specified pass should run.
bool isEnabled() const override
isEnabled() should return true before calling shouldRunPass().
Definition OptBisect.h:112
void setDisabled(StringRef Pass)
Parses the command line argument to extract the names of the passes to be disabled.
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.
bool contains(ArrayRef< IntegerInclusiveInterval > Intervals, int64_t Value)
Check if a value is contained in any of the intervals.
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_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.