Skip to content

[BOLT] Support pre-aggregated returns #143296

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

Open
wants to merge 2 commits into
base: users/aaupov/spr/main.bolt-support-pre-aggregated-returns
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion bolt/include/bolt/Profile/DataAggregator.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class DataAggregator : public DataReader {
static constexpr const uint64_t BR_ONLY = -1ULL;
static constexpr const uint64_t FT_ONLY = -1ULL;
static constexpr const uint64_t FT_EXTERNAL_ORIGIN = -2ULL;
static constexpr const uint64_t BR_EXTERNAL_RETURN = -3ULL;

uint64_t Branch;
uint64_t From;
Expand Down Expand Up @@ -388,7 +389,7 @@ class DataAggregator : public DataReader {
/// File format syntax:
/// E <event>
/// S <start> <count>
/// T <start> <end> <ft_end> <count>
/// [TR] <start> <end> <ft_end> <count>
/// B <start> <end> <count> <mispred_count>
/// [Ff] <start> <end> <count>
///
Expand All @@ -403,6 +404,7 @@ class DataAggregator : public DataReader {
/// jump to the block
/// T - an aggregated trace: branch from <start> to <end> with a fall-through
/// to <ft_end>
/// R - an aggregated trace originating at a return
///
/// <id> - build id of the object containing the address. We can skip it for
/// the main binary and use "X" for an unknown object. This will save some
Expand Down Expand Up @@ -532,6 +534,9 @@ inline raw_ostream &operator<<(raw_ostream &OS,
case DataAggregator::Trace::FT_ONLY:
case DataAggregator::Trace::FT_EXTERNAL_ORIGIN:
break;
case DataAggregator::Trace::BR_EXTERNAL_RETURN:
OS << "0 -> ";
break;
default:
OS << Twine::utohexstr(T.Branch) << " -> ";
}
Expand Down
11 changes: 9 additions & 2 deletions bolt/lib/Profile/DataAggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
INVALID = 0,
EVENT_NAME, // E
TRACE, // T
RETURN, // R
SAMPLE, // S
BRANCH, // B
FT, // F
Expand Down Expand Up @@ -1224,6 +1225,7 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {

Type = StringSwitch<AggregatedLBREntry>(Str)
.Case("T", TRACE)
.Case("R", RETURN)
.Case("S", SAMPLE)
.Case("E", EVENT_NAME)
.Case("B", BRANCH)
Expand All @@ -1237,7 +1239,7 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
}

using SSI = StringSwitch<int>;
AddrNum = SSI(Str).Case("T", 3).Case("S", 1).Case("E", 0).Default(2);
AddrNum = SSI(Str).Cases("T", "R", 3).Case("S", 1).Case("E", 0).Default(2);
CounterNum = SSI(Str).Case("B", 2).Case("E", 0).Default(1);
}

Expand Down Expand Up @@ -1295,8 +1297,13 @@ std::error_code DataAggregator::parseAggregatedLBREntry() {
Addr[0] = Location(Type == FT ? Trace::FT_ONLY : Trace::FT_EXTERNAL_ORIGIN);
}

if (Type == BRANCH) {
if (Type == BRANCH)
Addr[2] = Location(Trace::BR_ONLY);

if (Type == RETURN) {
if (!Addr[0]->Offset)
Addr[0]->Offset = Trace::BR_EXTERNAL_RETURN;
Returns.emplace(Addr[0]->Offset);
}

Trace T{Addr[0]->Offset, Addr[1]->Offset, Addr[2]->Offset};
Expand Down
13 changes: 13 additions & 0 deletions bolt/test/X86/callcont-fallthru.s
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# RUN: link_fdata %s %t %t.pa-ret PREAGG-RET
# Trace from an external location to a landing pad/entry point call continuation
# RUN: link_fdata %s %t %t.pa-ext PREAGG-EXT
# Return trace to a landing pad/entry point call continuation
# RUN: link_fdata %s %t %t.pa-pret PREAGG-PRET
# RUN-DISABLED: link_fdata %s %t %t.pa-plt PREAGG-PLT

# RUN: llvm-strip --strip-unneeded %t -o %t.strip
Expand Down Expand Up @@ -38,6 +40,15 @@
# RUN: llvm-bolt %t.strip --pa -p %t.pa-ext -o %t.out \
# RUN: --print-cfg --print-only=main | FileCheck %s --check-prefix=CHECK-SKIP

## Check pre-aggregated return traces from external location attach call
## continuation fallthrough count to secondary entry point (unstripped)
# RUN: llvm-bolt %t --pa -p %t.pa-pret -o %t.out \
# RUN: --print-cfg --print-only=main | FileCheck %s --check-prefix=CHECK-ATTACH
## Check pre-aggregated return traces from external location attach call
## continuation fallthrough count to landing pad (stripped, landing pad)
# RUN: llvm-bolt %t.strip --pa -p %t.pa-pret -o %t.out \
# RUN: --print-cfg --print-only=main | FileCheck %s --check-prefix=CHECK-ATTACH

## Check pre-aggregated traces don't report zero-sized PLT fall-through as
## invalid trace
# RUN-DISABLED: llvm-bolt %t.strip --pa -p %t.pa-plt -o %t.out | FileCheck %s \
Expand Down Expand Up @@ -92,6 +103,8 @@ Ltmp4_br:
# PREAGG-RET: T #Lfoo_ret# #Ltmp3# #Ltmp3_br# 1
## Target is a secondary entry point (unstripped) or a landing pad (stripped)
# PREAGG-EXT: T X:0 #Ltmp3# #Ltmp3_br# 1
## Pre-aggregated return trace
# PREAGG-PRET: R X:0 #Ltmp3# #Ltmp3_br# 1

# CHECK-ATTACH: callq foo
# CHECK-ATTACH-NEXT: count: 1
Expand Down
4 changes: 2 additions & 2 deletions bolt/test/link_fdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
fdata_pat = re.compile(r"([01].*) (?P<mispred>\d+) (?P<exec>\d+)")

# Pre-aggregated profile:
# {T|S|E|B|F|f} <start> [<end>] [<ft_end>] <count> [<mispred_count>]
# {T|R|S|E|B|F|f} <start> [<end>] [<ft_end>] <count> [<mispred_count>]
# <loc>: [<id>:]<offset>
preagg_pat = re.compile(r"(?P<type>[TSBFf]) (?P<offsets_count>.*)")
preagg_pat = re.compile(r"(?P<type>[TRSBFf]) (?P<offsets_count>.*)")

# No-LBR profile:
# <is symbol?> <closest elf symbol or DSO name> <relative address> <count>
Expand Down
Loading