Skip to content

Commit d0c3b61

Browse files
author
Krzysztof Parzyszek
committed
Delay initialization of OptBisect
When LLVM is used in other projects, it may happen that global cons- tructors will execute before the call to ParseCommandLineOptions. Since OptBisect is initialized via a constructor, and has no ability to be updated at a later time, passing "-opt-bisect-limit" to the parse function may have no effect. To avoid this problem use a cl::cb (callback) to set the bisection limit when the option is actually processed. Differential Revision: https://reviews.llvm.org/D104551
1 parent 1172a8a commit d0c3b61

File tree

2 files changed

+26
-18
lines changed

2 files changed

+26
-18
lines changed

llvm/include/llvm/IR/OptBisect.h

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Support/ManagedStatic.h"
19+
#include <limits>
1920

2021
namespace llvm {
2122

@@ -43,14 +44,12 @@ class OptPassGate {
4344
/// optimization-related problems.
4445
class OptBisect : public OptPassGate {
4546
public:
46-
/// Default constructor, initializes the OptBisect state based on the
47-
/// -opt-bisect-limit command line argument.
48-
///
49-
/// By default, bisection is disabled.
50-
///
47+
/// Default constructor. Initializes the state to "disabled". The bisection
48+
/// will be enabled by the cl::opt call-back when the command line option
49+
/// is processed.
5150
/// Clients should not instantiate this class directly. All access should go
5251
/// through LLVMContext.
53-
OptBisect();
52+
OptBisect() = default;
5453

5554
virtual ~OptBisect() = default;
5655

@@ -60,7 +59,14 @@ class OptBisect : public OptPassGate {
6059
bool shouldRunPass(const Pass *P, StringRef IRDescription) override;
6160

6261
/// isEnabled() should return true before calling shouldRunPass().
63-
bool isEnabled() const override { return BisectEnabled; }
62+
bool isEnabled() const override { return BisectLimit != Disabled; }
63+
64+
/// Set the new optimization limit and reset the counter. Passing
65+
/// OptBisect::Disabled disables the limiting.
66+
void setLimit(int Limit) {
67+
BisectLimit = Limit;
68+
LastBisectNum = 0;
69+
}
6470

6571
/// Checks the bisect limit to determine if the specified pass should run.
6672
///
@@ -75,9 +81,11 @@ class OptBisect : public OptPassGate {
7581
/// instance, function passes should call FunctionPass::skipFunction().
7682
bool checkPass(const StringRef PassName, const StringRef TargetDesc);
7783

84+
static const int Disabled = std::numeric_limits<int>::max();
85+
7886
private:
79-
bool BisectEnabled = false;
80-
unsigned LastBisectNum = 0;
87+
int BisectLimit = Disabled;
88+
int LastBisectNum = 0;
8189
};
8290

8391
/// Singleton instance of the OptBisect class, so multiple pass managers don't

llvm/lib/IR/OptBisect.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
using namespace llvm;
2323

2424
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
25-
cl::init(std::numeric_limits<int>::max()),
26-
cl::Optional,
25+
cl::init(OptBisect::Disabled), cl::Optional,
26+
cl::cb<void, int>([](int Limit) {
27+
llvm::OptBisector->setLimit(Limit);
28+
}),
2729
cl::desc("Maximum optimization to perform"));
2830

29-
OptBisect::OptBisect() : OptPassGate() {
30-
BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
31-
}
32-
3331
static void printPassMessage(const StringRef &Name, int PassNum,
3432
StringRef TargetDesc, bool Running) {
3533
StringRef Status = Running ? "" : "NOT ";
@@ -38,19 +36,21 @@ static void printPassMessage(const StringRef &Name, int PassNum,
3836
}
3937

4038
bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
41-
assert(BisectEnabled);
39+
assert(isEnabled());
4240

4341
return checkPass(P->getPassName(), IRDescription);
4442
}
4543

4644
bool OptBisect::checkPass(const StringRef PassName,
4745
const StringRef TargetDesc) {
48-
assert(BisectEnabled);
46+
assert(isEnabled());
4947

5048
int CurBisectNum = ++LastBisectNum;
51-
bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
49+
bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
5250
printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
5351
return ShouldRun;
5452
}
5553

54+
const int OptBisect::Disabled;
55+
5656
ManagedStatic<OptBisect> llvm::OptBisector;

0 commit comments

Comments
 (0)