Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 712653204
  • Loading branch information
rlavaee authored and copybara-github committed Jan 6, 2025
1 parent 657186b commit 785346d
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions propeller/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1204,6 +1204,7 @@ cc_test(
"//propeller/testdata:propeller_barebone_nopie_buildid",
"//propeller/testdata:propeller_barebone_pie_nobuildid_bin",
"//propeller/testdata:propeller_sample_1.bin",
"//propeller/testdata:sample_pgoanalysismap.bin",
],
deps = [
":binary_content",
Expand All @@ -1212,6 +1213,7 @@ cc_test(
"@abseil-cpp//absl/status:status_matchers",
"@abseil-cpp//absl/strings",
"@com_google_googletest//:gtest_main",
"@llvm-project//llvm:Object",
],
)

Expand Down
5 changes: 3 additions & 2 deletions propeller/binary_address_mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -805,11 +805,12 @@ absl::StatusOr<std::unique_ptr<BinaryAddressMapper>> BuildBinaryAddressMapper(
PropellerStats &stats, const absl::flat_hash_set<uint64_t> *hot_addresses) {
LOG(INFO) << "Started reading the binary content from: "
<< binary_content.file_name;
std::vector<BBAddrMap> bb_addr_map;
BbAddrMapData bb_addr_map;
ASSIGN_OR_RETURN(bb_addr_map, ReadBbAddrMap(binary_content));

return BinaryAddressMapperBuilder(ReadSymbolTable(binary_content),
std::move(bb_addr_map), stats, &options)
std::move(bb_addr_map.bb_addr_maps), stats,
&options)
.Build(hot_addresses);
}

Expand Down
9 changes: 6 additions & 3 deletions propeller/binary_content.cc
Original file line number Diff line number Diff line change
Expand Up @@ -293,17 +293,19 @@ ReadSymbolTable(const BinaryContent &binary_content) {
return symtab;
}

absl::StatusOr<std::vector<llvm::object::BBAddrMap>> ReadBbAddrMap(
absl::StatusOr<BbAddrMapData> ReadBbAddrMap(
const BinaryContent &binary_content) {
auto *elf_object = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(
binary_content.object_file.get());
CHECK_NE(elf_object, nullptr);
std::vector<llvm::object::PGOAnalysisMap> pgo_analyses;
llvm::Expected<std::vector<llvm::object::BBAddrMap>> bb_addr_map =
elf_object->readBBAddrMap(
binary_content.kernel_module.has_value()
? std::optional<unsigned>(
binary_content.kernel_module->text_section_index)
: std::nullopt);
: std::nullopt,
&pgo_analyses);
if (!bb_addr_map) {
return absl::InternalError(
llvm::formatv(
Expand All @@ -317,7 +319,8 @@ absl::StatusOr<std::vector<llvm::object::BBAddrMap>> ReadBbAddrMap(
"'%s' does not have a non-empty LLVM_BB_ADDR_MAP section.",
binary_content.file_name));
}
return std::move(*bb_addr_map);
return BbAddrMapData{.bb_addr_maps = *std::move(bb_addr_map),
.pgo_analyses = std::move(pgo_analyses)};
}

absl::StatusOr<absl::flat_hash_map<absl::string_view, absl::string_view>>
Expand Down
11 changes: 9 additions & 2 deletions propeller/binary_content.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@

namespace propeller {

// A container for the `BbAddrMap` and `PGOAnalysisMap` data read from the
// binary's SHT_LLVM_BB_ADDR_MAP section.
struct BbAddrMapData {
std::vector<llvm::object::BBAddrMap> bb_addr_maps;
std::vector<llvm::object::PGOAnalysisMap> pgo_analyses;
};

// BinaryContent represents information for an ELF executable or a shared
// object, the data contained include (loadable) segments, file name, file
// content and DYN tag (is_pie).
Expand Down Expand Up @@ -130,10 +137,10 @@ absl::StatusOr<int64_t> GetSymbolAddress(
absl::flat_hash_map<uint64_t, llvm::SmallVector<llvm::object::ELFSymbolRef>>
ReadSymbolTable(const BinaryContent &binary_content);

// Returns the binary's `BBAddrMap`s by calling LLVM-side decoding function
// Returns the binary's `BbAddrMapData`s by calling LLVM-side decoding function
// `ELFObjectFileBase::readBBAddrMap`. Returns error if the call fails or if the
// result is empty.
absl::StatusOr<std::vector<llvm::object::BBAddrMap>> ReadBbAddrMap(
absl::StatusOr<BbAddrMapData> ReadBbAddrMap(
const BinaryContent &binary_content);

} // namespace propeller
Expand Down
14 changes: 14 additions & 0 deletions propeller/binary_content_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/strings/string_view.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "llvm/Object/ELFTypes.h"
#include "propeller/status_testing_macros.h"

namespace propeller {
Expand Down Expand Up @@ -80,5 +81,18 @@ TEST(GetSymbolAddressTest, SymbolNotFound) {
Not(IsOk()));
}

TEST(ReadBbAddrMapTest, ReadBbAddrMap) {
const std::string binary =
absl::StrCat(::testing::SrcDir(),
"_main/propeller/testdata/sample_pgoanalysismap.bin");
ASSERT_OK_AND_ASSIGN(std::unique_ptr<BinaryContent> binary_content,
GetBinaryContent(binary));
absl::StatusOr<BbAddrMapData> bb_addr_map_data =
ReadBbAddrMap(*binary_content);
ASSERT_OK(bb_addr_map_data);
EXPECT_THAT(bb_addr_map_data->bb_addr_maps, SizeIs(4));
EXPECT_THAT(bb_addr_map_data->pgo_analyses, SizeIs(4));
}

} // namespace
} // namespace propeller
14 changes: 14 additions & 0 deletions propeller/testdata/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ exports_files([
"sample_different_buildid.bin",
"sample_ld_directives.arm.golden.txt",
"sample_ld_directives.golden.txt",
"sample_pgoanalysismap.bin",
"sample_section.bin",
"sample_section_cc_directives.golden.txt",
"sample_section_ld_directives.golden.txt",
Expand Down Expand Up @@ -302,6 +303,19 @@ genrule(
toolchains = _LLVM_PROPELLER_TESTDATA_TOOLCHAINS,
)

# This rule is used to manually generate a BBAddrMap binary with PGO analysis map.
genrule(
name = "sample_pgoanalysismap_bin",
srcs = ["sample.c"],
outs = [
"sample_pgoanalysismap.bin.gen",
],
cmd = "$(CC) $(CC_FLAGS) -g -O2 -Wl,-build-id -pie -fbasic-block-sections=labels " +
"-mllvm=-pgo-analysis-map=all $< -o $(RULEDIR)/sample_pgoanalysismap.bin.gen",
tags = ["manual"],
toolchains = _LLVM_PROPELLER_TESTDATA_TOOLCHAINS,
)

genrule(
name = "duplicate_symbols_bin",
srcs = [
Expand Down
Binary file added propeller/testdata/sample_pgoanalysismap.bin
Binary file not shown.

0 comments on commit 785346d

Please sign in to comment.