Skip to content

Commit 97025bd

Browse files
authored
[BOLT] Use getLocationName in YAMLProfileWriter (#92493)
Disambiguate local functions using the containing file symbol in BAT mode. Make local function naming consistent across BAT fdata and YAML profiles. Test Plan: updated register-fragments-bolt-symbols.s
1 parent 935b946 commit 97025bd

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

bolt/include/bolt/Profile/DataAggregator.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#define BOLT_PROFILE_DATA_AGGREGATOR_H
1616

1717
#include "bolt/Profile/DataReader.h"
18+
#include "bolt/Profile/YAMLProfileWriter.h"
1819
#include "llvm/ADT/StringRef.h"
1920
#include "llvm/Support/Error.h"
2021
#include "llvm/Support/Program.h"
@@ -248,7 +249,7 @@ class DataAggregator : public DataReader {
248249
BinaryFunction *getBATParentFunction(const BinaryFunction &Func) const;
249250

250251
/// Retrieve the location name to be used for samples recorded in \p Func.
251-
StringRef getLocationName(const BinaryFunction &Func) const;
252+
static StringRef getLocationName(const BinaryFunction &Func, bool BAT);
252253

253254
/// Semantic actions - parser hooks to interpret parsed perf samples
254255
/// Register a sample (non-LBR mode), i.e. a new hit at \p Address
@@ -490,6 +491,8 @@ class DataAggregator : public DataReader {
490491
/// Parse the output generated by "perf buildid-list" to extract build-ids
491492
/// and return a file name matching a given \p FileBuildID.
492493
std::optional<StringRef> getFileNameForBuildID(StringRef FileBuildID);
494+
495+
friend class YAMLProfileWriter;
493496
};
494497
} // namespace bolt
495498
} // namespace llvm

bolt/lib/Profile/DataAggregator.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,8 @@ DataAggregator::getBATParentFunction(const BinaryFunction &Func) const {
674674
return nullptr;
675675
}
676676

677-
StringRef DataAggregator::getLocationName(const BinaryFunction &Func) const {
677+
StringRef DataAggregator::getLocationName(const BinaryFunction &Func,
678+
bool BAT) {
678679
if (!BAT)
679680
return Func.getOneName();
680681

@@ -703,7 +704,7 @@ bool DataAggregator::doSample(BinaryFunction &OrigFunc, uint64_t Address,
703704
auto I = NamesToSamples.find(Func.getOneName());
704705
if (I == NamesToSamples.end()) {
705706
bool Success;
706-
StringRef LocName = getLocationName(Func);
707+
StringRef LocName = getLocationName(Func, BAT);
707708
std::tie(I, Success) = NamesToSamples.insert(
708709
std::make_pair(Func.getOneName(),
709710
FuncSampleData(LocName, FuncSampleData::ContainerTy())));
@@ -723,7 +724,7 @@ bool DataAggregator::doIntraBranch(BinaryFunction &Func, uint64_t From,
723724
FuncBranchData *AggrData = getBranchData(Func);
724725
if (!AggrData) {
725726
AggrData = &NamesToBranches[Func.getOneName()];
726-
AggrData->Name = getLocationName(Func);
727+
AggrData->Name = getLocationName(Func, BAT);
727728
setBranchData(Func, AggrData);
728729
}
729730

@@ -742,7 +743,7 @@ bool DataAggregator::doInterBranch(BinaryFunction *FromFunc,
742743
StringRef SrcFunc;
743744
StringRef DstFunc;
744745
if (FromFunc) {
745-
SrcFunc = getLocationName(*FromFunc);
746+
SrcFunc = getLocationName(*FromFunc, BAT);
746747
FromAggrData = getBranchData(*FromFunc);
747748
if (!FromAggrData) {
748749
FromAggrData = &NamesToBranches[FromFunc->getOneName()];
@@ -753,7 +754,7 @@ bool DataAggregator::doInterBranch(BinaryFunction *FromFunc,
753754
recordExit(*FromFunc, From, Mispreds, Count);
754755
}
755756
if (ToFunc) {
756-
DstFunc = getLocationName(*ToFunc);
757+
DstFunc = getLocationName(*ToFunc, BAT);
757758
ToAggrData = getBranchData(*ToFunc);
758759
if (!ToAggrData) {
759760
ToAggrData = &NamesToBranches[ToFunc->getOneName()];
@@ -2341,7 +2342,7 @@ std::error_code DataAggregator::writeBATYAML(BinaryContext &BC,
23412342
continue;
23422343
BinaryFunction *BF = BC.getBinaryFunctionAtAddress(FuncAddress);
23432344
assert(BF);
2344-
YamlBF.Name = getLocationName(*BF);
2345+
YamlBF.Name = getLocationName(*BF, BAT);
23452346
YamlBF.Id = BF->getFunctionNumber();
23462347
YamlBF.Hash = BAT->getBFHash(FuncAddress);
23472348
YamlBF.ExecCount = BF->getKnownExecutionCount();

bolt/lib/Profile/YAMLProfileWriter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "bolt/Core/BinaryBasicBlock.h"
1111
#include "bolt/Core/BinaryFunction.h"
1212
#include "bolt/Profile/BoltAddressTranslation.h"
13+
#include "bolt/Profile/DataAggregator.h"
1314
#include "bolt/Profile/ProfileReaderBase.h"
1415
#include "bolt/Rewrite/RewriteInstance.h"
1516
#include "llvm/Support/CommandLine.h"
@@ -63,7 +64,7 @@ YAMLProfileWriter::convert(const BinaryFunction &BF, bool UseDFS,
6364
BF.computeHash(UseDFS);
6465
BF.computeBlockHashes();
6566

66-
YamlBF.Name = BF.getPrintName();
67+
YamlBF.Name = DataAggregator::getLocationName(BF, BAT);
6768
YamlBF.Id = BF.getFunctionNumber();
6869
YamlBF.Hash = BF.getHash();
6970
YamlBF.NumBasicBlocks = BF.size();

bolt/test/X86/register-fragments-bolt-symbols.s

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
# RUN: FileCheck --input-file %t.bat.fdata --check-prefix=CHECK-FDATA %s
1919
# RUN: FileCheck --input-file %t.bat.yaml --check-prefix=CHECK-YAML %s
2020

21+
# RUN: link_fdata --no-redefine %s %t.bolt %t.preagg2 PREAGG2
22+
# PREAGG2: B X:0 #chain# 1 0
23+
# RUN: perf2bolt %t.bolt -p %t.preagg2 --pa -o %t.bat2.fdata -w %t.bat2.yaml
24+
# RUN: FileCheck %s --input-file %t.bat2.yaml --check-prefix=CHECK-YAML2
25+
2126
# CHECK-SYMS: l df *ABS* [[#]] chain.s
2227
# CHECK-SYMS: l F .bolt.org.text [[#]] chain
2328
# CHECK-SYMS: l F .text.cold [[#]] chain.cold.0
@@ -28,6 +33,9 @@
2833

2934
# CHECK-FDATA: 0 [unknown] 0 1 chain/chain.s/2 10 0 1
3035
# CHECK-YAML: - name: 'chain/chain.s/2'
36+
# CHECK-YAML2: - name: 'chain/chain.s/1'
37+
## non-BAT function has non-zero insns:
38+
# CHECK-YAML2: insns: 1
3139

3240
.file "chain.s"
3341
.text

bolt/test/link_fdata.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
parser.add_argument("prefix", nargs="?", default="FDATA", help="Custom FDATA prefix")
2020
parser.add_argument("--nmtool", default="nm", help="Path to nm tool")
2121
parser.add_argument("--no-lbr", action="store_true")
22+
parser.add_argument("--no-redefine", action="store_true")
2223

2324
args = parser.parse_args()
2425

@@ -90,6 +91,8 @@
9091
symbols = {}
9192
for symline in nm_output.splitlines():
9293
symval, _, symname = symline.split(maxsplit=2)
94+
if symname in symbols and args.no_redefine:
95+
continue
9396
symbols[symname] = symval
9497

9598

0 commit comments

Comments
 (0)