Skip to content

[llvm][dfa-jump-threading] Allow DFAJumpThreading with optsize #83049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion llvm/lib/Passes/PassBuilderPipelines.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its rather unfortunate that we need 2 flags to enable a single behavior ... Is there a common place we can put this flag and use it here and in DFAJumpThreading.cpp?


// TODO: turn on and remove flag
static cl::opt<bool> EnablePGOForceFunctionAttrs(
Expand Down Expand Up @@ -717,7 +722,8 @@ PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,

// Re-consider control flow based optimizations after redundancy elimination,
// redo DCE, etc.
if (EnableDFAJumpThreading && Level.getSizeLevel() == 0)
if (EnableDFAJumpThreading &&
((Level.getSizeLevel() == 0) || DFAJumpThreadingOptSize))
FPM.addPass(DFAJumpThreadingPass());

FPM.addPass(JumpThreadingPass());
Expand Down
7 changes: 6 additions & 1 deletion llvm/lib/Transforms/Scalar/DFAJumpThreading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
48 changes: 48 additions & 0 deletions llvm/test/Transforms/DFAJumpThreading/negative.ll
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -186,6 +187,53 @@ define i32 @negative5(i32 %num) minsize {
; CHECK: for.end:
; CHECK-NEXT: ret i32 0
;
; IGNORESIZE-LABEL: define i32 @negative5(
; 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

Expand Down