Skip to content

[NPM][NFC] Chain PreservedAnalyses methods #129505

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

Conversation

optimisan
Copy link
Contributor

@optimisan optimisan commented Mar 3, 2025

I think cascading these methods makes sense, but is this just unneeded fluff?

Copy link
Contributor Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

@optimisan optimisan changed the title [NPM] Chain PreservedAnalyses methods [NPM] Chain PreservedAnalyses methods NFC Mar 3, 2025
@optimisan optimisan marked this pull request as ready for review March 3, 2025 10:46
@llvmbot llvmbot added the llvm:ir label Mar 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 3, 2025

@llvm/pr-subscribers-llvm-ir

Author: Akshat Oke (optimisan)

Changes

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

2 Files Affected:

  • (modified) llvm/include/llvm/IR/Analysis.h (+16-6)
  • (modified) llvm/lib/CodeGen/MachineScheduler.cpp (+4-5)
diff --git a/llvm/include/llvm/IR/Analysis.h b/llvm/include/llvm/IR/Analysis.h
index 6c80c771d40b4..dd43f5ee5f706 100644
--- a/llvm/include/llvm/IR/Analysis.h
+++ b/llvm/include/llvm/IR/Analysis.h
@@ -128,11 +128,14 @@ class PreservedAnalyses {
   }
 
   /// Mark an analysis as preserved.
-  template <typename AnalysisT> void preserve() { preserve(AnalysisT::ID()); }
+  template <typename AnalysisT> PreservedAnalyses &preserve() {
+    preserve(AnalysisT::ID());
+    return *this;
+  }
 
   /// Given an analysis's ID, mark the analysis as preserved, adding it
   /// to the set.
-  void preserve(AnalysisKey *ID) {
+  PreservedAnalyses &preserve(AnalysisKey *ID) {
     // Clear this ID from the explicit not-preserved set if present.
     NotPreservedAnalysisIDs.erase(ID);
 
@@ -140,18 +143,21 @@ class PreservedAnalyses {
     // NotPreservedAnalysisIDs).
     if (!areAllPreserved())
       PreservedIDs.insert(ID);
+    return *this;
   }
 
   /// Mark an analysis set as preserved.
-  template <typename AnalysisSetT> void preserveSet() {
+  template <typename AnalysisSetT> PreservedAnalyses &preserveSet() {
     preserveSet(AnalysisSetT::ID());
+    return *this;
   }
 
   /// Mark an analysis set as preserved using its ID.
-  void preserveSet(AnalysisSetKey *ID) {
+  PreservedAnalyses &preserveSet(AnalysisSetKey *ID) {
     // If we're not already in the saturated 'all' state, add this set.
     if (!areAllPreserved())
       PreservedIDs.insert(ID);
+    return *this;
   }
 
   /// Mark an analysis as abandoned.
@@ -161,7 +167,10 @@ class PreservedAnalyses {
   ///
   /// Note that you can only abandon a specific analysis, not a *set* of
   /// analyses.
-  template <typename AnalysisT> void abandon() { abandon(AnalysisT::ID()); }
+  template <typename AnalysisT> PreservedAnalyses &abandon() {
+    abandon(AnalysisT::ID());
+    return *this;
+  }
 
   /// Mark an analysis as abandoned using its ID.
   ///
@@ -170,9 +179,10 @@ class PreservedAnalyses {
   ///
   /// Note that you can only abandon a specific analysis, not a *set* of
   /// analyses.
-  void abandon(AnalysisKey *ID) {
+  PreservedAnalyses &abandon(AnalysisKey *ID) {
     PreservedIDs.erase(ID);
     NotPreservedAnalysisIDs.insert(ID);
+    return *this;
   }
 
   /// Intersect this set with another in place.
diff --git a/llvm/lib/CodeGen/MachineScheduler.cpp b/llvm/lib/CodeGen/MachineScheduler.cpp
index d67e4ef572469..333311f2c2ebd 100644
--- a/llvm/lib/CodeGen/MachineScheduler.cpp
+++ b/llvm/lib/CodeGen/MachineScheduler.cpp
@@ -602,11 +602,10 @@ MachineSchedulerPass::run(MachineFunction &MF,
   if (!Changed)
     return PreservedAnalyses::all();
 
-  PreservedAnalyses PA = getMachineFunctionPassPreservedAnalyses();
-  PA.preserveSet<CFGAnalyses>();
-  PA.preserve<SlotIndexesAnalysis>();
-  PA.preserve<LiveIntervalsAnalysis>();
-  return PA;
+  return getMachineFunctionPassPreservedAnalyses()
+      .preserveSet<CFGAnalyses>()
+      .preserve<SlotIndexesAnalysis>()
+      .preserve<LiveIntervalsAnalysis>();
 }
 
 bool PostMachineSchedulerLegacy::runOnMachineFunction(MachineFunction &MF) {

@optimisan optimisan changed the title [NPM] Chain PreservedAnalyses methods NFC [NPM][NFC] Chain PreservedAnalyses methods Mar 3, 2025
@optimisan optimisan requested a review from davemgreen March 3, 2025 10:59
Copy link
Contributor

@aeubanks aeubanks left a comment

Choose a reason for hiding this comment

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

I like this, I do think it makes it nicer to work with PreservedAnalyses

@optimisan optimisan requested review from cdevadas and vikramRH March 4, 2025 04:52
@optimisan optimisan merged commit 3aab3fe into main Mar 4, 2025
12 of 15 checks passed
@optimisan optimisan deleted the users/optimisan/03-03-_npm_chain_preservedanalyses_methods branch March 4, 2025 04:53
jph-13 pushed a commit to jph-13/llvm-project that referenced this pull request Mar 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants