Skip to content

Commit 423af57

Browse files
dhoekwatercopybara-github
authored andcommitted
Add a library for aggregating from the branch_frequencies_proto
PiperOrigin-RevId: 696633206
1 parent 23bea13 commit 423af57

5 files changed

+159
-1
lines changed

propeller/BUILD

+32
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ cc_library(
105105
],
106106
)
107107

108+
cc_library(
109+
name = "proto_branch_frequencies_aggregator",
110+
srcs = ["proto_branch_frequencies_aggregator.cc"],
111+
hdrs = ["proto_branch_frequencies_aggregator.h"],
112+
deps = [
113+
":binary_content",
114+
":branch_frequencies",
115+
":branch_frequencies_aggregator",
116+
":branch_frequencies_cc_proto",
117+
":propeller_options_cc_proto",
118+
":propeller_statistics",
119+
"@abseil-cpp//absl/status:statusor",
120+
],
121+
)
122+
108123
cc_library(
109124
name = "frequencies_branch_aggregator",
110125
srcs = ["frequencies_branch_aggregator.cc"],
@@ -862,3 +877,20 @@ cc_test(
862877
"@com_google_googletest//:gtest_main",
863878
],
864879
)
880+
881+
cc_test(
882+
name = "proto_branch_frequencies_aggregator_test",
883+
srcs = ["proto_branch_frequencies_aggregator_test.cc"],
884+
deps = [
885+
":binary_content",
886+
":branch_frequencies",
887+
":branch_frequencies_cc_proto",
888+
":parse_text_proto",
889+
":propeller_options_cc_proto",
890+
":propeller_statistics",
891+
":proto_branch_frequencies_aggregator",
892+
":status_testing_macros",
893+
"@abseil-cpp//absl/status:status_matchers",
894+
"@com_google_googletest//:gtest_main",
895+
],
896+
)

propeller/CMakeLists.txt

+5-1
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ add_library(propeller_lib OBJECT
3535
cfg_node.cc
3636
file_perf_data_provider.cc
3737
frequencies_branch_aggregator.cc
38-
propeller_statistics.cc
38+
lbr_branch_aggregator.cc
3939
program_cfg.cc
4040
program_cfg_builder.cc
41+
propeller_statistics.cc
42+
proto_branch_frequencies_aggregator.cc
4143
resolve_mmap_name.cc
4244
spe_tid_pid_provider.cc
4345
)
@@ -71,7 +73,9 @@ propeller_generate_tests(
7173
file_perf_data_provider_test.cc
7274
frequencies_branch_aggregator_test.cc
7375
lazy_evaluator_test.cc
76+
lbr_branch_aggregator_test.cc
7477
propeller_statistics_test.cc
78+
proto_branch_frequencies_aggregator_test.cc
7579
spe_tid_pid_provider_test.cc
7680
status_macros_test.cc
7781
status_testing_macros_test.cc
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "propeller/proto_branch_frequencies_aggregator.h"
2+
3+
#include <utility>
4+
5+
#include "absl/status/statusor.h"
6+
#include "propeller/binary_content.h"
7+
#include "propeller/branch_frequencies.h"
8+
#include "propeller/branch_frequencies.pb.h"
9+
#include "propeller/propeller_statistics.h"
10+
11+
namespace propeller {
12+
13+
ProtoBranchFrequenciesAggregator ProtoBranchFrequenciesAggregator::Create(
14+
BranchFrequenciesProto proto) {
15+
return ProtoBranchFrequenciesAggregator(std::move(proto));
16+
}
17+
18+
absl::StatusOr<BranchFrequencies>
19+
ProtoBranchFrequenciesAggregator::AggregateBranchFrequencies(
20+
const PropellerOptions& options, const BinaryContent& binary_content,
21+
PropellerStats& stats) {
22+
return BranchFrequencies::Create(proto_);
23+
}
24+
25+
} // namespace propeller
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef DEVTOOLS_CROSSTOOL_AUTOFDO_PROTO_BRANCH_FREQUENCIES_AGGREGATOR_H_
2+
#define DEVTOOLS_CROSSTOOL_AUTOFDO_PROTO_BRANCH_FREQUENCIES_AGGREGATOR_H_
3+
4+
#include <utility>
5+
6+
#include "absl/status/statusor.h"
7+
#include "propeller/binary_content.h"
8+
#include "propeller/branch_frequencies.h"
9+
#include "propeller/branch_frequencies.pb.h"
10+
#include "propeller/branch_frequencies_aggregator.h"
11+
#include "propeller/propeller_options.pb.h"
12+
#include "propeller/propeller_statistics.h"
13+
14+
namespace propeller {
15+
// `ProtoBranchFrequenciesAggregator` is an implementation of
16+
// `BranchFrequenciesAggregator` that builds `BranchFrequencies` from a
17+
// `BranchFrequenciesProto`.
18+
class ProtoBranchFrequenciesAggregator : public BranchFrequenciesAggregator {
19+
public:
20+
// Directly create a ProtoBranchFrequenciesAggregator from a
21+
// BranchFrequenciesProto.
22+
static ProtoBranchFrequenciesAggregator Create(BranchFrequenciesProto proto);
23+
24+
// ProtoBranchFrequenciesAggregator is copyable and movable; explicitly define
25+
// both the move operations and copy operations.
26+
ProtoBranchFrequenciesAggregator(const ProtoBranchFrequenciesAggregator&) =
27+
default;
28+
ProtoBranchFrequenciesAggregator& operator=(
29+
const ProtoBranchFrequenciesAggregator&) = default;
30+
ProtoBranchFrequenciesAggregator(ProtoBranchFrequenciesAggregator&&) =
31+
default;
32+
ProtoBranchFrequenciesAggregator& operator=(
33+
ProtoBranchFrequenciesAggregator&&) = default;
34+
35+
absl::StatusOr<BranchFrequencies> AggregateBranchFrequencies(
36+
const PropellerOptions& options, const BinaryContent& binary_content,
37+
PropellerStats& stats) override;
38+
39+
private:
40+
explicit ProtoBranchFrequenciesAggregator(BranchFrequenciesProto proto)
41+
: proto_(std::move(proto)) {}
42+
43+
BranchFrequenciesProto proto_;
44+
};
45+
46+
} // namespace propeller
47+
48+
#endif // DEVTOOLS_CROSSTOOL_AUTOFDO_PROTO_BRANCH_FREQUENCIES_AGGREGATOR_H_
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include "propeller/proto_branch_frequencies_aggregator.h"
2+
3+
#include "propeller/status_testing_macros.h"
4+
#include "gmock/gmock.h"
5+
#include "gtest/gtest.h"
6+
#include "absl/status/status_matchers.h"
7+
#include "propeller/binary_content.h"
8+
#include "propeller/branch_frequencies.h"
9+
#include "propeller/branch_frequencies.pb.h"
10+
#include "propeller/parse_text_proto.h"
11+
#include "propeller/propeller_options.pb.h"
12+
#include "propeller/propeller_statistics.h"
13+
14+
namespace propeller {
15+
namespace {
16+
using ::propeller_testing::ParseTextProtoOrDie;
17+
using ::testing::AllOf;
18+
using ::testing::ElementsAre;
19+
using ::testing::Field;
20+
using ::testing::FieldsAre;
21+
using ::testing::Pair;
22+
using ::absl_testing::IsOkAndHolds;
23+
24+
TEST(ProtoBranchFrequenciesAggregator, AggregateBranchFrequencies) {
25+
BranchFrequenciesProto proto = ParseTextProtoOrDie(R"pb(
26+
taken_counts: { source: 1 dest: 2 count: 3 }
27+
not_taken_counts: { address: 1 count: 2 }
28+
)pb");
29+
30+
PropellerStats ignored;
31+
32+
EXPECT_THAT(
33+
ProtoBranchFrequenciesAggregator::Create(ParseTextProtoOrDie(R"pb(
34+
taken_counts: { source: 1 dest: 2 count: 3 }
35+
not_taken_counts: { address: 1 count: 2 }
36+
)pb"))
37+
.AggregateBranchFrequencies(PropellerOptions{}, BinaryContent{},
38+
ignored),
39+
IsOkAndHolds(
40+
AllOf(Field("taken_branch_counters",
41+
&BranchFrequencies::taken_branch_counters,
42+
ElementsAre(Pair(FieldsAre(/*.from=*/1, /*.to=*/2), 3))),
43+
Field("not_taken_branch_counters",
44+
&BranchFrequencies::not_taken_branch_counters,
45+
ElementsAre(Pair(FieldsAre(/*.address=*/1), 2))))));
46+
}
47+
48+
} // namespace
49+
} // namespace propeller

0 commit comments

Comments
 (0)