[llvm] [BOLT] Improve BinaryFunction::inferFallThroughCounts() (PR #105450)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 20 19:08:52 PDT 2024


https://github.com/ShatianWang updated https://github.com/llvm/llvm-project/pull/105450

>From e903d5fc4e7bffc51033416567e3954f7ebb9b69 Mon Sep 17 00:00:00 2001
From: Shatian Wang <shatian at meta.com>
Date: Tue, 20 Aug 2024 16:14:32 -0700
Subject: [PATCH 1/4] [BOLT] Improve BinaryFunction::inferFallThroughCounts()

---
 bolt/lib/Core/BinaryFunctionProfile.cpp |  3 +-
 bolt/test/X86/infer-fall-throughs.s     | 71 +++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 1 deletion(-)
 create mode 100644 bolt/test/X86/infer-fall-throughs.s

diff --git a/bolt/lib/Core/BinaryFunctionProfile.cpp b/bolt/lib/Core/BinaryFunctionProfile.cpp
index 55ebe5fc900e65..726da6a9d0829f 100644
--- a/bolt/lib/Core/BinaryFunctionProfile.cpp
+++ b/bolt/lib/Core/BinaryFunctionProfile.cpp
@@ -336,7 +336,8 @@ void BinaryFunction::inferFallThroughCounts() {
       if (SuccBI.Count == 0) {
         SuccBI.Count = Inferred;
         SuccBI.MispredictedCount = BinaryBasicBlock::COUNT_INFERRED;
-        Succ->ExecutionCount += Inferred;
+        Succ->ExecutionCount =
+            std::max(Succ->getKnownExecutionCount(), Inferred);
       }
     }
   }
diff --git a/bolt/test/X86/infer-fall-throughs.s b/bolt/test/X86/infer-fall-throughs.s
new file mode 100644
index 00000000000000..9a35fd33b299bb
--- /dev/null
+++ b/bolt/test/X86/infer-fall-throughs.s
@@ -0,0 +1,71 @@
+## Test that infer-fall-throughs would correctly infer the wrong fall-through
+## edge count in the example
+
+# RUN: llvm-mc --filetype=obj --triple x86_64-unknown-unknown %s -o %t.o
+# RUN: link_fdata %s %t.o %t.fdata
+# RUN: llvm-strip --strip-unneeded %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
+# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions \
+# RUN:         --print-split --print-only=chain --data=%t.fdata \
+# RUN:         --reorder-blocks=none \
+# RUN:     2>&1 | FileCheck --check-prefix=WITHOUTINFERENCE %s
+# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions --infer-fall-throughs \
+# RUN:         --print-split --print-only=chain --data=%t.fdata \
+# RUN:         --reorder-blocks=none \
+# RUN:     2>&1 | FileCheck --check-prefix=CORRECTINFERENCE %s
+
+
+# WITHOUTINFERENCE: Binary Function "chain" after split-functions
+# WITHOUTINFERENCE: {{^\.LBB00}}
+# WITHOUTINFERENCE: Successors: .Ltmp0 (mispreds: 0, count: 10), .LFT0 (mispreds: 0, count: 0)
+# WITHOUTINFERENCE: {{^\.LFT0}}
+# WITHOUTINFERENCE: Exec Count : 490
+
+# CORRECTINFERENCE: Binary Function "chain" after split-functions
+# CORRECTINFERENCE: {{^\.LBB00}}
+# CORRECTINFERENCE: Successors: .Ltmp0 (mispreds: 0, count: 10), .LFT0 (inferred count: 490)
+# CORRECTINFERENCE: {{^\.LFT0}}
+# CORRECTINFERENCE: Exec Count : 490
+
+
+        .text
+        .globl  chain
+        .type   chain, @function
+chain:
+        pushq   %rbp
+        movq    %rsp, %rbp
+        cmpl    $2, %edi
+LLstart:
+        jge     LLless
+# FDATA: 1 chain #LLstart# 1 chain #LLless# 0 10
+# FDATA: 1 chain #LLstart# 1 chain #LLmore# 0 0
+LLmore:
+        movl    $5, %eax
+LLmore_LLexit:
+        jmp     LLexit
+# FDATA: 1 chain #LLmore_LLexit# 1 chain #LLexit# 0 490
+LLless:
+        movl    $10, %eax
+LLdummy:
+        jmp     LLexit
+# FDATA: 1 chain #LLdummy# 1 chain #LLexit# 0 10
+LLexit:
+        popq    %rbp
+        ret
+LLchain_end:
+        .size   chain, LLchain_end-chain
+
+
+        .globl  main
+        .type   main, @function
+main:
+        pushq   %rbp
+        movq    %rsp, %rbp
+        movl    $1, %edi
+LLmain_chain:
+        call    chain
+# FDATA: 1 main #LLmain_chain# 1 chain 0 0 500
+        movl    $4, %edi
+        retq
+.Lmain_end:
+        .size   main, .Lmain_end-main

>From 14564bf388fce7655652deb4a60fe090049563f9 Mon Sep 17 00:00:00 2001
From: Shatian Wang <shatian at meta.com>
Date: Tue, 20 Aug 2024 18:45:23 -0700
Subject: [PATCH 2/4] fixup! [BOLT] Improve
 BinaryFunction::inferFallThroughCounts()

---
 bolt/test/X86/infer-fall-throughs.s | 65 ++++++++++-------------------
 1 file changed, 22 insertions(+), 43 deletions(-)

diff --git a/bolt/test/X86/infer-fall-throughs.s b/bolt/test/X86/infer-fall-throughs.s
index 9a35fd33b299bb..d718d37b8bad12 100644
--- a/bolt/test/X86/infer-fall-throughs.s
+++ b/bolt/test/X86/infer-fall-throughs.s
@@ -5,67 +5,46 @@
 # RUN: link_fdata %s %t.o %t.fdata
 # RUN: llvm-strip --strip-unneeded %t.o
 # RUN: %clang %cflags %t.o -o %t.exe -Wl,-q
-# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions \
-# RUN:         --print-split --print-only=chain --data=%t.fdata \
-# RUN:         --reorder-blocks=none \
+# RUN: llvm-bolt %t.exe -o %t.bolt \
+# RUN:         --print-estimate-edge-counts --data=%t.fdata \
 # RUN:     2>&1 | FileCheck --check-prefix=WITHOUTINFERENCE %s
-# RUN: llvm-bolt %t.exe -o %t.bolt --split-functions --infer-fall-throughs \
-# RUN:         --print-split --print-only=chain --data=%t.fdata \
-# RUN:         --reorder-blocks=none \
+# RUN: llvm-bolt %t.exe -o %t.bolt --infer-fall-throughs \
+# RUN:         --print-estimate-edge-counts --data=%t.fdata \
 # RUN:     2>&1 | FileCheck --check-prefix=CORRECTINFERENCE %s
 
 
-# WITHOUTINFERENCE: Binary Function "chain" after split-functions
-# WITHOUTINFERENCE: {{^\.LBB00}}
-# WITHOUTINFERENCE: Successors: .Ltmp0 (mispreds: 0, count: 10), .LFT0 (mispreds: 0, count: 0)
+# WITHOUTINFERENCE: Binary Function "main" after estimate-edge-counts
+# WITHOUTINFERENCE: {{^\.Ltmp0}}
+# WITHOUTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (mispreds: 0, count: 0)
 # WITHOUTINFERENCE: {{^\.LFT0}}
 # WITHOUTINFERENCE: Exec Count : 490
 
-# CORRECTINFERENCE: Binary Function "chain" after split-functions
-# CORRECTINFERENCE: {{^\.LBB00}}
-# CORRECTINFERENCE: Successors: .Ltmp0 (mispreds: 0, count: 10), .LFT0 (inferred count: 490)
+# CORRECTINFERENCE: Binary Function "main" after estimate-edge-counts
+# CORRECTINFERENCE: {{^\.Ltmp0}}
+# CORRECTINFERENCE: Successors: .Ltmp1 (mispreds: 0, count: 10), .LFT0 (inferred count: 490)
 # CORRECTINFERENCE: {{^\.LFT0}}
 # CORRECTINFERENCE: Exec Count : 490
 
 
-        .text
-        .globl  chain
-        .type   chain, @function
-chain:
-        pushq   %rbp
-        movq    %rsp, %rbp
-        cmpl    $2, %edi
+        .globl  main
+        .type   main, @function
+main:
+LLmain_LLstart:
+        jmp     LLstart
+# FDATA: 1 main #LLmain_LLstart# 1 main #LLstart# 0 500
 LLstart:
         jge     LLless
-# FDATA: 1 chain #LLstart# 1 chain #LLless# 0 10
-# FDATA: 1 chain #LLstart# 1 chain #LLmore# 0 0
+# FDATA: 1 main #LLstart# 1 main #LLless# 0 10
+# FDATA: 1 main #LLstart# 1 main #LLmore# 0 0
 LLmore:
         movl    $5, %eax
 LLmore_LLexit:
         jmp     LLexit
-# FDATA: 1 chain #LLmore_LLexit# 1 chain #LLexit# 0 490
+# FDATA: 1 main #LLmore_LLexit# 1 main #LLexit# 0 490
 LLless:
-        movl    $10, %eax
-LLdummy:
         jmp     LLexit
-# FDATA: 1 chain #LLdummy# 1 chain #LLexit# 0 10
+# FDATA: 1 main #LLless# 1 main #LLexit# 0 10
 LLexit:
-        popq    %rbp
         ret
-LLchain_end:
-        .size   chain, LLchain_end-chain
-
-
-        .globl  main
-        .type   main, @function
-main:
-        pushq   %rbp
-        movq    %rsp, %rbp
-        movl    $1, %edi
-LLmain_chain:
-        call    chain
-# FDATA: 1 main #LLmain_chain# 1 chain 0 0 500
-        movl    $4, %edi
-        retq
-.Lmain_end:
-        .size   main, .Lmain_end-main
+.LLmain_end:
+        .size   main, .LLmain_end-main

>From 4af894a12e0df8871b679f0a3eb00e77b5f35c75 Mon Sep 17 00:00:00 2001
From: Shatian Wang <shatian at meta.com>
Date: Tue, 20 Aug 2024 19:03:50 -0700
Subject: [PATCH 3/4] fixup! fixup! [BOLT] Improve
 BinaryFunction::inferFallThroughCounts()

---
 bolt/test/X86/infer-fall-throughs.s | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/bolt/test/X86/infer-fall-throughs.s b/bolt/test/X86/infer-fall-throughs.s
index d718d37b8bad12..086f37390f6a44 100644
--- a/bolt/test/X86/infer-fall-throughs.s
+++ b/bolt/test/X86/infer-fall-throughs.s
@@ -33,17 +33,14 @@ LLmain_LLstart:
         jmp     LLstart
 # FDATA: 1 main #LLmain_LLstart# 1 main #LLstart# 0 500
 LLstart:
-        jge     LLless
-# FDATA: 1 main #LLstart# 1 main #LLless# 0 10
+        jge     LLexit
+# FDATA: 1 main #LLstart# 1 main #LLexit# 0 10
 # FDATA: 1 main #LLstart# 1 main #LLmore# 0 0
 LLmore:
         movl    $5, %eax
 LLmore_LLexit:
         jmp     LLexit
 # FDATA: 1 main #LLmore_LLexit# 1 main #LLexit# 0 490
-LLless:
-        jmp     LLexit
-# FDATA: 1 main #LLless# 1 main #LLexit# 0 10
 LLexit:
         ret
 .LLmain_end:

>From d2f507a3b3f493fb8421834c01f36f10c0dae194 Mon Sep 17 00:00:00 2001
From: Shatian Wang <shatian at meta.com>
Date: Tue, 20 Aug 2024 19:08:36 -0700
Subject: [PATCH 4/4] fixup! fixup! fixup! [BOLT] Improve
 BinaryFunction::inferFallThroughCounts()

---
 bolt/test/X86/infer-fall-throughs.s | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/bolt/test/X86/infer-fall-throughs.s b/bolt/test/X86/infer-fall-throughs.s
index 086f37390f6a44..3289a8ea4aec51 100644
--- a/bolt/test/X86/infer-fall-throughs.s
+++ b/bolt/test/X86/infer-fall-throughs.s
@@ -38,9 +38,7 @@ LLstart:
 # FDATA: 1 main #LLstart# 1 main #LLmore# 0 0
 LLmore:
         movl    $5, %eax
-LLmore_LLexit:
-        jmp     LLexit
-# FDATA: 1 main #LLmore_LLexit# 1 main #LLexit# 0 490
+# FDATA: 1 main #LLmore# 1 main #LLexit# 0 490
 LLexit:
         ret
 .LLmain_end:



More information about the llvm-commits mailing list