Skip to content

Commit

Permalink
[Outliner] Add an option to only enable outlining of patterns above a…
Browse files Browse the repository at this point in the history
… certain threshold

Outlining isn't always a win when the saved instruction count is >= 1.
The overhead of representing a new function in the binary depends on
exception metadata and alignment. So parameterize this for local tuning.

Reviewed By: paquette

Differential Revision: https://reviews.llvm.org/D136774
  • Loading branch information
lanza committed Apr 8, 2023
1 parent 92910a5 commit 87c0f67
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
9 changes: 7 additions & 2 deletions llvm/lib/CodeGen/MachineOutliner.cpp
Expand Up @@ -116,6 +116,11 @@ static cl::opt<unsigned> OutlinerReruns(
cl::desc(
"Number of times to rerun the outliner after the initial outline"));

static cl::opt<unsigned> OutlinerBenefitThreshold(
"outliner-benefit-threshold", cl::init(1), cl::Hidden,
cl::desc(
"The minimum size in bytes before an outlining candidate is accepted"));

namespace {

/// Maps \p MachineInstrs to unsigned integers and stores the mappings.
Expand Down Expand Up @@ -664,7 +669,7 @@ void MachineOutliner::findCandidates(
continue;

// Is it better to outline this candidate than not?
if (OF->getBenefit() < 1) {
if (OF->getBenefit() < OutlinerBenefitThreshold) {
emitNotOutliningCheaperRemark(StringLen, CandidatesForRepeatedSeq, *OF);
continue;
}
Expand Down Expand Up @@ -840,7 +845,7 @@ bool MachineOutliner::outline(Module &M,
});

// If we made it unbeneficial to outline this function, skip it.
if (OF.getBenefit() < 1)
if (OF.getBenefit() < OutlinerBenefitThreshold)
continue;

// It's beneficial. Create the function and outline its sequence's
Expand Down
114 changes: 114 additions & 0 deletions llvm/test/CodeGen/AArch64/machine-outliner-threshold.mir
@@ -0,0 +1,114 @@
# RUN: llc -mtriple=aarch64--- -run-pass=machine-outliner -verify-machineinstrs %s -o - | FileCheck %s
# RUN: llc -mtriple=aarch64--- -outliner-benefit-threshold=10 -run-pass=machine-outliner -verify-machineinstrs %s -o - | FileCheck %s -check-prefix=THRESHOLD
--- |

define void @baz() #0 {
ret void
}

define void @bar(i32 %a) #0 {
ret void
}

attributes #0 = { noredzone }
...
---
# Check that two we outline two different sequences, one from bb1 and one from
# bb2 when the threshold is 1.
# CHECK-LABEL: bb.1:
# CHECK: BL @OUTLINED_FUNCTION_[[F0:[0-9]+]], implicit-def $lr, implicit $sp
# CHECK-NEXT: $w11 = ORRWri $wzr, 1
# CHECK-NEXT: $w11 = ORRWri $wzr, 1
# CHECK-NEXT: $w11 = ORRWri $wzr, 2
# CHECK-NEXT: BL @baz, implicit-def dead $lr, implicit $sp
# CHECK-NEXT: BL @OUTLINED_FUNCTION_[[F0]], implicit-def $lr, implicit $sp
# CHECK-NEXT: $w11 = ORRWri $wzr, 1
# CHECK-NEXT: $w11 = ORRWri $wzr, 1
# CHECK-NEXT: $w8 = ORRWri $wzr, 0
# CHECK-NOT: $w11 = KILL renamable $w11, implicit killed $w11

# CHECK-LABEL: bb.2:
# CHECK: BL @OUTLINED_FUNCTION_[[F1:[0-9]+]], implicit-def $lr, implicit $sp
# CHECK-NEXT: $w9 = ORRWri $wzr, 0
# CHECK-NEXT: BL @OUTLINED_FUNCTION_[[F1]], implicit-def $lr, implicit $sp
# CHECK-NEXT: $w8 = ORRWri $wzr, 0
# CHECK-NOT: $w11 = KILL renamable $w11, implicit killed $w11

# Check that the sequences in bb.2 don't get outlined with a threshold of 10 but
# the sequences in bb.1 do.
# THRESHOLD-LABEL: bb.1:
# THRESHOLD: BL @OUTLINED_FUNCTION_[[F0:[0-9]+]], implicit-def $lr, implicit $sp
# THRESHOLD-NEXT: $w11 = ORRWri $wzr, 1
# THRESHOLD-NEXT: $w11 = ORRWri $wzr, 1
# THRESHOLD-NEXT: $w11 = ORRWri $wzr, 2
# THRESHOLD-NEXT: BL @baz, implicit-def dead $lr, implicit $sp
# THRESHOLD-NEXT: BL @OUTLINED_FUNCTION_[[F0]], implicit-def $lr, implicit $sp
# THRESHOLD-NEXT: $w11 = ORRWri $wzr, 1
# THRESHOLD-NEXT: $w11 = ORRWri $wzr, 1
# THRESHOLD-NEXT: $w8 = ORRWri $wzr, 0
# THRESHOLD-NOT: $w11 = KILL renamable $w11, implicit killed $w11

# THRESHOLD-LABEL: bb.2:
# THRESHOLD-NOT: BL @OUTLINED_FUNCTION
name: bar
tracksRegLiveness: true
body: |
bb.0:
liveins: $w0, $lr, $w8
$sp = frame-setup SUBXri $sp, 32, 0
$fp = frame-setup ADDXri $sp, 16, 0
bb.1:
BL @baz, implicit-def dead $lr, implicit $sp
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
$w11 = KILL renamable $w11, implicit killed $w11
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
BL @baz, implicit-def dead $lr, implicit $sp
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 2
BL @baz, implicit-def dead $lr, implicit $sp
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
BL @baz, implicit-def dead $lr, implicit $sp
$w11 = ORRWri $wzr, 1
$w11 = ORRWri $wzr, 1
$w8 = ORRWri $wzr, 0
bb.2:
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$x15 = ADDXri $sp, 48, 0;
$w9 = ORRWri $wzr, 0
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$w15 = ORRWri $wzr, 1
$x15 = ADDXri $sp, 48, 0;
$w8 = ORRWri $wzr, 0
bb.3:
$fp, $lr = LDPXi $sp, 2
$sp = ADDXri $sp, 32, 0
RET undef $lr
...
---
name: baz
tracksRegLiveness: true
body: |
bb.0:
liveins: $w0, $lr, $w8
RET undef $lr
# CHECK-LABEL: name: OUTLINED_FUNCTION_{{[0-9]}}
# CHECK-LABEL: name: OUTLINED_FUNCTION_{{[1-9]}}

# THRESHOLD-LABEL: name: OUTLINED_FUNCTION_{{[0-9]}}
# THRESHOLD-NOT: name: OUTLINED_FUNCTION_{{[1-9]}}

0 comments on commit 87c0f67

Please sign in to comment.