Skip to content

[SHT_LLVM_FUNC_ADDR_MAP] Introduce function address map section and emit dynamic instruction count #123804

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -1139,6 +1139,7 @@ enum : unsigned {
SHT_LLVM_OFFLOADING = 0x6fff4c0b, // LLVM device offloading data.
SHT_LLVM_LTO = 0x6fff4c0c, // .llvm.lto for fat LTO.
SHT_LLVM_JT_SIZES = 0x6fff4c0d, // LLVM jump tables sizes.
SHT_LLVM_FUNC_ADDR_MAP = 0x6fff4c0e, // LLVM function address map.
// Android's experimental support for SHT_RELR sections.
// https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512
SHT_ANDROID_RELR = 0x6fffff00, // Relocation entries; only offsets.
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/CodeGen/AsmPrinter.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ class AsmPrinter : public MachineFunctionPass {

void emitBBAddrMapSection(const MachineFunction &MF);

void emitFuncAddrMapSection(const MachineFunction &MF);

void emitKCFITrapEntry(const MachineFunction &MF, const MCSymbol *Symbol);
virtual void emitKCFITypeId(const MachineFunction &MF);

Expand Down
5 changes: 5 additions & 0 deletions llvm/include/llvm/MC/MCContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,9 @@ class MCContext {
/// LLVM_BB_ADDR_MAP version to emit.
uint8_t BBAddrMapVersion = 2;

/// LLVM_FUNC_ADDR_MAP version to emit.
uint8_t FuncAddrMapVersion = 1;

/// The file name of the log file from the environment variable
/// AS_SECURE_LOG_FILE. Which must be set before the .secure_log_unique
/// directive is used or it is an error.
Expand Down Expand Up @@ -656,6 +659,8 @@ class MCContext {

uint8_t getBBAddrMapVersion() const { return BBAddrMapVersion; }

uint8_t getFuncAddrMapVersion() const { return FuncAddrMapVersion; }

/// @}

/// \name Dwarf Management
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/MC/MCObjectFileInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ class MCObjectFileInfo {

MCSection *getBBAddrMapSection(const MCSection &TextSec) const;

MCSection *getFuncAddrMapSection(const MCSection &TextSec) const;

MCSection *getKCFITrapSection(const MCSection &TextSec) const;

MCSection *getPseudoProbeSection(const MCSection &TextSec) const;
Expand Down
4 changes: 4 additions & 0 deletions llvm/include/llvm/Object/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ class ELFFile {
decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec = nullptr,
std::vector<PGOAnalysisMap> *PGOAnalyses = nullptr) const;

Expected<std::vector<FuncAddrMap>>
decodeFuncAddrMap(const Elf_Shdr &Sec,
const Elf_Shdr *RelaSec = nullptr) const;

/// Returns a map from every section matching \p IsMatch to its relocation
/// section, or \p nullptr if it has no relocation section. This function
/// returns an error if any of the \p IsMatch calls fail or if it fails to
Expand Down
38 changes: 38 additions & 0 deletions llvm/include/llvm/Object/ELFTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,44 @@ struct PGOAnalysisMap {
}
};

// Struct representing the FuncAddrMap for one function.
struct FuncAddrMap {

// Bitfield of optional features to control the extra information
// emitted/encoded in the the section.
struct Features {
bool DynamicInstCount : 1;

// Encodes to minimum bit width representation.
uint8_t encode() const {
return (static_cast<uint8_t>(DynamicInstCount) << 0);
}

// Decodes from minimum bit width representation and validates no
// unnecessary bits are used.
static Expected<Features> decode(uint8_t Val) {
Features Feat{static_cast<bool>(Val & (1 << 0))};
if (Feat.encode() != Val)
return createStringError(
std::error_code(),
"invalid encoding for FuncAddrMap::Features: 0x%x", Val);
return Feat;
}

bool operator==(const Features &Other) const {
return DynamicInstCount == Other.DynamicInstCount;
}
};

uint64_t FunctionAddress = 0; // Function entry address.
uint64_t DynamicInstCount = 0; // Dynamic instruction count for this function

// Flags to indicate if each feature was enabled in this function
Features FeatEnable;

uint64_t getFunctionAddress() const { return FunctionAddress; }
};

} // end namespace object.
} // end namespace llvm.

Expand Down
27 changes: 27 additions & 0 deletions llvm/include/llvm/ObjectYAML/ELFYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ struct PGOAnalysisMapEntry {
std::optional<std::vector<PGOBBEntry>> PGOBBEntries;
};

struct FuncAddrMapEntry {
uint8_t Version;
llvm::yaml::Hex8 Feature;
llvm::yaml::Hex64 Address;
llvm::yaml::Hex64 DynamicInstCount;
};

struct StackSizeEntry {
llvm::yaml::Hex64 Address;
llvm::yaml::Hex64 Size;
Expand Down Expand Up @@ -229,6 +236,7 @@ struct Chunk {
DependentLibraries,
CallGraphProfile,
BBAddrMap,
FuncAddrMap,

// Special chunks.
SpecialChunksStart,
Expand Down Expand Up @@ -355,6 +363,20 @@ struct BBAddrMapSection : Section {
}
};

struct FuncAddrMapSection : Section {
std::optional<std::vector<FuncAddrMapEntry>> Entries;

FuncAddrMapSection() : Section(ChunkKind::FuncAddrMap) {}

std::vector<std::pair<StringRef, bool>> getEntries() const override {
return {{"Entries", Entries.has_value()}};
};

static bool classof(const Chunk *S) {
return S->Kind == ChunkKind::FuncAddrMap;
}
};

struct StackSizesSection : Section {
std::optional<std::vector<StackSizeEntry>> Entries;

Expand Down Expand Up @@ -762,6 +784,7 @@ bool shouldAllocateFileSpace(ArrayRef<ProgramHeader> Phdrs,
} // end namespace llvm

LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::StackSizeEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::FuncAddrMapEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBEntry)
LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBRangeEntry)
Expand Down Expand Up @@ -929,6 +952,10 @@ template <> struct MappingTraits<ELFYAML::StackSizeEntry> {
static void mapping(IO &IO, ELFYAML::StackSizeEntry &Rel);
};

template <> struct MappingTraits<ELFYAML::FuncAddrMapEntry> {
static void mapping(IO &IO, ELFYAML::FuncAddrMapEntry &E);
};

template <> struct MappingTraits<ELFYAML::BBAddrMapEntry> {
static void mapping(IO &IO, ELFYAML::BBAddrMapEntry &E);
};
Expand Down
57 changes: 56 additions & 1 deletion llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ enum class PGOMapFeaturesEnum {
BrProb,
All,
};

enum class FuncAddrMapFeaturesEnum {
DynamicInstCount,
};

static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
"pgo-analysis-map", cl::Hidden, cl::CommaSeparated,
cl::values(
Expand All @@ -173,6 +178,13 @@ static cl::opt<bool> EmitJumpTableSizesSection(
cl::desc("Emit a section containing jump table addresses and sizes"),
cl::Hidden, cl::init(false));

static cl::bits<FuncAddrMapFeaturesEnum> FuncAddrMapFeatures(
"func-addr-map", cl::Hidden, cl::CommaSeparated,
cl::values(clEnumValN(FuncAddrMapFeaturesEnum::DynamicInstCount,
"dyn-inst-count", "Dynamic instruction count")),
cl::desc("Emit features of function address map in SHT_LLVM_FUNC_ADDR_MAP "
"section"));

// This isn't turned on by default, since several of the scheduling models are
// not completely accurate, and we don't want to be misleading.
static cl::opt<bool> PrintLatency(
Expand Down Expand Up @@ -1390,6 +1402,14 @@ static uint32_t getBBAddrMapMetadata(const MachineBasicBlock &MBB) {
.encode();
}

static llvm::object::FuncAddrMap::Features getFuncAddrMapFeature() {
return {FuncAddrMapFeatures.isSet(FuncAddrMapFeaturesEnum::DynamicInstCount)};
}

static bool isAnyFuncAddrMapFeature() {
return FuncAddrMapFeatures.getBits() != 0;
}

static llvm::object::BBAddrMap::Features
getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
// Ensure that the user has not passed in additional options while also
Expand Down Expand Up @@ -1424,6 +1444,39 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
static_cast<bool>(BBAddrMapSkipEmitBBEntries)};
}

void AsmPrinter::emitFuncAddrMapSection(const MachineFunction &MF) {
if (!isAnyFuncAddrMapFeature())
return;

MCSection *FuncAddrMapSection =
getObjFileLowering().getFuncAddrMapSection(*MF.getSection());
assert(FuncAddrMapSection &&
".llvm_func_addr_map section is not initialized.");
const MCSymbol *FunctionSymbol = getFunctionBegin();
OutStreamer->pushSection();
OutStreamer->switchSection(FuncAddrMapSection);
OutStreamer->AddComment("version");
uint8_t FuncAddrMapVersion =
OutStreamer->getContext().getFuncAddrMapVersion();
OutStreamer->emitInt8(FuncAddrMapVersion);
OutStreamer->AddComment("feature");
auto Features = getFuncAddrMapFeature();
OutStreamer->emitInt8(Features.encode());

OutStreamer->AddComment("function address");
OutStreamer->emitSymbolValue(FunctionSymbol, getPointerSize());
if (Features.DynamicInstCount) {
const MachineBlockFrequencyInfo *MBFI =
&getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI();
uint64_t DynInstCount = 0;
for (const MachineBasicBlock &MBB : MF)
DynInstCount += MBFI->getBlockProfileCount(&MBB).value_or(0) * MBB.size();
OutStreamer->AddComment("Dynamic instruction count");
OutStreamer->emitULEB128IntValue(DynInstCount);
}
OutStreamer->popSection();
}

void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
MCSection *BBAddrMapSection =
getObjFileLowering().getBBAddrMapSection(*MF.getSection());
Expand Down Expand Up @@ -2119,6 +2172,8 @@ void AsmPrinter::emitFunctionBody() {
MF->getContext().reportWarning(
SMLoc(), "pgo-analysis-map is enabled for function " + MF->getName() +
" but it does not have labels");

emitFuncAddrMapSection(*MF);
}

// Emit sections containing instruction and function PCs.
Expand Down Expand Up @@ -2749,7 +2804,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
F.hasFnAttribute("xray-instruction-threshold") ||
needFuncLabels(MF, *this) || NeedsLocalForSize ||
MF.getTarget().Options.EmitStackSizeSection ||
MF.getTarget().Options.BBAddrMap) {
MF.getTarget().Options.BBAddrMap || isAnyFuncAddrMapFeature()) {
CurrentFnBegin = createTempSymbol("func_begin");
if (NeedsLocalForSize)
CurrentFnSymForSize = CurrentFnBegin;
Expand Down
18 changes: 18 additions & 0 deletions llvm/lib/MC/MCObjectFileInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,6 +1120,24 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
}

MCSection *
MCObjectFileInfo::getFuncAddrMapSection(const MCSection &TextSec) const {
if (Ctx->getObjectFileType() != MCContext::IsELF)
return nullptr;

const MCSectionELF &ElfSec = static_cast<const MCSectionELF &>(TextSec);
unsigned Flags = ELF::SHF_LINK_ORDER;
StringRef GroupName;
if (const MCSymbol *Group = ElfSec.getGroup()) {
GroupName = Group->getName();
Flags |= ELF::SHF_GROUP;
}

return Ctx->getELFSection(".llvm_func_addr_map", ELF::SHT_LLVM_FUNC_ADDR_MAP,
Flags, 0, GroupName, true, ElfSec.getUniqueID(),
cast<MCSymbolELF>(TextSec.getBeginSymbol()));
}

MCSection *
MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
if (Ctx->getObjectFileType() != MCContext::IsELF)
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/MC/MCParser/ELFAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,8 @@ bool ELFAsmParser::parseSectionArguments(bool IsPush, SMLoc loc) {
Type = ELF::SHT_LLVM_LTO;
else if (TypeName == "llvm_jt_sizes")
Type = ELF::SHT_LLVM_JT_SIZES;
else if (TypeName == "llvm_func_addr_map")
Type = ELF::SHT_LLVM_FUNC_ADDR_MAP;
else if (TypeName.getAsInteger(0, Type))
return TokError("unknown section type");
}
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/MC/MCSectionELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ void MCSectionELF::printSwitchToSection(const MCAsmInfo &MAI, const Triple &T,
OS << "llvm_lto";
else if (Type == ELF::SHT_LLVM_JT_SIZES)
OS << "llvm_jt_sizes";
else if (Type == ELF::SHT_LLVM_FUNC_ADDR_MAP)
OS << "llvm_func_addr_map";
else
OS << "0x" << Twine::utohexstr(Type);

Expand Down
Loading
Loading