From 9a6bce6ff8c2fca01014d701bcb9e2ab71012cdb Mon Sep 17 00:00:00 2001 From: "xumingjie.enna1" Date: Wed, 10 Sep 2025 19:10:53 +0800 Subject: [PATCH 1/3] [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(Use)) + if (PHINode *PhiUse = dyn_cast(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; } From e0d096ffbbafdbbfaecce44a0d16eeb44ef91909 Mon Sep 17 00:00:00 2001 From: "xumingjie.enna1" Date: Thu, 11 Sep 2025 20:32:36 +0800 Subject: [PATCH 2/3] clang format --- llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp index c3643a7cb4c08..36431b680cd43 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -70,8 +70,7 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) { if (Visited.size() >= 16) return false; Stack.push_back(PhiUse); - } - else + } else return false; } } From f5657f2651e5cd0f783c0210490dce608deed152 Mon Sep 17 00:00:00 2001 From: "xumingjie.enna1" Date: Thu, 11 Sep 2025 22:01:26 +0800 Subject: [PATCH 3/3] fix... --- llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp index 36431b680cd43..15e7172c6ce12 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombinePHI.cpp @@ -60,11 +60,12 @@ bool InstCombinerImpl::foldDeadPhiWeb(PHINode &PN) { SmallVector Stack; SmallPtrSet Visited; Stack.push_back(&PN); + Visited.insert(&PN); while (!Stack.empty()) { PHINode *Phi = Stack.pop_back_val(); for (User *Use : Phi->users()) { if (PHINode *PhiUse = dyn_cast(Use)) { - if (!Visited.insert(Phi).second) + if (!Visited.insert(PhiUse).second) continue; // Early stop if the set of PHIs is large if (Visited.size() >= 16)