[llvm] [DFAJumpThreading] Prevent pass from using too much memory. (PR #145482)

Bushev Dmitry via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 24 02:47:09 PDT 2025


https://github.com/dybv-sc updated https://github.com/llvm/llvm-project/pull/145482

>From e89a0860f4da654390014dd4b8d3beaf5808d5af Mon Sep 17 00:00:00 2001
From: Dmitry Bushev <dmitry.bushev at syntacore.com>
Date: Mon, 23 Jun 2025 08:54:56 +0000
Subject: [PATCH 1/2] [DFAJumpThreading] Prevent pass from using too much
 memory.

The limit 'dfa-max-num-paths' that is used to control number of enumerated paths
was not checked against inside getPathsFromStateDefMap. It may lead to large memory consumption for
complex enough switch statements.
---
 llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
index 938aab5879044..ac7af712bcf12 100644
--- a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
@@ -618,6 +618,8 @@ struct AllSwitchPaths {
 
     VisitedBlocks UniqueBlocks;
     for (auto *IncomingBB : Phi->blocks()) {
+      if(Res.size() >= MaxNumPaths)
+        break;
       if (!UniqueBlocks.insert(IncomingBB).second)
         continue;
       if (!SwitchOuterLoop->contains(IncomingBB))
@@ -657,6 +659,8 @@ struct AllSwitchPaths {
             getPathsFromStateDefMap(StateDef, IncomingPhi, VB);
         for (ThreadingPath &Path : PredPaths) {
           Path.push_back(PhiBB);
+          if(Res.size() >= MaxNumPaths)
+            break;
           Res.push_back(std::move(Path));
         }
         continue;
@@ -679,6 +683,10 @@ struct AllSwitchPaths {
           ThreadingPath NewPath(Path);
           NewPath.appendExcludingFirst(IPath);
           NewPath.push_back(PhiBB);
+          if(Res.size() >= MaxNumPaths) {
+            VB.erase(PhiBB);
+            return Res;
+	  }
           Res.push_back(NewPath);
         }
       }

>From f0703ade890c6954f82b9a3dfbcbb6be4c38d650 Mon Sep 17 00:00:00 2001
From: Dmitry Bushev <dmitry.bushev at syntacore.com>
Date: Tue, 24 Jun 2025 09:46:39 +0000
Subject: [PATCH 2/2] Fix formatting

---
 llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
index ac7af712bcf12..54cf3c523bdff 100644
--- a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
@@ -618,7 +618,7 @@ struct AllSwitchPaths {
 
     VisitedBlocks UniqueBlocks;
     for (auto *IncomingBB : Phi->blocks()) {
-      if(Res.size() >= MaxNumPaths)
+      if (Res.size() >= MaxNumPaths)
         break;
       if (!UniqueBlocks.insert(IncomingBB).second)
         continue;
@@ -659,7 +659,7 @@ struct AllSwitchPaths {
             getPathsFromStateDefMap(StateDef, IncomingPhi, VB);
         for (ThreadingPath &Path : PredPaths) {
           Path.push_back(PhiBB);
-          if(Res.size() >= MaxNumPaths)
+          if (Res.size() >= MaxNumPaths)
             break;
           Res.push_back(std::move(Path));
         }
@@ -683,10 +683,10 @@ struct AllSwitchPaths {
           ThreadingPath NewPath(Path);
           NewPath.appendExcludingFirst(IPath);
           NewPath.push_back(PhiBB);
-          if(Res.size() >= MaxNumPaths) {
+          if (Res.size() >= MaxNumPaths) {
             VB.erase(PhiBB);
             return Res;
-	  }
+          }
           Res.push_back(NewPath);
         }
       }



More information about the llvm-commits mailing list