Skip to content

Commit 73f5f83

Browse files
authored
[BasicBlockSections] Using MBBSectionID as DenseMap key (#97295)
getSectionIDNum may return same value for two different MBBSectionID. e.g. A Cold type MBBSectionID with number 0 and a Default type MBBSectionID with number 2 get same value 2 from getSectionIDNum. This may lead to overwrite of MBBSectionRanges. Using MBBSectionID itself as DenseMap key is better choice.
1 parent a0c6b8a commit 73f5f83

File tree

6 files changed

+37
-20
lines changed

6 files changed

+37
-20
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ class AsmPrinter : public MachineFunctionPass {
137137
MCSymbol *BeginLabel, *EndLabel;
138138
};
139139

140-
MapVector<unsigned, MBBSectionRange> MBBSectionRanges;
140+
MapVector<MBBSectionID, MBBSectionRange> MBBSectionRanges;
141141

142142
/// Map global GOT equivalent MCSymbols to GlobalVariables and keep track of
143143
/// its number of uses by other globals.
@@ -157,7 +157,7 @@ class AsmPrinter : public MachineFunctionPass {
157157
/// Map a basic block section ID to the exception symbol associated with that
158158
/// section. Map entries are assigned and looked up via
159159
/// AsmPrinter::getMBBExceptionSym.
160-
DenseMap<unsigned, MCSymbol *> MBBSectionExceptionSyms;
160+
DenseMap<MBBSectionID, MCSymbol *> MBBSectionExceptionSyms;
161161

162162
// The symbol used to represent the start of the current BB section of the
163163
// function. This is used to calculate the size of the BB section.

llvm/include/llvm/CodeGen/MachineBasicBlock.h

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#ifndef LLVM_CODEGEN_MACHINEBASICBLOCK_H
1414
#define LLVM_CODEGEN_MACHINEBASICBLOCK_H
1515

16+
#include "llvm/ADT/DenseMapInfo.h"
1617
#include "llvm/ADT/GraphTraits.h"
1718
#include "llvm/ADT/SparseBitVector.h"
1819
#include "llvm/ADT/ilist.h"
@@ -74,6 +75,25 @@ struct MBBSectionID {
7475
MBBSectionID(SectionType T) : Type(T), Number(0) {}
7576
};
7677

78+
template <> struct DenseMapInfo<MBBSectionID> {
79+
using TypeInfo = DenseMapInfo<MBBSectionID::SectionType>;
80+
using NumberInfo = DenseMapInfo<unsigned>;
81+
82+
static inline MBBSectionID getEmptyKey() {
83+
return MBBSectionID(NumberInfo::getEmptyKey());
84+
}
85+
static inline MBBSectionID getTombstoneKey() {
86+
return MBBSectionID(NumberInfo::getTombstoneKey());
87+
}
88+
static unsigned getHashValue(const MBBSectionID &SecID) {
89+
return detail::combineHashValue(TypeInfo::getHashValue(SecID.Type),
90+
NumberInfo::getHashValue(SecID.Number));
91+
}
92+
static bool isEqual(const MBBSectionID &LHS, const MBBSectionID &RHS) {
93+
return LHS == RHS;
94+
}
95+
};
96+
7797
// This structure represents the information for a basic block pertaining to
7898
// the basic block sections profile.
7999
struct UniqueBBID {
@@ -658,12 +678,6 @@ class MachineBasicBlock
658678
/// Returns the section ID of this basic block.
659679
MBBSectionID getSectionID() const { return SectionID; }
660680

661-
/// Returns the unique section ID number of this basic block.
662-
unsigned getSectionIDNum() const {
663-
return ((unsigned)MBBSectionID::SectionType::Cold) -
664-
((unsigned)SectionID.Type) + SectionID.Number;
665-
}
666-
667681
/// Sets the fixed BBID of this basic block.
668682
void setBBID(const UniqueBBID &V) {
669683
assert(!BBID.has_value() && "Cannot change BBID.");

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
13741374
OutStreamer->emitULEB128IntValue(MBBSectionRanges.size());
13751375
}
13761376
// Number of blocks in each MBB section.
1377-
MapVector<unsigned, unsigned> MBBSectionNumBlocks;
1377+
MapVector<MBBSectionID, unsigned> MBBSectionNumBlocks;
13781378
const MCSymbol *PrevMBBEndSymbol = nullptr;
13791379
if (!Features.MultiBBRange) {
13801380
OutStreamer->AddComment("function address");
@@ -1388,7 +1388,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
13881388
BBCount++;
13891389
if (MBB.isEndSection()) {
13901390
// Store each section's basic block count when it ends.
1391-
MBBSectionNumBlocks[MBB.getSectionIDNum()] = BBCount;
1391+
MBBSectionNumBlocks[MBB.getSectionID()] = BBCount;
13921392
// Reset the count for the next section.
13931393
BBCount = 0;
13941394
}
@@ -1404,8 +1404,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) {
14041404
OutStreamer->AddComment("base address");
14051405
OutStreamer->emitSymbolValue(MBBSymbol, getPointerSize());
14061406
OutStreamer->AddComment("number of basic blocks");
1407-
OutStreamer->emitULEB128IntValue(
1408-
MBBSectionNumBlocks[MBB.getSectionIDNum()]);
1407+
OutStreamer->emitULEB128IntValue(MBBSectionNumBlocks[MBB.getSectionID()]);
14091408
PrevMBBEndSymbol = MBBSymbol;
14101409
}
14111410
// TODO: Remove this check when version 1 is deprecated.
@@ -1855,7 +1854,9 @@ void AsmPrinter::emitFunctionBody() {
18551854
OutContext);
18561855
OutStreamer->emitELFSize(CurrentSectionBeginSym, SizeExp);
18571856
}
1858-
MBBSectionRanges[MBB.getSectionIDNum()] =
1857+
assert(!MBBSectionRanges.contains(MBB.getSectionID()) &&
1858+
"Overwrite section range");
1859+
MBBSectionRanges[MBB.getSectionID()] =
18591860
MBBSectionRange{CurrentSectionBeginSym, MBB.getEndSymbol()};
18601861
}
18611862
}
@@ -1972,7 +1973,9 @@ void AsmPrinter::emitFunctionBody() {
19721973
for (auto &Handler : Handlers)
19731974
Handler->markFunctionEnd();
19741975

1975-
MBBSectionRanges[MF->front().getSectionIDNum()] =
1976+
assert(!MBBSectionRanges.contains(MF->front().getSectionID()) &&
1977+
"Overwrite section range");
1978+
MBBSectionRanges[MF->front().getSectionID()] =
19761979
MBBSectionRange{CurrentFnBegin, CurrentFnEnd};
19771980

19781981
// Print out jump tables referenced by the function.
@@ -2536,7 +2539,7 @@ bool AsmPrinter::doFinalization(Module &M) {
25362539
}
25372540

25382541
MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) {
2539-
auto Res = MBBSectionExceptionSyms.try_emplace(MBB.getSectionIDNum());
2542+
auto Res = MBBSectionExceptionSyms.try_emplace(MBB.getSectionID());
25402543
if (Res.second)
25412544
Res.first->second = createTempSymbol("exception");
25422545
return Res.first->second;

llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ void DwarfCompileUnit::attachRangesOrLowHighPC(
684684
// the order of blocks will be frozen beyond this point.
685685
do {
686686
if (MBB->sameSection(EndMBB) || MBB->isEndSection()) {
687-
auto MBBSectionRange = Asm->MBBSectionRanges[MBB->getSectionIDNum()];
687+
auto MBBSectionRange = Asm->MBBSectionRanges[MBB->getSectionID()];
688688
List.push_back(
689689
{MBB->sameSection(BeginMBB) ? BeginLabel
690690
: MBBSectionRange.BeginLabel,

llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ bool DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
17131713
const MCSymbol *EndLabel;
17141714
if (std::next(EI) == Entries.end()) {
17151715
const MachineBasicBlock &EndMBB = Asm->MF->back();
1716-
EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionIDNum()].EndLabel;
1716+
EndLabel = Asm->MBBSectionRanges[EndMBB.getSectionID()].EndLabel;
17171717
if (EI->isClobber())
17181718
EndMI = EI->getInstr();
17191719
}
@@ -2064,7 +2064,7 @@ void DwarfDebug::beginInstruction(const MachineInstr *MI) {
20642064

20652065
bool PrevInstInSameSection =
20662066
(!PrevInstBB ||
2067-
PrevInstBB->getSectionIDNum() == MI->getParent()->getSectionIDNum());
2067+
PrevInstBB->getSectionID() == MI->getParent()->getSectionID());
20682068
if (DL == PrevInstLoc && PrevInstInSameSection) {
20692069
// If we have an ongoing unspecified location, nothing to do here.
20702070
if (!DL)

llvm/lib/CodeGen/AsmPrinter/EHStreamer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,8 @@ void EHStreamer::computeCallSiteTable(
253253
// We start a call-site range upon function entry and at the beginning of
254254
// every basic block section.
255255
CallSiteRanges.push_back(
256-
{Asm->MBBSectionRanges[MBB.getSectionIDNum()].BeginLabel,
257-
Asm->MBBSectionRanges[MBB.getSectionIDNum()].EndLabel,
256+
{Asm->MBBSectionRanges[MBB.getSectionID()].BeginLabel,
257+
Asm->MBBSectionRanges[MBB.getSectionID()].EndLabel,
258258
Asm->getMBBExceptionSym(MBB), CallSites.size()});
259259
PreviousIsInvoke = false;
260260
SawPotentiallyThrowing = false;

0 commit comments

Comments
 (0)