Skip to content

[NewPM] Use perfect forwarding in run, getResult and getResultImpl #124501

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

Closed
wants to merge 1 commit into from
Closed

[NewPM] Use perfect forwarding in run, getResult and getResultImpl #124501

wants to merge 1 commit into from

Conversation

caozhanhao
Copy link

Use perfect forwarding in run, getResult and getResultImpl.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2025

@llvm/pr-subscribers-llvm-ir

Author: caozhanhao (caozhanhao)

Changes

Use perfect forwarding in run, getResult and getResultImpl.


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

2 Files Affected:

  • (modified) llvm/include/llvm/IR/PassManager.h (+5-5)
  • (modified) llvm/include/llvm/IR/PassManagerImpl.h (+6-6)
diff --git a/llvm/include/llvm/IR/PassManager.h b/llvm/include/llvm/IR/PassManager.h
index b5230047b0e128..087cba381d13a6 100644
--- a/llvm/include/llvm/IR/PassManager.h
+++ b/llvm/include/llvm/IR/PassManager.h
@@ -188,7 +188,7 @@ class PassManager : public PassInfoMixin<
   /// Run all of the passes in this manager over the given unit of IR.
   /// ExtraArgs are passed to each pass.
   PreservedAnalyses run(IRUnitT &IR, AnalysisManagerT &AM,
-                        ExtraArgTs... ExtraArgs);
+                        ExtraArgTs &&... ExtraArgs);
 
   template <typename PassT>
   LLVM_ATTRIBUTE_MINSIZE std::enable_if_t<!std::is_same_v<PassT, PassManager>>
@@ -407,11 +407,11 @@ template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
   ///
   /// Runs the analysis if a cached result is not available.
   template <typename PassT>
-  typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs) {
+  typename PassT::Result &getResult(IRUnitT &IR, ExtraArgTs &&... ExtraArgs) {
     assert(AnalysisPasses.count(PassT::ID()) &&
            "This analysis pass was not registered prior to being queried");
     ResultConceptT &ResultConcept =
-        getResultImpl(PassT::ID(), IR, ExtraArgs...);
+        getResultImpl(PassT::ID(), IR, std::forward<ExtraArgTs>(ExtraArgs)...);
 
     using ResultModelT =
         detail::AnalysisResultModel<IRUnitT, PassT, typename PassT::Result,
@@ -508,7 +508,7 @@ template <typename IRUnitT, typename... ExtraArgTs> class AnalysisManager {
 
   /// Get an analysis result, running the pass if necessary.
   ResultConceptT &getResultImpl(AnalysisKey *ID, IRUnitT &IR,
-                                ExtraArgTs... ExtraArgs);
+                                ExtraArgTs &&... ExtraArgs);
 
   /// Get a cached analysis result or return null.
   ResultConceptT *getCachedResultImpl(AnalysisKey *ID, IRUnitT &IR) const {
@@ -778,7 +778,7 @@ class OuterAnalysisManagerProxy
   /// Nothing to see here, it just forwards the \c OuterAM reference into the
   /// result.
   Result run(IRUnitT &, AnalysisManager<IRUnitT, ExtraArgTs...> &,
-             ExtraArgTs...) {
+             ExtraArgTs &&...) {
     return Result(*OuterAM);
   }
 
diff --git a/llvm/include/llvm/IR/PassManagerImpl.h b/llvm/include/llvm/IR/PassManagerImpl.h
index 67e3fbe4f3068c..d6d643f11d0f93 100644
--- a/llvm/include/llvm/IR/PassManagerImpl.h
+++ b/llvm/include/llvm/IR/PassManagerImpl.h
@@ -27,7 +27,7 @@ namespace llvm {
 
 template <typename IRUnitT, typename AnalysisManagerT, typename... ExtraArgTs>
 PreservedAnalyses PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>::run(
-    IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs... ExtraArgs) {
+    IRUnitT &IR, AnalysisManagerT &AM, ExtraArgTs &&... ExtraArgs) {
   class StackTraceEntry : public PrettyStackTraceEntry {
     const PassInstrumentation &PI;
     IRUnitT &IR;
@@ -62,7 +62,7 @@ PreservedAnalyses PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>::run(
   // AnalysisManager's arguments out of the whole ExtraArgs set.
   PassInstrumentation PI =
       detail::getAnalysisResult<PassInstrumentationAnalysis>(
-          AM, IR, std::tuple<ExtraArgTs...>(ExtraArgs...));
+          AM, IR, std::forward_as_tuple(std::forward<ExtraArgTs>(ExtraArgs)...));
 
   // RemoveDIs: if requested, convert debug-info to DbgRecord representation
   // for duration of these passes.
@@ -78,7 +78,7 @@ PreservedAnalyses PassManager<IRUnitT, AnalysisManagerT, ExtraArgTs...>::run(
     if (!PI.runBeforePass<IRUnitT>(*Pass, IR))
       continue;
 
-    PreservedAnalyses PassPA = Pass->run(IR, AM, ExtraArgs...);
+    PreservedAnalyses PassPA = Pass->run(IR, AM, std::forward<ExtraArgTs>(ExtraArgs)...);
 
     // Update the analysis manager as each pass runs and potentially
     // invalidates analyses.
@@ -135,7 +135,7 @@ AnalysisManager<IRUnitT, ExtraArgTs...>::clear(IRUnitT &IR,
 template <typename IRUnitT, typename... ExtraArgTs>
 inline typename AnalysisManager<IRUnitT, ExtraArgTs...>::ResultConceptT &
 AnalysisManager<IRUnitT, ExtraArgTs...>::getResultImpl(
-    AnalysisKey *ID, IRUnitT &IR, ExtraArgTs... ExtraArgs) {
+    AnalysisKey *ID, IRUnitT &IR, ExtraArgTs &&... ExtraArgs) {
   typename AnalysisResultMapT::iterator RI;
   bool Inserted;
   std::tie(RI, Inserted) = AnalysisResults.insert(std::make_pair(
@@ -148,12 +148,12 @@ AnalysisManager<IRUnitT, ExtraArgTs...>::getResultImpl(
 
     PassInstrumentation PI;
     if (ID != PassInstrumentationAnalysis::ID()) {
-      PI = getResult<PassInstrumentationAnalysis>(IR, ExtraArgs...);
+      PI = getResult<PassInstrumentationAnalysis>(IR, std::forward<ExtraArgTs>(ExtraArgs)...);
       PI.runBeforeAnalysis(P, IR);
     }
 
     AnalysisResultListT &ResultList = AnalysisResultLists[&IR];
-    ResultList.emplace_back(ID, P.run(IR, *this, ExtraArgs...));
+    ResultList.emplace_back(ID, P.run(IR, *this, std::forward<ExtraArgTs>(ExtraArgs)...));
 
     PI.runAfterAnalysis(P, IR);
 

@caozhanhao
Copy link
Author

Sorry, I've misunderstood this.

@caozhanhao caozhanhao closed this Jan 27, 2025
@caozhanhao caozhanhao deleted the newpm-perfect-forwarding branch January 27, 2025 05:20
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.

2 participants