[llvm-branch-commits] [llvm] [BOLT] Hash-based function matching (PR #95821)
shaw young via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Jun 18 14:19:56 PDT 2024
https://github.com/shawbyoung updated https://github.com/llvm/llvm-project/pull/95821
>From 92212c96ea169d26ac10bf8d750539bc5dd72c49 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Mon, 17 Jun 2024 15:39:02 -0700
Subject: [PATCH 1/6] spr amend
Created using spr 1.3.4
---
bolt/lib/Profile/YAMLProfileReader.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index f0fcb1c130002..2bca83c9d11ec 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -421,6 +421,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
StrictBinaryFunctionHashes.reserve(BC.getBinaryFunctions().size());
for (auto& [_, BF] : BC.getBinaryFunctions()) {
+ if (!ProfiledFunctions.count(&BF))
+ continue;
StrictBinaryFunctionHashes[BF.getHash()] = &BF;
}
>From 2497922ccc46e3189870563b1fe819b67172778d Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Mon, 17 Jun 2024 15:39:39 -0700
Subject: [PATCH 2/6] spr amend
Created using spr 1.3.4
---
bolt/lib/Profile/YAMLProfileReader.cpp | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 2bca83c9d11ec..56474a67307ed 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -417,10 +417,10 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
// Uses the strict hash of profiled and binary functions to match functions
// that are not matched by name or common name.
- std::unordered_map<size_t, BinaryFunction*> StrictBinaryFunctionHashes;
+ std::unordered_map<size_t, BinaryFunction *> StrictBinaryFunctionHashes;
StrictBinaryFunctionHashes.reserve(BC.getBinaryFunctions().size());
- for (auto& [_, BF] : BC.getBinaryFunctions()) {
+ for (auto &[_, BF] : BC.getBinaryFunctions()) {
if (!ProfiledFunctions.count(&BF))
continue;
StrictBinaryFunctionHashes[BF.getHash()] = &BF;
@@ -428,7 +428,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
for (auto YamlBF : YamlBP.Functions) {
auto It = StrictBinaryFunctionHashes.find(YamlBF.Hash);
- if (It != StrictBinaryFunctionHashes.end() && !ProfiledFunctions.count(It->second)) {
+ if (It != StrictBinaryFunctionHashes.end() &&
+ !ProfiledFunctions.count(It->second)) {
auto *BF = It->second;
matchProfileToFunction(YamlBF, *BF);
}
>From 8e7b2229a69c3795e723404c56e0d4298eef412a Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Mon, 17 Jun 2024 15:55:58 -0700
Subject: [PATCH 3/6] spr amend
Created using spr 1.3.4
---
bolt/lib/Profile/YAMLProfileReader.cpp | 2 +-
bolt/test/X86/profile-passthrough-block.test | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 56474a67307ed..779d60bce3b66 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -421,7 +421,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
StrictBinaryFunctionHashes.reserve(BC.getBinaryFunctions().size());
for (auto &[_, BF] : BC.getBinaryFunctions()) {
- if (!ProfiledFunctions.count(&BF))
+ if (ProfiledFunctions.count(&BF))
continue;
StrictBinaryFunctionHashes[BF.getHash()] = &BF;
}
diff --git a/bolt/test/X86/profile-passthrough-block.test b/bolt/test/X86/profile-passthrough-block.test
index 1b875885260dc..ed2a8117ddfc4 100644
--- a/bolt/test/X86/profile-passthrough-block.test
+++ b/bolt/test/X86/profile-passthrough-block.test
@@ -57,7 +57,7 @@ header:
functions:
- name: main
fid: 0
- hash: 0x0000000000000000
+ hash: 0x0000000000000001
exec: 1
nblocks: 6
blocks:
>From ef5f0dac9185dbb7a62345938d4f309c3379a85d Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Mon, 17 Jun 2024 15:58:22 -0700
Subject: [PATCH 4/6] spr amend
Created using spr 1.3.4
---
bolt/lib/Profile/YAMLProfileReader.cpp | 2 ++
1 file changed, 2 insertions(+)
diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index 779d60bce3b66..e3d30bfdb74e4 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -427,6 +427,8 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
}
for (auto YamlBF : YamlBP.Functions) {
+ if (YamlBF.Used)
+ continue;
auto It = StrictBinaryFunctionHashes.find(YamlBF.Hash);
if (It != StrictBinaryFunctionHashes.end() &&
!ProfiledFunctions.count(It->second)) {
>From 41ce2897a445e47dfe685da66b4af080824e78ed Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Mon, 17 Jun 2024 16:00:27 -0700
Subject: [PATCH 5/6] spr amend
Created using spr 1.3.4
---
bolt/test/X86/profile-passthrough-block.test | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/bolt/test/X86/profile-passthrough-block.test b/bolt/test/X86/profile-passthrough-block.test
index ed2a8117ddfc4..1b875885260dc 100644
--- a/bolt/test/X86/profile-passthrough-block.test
+++ b/bolt/test/X86/profile-passthrough-block.test
@@ -57,7 +57,7 @@ header:
functions:
- name: main
fid: 0
- hash: 0x0000000000000001
+ hash: 0x0000000000000000
exec: 1
nblocks: 6
blocks:
>From 99cf8918e945356d12b5d29fe8174c610f305559 Mon Sep 17 00:00:00 2001
From: shawbyoung <shawbyoung at gmail.com>
Date: Tue, 18 Jun 2024 14:19:45 -0700
Subject: [PATCH 6/6] spr amend
Created using spr 1.3.4
---
bolt/lib/Profile/YAMLProfileReader.cpp | 51 +++++++-------
.../X86/hashing-based-function-matching.test | 67 +++++++++++++++++++
2 files changed, 93 insertions(+), 25 deletions(-)
create mode 100644 bolt/test/X86/hashing-based-function-matching.test
diff --git a/bolt/lib/Profile/YAMLProfileReader.cpp b/bolt/lib/Profile/YAMLProfileReader.cpp
index e3d30bfdb74e4..78d46eea5c728 100644
--- a/bolt/lib/Profile/YAMLProfileReader.cpp
+++ b/bolt/lib/Profile/YAMLProfileReader.cpp
@@ -363,9 +363,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
return Profile.Hash == static_cast<uint64_t>(BF.getHash());
};
- // We have to do 2 passes since LTO introduces an ambiguity in function
- // names. The first pass assigns profiles that match 100% by name and
- // by hash. The second pass allows name ambiguity for LTO private functions.
+ // This first pass assigns profiles that match 100% by name and by hash.
for (auto [YamlBF, BF] : llvm::zip_equal(YamlBP.Functions, ProfileBFs)) {
if (!BF)
continue;
@@ -383,6 +381,30 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
matchProfileToFunction(YamlBF, Function);
}
+ // Uses the strict hash of profiled and binary functions to match functions
+ // that are not matched by name or common name.
+ std::unordered_map<size_t, BinaryFunction *> StrictBinaryFunctionHashes;
+ StrictBinaryFunctionHashes.reserve(BC.getBinaryFunctions().size());
+
+ for (auto &[_, BF] : BC.getBinaryFunctions()) {
+ if (ProfiledFunctions.count(&BF))
+ continue;
+ BF.computeHash(YamlBP.Header.IsDFSOrder, YamlBP.Header.HashFunction);
+ StrictBinaryFunctionHashes[BF.getHash()] = &BF;
+ }
+
+ for (auto YamlBF : YamlBP.Functions) {
+ if (YamlBF.Used)
+ continue;
+ auto It = StrictBinaryFunctionHashes.find(YamlBF.Hash);
+ if (It != StrictBinaryFunctionHashes.end() &&
+ !ProfiledFunctions.count(It->second)) {
+ auto *BF = It->second;
+ matchProfileToFunction(YamlBF, *BF);
+ }
+ }
+
+ // This second pass allows name ambiguity for LTO private functions.
for (const auto &[CommonName, LTOProfiles] : LTOCommonNameMap) {
if (!LTOCommonNameFunctionMap.contains(CommonName))
continue;
@@ -415,33 +437,12 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
if (!YamlBF.Used && BF && !ProfiledFunctions.count(BF))
matchProfileToFunction(YamlBF, *BF);
- // Uses the strict hash of profiled and binary functions to match functions
- // that are not matched by name or common name.
- std::unordered_map<size_t, BinaryFunction *> StrictBinaryFunctionHashes;
- StrictBinaryFunctionHashes.reserve(BC.getBinaryFunctions().size());
-
- for (auto &[_, BF] : BC.getBinaryFunctions()) {
- if (ProfiledFunctions.count(&BF))
- continue;
- StrictBinaryFunctionHashes[BF.getHash()] = &BF;
- }
-
- for (auto YamlBF : YamlBP.Functions) {
- if (YamlBF.Used)
- continue;
- auto It = StrictBinaryFunctionHashes.find(YamlBF.Hash);
- if (It != StrictBinaryFunctionHashes.end() &&
- !ProfiledFunctions.count(It->second)) {
- auto *BF = It->second;
- matchProfileToFunction(YamlBF, *BF);
- }
- }
-
for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions)
if (!YamlBF.Used && opts::Verbosity >= 1)
errs() << "BOLT-WARNING: profile ignored for function " << YamlBF.Name
<< '\n';
+
// Set for parseFunctionProfile().
NormalizeByInsnCount = usesEvent("cycles") || usesEvent("instructions");
NormalizeByCalls = usesEvent("branches");
diff --git a/bolt/test/X86/hashing-based-function-matching.test b/bolt/test/X86/hashing-based-function-matching.test
new file mode 100644
index 0000000000000..3c2cd834f90bf
--- /dev/null
+++ b/bolt/test/X86/hashing-based-function-matching.test
@@ -0,0 +1,67 @@
+## Test YAMLProfileReader support for pass-through blocks in non-matching edges:
+## match the profile edge A -> C to the CFG with blocks A -> B -> C.
+
+# REQUIRES: system-linux
+# RUN: split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-unknown %t/main.s -o %t.o
+# RUN: %clang %cflags %t.o -o %t.exe -Wl,-q -nostdlib
+# RUN: llvm-bolt %t.exe -o %t.out --data %t/yaml -v=1 \
+# RUN: --print-cfg 2>&1 | FileCheck %s
+
+# CHECK: Binary Function "main" after building cfg
+# CHECK: Profile Acc : 100.0%
+# CHECK-NOT: BOLT-WARNING: no successor for block .LFT0 that matches index 3 or block .Ltmp0
+
+#--- main.s
+.globl main
+.type main, @function
+main:
+ .cfi_startproc
+.LBB00:
+ pushq %rbp
+ movq %rsp, %rbp
+ subq $16, %rsp
+ testq %rax, %rax
+ js .LBB03
+.LBB01:
+ jne .LBB04
+.LBB02:
+ nop
+.LBB03:
+ xorl %eax, %eax
+ addq $16, %rsp
+ popq %rbp
+ retq
+.LBB04:
+ xorl %eax, %eax
+ addq $16, %rsp
+ popq %rbp
+ retq
+## For relocations against .text
+.LBB05:
+ call exit
+ .cfi_endproc
+ .size main, .-main
+
+#--- yaml
+---
+header:
+ profile-version: 1
+ binary-name: 'profile-passthrough-block.s.tmp.exe'
+ binary-build-id: '<unknown>'
+ profile-flags: [ lbr ]
+ profile-origin: branch profile reader
+ profile-events: ''
+ dfs-order: false
+ hash-func: xxh3
+functions:
+ - name: main2
+ fid: 0
+ hash: 0x72f82deaa6fe65fb
+ exec: 1
+ nblocks: 6
+ blocks:
+ - bid: 1
+ insns: 1
+ succ: [ { bid: 3, cnt: 1} ]
+...
More information about the llvm-branch-commits
mailing list