[llvm] [JumpThreading] Add a hook to override the duplication threshold (PR #111826)

Pengcheng Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 05:18:16 PDT 2024


https://github.com/wangpc-pp created https://github.com/llvm/llvm-project/pull/111826

We can override the default duplication threshold via the constructor
of `JumpThreadingPass` but it is unused in current pass pipeline.

So we remove the constructor and add a target hook so that targets
can override the default value.


>From ea212d69753987b746db928753646629243ccc1f Mon Sep 17 00:00:00 2001
From: Wang Pengcheng <wangpengcheng.pp at bytedance.com>
Date: Thu, 10 Oct 2024 20:15:11 +0800
Subject: [PATCH] [JumpThreading] Add a hook to override the duplication
 threshold

We can override the default duplication threshold via the constructor
of `JumpThreadingPass` but it is unused in current pass pipeline.

So we remove the constructor and add a target hook so that targets
can override the default value.
---
 llvm/include/llvm/Analysis/TargetTransformInfo.h     | 9 +++++++++
 llvm/include/llvm/Analysis/TargetTransformInfoImpl.h | 4 ++++
 llvm/include/llvm/Transforms/Scalar/JumpThreading.h  | 3 ---
 llvm/lib/Analysis/TargetTransformInfo.cpp            | 5 +++++
 llvm/lib/Transforms/Scalar/JumpThreading.cpp         | 9 ++-------
 5 files changed, 20 insertions(+), 10 deletions(-)

diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 5c5da5e06c1bff..bd41433da1059d 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -1810,6 +1810,10 @@ class TargetTransformInfo {
   /// \return The maximum number of function arguments the target supports.
   unsigned getMaxNumArgs() const;
 
+  /// \return The maximum number of duplicate threshold when doing
+  /// JumpThreading.
+  unsigned getJumpThreadingDupThreshold(bool OptForSize) const;
+
   /// @}
 
 private:
@@ -2211,6 +2215,7 @@ class TargetTransformInfo::Concept {
   getVPLegalizationStrategy(const VPIntrinsic &PI) const = 0;
   virtual bool hasArmWideBranch(bool Thumb) const = 0;
   virtual unsigned getMaxNumArgs() const = 0;
+  virtual unsigned getJumpThreadingDupThreshold(bool OptForSize) const = 0;
 };
 
 template <typename T>
@@ -3004,6 +3009,10 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
   unsigned getMaxNumArgs() const override {
     return Impl.getMaxNumArgs();
   }
+
+  unsigned getJumpThreadingDupThreshold(bool OptForSize) const override {
+    return Impl.getJumpThreadingDupThreshold(OptForSize);
+  }
 };
 
 template <typename T>
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
index 6d3ce93acbe451..ba3187c89faf91 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfoImpl.h
@@ -995,6 +995,10 @@ class TargetTransformInfoImplBase {
 
   unsigned getMaxNumArgs() const { return UINT_MAX; }
 
+  unsigned getJumpThreadingDupThreshold(bool OptForSize) const {
+    return OptForSize ? 3 : 6;
+  }
+
 protected:
   // Obtain the minimum required size to hold the value (without the sign)
   // In case of a vector it returns the min required size for one element.
diff --git a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
index 7d11fc0ad69384..8e242a2c256062 100644
--- a/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
+++ b/llvm/include/llvm/Transforms/Scalar/JumpThreading.h
@@ -95,11 +95,8 @@ class JumpThreadingPass : public PassInfoMixin<JumpThreadingPass> {
 #endif
 
   unsigned BBDupThreshold;
-  unsigned DefaultBBDupThreshold;
 
 public:
-  JumpThreadingPass(int T = -1);
-
   // Glue for old PM.
   bool runImpl(Function &F, FunctionAnalysisManager *FAM,
                TargetLibraryInfo *TLI, TargetTransformInfo *TTI,
diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp
index 3dc29fc7cd77b1..2aca9e4b2917c4 100644
--- a/llvm/lib/Analysis/TargetTransformInfo.cpp
+++ b/llvm/lib/Analysis/TargetTransformInfo.cpp
@@ -1328,6 +1328,11 @@ unsigned TargetTransformInfo::getMaxNumArgs() const {
   return TTIImpl->getMaxNumArgs();
 }
 
+unsigned
+TargetTransformInfo::getJumpThreadingDupThreshold(bool OptForSize) const {
+  return TTIImpl->getJumpThreadingDupThreshold(OptForSize);
+}
+
 bool TargetTransformInfo::shouldExpandReduction(const IntrinsicInst *II) const {
   return TTIImpl->shouldExpandReduction(II);
 }
diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
index 7a0b661a07799a..ea6c1ee1bea6b7 100644
--- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp
@@ -107,10 +107,6 @@ static cl::opt<bool> ThreadAcrossLoopHeaders(
     cl::desc("Allow JumpThreading to thread across loop headers, for testing"),
     cl::init(false), cl::Hidden);
 
-JumpThreadingPass::JumpThreadingPass(int T) {
-  DefaultBBDupThreshold = (T == -1) ? BBDuplicateThreshold : unsigned(T);
-}
-
 // Update branch probability information according to conditional
 // branch probability. This is usually made possible for cloned branches
 // in inline instances by the context specific profile in the caller.
@@ -304,10 +300,9 @@ bool JumpThreadingPass::runImpl(Function &F_, FunctionAnalysisManager *FAM_,
   // size.
   if (BBDuplicateThreshold.getNumOccurrences())
     BBDupThreshold = BBDuplicateThreshold;
-  else if (F->hasFnAttribute(Attribute::MinSize))
-    BBDupThreshold = 3;
   else
-    BBDupThreshold = DefaultBBDupThreshold;
+    BBDupThreshold = TTI->getJumpThreadingDupThreshold(
+        F->hasFnAttribute(Attribute::MinSize));
 
   // JumpThreading must not processes blocks unreachable from entry. It's a
   // waste of compute time and can potentially lead to hangs.



More information about the llvm-commits mailing list