[llvm-branch-commits] [llvm] [llvm][dfa-jump-threading] Allow DFAJumpThreading with optsize (PR #83049)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Feb 28 10:53:56 PST 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Paul Kirth (ilovepi)

<details>
<summary>Changes</summary>

Though DFAJumpThreadingPass typically introduces size increases, targets
optimizing for size may want to enable the pass for certain TUs and
workloads. This change adds options to override the default behavior,
and enable the DFAJumpThreadingPass to run, even when compiling for
size.


---
Full diff: https://github.com/llvm/llvm-project/pull/83049.diff


3 Files Affected:

- (modified) llvm/lib/Passes/PassBuilderPipelines.cpp (+5) 
- (modified) llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp (+6-1) 
- (modified) llvm/test/Transforms/DFAJumpThreading/negative.ll (+49) 


``````````diff
diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp
index cbbbec0ccc8c4d..3b2d4523ef7e8e 100644
--- a/llvm/lib/Passes/PassBuilderPipelines.cpp
+++ b/llvm/lib/Passes/PassBuilderPipelines.cpp
@@ -222,6 +222,11 @@ static cl::opt<bool>
     EnableDFAJumpThreading("enable-dfa-jump-thread",
                            cl::desc("Enable DFA jump threading"),
                            cl::init(false), cl::Hidden);
+static cl::opt<bool>
+    DFAJumpThreadingOptSize("dfa-jump-thread-optsize",
+                           cl::desc("Enable DFA jump threading when optimizing for size"),
+                           cl::init(false), cl::Hidden);
+
 
 // TODO: turn on and remove flag
 static cl::opt<bool> EnablePGOForceFunctionAttrs(
diff --git a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
index 85d4065286e41f..ce5e823e531e0f 100644
--- a/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
+++ b/llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
@@ -110,6 +110,11 @@ static cl::opt<unsigned>
                   cl::desc("Maximum cost accepted for the transformation"),
                   cl::Hidden, cl::init(50));
 
+static cl::opt<bool> DFAJumpThreadIgnoreOptSize(
+    "dfa-jump-ignore-optsize",
+    cl::desc("Enable dfa jump threading, even when optimizing for size"),
+    cl::Hidden, cl::init(false));
+
 namespace {
 
 class SelectInstToUnfold {
@@ -1244,7 +1249,7 @@ struct TransformDFA {
 bool DFAJumpThreading::run(Function &F) {
   LLVM_DEBUG(dbgs() << "\nDFA Jump threading: " << F.getName() << "\n");
 
-  if (F.hasOptSize()) {
+  if (!DFAJumpThreadIgnoreOptSize && F.hasOptSize()) {
     LLVM_DEBUG(dbgs() << "Skipping due to the 'minsize' attribute\n");
     return false;
   }
diff --git a/llvm/test/Transforms/DFAJumpThreading/negative.ll b/llvm/test/Transforms/DFAJumpThreading/negative.ll
index a9642814276992..c822b6613f7334 100644
--- a/llvm/test/Transforms/DFAJumpThreading/negative.ll
+++ b/llvm/test/Transforms/DFAJumpThreading/negative.ll
@@ -1,6 +1,7 @@
 ; RUN: opt -passes=dfa-jump-threading -dfa-cost-threshold=25 -pass-remarks-missed='dfa-jump-threading' -pass-remarks-output=%t -disable-output %s
 ; RUN: FileCheck --input-file %t --check-prefix=REMARK %s
 ; RUN: opt -S -passes=dfa-jump-threading %s | FileCheck %s
+; RUN: opt -S -passes=dfa-jump-threading -dfa-jump-ignore-optsize %s | FileCheck %s --check-prefix=IGNORESIZE
 
 ; This negative test case checks that the optimization doesn't trigger
 ; when the code size cost is too high.
@@ -186,6 +187,54 @@ define i32 @negative5(i32 %num) minsize {
 ; CHECK:       for.end:
 ; CHECK-NEXT:    ret i32 0
 ;
+; IGNORESIZE-LABEL: define i32 @negative5(
+; IGNORESIZE-SAME: i32 [[NUM:%.*]]) #[[ATTR0:[0-9]+]] {
+; IGNORESIZE-NEXT:  entry:
+; IGNORESIZE-NEXT:    br label [[FOR_BODY:%.*]]
+; IGNORESIZE:       for.body:
+; IGNORESIZE-NEXT:    [[COUNT:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[INC:%.*]], [[FOR_INC:%.*]] ]
+; IGNORESIZE-NEXT:    [[STATE:%.*]] = phi i32 [ 1, [[ENTRY]] ], [ poison, [[FOR_INC]] ]
+; IGNORESIZE-NEXT:    switch i32 [[STATE]], label [[FOR_INC_JT1:%.*]] [
+; IGNORESIZE-NEXT:    i32 1, label [[CASE1:%.*]]
+; IGNORESIZE-NEXT:    i32 2, label [[CASE2:%.*]]
+; IGNORESIZE-NEXT:    ]
+; IGNORESIZE:       for.body.jt2:
+; IGNORESIZE-NEXT:    [[COUNT_JT2:%.*]] = phi i32 [ [[INC_JT2:%.*]], [[FOR_INC_JT2:%.*]] ]
+; IGNORESIZE-NEXT:    [[STATE_JT2:%.*]] = phi i32 [ [[STATE_NEXT_JT2:%.*]], [[FOR_INC_JT2]] ]
+; IGNORESIZE-NEXT:    br label [[CASE2]]
+; IGNORESIZE:       for.body.jt1:
+; IGNORESIZE-NEXT:    [[COUNT_JT1:%.*]] = phi i32 [ [[INC_JT1:%.*]], [[FOR_INC_JT1]] ]
+; IGNORESIZE-NEXT:    [[STATE_JT1:%.*]] = phi i32 [ [[STATE_NEXT_JT1:%.*]], [[FOR_INC_JT1]] ]
+; IGNORESIZE-NEXT:    br label [[CASE1]]
+; IGNORESIZE:       case1:
+; IGNORESIZE-NEXT:    [[COUNT2:%.*]] = phi i32 [ [[COUNT_JT1]], [[FOR_BODY_JT1:%.*]] ], [ [[COUNT]], [[FOR_BODY]] ]
+; IGNORESIZE-NEXT:    br label [[FOR_INC_JT2]]
+; IGNORESIZE:       case2:
+; IGNORESIZE-NEXT:    [[COUNT1:%.*]] = phi i32 [ [[COUNT_JT2]], [[FOR_BODY_JT2:%.*]] ], [ [[COUNT]], [[FOR_BODY]] ]
+; IGNORESIZE-NEXT:    [[CMP:%.*]] = icmp eq i32 [[COUNT1]], 50
+; IGNORESIZE-NEXT:    br i1 [[CMP]], label [[FOR_INC_JT1]], label [[SI_UNFOLD_FALSE:%.*]]
+; IGNORESIZE:       si.unfold.false:
+; IGNORESIZE-NEXT:    br label [[FOR_INC_JT2]]
+; IGNORESIZE:       for.inc:
+; IGNORESIZE-NEXT:    [[INC]] = add nsw i32 undef, 1
+; IGNORESIZE-NEXT:    [[CMP_EXIT:%.*]] = icmp slt i32 [[INC]], [[NUM]]
+; IGNORESIZE-NEXT:    br i1 [[CMP_EXIT]], label [[FOR_BODY]], label [[FOR_END:%.*]]
+; IGNORESIZE:       for.inc.jt2:
+; IGNORESIZE-NEXT:    [[COUNT4:%.*]] = phi i32 [ [[COUNT1]], [[SI_UNFOLD_FALSE]] ], [ [[COUNT2]], [[CASE1]] ]
+; IGNORESIZE-NEXT:    [[STATE_NEXT_JT2]] = phi i32 [ 2, [[CASE1]] ], [ 2, [[SI_UNFOLD_FALSE]] ]
+; IGNORESIZE-NEXT:    [[INC_JT2]] = add nsw i32 [[COUNT4]], 1
+; IGNORESIZE-NEXT:    [[CMP_EXIT_JT2:%.*]] = icmp slt i32 [[INC_JT2]], [[NUM]]
+; IGNORESIZE-NEXT:    br i1 [[CMP_EXIT_JT2]], label [[FOR_BODY_JT2]], label [[FOR_END]]
+; IGNORESIZE:       for.inc.jt1:
+; IGNORESIZE-NEXT:    [[COUNT3:%.*]] = phi i32 [ [[COUNT1]], [[CASE2]] ], [ [[COUNT]], [[FOR_BODY]] ]
+; IGNORESIZE-NEXT:    [[STATE_NEXT_JT1]] = phi i32 [ 1, [[CASE2]] ], [ 1, [[FOR_BODY]] ]
+; IGNORESIZE-NEXT:    [[INC_JT1]] = add nsw i32 [[COUNT3]], 1
+; IGNORESIZE-NEXT:    [[CMP_EXIT_JT1:%.*]] = icmp slt i32 [[INC_JT1]], [[NUM]]
+; IGNORESIZE-NEXT:    br i1 [[CMP_EXIT_JT1]], label [[FOR_BODY_JT1]], label [[FOR_END]]
+; IGNORESIZE:       for.end:
+; IGNORESIZE-NEXT:    ret i32 0
+;
+
 entry:
   br label %for.body
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/83049


More information about the llvm-branch-commits mailing list