-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Clean up strange uses of getAnalysisIfAvailable #65729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
After a pass calls addRequired<X>() it is strange to call getAnalysisIfAvailable<X>() because analysis X should always be available. Use getAnalysis<X>() instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
GVN seems to have different behavior regarding LoopInfo in the LegacyPM and NewPM, but that's an orthogonal issue. I'll check whether always requiring LoopInfo in GVN has any compile-time impact, as I don't think this is supposed to be an optional analysis there (otherwise loop load PRE is not going to work reliably).
@llvm/pr-subscribers-llvm-transforms Author: Jay Foad (jayfoad) ChangesAfter a pass calls addRequired<X>() it is strange to call Full diff: https://github.com/llvm/llvm-project/pull/65729.diff 5 Files Affected:
diff --git a/llvm/lib/CodeGen/EarlyIfConversion.cpp b/llvm/lib/CodeGen/EarlyIfConversion.cpp
index 61867d74bfa293c..3086d953a23699f 100644
--- a/llvm/lib/CodeGen/EarlyIfConversion.cpp
+++ b/llvm/lib/CodeGen/EarlyIfConversion.cpp
@@ -1092,7 +1092,7 @@ bool EarlyIfConverter::runOnMachineFunction(MachineFunction &MF) {
SchedModel = STI.getSchedModel();
MRI = &MF.getRegInfo();
DomTree = &getAnalysis<MachineDominatorTree>();
- Loops = getAnalysisIfAvailable<MachineLoopInfo>();
+ Loops = &getAnalysis<MachineLoopInfo>();
Traces = &getAnalysis<MachineTraceMetrics>();
MinInstr = nullptr;
@@ -1226,7 +1226,7 @@ bool EarlyIfPredicator::runOnMachineFunction(MachineFunction &MF) {
MRI = &MF.getRegInfo();
SchedModel.init(&STI);
DomTree = &getAnalysis<MachineDominatorTree>();
- Loops = getAnalysisIfAvailable<MachineLoopInfo>();
+ Loops = &getAnalysis<MachineLoopInfo>();
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
bool Changed = false;
diff --git a/llvm/lib/CodeGen/TypePromotion.cpp b/llvm/lib/CodeGen/TypePromotion.cpp
index 51f77e5fd8b08af..053caf518bd1f78 100644
--- a/llvm/lib/CodeGen/TypePromotion.cpp
+++ b/llvm/lib/CodeGen/TypePromotion.cpp
@@ -1016,11 +1016,8 @@ bool TypePromotionLegacy::runOnFunction(Function &F) {
if (skipFunction(F))
return false;
- auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
- if (!TPC)
- return false;
-
- auto *TM = &TPC->getTM<TargetMachine>();
+ auto &TPC = getAnalysis<TargetPassConfig>();
+ auto *TM = &TPC.getTM<TargetMachine>();
auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
diff --git a/llvm/lib/CodeGen/VirtRegMap.cpp b/llvm/lib/CodeGen/VirtRegMap.cpp
index c1c6ce227334a4b..6e29a41b617a87e 100644
--- a/llvm/lib/CodeGen/VirtRegMap.cpp
+++ b/llvm/lib/CodeGen/VirtRegMap.cpp
@@ -261,7 +261,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) {
Indexes = &getAnalysis<SlotIndexes>();
LIS = &getAnalysis<LiveIntervals>();
VRM = &getAnalysis<VirtRegMap>();
- DebugVars = getAnalysisIfAvailable<LiveDebugVariables>();
+ DebugVars = &getAnalysis<LiveDebugVariables>();
LLVM_DEBUG(dbgs() << "********** REWRITE VIRTUAL REGISTERS **********\n"
<< "********** Function: " << MF->getName() << '\n');
LLVM_DEBUG(VRM->dump());
diff --git a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
index 4c8c03a4c693fe1..ad0ff8335647419 100644
--- a/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp
@@ -935,7 +935,7 @@ bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) {
SchedModel = MF.getSubtarget().getSchedModel();
MRI = &MF.getRegInfo();
DomTree = &getAnalysis<MachineDominatorTree>();
- Loops = getAnalysisIfAvailable<MachineLoopInfo>();
+ Loops = &getAnalysis<MachineLoopInfo>();
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
Traces = &getAnalysis<MachineTraceMetrics>();
MinInstr = nullptr;
diff --git a/llvm/lib/Transforms/Scalar/GVN.cpp b/llvm/lib/Transforms/Scalar/GVN.cpp
index 4c5b0ff3af16e53..f419d03c0ce3837 100644
--- a/llvm/lib/Transforms/Scalar/GVN.cpp
+++ b/llvm/lib/Transforms/Scalar/GVN.cpp
@@ -3286,7 +3286,7 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
if (skipFunction(F))
return false;
- auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>();
+ auto &LIWP = getAnalysis<LoopInfoWrapperPass>();
auto *MSSAWP = getAnalysisIfAvailable<MemorySSAWrapperPass>();
return Impl.runImpl(
@@ -3297,7 +3297,7 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass {
Impl.isMemDepEnabled()
? &getAnalysis<MemoryDependenceWrapperPass>().getMemDep()
: nullptr,
- LIWP ? &LIWP->getLoopInfo() : nullptr,
+ &LIWP.getLoopInfo(),
&getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE(),
MSSAWP ? &MSSAWP->getMSSA() : nullptr);
}
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I don't know how pull requests and reviewing works in github. I actually added 3 comments on this patch a several days (or weeks) ago. But turns out that they were "pending" because I had only "started review" and not found the place to "submit review".
So sorry for some late comments here. Since this has been pushed already, then I guess you may ignore them.
@@ -261,7 +261,7 @@ bool VirtRegRewriter::runOnMachineFunction(MachineFunction &fn) { | |||
Indexes = &getAnalysis<SlotIndexes>(); | |||
LIS = &getAnalysis<LiveIntervals>(); | |||
VRM = &getAnalysis<VirtRegMap>(); | |||
DebugVars = getAnalysisIfAvailable<LiveDebugVariables>(); | |||
DebugVars = &getAnalysis<LiveDebugVariables>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure how to add a comment on line 278 if I want to suggest an edit on that line (in Phabricator it would have been easy). But I guess the check if DebugVars is null or not on line 278 is redundant now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Fixed in 05c16f4
@@ -935,7 +935,7 @@ bool AArch64ConditionalCompares::runOnMachineFunction(MachineFunction &MF) { | |||
SchedModel = MF.getSubtarget().getSchedModel(); | |||
MRI = &MF.getRegInfo(); | |||
DomTree = &getAnalysis<MachineDominatorTree>(); | |||
Loops = getAnalysisIfAvailable<MachineLoopInfo>(); | |||
Loops = &getAnalysis<MachineLoopInfo>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are some "weird" things going on here. But maybe not worth digging into those unless being an AArch64 maintainer.
Afaict the goal here is to make sure MachineLoopInfo is preserved. So this pass shouldn't really need to require it explicitly. However, the MachineTraceMetrics pass is also using MachineLoopInfo transitively. So, the MachineLoopInfo analysis will be run. Although I think there is a "bug" in MachineTraceMetrics related to not using addRequiredTransitive. So things might break if removing the addRequired in this pass.
To sum up. The proposed change is probably harmless and OK. Just saying that the analysis pass dependencies used in here seem to be a bit ad-hoc(?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything seems to work if I change AArch64ConditionalCompares
to not require MachineLoopInfo
. But I think I'll leave it to the AArch64 maintainers like you suggest.
@@ -3280,7 +3280,7 @@ class llvm::gvn::GVNLegacyPass : public FunctionPass { | |||
if (skipFunction(F)) | |||
return false; | |||
|
|||
auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>(); | |||
auto &LIWP = getAnalysis<LoopInfoWrapperPass>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IIUC GVN doesn't depend on LoopInfo itself. It should just make sure to preserve it if it is available. So one might wonder if it is the addRequire that should be questioned and possibly removed instead.
But it's been like this for years (that it is required), and messing around with the legacy PM analysis dependencies for an IR pass (even if it is used by some backends) is perhaps not worth it.
Therefore I think you proposed change is ok. Or you could just leave this as is, since the IfAvailable part in some sense indicate that the pass itself doesn't depend on the analysis.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not to leave it as is - I think either we should get the analysis with getAnalysis
or we should remove the addRequired
, so that they are consistent with each other.
For that reason I usually click "add single comment" instead of "start a review". |
I've checked, and LoopInfo is always available in pipeline positions where GVN runs, so this effectively has no impact. I've made it a required analysis for the NewPM as well in fa58847. |
Thanks. I'll try to remember that. If they for example had named the buttons "Post comment directly" and "Save as draft", then it has been more obvious. |
After a pass calls addRequired() it is strange to call
getAnalysisIfAvailable() because analysis X should always be
available. Use getAnalysis() instead.