Skip to content

[AMDGPU] Improve StructurizeCFG pass performance by using SSAUpdaterBulk. #135181

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

Open
wants to merge 1 commit into
base: users/vpykhtin/04-10-ssaupdaterbulk_add_phi_optimization
Choose a base branch
from

Conversation

vpykhtin
Copy link
Contributor

@vpykhtin vpykhtin commented Apr 10, 2025

This is a replacement PR for #130611, stacked version.

Copy link
Contributor Author

vpykhtin commented Apr 10, 2025

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more

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

@vpykhtin vpykhtin changed the title amdgpu_use_ssaupdaterbulk_in_structurizecfg [AMDGPU] Improve StructurizeCFG pass performance by using SSAUpdaterBulk. Apr 10, 2025
@vpykhtin vpykhtin requested a review from arsenm April 10, 2025 13:58
@vpykhtin vpykhtin marked this pull request as ready for review April 10, 2025 14:00
@llvmbot
Copy link
Member

llvmbot commented Apr 10, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Valery Pykhtin (vpykhtin)

Changes

This is a replacement PR for #130611, stacked version.


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

1 Files Affected:

  • (modified) llvm/lib/Transforms/Scalar/StructurizeCFG.cpp (+15-10)
diff --git a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
index 00c4fcc76e791..95c68ecd2255b 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -47,6 +47,7 @@
 #include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Transforms/Utils/SSAUpdater.h"
+#include "llvm/Transforms/Utils/SSAUpdaterBulk.h"
 #include <cassert>
 #include <utility>
 
@@ -317,7 +318,7 @@ class StructurizeCFG {
 
   void collectInfos();
 
-  void insertConditions(bool Loops);
+  void insertConditions(bool Loops, SSAUpdaterBulk &PhiInserter);
 
   void simplifyConditions();
 
@@ -600,10 +601,9 @@ void StructurizeCFG::collectInfos() {
 }
 
 /// Insert the missing branch conditions
-void StructurizeCFG::insertConditions(bool Loops) {
+void StructurizeCFG::insertConditions(bool Loops, SSAUpdaterBulk &PhiInserter) {
   BranchVector &Conds = Loops ? LoopConds : Conditions;
   Value *Default = Loops ? BoolTrue : BoolFalse;
-  SSAUpdater PhiInserter;
 
   for (BranchInst *Term : Conds) {
     assert(Term->isConditional());
@@ -619,22 +619,23 @@ void StructurizeCFG::insertConditions(bool Loops) {
       Term->setCondition(PI.Pred);
       CondBranchWeights::setMetadata(*Term, PI.Weights);
     } else {
-      PhiInserter.Initialize(Boolean, "");
-      PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
+      unsigned Variable = PhiInserter.AddVariable("", Boolean);
+      PhiInserter.AddAvailableValue(Variable, Loops ? SuccFalse : Parent,
+                                    Default);
 
       NearestCommonDominator Dominator(DT);
       Dominator.addBlock(Parent);
 
       for (auto [BB, PI] : Preds) {
         assert(BB != Parent);
-        PhiInserter.AddAvailableValue(BB, PI.Pred);
+        PhiInserter.AddAvailableValue(Variable, BB, PI.Pred);
         Dominator.addAndRememberBlock(BB);
       }
 
       if (!Dominator.resultIsRememberedBlock())
-        PhiInserter.AddAvailableValue(Dominator.result(), Default);
+        PhiInserter.AddAvailableValue(Variable, Dominator.result(), Default);
 
-      Term->setCondition(PhiInserter.GetValueInMiddleOfBlock(Parent));
+      PhiInserter.AddUse(Variable, &Term->getOperandUse(0));
     }
   }
 }
@@ -1318,8 +1319,12 @@ bool StructurizeCFG::run(Region *R, DominatorTree *DT) {
   orderNodes();
   collectInfos();
   createFlow();
-  insertConditions(false);
-  insertConditions(true);
+
+  SSAUpdaterBulk PhiInserter;
+  insertConditions(false, PhiInserter);
+  insertConditions(true, PhiInserter);
+  PhiInserter.RewriteAndOptimizeAllUses(DT);
+
   setPhiValues();
   simplifyConditions();
   simplifyAffectedPhis();

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.

3 participants