Skip to content

Commit a61ff14

Browse files
authored
[SHT_LLVM_BB_ADDR_MAP] Change the callsite feature to emit end of callsites. (#155041)
This PR simply moves the callsite anchors from the beginning of callsites to their end. Emitting the end of callsites is more sensible as it allows breaking the basic block into subblocks which end with control transfer instructions.
1 parent 7b74a27 commit a61ff14

File tree

14 files changed

+67
-67
lines changed

14 files changed

+67
-67
lines changed

llvm/docs/Extensions.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,8 @@ two years old). Each function entry starts with a version byte which specifies
410410
the encoding version to use. This is followed by a feature byte which specifies
411411
the features specific to this particular entry. The function base address is
412412
stored as a full address. Other addresses in the entry (block begin and end
413-
addresses and callsite addresses) are stored in a running-offset fashion, as
414-
offsets relative to prior addresses.
413+
addresses and callsite end addresses) are stored in a running-offset fashion,
414+
as offsets relative to prior addresses.
415415

416416
The following versioning schemes are currently supported (newer versions support
417417
features of the older versions).
@@ -438,8 +438,8 @@ Example:
438438
.byte 1 # BB_1 ID
439439
.uleb128 .LBB0_1-.LBB_END0_0 # BB_1 offset relative to the end of last block (BB_0).
440440
.byte 2 # number of callsites in this block
441-
.uleb128 .LBB0_1_CS0-.LBB0_1 # offset of callsite relative to the previous offset (.LBB0_1)
442-
.uleb128 .LBB0_1_CS1-.LBB0_1_CS0 # offset of callsite relative to the previous offset (.LBB0_1_CS0)
441+
.uleb128 .LBB0_1_CS0-.LBB0_1 # offset of callsite end relative to the previous offset (.LBB0_1)
442+
.uleb128 .LBB0_1_CS1-.LBB0_1_CS0 # offset of callsite end relative to the previous offset (.LBB0_1_CS0)
443443
.uleb128 .LBB_END0_1-.LBB0_1_CS1 # BB_1 size offset (Offset of the block end relative to the previous offset).
444444
.byte y # BB_1 metadata
445445

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
135135
/// default, this is equal to CurrentFnSym.
136136
MCSymbol *CurrentFnSymForSize = nullptr;
137137

138-
/// Vector of symbols marking the position of callsites in the current
138+
/// Vector of symbols marking the end of the callsites in the current
139139
/// function, keyed by their containing basic block.
140140
/// The callsite symbols of each block are stored in the order they appear
141141
/// in that block.
142142
DenseMap<const MachineBasicBlock *, SmallVector<MCSymbol *, 1>>
143-
CurrentFnCallsiteSymbols;
143+
CurrentFnCallsiteEndSymbols;
144144

145145
/// Provides the profile information for constants.
146146
const StaticDataProfileInfo *SDPI = nullptr;
@@ -332,9 +332,9 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
332332
/// to emit them as well, return the whole set.
333333
ArrayRef<MCSymbol *> getAddrLabelSymbolToEmit(const BasicBlock *BB);
334334

335-
/// Creates a new symbol to be used for the beginning of a callsite at the
336-
/// specified basic block.
337-
MCSymbol *createCallsiteSymbol(const MachineBasicBlock &MBB);
335+
/// Creates a new symbol to be used for the end of a callsite at the specified
336+
/// basic block.
337+
MCSymbol *createCallsiteEndSymbol(const MachineBasicBlock &MBB);
338338

339339
/// If the specified function has had any references to address-taken blocks
340340
/// generated, but the block got deleted, return the symbol now so we can

llvm/include/llvm/Object/ELFTypes.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ struct BBAddrMap {
832832
bool BrProb : 1;
833833
bool MultiBBRange : 1;
834834
bool OmitBBEntries : 1;
835-
bool CallsiteOffsets : 1;
835+
bool CallsiteEndOffsets : 1;
836836

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

@@ -845,7 +845,7 @@ struct BBAddrMap {
845845
(static_cast<uint8_t>(BrProb) << 2) |
846846
(static_cast<uint8_t>(MultiBBRange) << 3) |
847847
(static_cast<uint8_t>(OmitBBEntries) << 4) |
848-
(static_cast<uint8_t>(CallsiteOffsets) << 5);
848+
(static_cast<uint8_t>(CallsiteEndOffsets) << 5);
849849
}
850850

851851
// Decodes from minimum bit width representation and validates no
@@ -864,10 +864,10 @@ struct BBAddrMap {
864864

865865
bool operator==(const Features &Other) const {
866866
return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange,
867-
OmitBBEntries, CallsiteOffsets) ==
867+
OmitBBEntries, CallsiteEndOffsets) ==
868868
std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb,
869869
Other.MultiBBRange, Other.OmitBBEntries,
870-
Other.CallsiteOffsets);
870+
Other.CallsiteEndOffsets);
871871
}
872872
};
873873

@@ -918,20 +918,19 @@ struct BBAddrMap {
918918
uint32_t Size = 0; // Size of the basic block.
919919
Metadata MD = {false, false, false, false,
920920
false}; // Metdata for this basic block.
921-
// Offsets of callsites (beginning of call instructions), relative to the
922-
// basic block start.
923-
SmallVector<uint32_t, 1> CallsiteOffsets;
921+
// Offsets of end of call instructions, relative to the basic block start.
922+
SmallVector<uint32_t, 1> CallsiteEndOffsets;
924923

925924
BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD,
926-
SmallVector<uint32_t, 1> CallsiteOffsets)
925+
SmallVector<uint32_t, 1> CallsiteEndOffsets)
927926
: ID(ID), Offset(Offset), Size(Size), MD(MD),
928-
CallsiteOffsets(std::move(CallsiteOffsets)) {}
927+
CallsiteEndOffsets(std::move(CallsiteEndOffsets)) {}
929928

930929
UniqueBBID getID() const { return {ID, 0}; }
931930

932931
bool operator==(const BBEntry &Other) const {
933932
return ID == Other.ID && Offset == Other.Offset && Size == Other.Size &&
934-
MD == Other.MD && CallsiteOffsets == Other.CallsiteOffsets;
933+
MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets;
935934
}
936935

937936
bool hasReturn() const { return MD.HasReturn; }

llvm/include/llvm/ObjectYAML/ELFYAML.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ struct BBAddrMapEntry {
162162
llvm::yaml::Hex64 AddressOffset;
163163
llvm::yaml::Hex64 Size;
164164
llvm::yaml::Hex64 Metadata;
165-
std::optional<std::vector<llvm::yaml::Hex64>> CallsiteOffsets;
165+
std::optional<std::vector<llvm::yaml::Hex64>> CallsiteEndOffsets;
166166
};
167167
uint8_t Version;
168168
llvm::yaml::Hex8 Feature;
@@ -183,14 +183,14 @@ struct BBAddrMapEntry {
183183
}
184184

185185
// Returns if any BB entries have non-empty callsite offsets.
186-
bool hasAnyCallsiteOffsets() const {
186+
bool hasAnyCallsiteEndOffsets() const {
187187
if (!BBRanges)
188188
return false;
189189
for (const ELFYAML::BBAddrMapEntry::BBRangeEntry &BBR : *BBRanges) {
190190
if (!BBR.BBEntries)
191191
continue;
192192
for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *BBR.BBEntries)
193-
if (BBE.CallsiteOffsets && !BBE.CallsiteOffsets->empty())
193+
if (BBE.CallsiteEndOffsets && !BBE.CallsiteEndOffsets->empty())
194194
return true;
195195
}
196196
return false;

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14321432
MCSection *BBAddrMapSection =
14331433
getObjFileLowering().getBBAddrMapSection(*MF.getSection());
14341434
assert(BBAddrMapSection && ".llvm_bb_addr_map section is not initialized.");
1435-
bool HasCalls = !CurrentFnCallsiteSymbols.empty();
1435+
bool HasCalls = !CurrentFnCallsiteEndSymbols.empty();
14361436

14371437
const MCSymbol *FunctionSymbol = getFunctionBegin();
14381438

@@ -1497,13 +1497,13 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14971497
emitLabelDifferenceAsULEB128(MBBSymbol, PrevMBBEndSymbol);
14981498
const MCSymbol *CurrentLabel = MBBSymbol;
14991499
if (HasCalls) {
1500-
auto CallsiteSymbols = CurrentFnCallsiteSymbols.lookup(&MBB);
1500+
auto CallsiteEndSymbols = CurrentFnCallsiteEndSymbols.lookup(&MBB);
15011501
OutStreamer->AddComment("number of callsites");
1502-
OutStreamer->emitULEB128IntValue(CallsiteSymbols.size());
1503-
for (const MCSymbol *CallsiteSymbol : CallsiteSymbols) {
1502+
OutStreamer->emitULEB128IntValue(CallsiteEndSymbols.size());
1503+
for (const MCSymbol *CallsiteEndSymbol : CallsiteEndSymbols) {
15041504
// Emit the callsite offset.
1505-
emitLabelDifferenceAsULEB128(CallsiteSymbol, CurrentLabel);
1506-
CurrentLabel = CallsiteSymbol;
1505+
emitLabelDifferenceAsULEB128(CallsiteEndSymbol, CurrentLabel);
1506+
CurrentLabel = CallsiteEndSymbol;
15071507
}
15081508
}
15091509
// Emit the offset to the end of the block, which can be used to compute
@@ -1941,8 +1941,6 @@ void AsmPrinter::emitFunctionBody() {
19411941
!MI.isDebugInstr()) {
19421942
HasAnyRealCode = true;
19431943
}
1944-
if (MI.isCall() && MF->getTarget().Options.BBAddrMap)
1945-
OutStreamer->emitLabel(createCallsiteSymbol(MBB));
19461944

19471945
// If there is a pre-instruction symbol, emit a label for it here.
19481946
if (MCSymbol *S = MI.getPreInstrSymbol())
@@ -2064,6 +2062,9 @@ void AsmPrinter::emitFunctionBody() {
20642062
break;
20652063
}
20662064

2065+
if (MI.isCall() && MF->getTarget().Options.BBAddrMap)
2066+
OutStreamer->emitLabel(createCallsiteEndSymbol(MBB));
2067+
20672068
if (TM.Options.EmitCallGraphSection && MI.isCall())
20682069
emitIndirectCalleeLabels(FuncInfo, CallSitesInfoMap, MI);
20692070

@@ -2897,11 +2898,11 @@ MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) {
28972898
return Res.first->second;
28982899
}
28992900

2900-
MCSymbol *AsmPrinter::createCallsiteSymbol(const MachineBasicBlock &MBB) {
2901+
MCSymbol *AsmPrinter::createCallsiteEndSymbol(const MachineBasicBlock &MBB) {
29012902
MCContext &Ctx = MF->getContext();
29022903
MCSymbol *Sym = Ctx.createTempSymbol("BB" + Twine(MF->getFunctionNumber()) +
29032904
"_" + Twine(MBB.getNumber()) + "_CS");
2904-
CurrentFnCallsiteSymbols[&MBB].push_back(Sym);
2905+
CurrentFnCallsiteEndSymbols[&MBB].push_back(Sym);
29052906
return Sym;
29062907
}
29072908

@@ -2939,7 +2940,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) {
29392940
CurrentFnBegin = nullptr;
29402941
CurrentFnBeginLocal = nullptr;
29412942
CurrentSectionBeginSym = nullptr;
2942-
CurrentFnCallsiteSymbols.clear();
2943+
CurrentFnCallsiteEndSymbols.clear();
29432944
MBBSectionRanges.clear();
29442945
MBBSectionExceptionSyms.clear();
29452946
bool NeedsLocalForSize = MAI->needsLocalForSize();

llvm/lib/Object/ELF.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
847847
if (!FeatEnableOrErr)
848848
return FeatEnableOrErr.takeError();
849849
FeatEnable = *FeatEnableOrErr;
850-
if (FeatEnable.CallsiteOffsets && Version < 3)
850+
if (FeatEnable.CallsiteEndOffsets && Version < 3)
851851
return createError("version should be >= 3 for SHT_LLVM_BB_ADDR_MAP when "
852852
"callsite offsets feature is enabled: version = " +
853853
Twine(static_cast<int>(Version)) +
@@ -890,22 +890,22 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
890890
uint32_t ID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
891891
uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
892892
// Read the callsite offsets.
893-
uint32_t LastCallsiteOffset = 0;
894-
SmallVector<uint32_t, 1> CallsiteOffsets;
895-
if (FeatEnable.CallsiteOffsets) {
893+
uint32_t LastCallsiteEndOffset = 0;
894+
SmallVector<uint32_t, 1> CallsiteEndOffsets;
895+
if (FeatEnable.CallsiteEndOffsets) {
896896
uint32_t NumCallsites =
897897
readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
898-
CallsiteOffsets.reserve(NumCallsites);
898+
CallsiteEndOffsets.reserve(NumCallsites);
899899
for (uint32_t CallsiteIndex = 0;
900900
!ULEBSizeErr && Cur && (CallsiteIndex < NumCallsites);
901901
++CallsiteIndex) {
902-
LastCallsiteOffset +=
902+
LastCallsiteEndOffset +=
903903
readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
904-
CallsiteOffsets.push_back(LastCallsiteOffset);
904+
CallsiteEndOffsets.push_back(LastCallsiteEndOffset);
905905
}
906906
}
907907
uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr) +
908-
LastCallsiteOffset;
908+
LastCallsiteEndOffset;
909909
uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
910910
Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
911911
BBAddrMap::BBEntry::Metadata::decode(MD);
@@ -914,7 +914,7 @@ decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
914914
break;
915915
}
916916
BBEntries.push_back({ID, Offset + PrevBBEndOffset, Size,
917-
*MetadataOrErr, CallsiteOffsets});
917+
*MetadataOrErr, CallsiteEndOffsets});
918918
PrevBBEndOffset += Offset + Size;
919919
}
920920
TotalNumBlocks += BBEntries.size();

llvm/lib/ObjectYAML/ELFEmitter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,8 +1487,8 @@ void ELFState<ELFT>::writeSectionContent(
14871487
if (!E.BBRanges)
14881488
continue;
14891489
uint64_t TotalNumBlocks = 0;
1490-
bool EmitCallsiteOffsets =
1491-
FeatureOrErr->CallsiteOffsets || E.hasAnyCallsiteOffsets();
1490+
bool EmitCallsiteEndOffsets =
1491+
FeatureOrErr->CallsiteEndOffsets || E.hasAnyCallsiteEndOffsets();
14921492
for (const ELFYAML::BBAddrMapEntry::BBRangeEntry &BBR : *E.BBRanges) {
14931493
// Write the base address of the range.
14941494
CBA.write<uintX_t>(BBR.BaseAddress, ELFT::Endianness);
@@ -1506,12 +1506,12 @@ void ELFState<ELFT>::writeSectionContent(
15061506
if (Section.Type == llvm::ELF::SHT_LLVM_BB_ADDR_MAP && E.Version > 1)
15071507
SHeader.sh_size += CBA.writeULEB128(BBE.ID);
15081508
SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset);
1509-
if (EmitCallsiteOffsets) {
1510-
size_t NumCallsiteOffsets =
1511-
BBE.CallsiteOffsets ? BBE.CallsiteOffsets->size() : 0;
1512-
SHeader.sh_size += CBA.writeULEB128(NumCallsiteOffsets);
1513-
if (BBE.CallsiteOffsets) {
1514-
for (uint32_t Offset : *BBE.CallsiteOffsets)
1509+
if (EmitCallsiteEndOffsets) {
1510+
size_t NumCallsiteEndOffsets =
1511+
BBE.CallsiteEndOffsets ? BBE.CallsiteEndOffsets->size() : 0;
1512+
SHeader.sh_size += CBA.writeULEB128(NumCallsiteEndOffsets);
1513+
if (BBE.CallsiteEndOffsets) {
1514+
for (uint32_t Offset : *BBE.CallsiteEndOffsets)
15151515
SHeader.sh_size += CBA.writeULEB128(Offset);
15161516
}
15171517
}

llvm/lib/ObjectYAML/ELFYAML.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1884,7 +1884,7 @@ void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping(
18841884
IO.mapRequired("AddressOffset", E.AddressOffset);
18851885
IO.mapRequired("Size", E.Size);
18861886
IO.mapRequired("Metadata", E.Metadata);
1887-
IO.mapOptional("CallsiteOffsets", E.CallsiteOffsets);
1887+
IO.mapOptional("CallsiteEndOffsets", E.CallsiteEndOffsets);
18881888
}
18891889

18901890
void MappingTraits<ELFYAML::PGOAnalysisMapEntry>::mapping(

llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
# CHECK-NEXT: {
5050
# CHECK-NEXT: ID: 2
5151
# CHECK-NEXT: Offset: 0x3
52-
# CHECK-NEXT: Callsite Offsets: [1, 3]
52+
# CHECK-NEXT: Callsite End Offsets: [1, 3]
5353
# CHECK-NEXT: Size: 0x7
5454
# CHECK-NEXT: HasReturn: Yes
5555
# CHECK-NEXT: HasTailCall: No
@@ -159,7 +159,7 @@ Sections:
159159
AddressOffset: 0x3
160160
Size: 0x4
161161
Metadata: 0x15
162-
CallsiteOffsets: [ 0x1 , 0x2 ]
162+
CallsiteEndOffsets: [ 0x1 , 0x2 ]
163163
- Version: 2
164164
BBRanges:
165165
- BaseAddress: 0x22222

llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
# VALID-NEXT: AddressOffset: 0xA
4040
# VALID-NEXT: Size: 0xB
4141
# VALID-NEXT: Metadata: 0xC
42-
# VALID-NEXT: CallsiteOffsets: [ 0x1, 0x2 ]
42+
# VALID-NEXT: CallsiteEndOffsets: [ 0x1, 0x2 ]
4343

4444
--- !ELF
4545
FileHeader:
@@ -79,7 +79,7 @@ Sections:
7979
AddressOffset: 0xA
8080
Size: 0xB
8181
Metadata: 0xC
82-
CallsiteOffsets: [ 0x1, 0x2 ]
82+
CallsiteEndOffsets: [ 0x1, 0x2 ]
8383

8484
## Check obj2yaml can dump empty .llvm_bb_addr_map sections.
8585

0 commit comments

Comments
 (0)