Skip to content

Commit 98e2d63

Browse files
committed
[BOLT][NFCI] Use MetadataRewriter interface to update SDT markers
Migrate SDT markers processing to the new MetadataRewriter interface. Depends on D154020 Reviewed By: Amir Differential Revision: https://reviews.llvm.org/D154021
1 parent c9b1f06 commit 98e2d63

File tree

10 files changed

+197
-130
lines changed

10 files changed

+197
-130
lines changed

bolt/include/bolt/Core/BinaryBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class BinaryBasicBlock {
103103
/// After output/codegen, map output offsets of instructions in this basic
104104
/// block to instruction offsets in the original function. Note that the
105105
/// output basic block could be different from the input basic block.
106-
/// We only map instruction of interest, such as calls, and sdt markers.
106+
/// We only map instruction of interest, such as calls and markers.
107107
///
108108
/// We store the offset array in a basic block to facilitate BAT tables
109109
/// generation. Otherwise, the mapping could be done at function level.

bolt/include/bolt/Core/BinaryContext.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,9 +673,6 @@ class BinaryContext {
673673
/// List of functions that always trap.
674674
std::vector<const BinaryFunction *> TrappedFunctions;
675675

676-
/// Map SDT locations to SDT markers info
677-
std::unordered_map<uint64_t, SDTMarkerInfo> SDTMarkers;
678-
679676
/// Map linux kernel program locations/instructions to their pointers in
680677
/// special linux kernel sections
681678
std::unordered_map<uint64_t, std::vector<LKInstructionMarkerInfo>> LKMarkers;

bolt/include/bolt/Core/BinaryFunction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,8 @@ class BinaryFunction {
17021702
/// Indicate that another function body was merged with this function.
17031703
void setHasFunctionsFoldedInto() { HasFunctionsFoldedInto = true; }
17041704

1705+
void setHasSDTMarker(bool V) { HasSDTMarker = V; }
1706+
17051707
BinaryFunction &setPersonalityFunction(uint64_t Addr) {
17061708
assert(!PersonalityFunction && "can't set personality function twice");
17071709
PersonalityFunction = BC.getOrCreateGlobalSymbol(Addr, "FUNCat");

bolt/include/bolt/Core/BinarySection.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -509,18 +509,6 @@ inline raw_ostream &operator<<(raw_ostream &OS, const BinarySection &Section) {
509509
return OS;
510510
}
511511

512-
struct SDTMarkerInfo {
513-
uint64_t PC;
514-
uint64_t Base;
515-
uint64_t Semaphore;
516-
StringRef Provider;
517-
StringRef Name;
518-
StringRef Args;
519-
520-
/// The offset of PC within the note section
521-
unsigned PCOffset;
522-
};
523-
524512
/// Linux Kernel special sections point to a specific instruction in many cases.
525513
/// Unlike SDTMarkerInfo, these markers can come from different sections.
526514
struct LKInstructionMarkerInfo {

bolt/include/bolt/Rewrite/MetadataRewriters.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ namespace llvm {
1515
namespace bolt {
1616

1717
class MetadataRewriter;
18+
class BinaryContext;
1819

19-
/// The list of rewriter build functions (e.g. createNewMetadataRewriter()) that
20-
/// return std::unique_ptr<MetadataRewriter>.
20+
// The list of rewriter build functions.
21+
22+
std::unique_ptr<MetadataRewriter> createSDTRewriter(BinaryContext &);
2123

2224
} // namespace bolt
2325
} // namespace llvm

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,6 @@ class RewriteInstance {
198198
/// Update debug and other auxiliary information in the file.
199199
void updateMetadata();
200200

201-
/// Update SDTMarkers' locations for the output binary.
202-
void updateSDTMarkers();
203-
204201
/// Update LKMarkers' locations for the output binary.
205202
void updateLKMarkers();
206203

@@ -398,16 +395,10 @@ class RewriteInstance {
398395
/// of appending contents to it.
399396
bool willOverwriteSection(StringRef SectionName);
400397

401-
/// Parse .note.stapsdt section
402-
void parseSDTNotes();
403-
404398
/// Parse .pseudo_probe_desc section and .pseudo_probe section
405399
/// Setup Pseudo probe decoder
406400
void parsePseudoProbe();
407401

408-
/// Print all SDT markers
409-
void printSDTMarkers();
410-
411402
public:
412403
/// Standard ELF sections we overwrite.
413404
static constexpr const char *SectionsToOverwrite[] = {
@@ -589,10 +580,6 @@ class RewriteInstance {
589580
/// .note.gnu.build-id section.
590581
ErrorOr<BinarySection &> BuildIDSection{std::errc::bad_address};
591582

592-
/// .note.stapsdt section.
593-
/// Contains information about statically defined tracing points
594-
ErrorOr<BinarySection &> SDTSection{std::errc::bad_address};
595-
596583
/// .pseudo_probe_desc section.
597584
/// Contains information about pseudo probe description, like its related
598585
/// function

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,19 +1988,20 @@ bool BinaryFunction::buildCFG(MCPlusBuilder::AllocatorIdTy AllocatorId) {
19881988
updateOffset(LastInstrOffset);
19891989
}
19901990

1991-
const uint64_t InstrInputAddr = I->first + Address;
1992-
bool IsSDTMarker =
1993-
MIB->isNoop(Instr) && BC.SDTMarkers.count(InstrInputAddr);
1994-
bool IsLKMarker = BC.LKMarkers.count(InstrInputAddr);
1991+
bool IsLKMarker = BC.LKMarkers.count(I->first + Address);
19951992
// Mark all nops with Offset for profile tracking purposes.
19961993
if (MIB->isNoop(Instr) || IsLKMarker) {
1997-
if (!MIB->getOffset(Instr))
1994+
// If "Offset" annotation is not present, set it and mark the nop for
1995+
// deletion.
1996+
if (!MIB->getOffset(Instr)) {
19981997
MIB->setOffset(Instr, static_cast<uint32_t>(Offset), AllocatorId);
1999-
if (IsSDTMarker || IsLKMarker)
2000-
HasSDTMarker = true;
2001-
else
20021998
// Annotate ordinary nops, so we can safely delete them if required.
2003-
MIB->addAnnotation(Instr, "NOP", static_cast<uint32_t>(1), AllocatorId);
1999+
if (!IsLKMarker)
2000+
MIB->addAnnotation(Instr, "NOP", static_cast<uint32_t>(1),
2001+
AllocatorId);
2002+
}
2003+
if (IsLKMarker)
2004+
HasSDTMarker = true;
20042005
}
20052006

20062007
if (!InsertBB) {

bolt/lib/Rewrite/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ add_llvm_library(LLVMBOLTRewrite
1717
MachORewriteInstance.cpp
1818
MetadataManager.cpp
1919
RewriteInstance.cpp
20+
SDTRewriter.cpp
2021

2122
DISABLE_LLVM_LINK_LLVM_DYLIB
2223

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 1 addition & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ static cl::opt<bool> PrintLoopInfo("print-loops",
196196
cl::desc("print loop related information"),
197197
cl::Hidden, cl::cat(BoltCategory));
198198

199-
static cl::opt<bool> PrintSDTMarkers("print-sdt",
200-
cl::desc("print all SDT markers"),
201-
cl::Hidden, cl::cat(BoltCategory));
202-
203199
enum PrintPseudoProbesOptions {
204200
PPP_None = 0,
205201
PPP_Probes_Section_Decode = 0x1,
@@ -634,51 +630,6 @@ Error RewriteInstance::discoverStorage() {
634630
return Error::success();
635631
}
636632

637-
void RewriteInstance::parseSDTNotes() {
638-
if (!SDTSection)
639-
return;
640-
641-
StringRef Buf = SDTSection->getContents();
642-
DataExtractor DE = DataExtractor(Buf, BC->AsmInfo->isLittleEndian(),
643-
BC->AsmInfo->getCodePointerSize());
644-
uint64_t Offset = 0;
645-
646-
while (DE.isValidOffset(Offset)) {
647-
uint32_t NameSz = DE.getU32(&Offset);
648-
DE.getU32(&Offset); // skip over DescSz
649-
uint32_t Type = DE.getU32(&Offset);
650-
Offset = alignTo(Offset, 4);
651-
652-
if (Type != 3)
653-
errs() << "BOLT-WARNING: SDT note type \"" << Type
654-
<< "\" is not expected\n";
655-
656-
if (NameSz == 0)
657-
errs() << "BOLT-WARNING: SDT note has empty name\n";
658-
659-
StringRef Name = DE.getCStr(&Offset);
660-
661-
if (!Name.equals("stapsdt"))
662-
errs() << "BOLT-WARNING: SDT note name \"" << Name
663-
<< "\" is not expected\n";
664-
665-
// Parse description
666-
SDTMarkerInfo Marker;
667-
Marker.PCOffset = Offset;
668-
Marker.PC = DE.getU64(&Offset);
669-
Marker.Base = DE.getU64(&Offset);
670-
Marker.Semaphore = DE.getU64(&Offset);
671-
Marker.Provider = DE.getCStr(&Offset);
672-
Marker.Name = DE.getCStr(&Offset);
673-
Marker.Args = DE.getCStr(&Offset);
674-
Offset = alignTo(Offset, 4);
675-
BC->SDTMarkers[Marker.PC] = Marker;
676-
}
677-
678-
if (opts::PrintSDTMarkers)
679-
printSDTMarkers();
680-
}
681-
682633
void RewriteInstance::parsePseudoProbe() {
683634
if (!PseudoProbeDescSection && !PseudoProbeSection) {
684635
// pesudo probe is not added to binary. It is normal and no warning needed.
@@ -728,19 +679,6 @@ void RewriteInstance::parsePseudoProbe() {
728679
}
729680
}
730681

731-
void RewriteInstance::printSDTMarkers() {
732-
outs() << "BOLT-INFO: Number of SDT markers is " << BC->SDTMarkers.size()
733-
<< "\n";
734-
for (auto It : BC->SDTMarkers) {
735-
SDTMarkerInfo &Marker = It.second;
736-
outs() << "BOLT-INFO: PC: " << utohexstr(Marker.PC)
737-
<< ", Base: " << utohexstr(Marker.Base)
738-
<< ", Semaphore: " << utohexstr(Marker.Semaphore)
739-
<< ", Provider: " << Marker.Provider << ", Name: " << Marker.Name
740-
<< ", Args: " << Marker.Args << "\n";
741-
}
742-
}
743-
744682
void RewriteInstance::parseBuildID() {
745683
if (!BuildIDSection)
746684
return;
@@ -1853,7 +1791,6 @@ Error RewriteInstance::readSpecialSections() {
18531791
LSDASection = BC->getUniqueSectionByName(".gcc_except_table");
18541792
EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
18551793
BuildIDSection = BC->getUniqueSectionByName(".note.gnu.build-id");
1856-
SDTSection = BC->getUniqueSectionByName(".note.stapsdt");
18571794
PseudoProbeDescSection = BC->getUniqueSectionByName(".pseudo_probe_desc");
18581795
PseudoProbeSection = BC->getUniqueSectionByName(".pseudo_probe");
18591796

@@ -1910,8 +1847,6 @@ Error RewriteInstance::readSpecialSections() {
19101847
if (std::optional<std::string> FileBuildID = getPrintableBuildID())
19111848
BC->setFileBuildID(*FileBuildID);
19121849

1913-
parseSDTNotes();
1914-
19151850
// Read .dynamic/PT_DYNAMIC.
19161851
return readELFDynamic();
19171852
}
@@ -3245,7 +3180,7 @@ void RewriteInstance::preprocessProfileData() {
32453180
}
32463181

32473182
void RewriteInstance::initializeMetadataManager() {
3248-
// TODO
3183+
MetadataManager.registerRewriter(createSDTRewriter(*BC));
32493184
}
32503185

32513186
void RewriteInstance::processMetadataPreCFG() {
@@ -3621,7 +3556,6 @@ void RewriteInstance::updateMetadata() {
36213556
MetadataManager.runFinalizersAfterEmit();
36223557

36233558
// TODO: use MetadataManager for updates.
3624-
updateSDTMarkers();
36253559
updateLKMarkers();
36263560
parsePseudoProbe();
36273561
updatePseudoProbes();
@@ -3902,29 +3836,6 @@ void RewriteInstance::encodePseudoProbes() {
39023836
}
39033837
}
39043838

3905-
void RewriteInstance::updateSDTMarkers() {
3906-
NamedRegionTimer T("updateSDTMarkers", "update SDT markers", TimerGroupName,
3907-
TimerGroupDesc, opts::TimeRewrite);
3908-
3909-
if (!SDTSection)
3910-
return;
3911-
SDTSection->registerPatcher(std::make_unique<SimpleBinaryPatcher>());
3912-
3913-
SimpleBinaryPatcher *SDTNotePatcher =
3914-
static_cast<SimpleBinaryPatcher *>(SDTSection->getPatcher());
3915-
for (auto &SDTInfoKV : BC->SDTMarkers) {
3916-
const uint64_t OriginalAddress = SDTInfoKV.first;
3917-
SDTMarkerInfo &SDTInfo = SDTInfoKV.second;
3918-
const BinaryFunction *F =
3919-
BC->getBinaryFunctionContainingAddress(OriginalAddress);
3920-
if (!F)
3921-
continue;
3922-
const uint64_t NewAddress =
3923-
F->translateInputToOutputAddress(OriginalAddress);
3924-
SDTNotePatcher->addLE64Patch(SDTInfo.PCOffset, NewAddress);
3925-
}
3926-
}
3927-
39283839
void RewriteInstance::updateLKMarkers() {
39293840
if (BC->LKMarkers.size() == 0)
39303841
return;

0 commit comments

Comments
 (0)