Skip to content
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

[MachineOutliner] Sort by Benefit to Cost Ratio #90264

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 4 additions & 2 deletions llvm/lib/CodeGen/MachineOutliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -828,10 +828,12 @@ bool MachineOutliner::outline(Module &M,
<< "\n");
bool OutlinedSomething = false;

// Sort by benefit. The most beneficial functions should be outlined first.
// Sort by priority where priority := getNotOutlinedCost / getOutliningCost.
// The function with highest priority should be outlined first.
stable_sort(FunctionList,
[](const OutlinedFunction &LHS, const OutlinedFunction &RHS) {
return LHS.getBenefit() > RHS.getBenefit();
return LHS.getNotOutlinedCost() * RHS.getOutliningCost() >
RHS.getNotOutlinedCost() * LHS.getOutliningCost();
});

// Walk over each function, outlining them as we go along. Functions are
Expand Down
96 changes: 96 additions & 0 deletions llvm/test/CodeGen/AArch64/machine-outliner-sort-per-priority.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
; This tests the order in which functions are outlined in MachineOutliner
; There are TWO key OutlinedFunction in FunctionList
;
; ===================== First One =====================
; ```
; mov w0, #1
; mov w1, #2
; mov w2, #3
; mov w3, #4
; mov w4, #5
; ```
; It has:
; - `SequenceSize=20` and `OccurrenceCount=6`
; - each Candidate has `CallOverhead=12` and `FrameOverhead=4`
; - `NotOutlinedCost=20*6=120` and `OutliningCost=12*6+20+4=96`
; - `Benefit=120-96=24` and `Priority=120/96=1.25`
;
; ===================== Second One =====================
; ```
; mov w6, #6
; mov w7, #7
; b
; ```
; It has:
; - `SequenceSize=12` and `OccurrenceCount=4`
; - each Candidate has `CallOverhead=4` and `FrameOverhead=0`
; - `NotOutlinedCost=12*4=48` and `OutliningCost=4*4+12+0=28`
; - `Benefit=120-96=20` and `Priority=48/28=1.71`
xuanzhang816 marked this conversation as resolved.
Show resolved Hide resolved
;
; Note that the first one has higher benefit, but lower priority.
; Hence, when outlining per priority, the second one will be outlined first.

; RUN: llc %s -enable-machine-outliner=always -filetype=obj -o %t
; RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=CHECK-SORT-BY-PRIORITY

; RUN: llc %s -enable-machine-outliner=always -outliner-benefit-threshold=22 -filetype=obj -o %t
; RUN: llvm-objdump -d %t | FileCheck %s --check-prefix=CHECK-THRESHOLD


target datalayout = "e-m:o-i64:64-i128:128-n32:64-S128"
target triple = "arm64-apple-macosx14.0.0"

declare i32 @_Z3fooiiii(i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef, i32 noundef)

define i32 @_Z2f1v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 11, i32 noundef 6, i32 noundef 7)
ret i32 %1
}

define i32 @_Z2f2v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 12, i32 noundef 6, i32 noundef 7)
ret i32 %1
}

define i32 @_Z2f3v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 13, i32 noundef 6, i32 noundef 7)
ret i32 %1
}

define i32 @_Z2f4v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 14, i32 noundef 6, i32 noundef 7)
ret i32 %1
}

define i32 @_Z2f5v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 15, i32 noundef 8, i32 noundef 9)
ret i32 %1
}

define i32 @_Z2f6v() minsize {
%1 = tail call i32 @_Z3fooiiii(i32 noundef 1, i32 noundef 2, i32 noundef 3, i32 noundef 4, i32 noundef 5, i32 noundef 16, i32 noundef 9, i32 noundef 8)
ret i32 %1
}

; CHECK-SORT-BY-PRIORITY: <_OUTLINED_FUNCTION_0>:
; CHECK-SORT-BY-PRIORITY-NEXT: mov w6, #0x6
; CHECK-SORT-BY-PRIORITY-NEXT: mov w7, #0x7
; CHECK-SORT-BY-PRIORITY-NEXT: b

; CHECK-SORT-BY-PRIORITY: <_OUTLINED_FUNCTION_1>:
; CHECK-SORT-BY-PRIORITY-NEXT: mov w0, #0x1
; CHECK-SORT-BY-PRIORITY-NEXT: mov w1, #0x2
; CHECK-SORT-BY-PRIORITY-NEXT: mov w2, #0x3
; CHECK-SORT-BY-PRIORITY-NEXT: mov w3, #0x4
; CHECK-SORT-BY-PRIORITY-NEXT: mov w4, #0x5
; CHECK-SORT-BY-PRIORITY-NEXT: ret

; CHECK-THRESHOLD: <_OUTLINED_FUNCTION_0>:
; CHECK-THRESHOLD-NEXT: mov w0, #0x1
; CHECK-THRESHOLD-NEXT: mov w1, #0x2
; CHECK-THRESHOLD-NEXT: mov w2, #0x3
; CHECK-THRESHOLD-NEXT: mov w3, #0x4
; CHECK-THRESHOLD-NEXT: mov w4, #0x5
; CHECK-THRESHOLD-NEXT: ret

; CHECK-THRESHOLD-NOT: <_OUTLINED_FUNCTION_1>:
80 changes: 28 additions & 52 deletions llvm/test/CodeGen/ARM/machine-outliner-calls.mir
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ body: |
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -4
; CHECK: frame-setup CFI_INSTRUCTION offset $r4, -8
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: bb.1:
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: bb.2:
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: bb.3:
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: bb.4:
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: bb.5:
; CHECK: $sp = frame-destroy LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r4, def $lr
; CHECK: BX_RET 14 /* CC::al */, $noreg
Expand Down Expand Up @@ -139,13 +139,13 @@ body: |
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -4
; CHECK: frame-setup CFI_INSTRUCTION offset $r4, -8
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: bb.1:
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: bb.2:
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: bb.3:
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: bb.4:
; CHECK: $sp = frame-destroy LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r4, def $lr
; CHECK: BX_RET 14 /* CC::al */, $noreg
Expand Down Expand Up @@ -245,19 +245,19 @@ body: |
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -4
; CHECK: frame-setup CFI_INSTRUCTION offset $r4, -8
; CHECK: BL @"\01mcount", csr_aapcs, implicit-def dead $lr, implicit $sp
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: bb.1:
; CHECK: BL @"\01mcount", csr_aapcs, implicit-def dead $lr, implicit $sp
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: bb.2:
; CHECK: BL @"\01mcount", csr_aapcs, implicit-def dead $lr, implicit $sp
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: bb.3:
; CHECK: BL @"\01mcount", csr_aapcs, implicit-def dead $lr, implicit $sp
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: bb.4:
; CHECK: BL @"\01mcount", csr_aapcs, implicit-def dead $lr, implicit $sp
; CHECK: BL @OUTLINED_FUNCTION_2
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: bb.5:
; CHECK: $sp = frame-destroy LDMIA_UPD $sp, 14 /* CC::al */, $noreg, def $r4, def $lr
; CHECK: BX_RET 14 /* CC::al */, $noreg
Expand Down Expand Up @@ -307,38 +307,17 @@ body: |
bb.0:
BX_RET 14, $noreg


; CHECK-LABEL: name: OUTLINED_FUNCTION_0
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r7, $r6, $r5, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8, $lr
; CHECK: early-clobber $sp = frame-setup STR_PRE_IMM killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -8
; CHECK: BL @bar, implicit-def dead $lr, implicit $sp
; CHECK: $r0 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r3 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r4 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $lr, $sp = frame-destroy LDR_POST_IMM $sp, $noreg, 8, 14 /* CC::al */, $noreg
; CHECK: MOVPCLR 14 /* CC::al */, $noreg

; CHECK-LABEL: name: OUTLINED_FUNCTION_1
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r7, $r6, $r5, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8, $lr
; CHECK: early-clobber $sp = frame-setup STR_PRE_IMM killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -8
; CHECK: BL @bar, implicit-def dead $lr, implicit $sp
; CHECK: liveins: $r11, $r10, $r9, $r8, $r7, $r6, $r5, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8
; CHECK: $r0 = MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r3 = MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r4 = MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $lr, $sp = frame-destroy LDR_POST_IMM $sp, $noreg, 8, 14 /* CC::al */, $noreg
; CHECK: TAILJMPd @bar, implicit $sp

; CHECK-LABEL: name: OUTLINED_FUNCTION_2
; CHECK-LABEL: name: OUTLINED_FUNCTION_1
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r7, $r6, $r5, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8
; CHECK: $r0 = MOVi 3, 14 /* CC::al */, $noreg, $noreg
Expand All @@ -348,31 +327,28 @@ body: |
; CHECK: $r4 = MOVi 3, 14 /* CC::al */, $noreg, $noreg
; CHECK: MOVPCLR 14 /* CC::al */, $noreg

; CHECK-LABEL: name: OUTLINED_FUNCTION_2
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r7, $r6, $r5, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8
; CHECK: $r0 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r3 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r4 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: MOVPCLR 14 /* CC::al */, $noreg

; CHECK-LABEL: name: OUTLINED_FUNCTION_3
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r6, $r5, $r4, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8, $lr
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -8
; CHECK: tBL 14 /* CC::al */, $noreg, @bar, implicit-def dead $lr, implicit $sp
; CHECK: liveins: $r11, $r10, $r9, $r8, $r6, $r5, $r4, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8
; CHECK: $r0 = t2MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = t2MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = t2MOVi 2, 14 /* CC::al */, $noreg, $noreg
; CHECK: $lr, $sp = frame-destroy t2LDR_POST $sp, 8, 14 /* CC::al */, $noreg
; CHECK: tTAILJMPdND @bar, 14 /* CC::al */, $noreg, implicit $sp

; CHECK-LABEL: name: OUTLINED_FUNCTION_4
; CHECK: bb.0:
; CHECK: liveins: $r11, $r10, $r9, $r8, $r6, $r5, $r4, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8, $lr
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: frame-setup CFI_INSTRUCTION def_cfa_offset 8
; CHECK: frame-setup CFI_INSTRUCTION offset $lr, -8
; CHECK: tBL 14 /* CC::al */, $noreg, @bar, implicit-def dead $lr, implicit $sp
; CHECK: liveins: $r11, $r10, $r9, $r8, $r6, $r5, $r4, $d15, $d14, $d13, $d12, $d11, $d10, $d9, $d8
; CHECK: $r0 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $lr, $sp = frame-destroy t2LDR_POST $sp, 8, 14 /* CC::al */, $noreg
; CHECK: tBX_RET 14 /* CC::al */, $noreg



33 changes: 15 additions & 18 deletions llvm/test/CodeGen/ARM/machine-outliner-default.mir
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ body: |
; CHECK: bb.0:
; CHECK: liveins: $lr
; CHECK: early-clobber $sp = frame-setup STR_PRE_IMM killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: $lr, $sp = frame-destroy LDR_POST_IMM $sp, $noreg, 8, 14 /* CC::al */, $noreg
; CHECK: bb.1:
; CHECK: liveins: $lr, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: early-clobber $sp = frame-setup STR_PRE_IMM killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: $lr, $sp = frame-destroy LDR_POST_IMM $sp, $noreg, 8, 14 /* CC::al */, $noreg
; CHECK: bb.2:
; CHECK: liveins: $lr, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: early-clobber $sp = frame-setup STR_PRE_IMM killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: BL @OUTLINED_FUNCTION_0
; CHECK: BL @OUTLINED_FUNCTION_1
; CHECK: $lr, $sp = frame-destroy LDR_POST_IMM $sp, $noreg, 8, 14 /* CC::al */, $noreg
; CHECK: bb.3:
; CHECK: liveins: $lr, $r6, $r7, $r8, $r9, $r10, $r11
Expand Down Expand Up @@ -73,17 +73,17 @@ body: |
; CHECK: bb.0:
; CHECK: liveins: $lr
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_1
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_0
; CHECK: $lr, $sp = frame-destroy t2LDR_POST $sp, 8, 14 /* CC::al */, $noreg
; CHECK: bb.1:
; CHECK: liveins: $lr, $r4, $r5, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_1
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_0
; CHECK: $lr, $sp = frame-destroy t2LDR_POST $sp, 8, 14 /* CC::al */, $noreg
; CHECK: bb.2:
; CHECK: liveins: $lr, $r4, $r5, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: early-clobber $sp = frame-setup t2STR_PRE killed $lr, $sp, -8, 14 /* CC::al */, $noreg
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_1
; CHECK: tBL 14 /* CC::al */, $noreg, @OUTLINED_FUNCTION_0
; CHECK: $lr, $sp = frame-destroy t2LDR_POST $sp, 8, 14 /* CC::al */, $noreg
; CHECK: bb.3:
; CHECK: liveins: $lr, $r4, $r5, $r6, $r7, $r8, $r9, $r10, $r11
Expand Down Expand Up @@ -114,6 +114,15 @@ body: |

; CHECK-LABEL: name: OUTLINED_FUNCTION_0
; CHECK: bb.0:
; CHECK: liveins: $lr, $r4, $r5, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: $r0 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r3 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: tBX_RET 14 /* CC::al */, $noreg

; CHECK-LABEL: name: OUTLINED_FUNCTION_1
; CHECK: bb.0:
; CHECK: liveins: $lr, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: $r0 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
Expand All @@ -122,15 +131,3 @@ body: |
; CHECK: $r4 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r5 = MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: MOVPCLR 14 /* CC::al */, $noreg

; CHECK-LABEL: name: OUTLINED_FUNCTION_1
; CHECK: bb.0:
; CHECK: liveins: $lr, $r4, $r5, $r6, $r7, $r8, $r9, $r10, $r11
; CHECK: $r0 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r1 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r2 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: $r3 = t2MOVi 1, 14 /* CC::al */, $noreg, $noreg
; CHECK: tBX_RET 14 /* CC::al */, $noreg



Loading
Loading