Skip to content

Commit

Permalink
[TailDuplication] Add option to enable tail duplication of fallthroug…
Browse files Browse the repository at this point in the history
…h BBs

During the TailDuplication pass, fallthrough BBs are
not duplicated. This is generally acceptable, but in
scenarios where a loop contains a large switch case
and the latch BB is a fallthrough, duplicating the
latch BB can eliminate jump instructions in its
predecessors. This patch introduces the
`tail-dup-fallthrough-bbs` option to enable the
duplication of fallthrough BBs.

PR: #727.

Signed-off-by: Vladimir Radosavljevic <[email protected]>
  • Loading branch information
vladimirradosavljevic authored and akiramenai committed Oct 30, 2024
1 parent e3e4c2f commit 2a03cc8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
11 changes: 10 additions & 1 deletion llvm/lib/CodeGen/TailDuplicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ static cl::opt<bool>
static cl::opt<unsigned> TailDupLimit("tail-dup-limit", cl::init(~0U),
cl::Hidden);

// EraVM local begin
static cl::opt<bool>
TailDupFallthroughBBs("tail-dup-fallthrough-bbs",
cl::desc("Tail duplicate fallthrough basic blocks"),
cl::init(false), cl::Hidden);
// EraVM local end

void TailDuplicator::initMF(MachineFunction &MFin, bool PreRegAlloc,
const MachineBranchProbabilityInfo *MBPIin,
MBFIWrapper *MBFIin,
Expand Down Expand Up @@ -559,8 +566,10 @@ bool TailDuplicator::shouldTailDuplicate(bool IsSimple,
// When doing tail-duplication during layout, the block ordering is in flux,
// so canFallThrough returns a result based on incorrect information and
// should just be ignored.
if (!LayoutMode && TailBB.canFallThrough())
// EraVM local begin
if (!TailDupFallthroughBBs && !LayoutMode && TailBB.canFallThrough())
return false;
// EraVM local end

// Don't try to tail-duplicate single-block loops.
if (TailBB.isSuccessor(&TailBB))
Expand Down
11 changes: 3 additions & 8 deletions llvm/test/CodeGen/EraVM/tail-dup-fallthrough-bbs.mir
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# NOTE: Assertions have been autogenerated by utils/update_mir_test_checks.py UTC_ARGS: --version 2
# RUN: llc -x mir -run-pass tailduplication -verify-machineinstrs -compile-twice=false < %s | FileCheck %s
# RUN: llc -x mir -run-pass tailduplication -tail-dup-fallthrough-bbs -verify-machineinstrs -compile-twice=false < %s | FileCheck %s

--- |

Expand All @@ -15,17 +15,12 @@ tracksRegLiveness: true
body: |
; CHECK-LABEL: name: test
; CHECK: bb.0:
; CHECK-NEXT: successors: %bb.1(0x80000000)
; CHECK-NEXT: liveins: $r1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: J %bb.1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.1:
; CHECK-NEXT: successors: %bb.3(0x40000000), %bb.2(0x40000000)
; CHECK-NEXT: liveins: $r1
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: dead $r0 = SUBxrr_v i256 10, $r1, i256 0, implicit-def $flags
; CHECK-NEXT: JCl %bb.3, i256 3, implicit killed $flags
; CHECK-NEXT: JCl %bb.3, 3, implicit $flags
; CHECK-NEXT: J %bb.2
; CHECK-NEXT: {{ $}}
; CHECK-NEXT: bb.2:
; CHECK-NEXT: liveins: $r1
Expand Down

0 comments on commit 2a03cc8

Please sign in to comment.