aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Parzyszek <kparzysz@quicinc.com>2021-06-18 12:24:03 -0500
committerKrzysztof Parzyszek <kparzysz@quicinc.com>2021-08-05 09:04:17 -0500
commitd0c3b61498ecc04c9d95f6af7eb7560727500d56 (patch)
treef50d46474fbcdcd5372feeef0c06d528e3da856d
parent1172a8a7639399fe0b8a6c78a7123b1c3f9cf833 (diff)
downloadllvm-d0c3b61498ecc04c9d95f6af7eb7560727500d56.zip
llvm-d0c3b61498ecc04c9d95f6af7eb7560727500d56.tar.gz
llvm-d0c3b61498ecc04c9d95f6af7eb7560727500d56.tar.bz2
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
-rw-r--r--llvm/include/llvm/IR/OptBisect.h26
-rw-r--r--llvm/lib/IR/OptBisect.cpp18
2 files changed, 26 insertions, 18 deletions
diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h
index 6c2a1b0..63fd980 100644
--- a/llvm/include/llvm/IR/OptBisect.h
+++ b/llvm/include/llvm/IR/OptBisect.h
@@ -16,6 +16,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ManagedStatic.h"
+#include <limits>
namespace llvm {
@@ -43,14 +44,12 @@ public:
/// optimization-related problems.
class OptBisect : public OptPassGate {
public:
- /// Default constructor, initializes the OptBisect state based on the
- /// -opt-bisect-limit command line argument.
- ///
- /// By default, bisection is disabled.
- ///
+ /// Default constructor. Initializes the state to "disabled". The bisection
+ /// will be enabled by the cl::opt call-back when the command line option
+ /// is processed.
/// Clients should not instantiate this class directly. All access should go
/// through LLVMContext.
- OptBisect();
+ OptBisect() = default;
virtual ~OptBisect() = default;
@@ -60,7 +59,14 @@ public:
bool shouldRunPass(const Pass *P, StringRef IRDescription) override;
/// isEnabled() should return true before calling shouldRunPass().
- bool isEnabled() const override { return BisectEnabled; }
+ bool isEnabled() const override { return BisectLimit != Disabled; }
+
+ /// Set the new optimization limit and reset the counter. Passing
+ /// OptBisect::Disabled disables the limiting.
+ void setLimit(int Limit) {
+ BisectLimit = Limit;
+ LastBisectNum = 0;
+ }
/// Checks the bisect limit to determine if the specified pass should run.
///
@@ -75,9 +81,11 @@ public:
/// instance, function passes should call FunctionPass::skipFunction().
bool checkPass(const StringRef PassName, const StringRef TargetDesc);
+ static const int Disabled = std::numeric_limits<int>::max();
+
private:
- bool BisectEnabled = false;
- unsigned LastBisectNum = 0;
+ int BisectLimit = Disabled;
+ int LastBisectNum = 0;
};
/// Singleton instance of the OptBisect class, so multiple pass managers don't
diff --git a/llvm/lib/IR/OptBisect.cpp b/llvm/lib/IR/OptBisect.cpp
index 2cf2298..55c0dba 100644
--- a/llvm/lib/IR/OptBisect.cpp
+++ b/llvm/lib/IR/OptBisect.cpp
@@ -22,14 +22,12 @@
using namespace llvm;
static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
- cl::init(std::numeric_limits<int>::max()),
- cl::Optional,
+ cl::init(OptBisect::Disabled), cl::Optional,
+ cl::cb<void, int>([](int Limit) {
+ llvm::OptBisector->setLimit(Limit);
+ }),
cl::desc("Maximum optimization to perform"));
-OptBisect::OptBisect() : OptPassGate() {
- BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
-}
-
static void printPassMessage(const StringRef &Name, int PassNum,
StringRef TargetDesc, bool Running) {
StringRef Status = Running ? "" : "NOT ";
@@ -38,19 +36,21 @@ static void printPassMessage(const StringRef &Name, int PassNum,
}
bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
- assert(BisectEnabled);
+ assert(isEnabled());
return checkPass(P->getPassName(), IRDescription);
}
bool OptBisect::checkPass(const StringRef PassName,
const StringRef TargetDesc) {
- assert(BisectEnabled);
+ assert(isEnabled());
int CurBisectNum = ++LastBisectNum;
- bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
+ bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
return ShouldRun;
}
+const int OptBisect::Disabled;
+
ManagedStatic<OptBisect> llvm::OptBisector;