Skip to content

[NewPM] LiveIntervals: Check dependencies for invalidation #123563

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
Jan 24, 2025

Conversation

optimisan
Copy link
Contributor

No description provided.

@llvmbot
Copy link
Member

llvmbot commented Jan 20, 2025

@llvm/pr-subscribers-llvm-regalloc

Author: Akshat Oke (optimisan)

Changes

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

2 Files Affected:

  • (modified) llvm/include/llvm/CodeGen/LiveIntervals.h (+3)
  • (modified) llvm/lib/CodeGen/LiveIntervals.cpp (+14)
diff --git a/llvm/include/llvm/CodeGen/LiveIntervals.h b/llvm/include/llvm/CodeGen/LiveIntervals.h
index 161bb247a0e968..540651ea114427 100644
--- a/llvm/include/llvm/CodeGen/LiveIntervals.h
+++ b/llvm/include/llvm/CodeGen/LiveIntervals.h
@@ -113,6 +113,9 @@ class LiveIntervals {
   LiveIntervals(LiveIntervals &&) = default;
   ~LiveIntervals();
 
+  bool invalidate(MachineFunction &MF, const PreservedAnalyses &PA,
+                  MachineFunctionAnalysisManager::Invalidator &Inv);
+
   /// Calculate the spill weight to assign to a single instruction.
   /// If \p PSI is provided the calculation is altered for optsize functions.
   static float getSpillWeight(bool isDef, bool isUse,
diff --git a/llvm/lib/CodeGen/LiveIntervals.cpp b/llvm/lib/CodeGen/LiveIntervals.cpp
index f38527a3ce6a31..64c184f5553ab2 100644
--- a/llvm/lib/CodeGen/LiveIntervals.cpp
+++ b/llvm/lib/CodeGen/LiveIntervals.cpp
@@ -127,6 +127,20 @@ LiveIntervalsWrapperPass::LiveIntervalsWrapperPass() : MachineFunctionPass(ID) {
 
 LiveIntervals::~LiveIntervals() { clear(); }
 
+bool LiveIntervals::invalidate(
+    MachineFunction &MF, const PreservedAnalyses &PA,
+    MachineFunctionAnalysisManager::Invalidator &Inv) {
+  auto PAC = PA.getChecker<LiveIntervalsAnalysis>();
+
+  if (!PAC.preserved() && !PAC.preservedSet<AllAnalysesOn<MachineFunction>>())
+    return true;
+
+  // LiveIntervals holds pointers to these results, so check for their
+  // invalidation.
+  return (Inv.invalidate<SlotIndexesAnalysis>(MF, PA) ||
+          Inv.invalidate<MachineDominatorTreeAnalysis>(MF, PA));
+}
+
 void LiveIntervals::clear() {
   // Free the live intervals themselves.
   for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i)

// invalidation.
return (Inv.invalidate<SlotIndexesAnalysis>(MF, PA) ||
Inv.invalidate<MachineDominatorTreeAnalysis>(MF, PA));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC when control flow reaches here, it implies live intervals or all machine function analyses are preserved. It invalidates them forcibly. Move them to SlotIndexesAnalysis?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

invalidate() actually means isThisResultInvalid(IR, PA), so if SlotIndexes reports that it has been made invalid, this analysis should also be invalid. SI and MDT are good with the default invalidate() implementation since they don't depend on other analyses.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then

PreservedAnalyses SomePass::run(IRUnitT &, AnalysisManager &) {
  PreservedAnalyses PA;
  PA.preserve<LiveIntervals>();
  return PA;
}

will no longer work, which is counter-intuitive... because it doesn't preserve SlotIndexes explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is related here #123045 (comment)

New PM can't report such cases. Could add such a functionality behind a flag but I guess it's unlikely for more cases like this to exist, since they would lead to invalid reads (as the PM deletes the results).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then

PreservedAnalyses SomePass::run(IRUnitT &, AnalysisManager &) {
  PreservedAnalyses PA;
  PA.preserve<LiveIntervals>();
  return PA;
}

will no longer work, which is counter-intuitive... because it doesn't preserve SlotIndexes explicitly.

if SlotIndexes is invalidated, then LiveIntervals may reference an invalid SlotIndexes, so if we kept around LiveIntervals it may be invalid to use. so invalidating it regardless makes sense.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is related here #123045 (comment)

New PM can't report such cases. Could add such a functionality behind a flag but I guess it's unlikely for more cases like this to exist, since they would lead to invalid reads (as the PM deletes the results).

I don't think it's easily trackable which analyses depend on other analyses without a bunch of extra infrastructure, so unless this is a problem that comes up a lot I'm inclined to say it's not worth the effort

@paperchalice paperchalice requested a review from aeubanks January 20, 2025 12:05
// invalidation.
return (Inv.invalidate<SlotIndexesAnalysis>(MF, PA) ||
Inv.invalidate<MachineDominatorTreeAnalysis>(MF, PA));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then

PreservedAnalyses SomePass::run(IRUnitT &, AnalysisManager &) {
  PreservedAnalyses PA;
  PA.preserve<LiveIntervals>();
  return PA;
}

will no longer work, which is counter-intuitive... because it doesn't preserve SlotIndexes explicitly.

if SlotIndexes is invalidated, then LiveIntervals may reference an invalid SlotIndexes, so if we kept around LiveIntervals it may be invalid to use. so invalidating it regardless makes sense.

// invalidation.
return (Inv.invalidate<SlotIndexesAnalysis>(MF, PA) ||
Inv.invalidate<MachineDominatorTreeAnalysis>(MF, PA));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is related here #123045 (comment)

New PM can't report such cases. Could add such a functionality behind a flag but I guess it's unlikely for more cases like this to exist, since they would lead to invalid reads (as the PM deletes the results).

I don't think it's easily trackable which analyses depend on other analyses without a bunch of extra infrastructure, so unless this is a problem that comes up a lot I'm inclined to say it's not worth the effort

@optimisan optimisan merged commit a9c61e0 into llvm:main Jan 24, 2025
8 checks passed
@optimisan optimisan deleted the live-intervals-inv branch January 24, 2025 05:53
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.

5 participants