Skip to content

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

Merged
merged 2 commits into from
Oct 11, 2023
Merged

Clean up strange uses of getAnalysisIfAvailable #65729

merged 2 commits into from
Oct 11, 2023

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Sep 8, 2023

After a pass calls addRequired() it is strange to call
getAnalysisIfAvailable() because analysis X should always be
available. Use getAnalysis() instead.

After a pass calls addRequired<X>() it is strange to call
getAnalysisIfAvailable<X>() because analysis X should always be
available. Use getAnalysis<X>() instead.
@jayfoad jayfoad requested review from a team as code owners September 8, 2023 09:12
@dtcxzyw dtcxzyw requested review from nikic and removed request for a team October 11, 2023 06:42
Copy link
Contributor

@nikic nikic left a 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).

@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2023

@llvm/pr-subscribers-llvm-transforms

Author: Jay Foad (jayfoad)

Changes

After a pass calls addRequired<X>() it is strange to call
getAnalysisIfAvailable<X>() because analysis X should always be
available. Use getAnalysis<X>() instead.


Full diff: https://github.com/llvm/llvm-project/pull/65729.diff

5 Files Affected:

  • (modified) llvm/lib/CodeGen/EarlyIfConversion.cpp (+2-2)
  • (modified) llvm/lib/CodeGen/TypePromotion.cpp (+2-5)
  • (modified) llvm/lib/CodeGen/VirtRegMap.cpp (+1-1)
  • (modified) llvm/lib/Target/AArch64/AArch64ConditionalCompares.cpp (+1-1)
  • (modified) llvm/lib/Transforms/Scalar/GVN.cpp (+2-2)
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);
   }

@jayfoad jayfoad merged commit b78f3ea into llvm:main Oct 11, 2023
Copy link
Collaborator

@bjope bjope left a 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>();
Copy link
Collaborator

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.

Copy link
Contributor Author

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>();
Copy link
Collaborator

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(?).

Copy link
Contributor Author

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>();
Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@jayfoad
Copy link
Contributor Author

jayfoad commented Oct 11, 2023

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".

For that reason I usually click "add single comment" instead of "start a review".

@nikic
Copy link
Contributor

nikic commented Oct 11, 2023

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).

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.

@jayfoad jayfoad deleted the getanalysisifavailable branch October 11, 2023 09:36
jayfoad added a commit that referenced this pull request Oct 11, 2023
@bjope
Copy link
Collaborator

bjope commented Oct 12, 2023

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".

For that reason I usually click "add single comment" instead of "start a review".

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.
I also do not know how to find all my pending "draft reviews" (unless I'm already "assigned" or "requested as reviewer") as the https://github.com/pulls is just crap compared to my old dashboard in phabricator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants