Skip to content

Conversation

@vpykhtin
Copy link
Contributor

@vpykhtin vpykhtin commented Jul 28, 2025

This is a replacement PR for #130611 and PR llvm/llvm-project #135181 due to the parent branches change. No changes since.

Copy link
Contributor Author

vpykhtin commented Jul 28, 2025

@vpykhtin vpykhtin changed the title amdgpu_use_ssaupdaterbulk_in_structurizecfg [AMDGPU] Improve StructurizeCFG pass performance by using SSAUpdaterBulk. Jul 28, 2025
@vpykhtin vpykhtin requested review from arsenm and nikic July 28, 2025 13:09
@vpykhtin vpykhtin marked this pull request as ready for review July 28, 2025 13:10
@llvmbot
Copy link
Member

llvmbot commented Jul 28, 2025

@llvm/pr-subscribers-llvm-transforms

Author: Valery Pykhtin (vpykhtin)

Changes

This is a replacement PR for #130611 and PR llvm/llvm-project #135181 due to the parent branches change. No changes since.


Full diff: https://github.com/llvm/llvm-project/pull/150937.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 44e63a0583d1a..5d4f2d1a38407 100644
--- a/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
+++ b/llvm/lib/Transforms/Scalar/StructurizeCFG.cpp
@@ -48,6 +48,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>
 
@@ -319,7 +320,7 @@ class StructurizeCFG {
 
   void collectInfos();
 
-  void insertConditions(bool Loops);
+  void insertConditions(bool Loops, SSAUpdaterBulk &PhiInserter);
 
   void simplifyConditions();
 
@@ -668,10 +669,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());
@@ -680,8 +680,9 @@ void StructurizeCFG::insertConditions(bool Loops) {
     BasicBlock *SuccTrue = Term->getSuccessor(0);
     BasicBlock *SuccFalse = Term->getSuccessor(1);
 
-    PhiInserter.Initialize(Boolean, "");
-    PhiInserter.AddAvailableValue(Loops ? SuccFalse : Parent, Default);
+    unsigned Variable = PhiInserter.AddVariable("", Boolean);
+    PhiInserter.AddAvailableValue(Variable, Loops ? SuccFalse : Parent,
+                                  Default);
 
     BBPredicates &Preds = Loops ? LoopPreds[SuccFalse] : Predicates[SuccTrue];
 
@@ -694,7 +695,7 @@ void StructurizeCFG::insertConditions(bool Loops) {
         ParentInfo = PI;
         break;
       }
-      PhiInserter.AddAvailableValue(BB, PI.Pred);
+      PhiInserter.AddAvailableValue(Variable, BB, PI.Pred);
       Dominator.addAndRememberBlock(BB);
     }
 
@@ -703,9 +704,9 @@ void StructurizeCFG::insertConditions(bool Loops) {
       CondBranchWeights::setMetadata(*Term, ParentInfo.Weights);
     } else {
       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));
     }
   }
 }
@@ -1410,8 +1411,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();
   simplifyHoistedPhis();
   simplifyConditions();

@shiltian
Copy link
Contributor

shiltian commented Jul 28, 2025

Can you request an internal psdb full cycle to test this PR? StructurizeCFG seems very sensitive to changes and has caused a lot of downstream failures.

@vpykhtin
Copy link
Contributor Author

Can you request an internal psdb full cycle to test this PR? StructurizeCFG seems very sensitive to changes and has caused a lot of downstream failures.

I did that before and it passed, but I'll double check, thanks!

@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from 05eee4a to 4bcba8d Compare August 25, 2025 08:35
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization branch from 9e8736c to 79e1b41 Compare August 25, 2025 08:35
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from 4bcba8d to 8433c98 Compare August 25, 2025 08:57
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization branch from 79e1b41 to 444e960 Compare August 25, 2025 08:57
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization branch from 444e960 to 6722e83 Compare September 17, 2025 16:36
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from 8433c98 to 654a23b Compare September 17, 2025 16:36
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization branch from 6722e83 to b5043f6 Compare September 18, 2025 15:29
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from 654a23b to ae3589e Compare September 18, 2025 15:29
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from ae3589e to fa7a951 Compare October 10, 2025 13:44
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization branch from b5043f6 to df6dbd1 Compare October 10, 2025 13:44
Base automatically changed from users/vpykhtin/07-28-ssaupdaterbulk_add_phi_optimization to main October 13, 2025 13:02
@vpykhtin vpykhtin force-pushed the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch from fa7a951 to a7d1d3b Compare October 14, 2025 14:56
@vpykhtin vpykhtin merged commit fc2afbd into main Oct 14, 2025
10 checks passed
@vpykhtin vpykhtin deleted the users/vpykhtin/07-28-amdgpu_use_ssaupdaterbulk_in_structurizecfg branch October 14, 2025 18:07
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
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