Skip to content

[SHT_LLVM_BB_ADDR_MAP] Add a new PGOAnalysisMap feature to emit dynamic instruction count #119303

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 1 commit into
base: main
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
21 changes: 14 additions & 7 deletions llvm/include/llvm/Object/ELFTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -831,8 +831,11 @@ struct BBAddrMap {
bool BrProb : 1;
bool MultiBBRange : 1;
bool OmitBBEntries : 1;
bool DynamicInstCount : 1;

bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; }
bool hasPGOAnalysis() const {
return FuncEntryCount || BBFreq || BrProb || DynamicInstCount;
}

bool hasPGOAnalysisBBData() const { return BBFreq || BrProb; }

Expand All @@ -842,7 +845,8 @@ struct BBAddrMap {
(static_cast<uint8_t>(BBFreq) << 1) |
(static_cast<uint8_t>(BrProb) << 2) |
(static_cast<uint8_t>(MultiBBRange) << 3) |
(static_cast<uint8_t>(OmitBBEntries) << 4);
(static_cast<uint8_t>(OmitBBEntries) << 4) |
(static_cast<uint8_t>(DynamicInstCount) << 5);
}

// Decodes from minimum bit width representation and validates no
Expand All @@ -851,7 +855,7 @@ struct BBAddrMap {
Features Feat{
static_cast<bool>(Val & (1 << 0)), static_cast<bool>(Val & (1 << 1)),
static_cast<bool>(Val & (1 << 2)), static_cast<bool>(Val & (1 << 3)),
static_cast<bool>(Val & (1 << 4))};
static_cast<bool>(Val & (1 << 4)), static_cast<bool>(Val & (1 << 5))};
if (Feat.encode() != Val)
return createStringError(
std::error_code(), "invalid encoding for BBAddrMap::Features: 0x%x",
Expand All @@ -861,9 +865,10 @@ struct BBAddrMap {

bool operator==(const Features &Other) const {
return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
OmitBBEntries) ==
OmitBBEntries, DynamicInstCount) ==
std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
Other.MultiBBRange, Other.OmitBBEntries);
Other.MultiBBRange, Other.OmitBBEntries,
Other.DynamicInstCount);
}
};

Expand Down Expand Up @@ -1016,14 +1021,16 @@ struct PGOAnalysisMap {
};

uint64_t FuncEntryCount; // Prof count from IR function
uint64_t DynamicInstCount; // Dynamic instruction count
std::vector<PGOBBEntry> BBEntries; // Extended basic block entries

// Flags to indicate if each PGO related info was enabled in this function
BBAddrMap::Features FeatEnable;

bool operator==(const PGOAnalysisMap &Other) const {
return std::tie(FuncEntryCount, BBEntries, FeatEnable) ==
std::tie(Other.FuncEntryCount, Other.BBEntries, Other.FeatEnable);
return std::tie(FuncEntryCount, DynamicInstCount, BBEntries, FeatEnable) ==
std::tie(Other.FuncEntryCount, Other.DynamicInstCount,
Other.BBEntries, Other.FeatEnable);
}
};

Expand Down
1 change: 1 addition & 0 deletions llvm/include/llvm/ObjectYAML/ELFYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ struct PGOAnalysisMapEntry {
std::optional<std::vector<SuccessorEntry>> Successors;
};
std::optional<uint64_t> FuncEntryCount;
std::optional<uint64_t> DynamicInstCount;
std::optional<std::vector<PGOBBEntry>> PGOBBEntries;
};

Expand Down
26 changes: 22 additions & 4 deletions llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ using namespace llvm;
enum class PGOMapFeaturesEnum {
None,
FuncEntryCount,
DynInstCount,
BBFreq,
BrProb,
All,
Expand All @@ -153,6 +154,8 @@ static cl::bits<PGOMapFeaturesEnum> PgoAnalysisMapFeatures(
clEnumValN(PGOMapFeaturesEnum::None, "none", "Disable all options"),
clEnumValN(PGOMapFeaturesEnum::FuncEntryCount, "func-entry-count",
"Function Entry Count"),
clEnumValN(PGOMapFeaturesEnum::DynInstCount, "dyn-inst-count",
"Dynamic instruction count"),
clEnumValN(PGOMapFeaturesEnum::BBFreq, "bb-freq",
"Basic Block Frequency"),
clEnumValN(PGOMapFeaturesEnum::BrProb, "br-prob", "Branch Probability"),
Expand Down Expand Up @@ -1412,6 +1415,9 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
bool FuncEntryCountEnabled =
AllFeatures || (!NoFeatures && PgoAnalysisMapFeatures.isSet(
PGOMapFeaturesEnum::FuncEntryCount));
bool DynInstCountEnabled =
AllFeatures || (!NoFeatures && PgoAnalysisMapFeatures.isSet(
PGOMapFeaturesEnum::DynInstCount));
bool BBFreqEnabled =
AllFeatures ||
(!NoFeatures && PgoAnalysisMapFeatures.isSet(PGOMapFeaturesEnum::BBFreq));
Expand All @@ -1424,9 +1430,12 @@ getBBAddrMapFeature(const MachineFunction &MF, int NumMBBSectionRanges) {
"BB entries info is required for BBFreq and BrProb "
"features");
}
return {FuncEntryCountEnabled, BBFreqEnabled, BrProbEnabled,
return {FuncEntryCountEnabled,
BBFreqEnabled,
BrProbEnabled,
MF.hasBBSections() && NumMBBSectionRanges > 1,
static_cast<bool>(BBAddrMapSkipEmitBBEntries)};
static_cast<bool>(BBAddrMapSkipEmitBBEntries),
DynInstCountEnabled};
}

void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
Expand Down Expand Up @@ -1519,16 +1528,21 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
MaybeEntryCount ? MaybeEntryCount->getCount() : 0);
}
const MachineBlockFrequencyInfo *MBFI =
Features.BBFreq
Features.BBFreq || Features.DynamicInstCount
? &getAnalysis<LazyMachineBlockFrequencyInfoPass>().getBFI()
: nullptr;
const MachineBranchProbabilityInfo *MBPI =
Features.BrProb
? &getAnalysis<MachineBranchProbabilityInfoWrapperPass>().getMBPI()
: nullptr;

if (Features.BBFreq || Features.BrProb) {
if (Features.BBFreq || Features.BrProb || Features.DynamicInstCount) {
uint64_t DynInstCount = 0;
for (const MachineBasicBlock &MBB : MF) {
if (Features.DynamicInstCount) {
DynInstCount +=
MBFI->getBlockProfileCount(&MBB).value_or(0) * MBB.size();
}
if (Features.BBFreq) {
OutStreamer->AddComment("basic block frequency");
OutStreamer->emitULEB128IntValue(
Expand All @@ -1547,6 +1561,10 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
}
}
}
if (Features.DynamicInstCount) {
OutStreamer->AddComment("Dynamic instruction count");
OutStreamer->emitULEB128IntValue(DynInstCount);
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions llvm/lib/Object/ELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,12 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
: 0;

// Dynamic instruction count
uint64_t DynInstCount =
FeatEnable.DynamicInstCount
? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
: 0;

std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
for (uint32_t BlockIndex = 0;
FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
Expand Down Expand Up @@ -915,8 +921,8 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
}

if (PGOAnalyses)
PGOAnalyses->push_back(
{FuncEntryCount, std::move(PGOBBEntries), FeatEnable});
PGOAnalyses->push_back({FuncEntryCount, DynInstCount,
std::move(PGOBBEntries), FeatEnable});
}
}
// Either Cur is in the error state, or we have an error in ULEBSizeErr or
Expand Down
3 changes: 3 additions & 0 deletions llvm/lib/ObjectYAML/ELFEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,9 @@ void ELFState<ELFT>::writeSectionContent(
if (PGOEntry.FuncEntryCount)
SHeader.sh_size += CBA.writeULEB128(*PGOEntry.FuncEntryCount);

if (PGOEntry.DynamicInstCount)
SHeader.sh_size += CBA.writeULEB128(*PGOEntry.DynamicInstCount);

if (!PGOEntry.PGOBBEntries)
continue;

Expand Down
1 change: 1 addition & 0 deletions llvm/lib/ObjectYAML/ELFYAML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,7 @@ void MappingTraits<ELFYAML::PGOAnalysisMapEntry>::mapping(
IO &IO, ELFYAML::PGOAnalysisMapEntry &E) {
assert(IO.getContext() && "The IO context is not initialized");
IO.mapOptional("FuncEntryCount", E.FuncEntryCount);
IO.mapOptional("DynamicInstCount", E.DynamicInstCount);
IO.mapOptional("PGOBBEntries", E.PGOBBEntries);
}

Expand Down
10 changes: 7 additions & 3 deletions llvm/test/CodeGen/X86/basic-block-address-map-pgo-features.ll
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=none | FileCheck %s --check-prefixes=CHECK,BASIC,PGO-NONE

;; Also verify this holds for all PGO features enabled
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count,bb-freq,br-prob | FileCheck %s --check-prefixes=CHECK,PGO-ALL,PGO-FEC,PGO-BBF,PGO-BRP
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=all | FileCheck %s --check-prefixes=CHECK,PGO-ALL,PGO-FEC,PGO-BBF,PGO-BRP
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count,bb-freq,br-prob,dyn-inst-count | FileCheck %s --check-prefixes=CHECK,PGO-ALL,PGO-FEC,PGO-BBF,PGO-BRP
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=all | FileCheck %s --check-prefixes=CHECK,PGO-ALL,PGO-FEC,PGO-BBF,PGO-BRP,PGO-DINST

;; Also verify that pgo extension only includes the enabled feature
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count | FileCheck %s --check-prefixes=CHECK,PGO-FEC,FEC-ONLY
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq | FileCheck %s --check-prefixes=CHECK,PGO-BBF,BBF-ONLY
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=br-prob | FileCheck %s --check-prefixes=CHECK,PGO-BRP,BRP-ONLY
; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=dyn-inst-count | FileCheck %s --check-prefixes=CHECK,PGO-DINST,DINST-ONLY

; RUN: llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=func-entry-count -basic-block-address-map-skip-bb-entries | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES
; RUN: not llc < %s -mtriple=x86_64 -function-sections -unique-section-names=true -basic-block-address-map -pgo-analysis-map=bb-freq -basic-block-address-map-skip-bb-entries 2>&1 | FileCheck %s --check-prefixes=SKIP-BB-ENTRIES-ERROR
Expand Down Expand Up @@ -71,8 +72,9 @@ declare i32 @__gxx_personality_v0(...)
; CHECK: .section .llvm_bb_addr_map,"o",@llvm_bb_addr_map,.text._Z3bazb{{$}}
; CHECK-NEXT: .byte 2 # version
; BASIC-NEXT: .byte 0 # feature
; PGO-ALL-NEXT: .byte 7 # feature
; PGO-ALL-NEXT: .byte 39 # feature
; FEC-ONLY-NEXT:.byte 1 # feature
; DINST-ONLY-NEXT:.byte 32 # feature
; BBF-ONLY-NEXT:.byte 2 # feature
; BRP-ONLY-NEXT:.byte 4 # feature
; CHECK-NEXT: .quad .Lfunc_begin0 # function address
Expand Down Expand Up @@ -138,6 +140,8 @@ declare i32 @__gxx_personality_v0(...)
; PGO-BRP-NEXT: .byte 5 # successor BB ID
; PGO-BRP-NEXT: .ascii "\200\200\200\200\b" # successor branch probability

; PGO-DINST: .ascii "\341\f" # Dynamic instruction count

; SKIP-BB-ENTRIES: .byte 17 # feature
; SKIP-BB-ENTRIES-NEXT: .quad .Lfunc_begin0 # function address
; SKIP-BB-ENTRIES-NEXT: .byte 6 # number of basic blocks
Expand Down
51 changes: 46 additions & 5 deletions llvm/test/tools/llvm-objdump/X86/elf-pgoanalysismap.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,49 @@ Symbols:
# ENTRYCOUNT: <foo>:
# ENTRYCOUNT: <BB3> (Entry count: 1000):

## Check the case where we only have dynamic instruction count.
# RUN: yaml2obj --docnum=2 %s -o %t1
# RUN: llvm-objdump %t1 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck %s --check-prefix=DYNICNT

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Sections:
- Name: .text.foo
Type: SHT_PROGBITS
Address: 0x0
Flags: [SHF_ALLOC, SHF_EXECINSTR]
Content: '50'
- Name: .llvm_bb_addr_map.foo
Type: SHT_LLVM_BB_ADDR_MAP
Link: .text.foo
Entries:
- Version: 2
Feature: 0x20
BBRanges:
- BaseAddress: 0x0
BBEntries:
- ID: 3
AddressOffset: 0x0
Size: 0x1
Metadata: 0x1
PGOAnalyses:
- DynamicInstCount: 1000
Symbols:
- Name: foo
Section: .text.foo
Value: 0x0

# DYNICNT: <foo>:
# DYNICNT: <BB3> (Dynamic instruction count: 1000):

## Check the case where we have entry points and block frequency information

# RUN: yaml2obj %s --docnum=2 -o %t2
# RUN: yaml2obj %s --docnum=3 -o %t2
# RUN: llvm-objdump %t2 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRYCOUNT-BLOCKFREQ
# RUN: llvm-objdump %t2 -d --symbolize-operands --pretty-pgo-analysis-map --no-show-raw-insn --no-leading-addr | \
Expand All @@ -68,7 +108,7 @@ Sections:
Link: .text.foo
Entries:
- Version: 2
Feature: 0x3
Feature: 0x23
BBRanges:
- BaseAddress: 0x0
BBEntries:
Expand All @@ -90,6 +130,7 @@ Sections:
Metadata: 0x2
PGOAnalyses:
- FuncEntryCount: 1000
DynamicInstCount: 2000
PGOBBEntries:
- BBFreq: 1000
- BBFreq: 133
Expand All @@ -101,21 +142,21 @@ Symbols:
Value: 0x0

# ENTRYCOUNT-BLOCKFREQ:<foo>:
# ENTRYCOUNT-BLOCKFREQ:<BB3> (Entry count: 1000, Frequency: 1000):
# ENTRYCOUNT-BLOCKFREQ:<BB3> (Entry count: 1000, Dynamic instruction count: 2000, Frequency: 1000):
# ENTRYCOUNT-BLOCKFREQ:<BB1> (Frequency: 133):
# ENTRYCOUNT-BLOCKFREQ:<BB2> (Frequency: 18):
# ENTRYCOUNT-BLOCKFREQ:<BB5> (Frequency: 1000):

# ENTRYCOUNT-BLOCKFREQ-PRETTY:<foo>:
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB3> (Entry count: 1000, Frequency: 1.0):
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB3> (Entry count: 1000, Dynamic instruction count: 2000, Frequency: 1.0):
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB1> (Frequency: 0.133):
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB2> (Frequency: 0.018):
# ENTRYCOUNT-BLOCKFREQ-PRETTY:<BB5> (Frequency: 1.0):

## Check the case where we have entry points, block frequency, and branch
## proabability information.

# RUN: yaml2obj %s --docnum=3 -o %t3
# RUN: yaml2obj %s --docnum=4 -o %t3
# RUN: llvm-objdump %t3 -d --symbolize-operands --no-show-raw-insn --no-leading-addr | \
# RUN: FileCheck --match-full-lines --strict-whitespace %s --check-prefix=ENTRY-FREQ-PROB
# RUN: llvm-objdump %t3 -d --symbolize-operands --pretty-pgo-analysis-map --no-show-raw-insn --no-leading-addr | \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

## Check that a malformed section can be handled.
# RUN: yaml2obj %s -DBITS=32 -DSIZE=24 -o %t2.o
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DOFFSET=0x00000018 -DFILE=%t2.o --check-prefix=TRUNCATED
# RUN: llvm-readobj %t2.o --bb-addr-map 2>&1 | FileCheck --match-full-lines %s -DOFFSET=0x00000014 -DFILE=%t2.o --check-prefix=TRUNCATED

## Check that missing features can be handled.
# RUN: yaml2obj %s -DBITS=32 -DFEATURE=0x2 -o %t3.o
Expand Down Expand Up @@ -55,6 +55,7 @@
# CHECK-NEXT: ]
# CHECK-NEXT: PGO analyses {
# CHECK-NEXT: FuncEntryCount: 100
# CHECK-NEXT: DynamicInstCount: 100
# CHECK-NEXT: PGO BB entries [
# CHECK-NEXT: {
# RAW-NEXT: Frequency: 100
Expand Down Expand Up @@ -98,6 +99,7 @@
# CHECK-NEXT: ]
# CHECK-NEXT: PGO analyses {
# CHECK-NEXT: FuncEntryCount: 8888
# CHECK-NEXT: DynamicInstCount: 8888
# CHECK-NEXT: PGO BB entries [
# CHECK-NEXT: {
# RAW-NEXT: Frequency: 9000
Expand Down Expand Up @@ -173,7 +175,7 @@ Sections:
Link: .text
Entries:
- Version: 2
Feature: 0x7
Feature: 0x27
BBRanges:
- BaseAddress: [[ADDR=0x11111]]
BBEntries:
Expand All @@ -186,7 +188,7 @@ Sections:
Size: 0x4
Metadata: 0x15
- Version: 2
Feature: 0x3
Feature: 0x23
BBRanges:
- BaseAddress: 0x22222
BBEntries:
Expand All @@ -196,6 +198,7 @@ Sections:
Metadata: 0x8
PGOAnalyses:
- FuncEntryCount: 100
DynamicInstCount: 100
PGOBBEntries:
- BBFreq: 100
Successors:
Expand All @@ -204,6 +207,7 @@ Sections:
- BBFreq: 100
Successors: []
- FuncEntryCount: 8888
DynamicInstCount: 8888
PGOBBEntries:
- BBFreq: 9000
- Name: dummy_section
Expand Down Expand Up @@ -237,4 +241,3 @@ Symbols:
Section: .text.bar
Type: STT_FUNC
Value: 0x33333

Loading
Loading