[llvm] [InstCombine] Improve `foldDeadPhiWeb` compile time (PR #158057)
Mingjie Xu via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 11 05:27:12 PDT 2025
https://github.com/Enna1 created https://github.com/llvm/llvm-project/pull/158057
The foldDeadPhiWeb function identifies and removes small dead PHI webs, it bails out if the web size exceeds threshold (16).
In the current implementation, when there is a phi node has large number of users that most of them are phi nodes, we still push them on the `Stack` even if the number of phi nodes user exceeds the threshold.
This patch checks the early stop condition when we push an unvisited phi node on the `Stack`.
With this change, the wall duration of total instcombine pass decreased from 523,649.276 ms to 30,239.399 ms in an our internal case.
>From 9a6bce6ff8c2fca01014d701bcb9e2ab71012cdb Mon Sep 17 00:00:00 2001
From: "xumingjie.enna1" <xumingjie.enna1 at bytedance.com>
Date: Wed, 10 Sep 2025 19:10:53 +0800
Subject: [PATCH] [InstCombine] Improve `foldDeadPhiWeb` compile time
The foldDeadPhiWeb function identifies and removes small dead PHI webs, it bails
out if the web size exceeds threshold (16).
In the current implementation, when there is a phi node has large number of
users that most of them are phi nodes, we still push them on the `Stack` even
if the numbers of phi nodes user already exceed the threshold. It is unnecessary
and may consume too much time.
This patch checks the early stop condition when we push an unvisited phi node
on the `Stack`.
With this change, the wall duration of total instcombine pass decreased from
523,649.276 ms to 30,239.399 ms in an our internal case.
---
llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
index ed9a0be6981fa..c3643a7cb4c08 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp
@@ -62,14 +62,15 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) {
Stack.push_back(&PN);
while (!Stack.empty()) {
PHINode *Phi = Stack.pop_back_val();
- if (!Visited.insert(Phi).second)
- continue;
- // Early stop if the set of PHIs is large
- if (Visited.size() == 16)
- return false;
for (User *Use : Phi->users()) {
- if (PHINode *PhiUse = dyn_cast<PHINode>(Use))
+ if (PHINode *PhiUse = dyn_cast<PHINode>(Use)) {
+ if (!Visited.insert(Phi).second)
+ continue;
+ // Early stop if the set of PHIs is large
+ if (Visited.size() >= 16)
+ return false;
Stack.push_back(PhiUse);
+ }
else
return false;
}
More information about the llvm-commits
mailing list