diff --git a/llvm/docs/Extensions.rst b/llvm/docs/Extensions.rst index d8fb87b6998ad..6b1d9fe08e911 100644 --- a/llvm/docs/Extensions.rst +++ b/llvm/docs/Extensions.rst @@ -410,8 +410,8 @@ two years old). Each function entry starts with a version byte which specifies the encoding version to use. This is followed by a feature byte which specifies the features specific to this particular entry. The function base address is stored as a full address. Other addresses in the entry (block begin and end -addresses and callsite addresses) are stored in a running-offset fashion, as -offsets relative to prior addresses. +addresses and callsite end addresses) are stored in a running-offset fashion, +as offsets relative to prior addresses. The following versioning schemes are currently supported (newer versions support features of the older versions). @@ -438,8 +438,8 @@ Example: .byte 1 # BB_1 ID .uleb128 .LBB0_1-.LBB_END0_0 # BB_1 offset relative to the end of last block (BB_0). .byte 2 # number of callsites in this block - .uleb128 .LBB0_1_CS0-.LBB0_1 # offset of callsite relative to the previous offset (.LBB0_1) - .uleb128 .LBB0_1_CS1-.LBB0_1_CS0 # offset of callsite relative to the previous offset (.LBB0_1_CS0) + .uleb128 .LBB0_1_CS0-.LBB0_1 # offset of callsite end relative to the previous offset (.LBB0_1) + .uleb128 .LBB0_1_CS1-.LBB0_1_CS0 # offset of callsite end relative to the previous offset (.LBB0_1_CS0) .uleb128 .LBB_END0_1-.LBB0_1_CS1 # BB_1 size offset (Offset of the block end relative to the previous offset). .byte y # BB_1 metadata diff --git a/llvm/include/llvm/CodeGen/AsmPrinter.h b/llvm/include/llvm/CodeGen/AsmPrinter.h index 91c014236f6cb..71317619098ad 100644 --- a/llvm/include/llvm/CodeGen/AsmPrinter.h +++ b/llvm/include/llvm/CodeGen/AsmPrinter.h @@ -135,12 +135,12 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass { /// default, this is equal to CurrentFnSym. MCSymbol *CurrentFnSymForSize = nullptr; - /// Vector of symbols marking the position of callsites in the current + /// Vector of symbols marking the end of the callsites in the current /// function, keyed by their containing basic block. /// The callsite symbols of each block are stored in the order they appear /// in that block. DenseMap> - CurrentFnCallsiteSymbols; + CurrentFnCallsiteEndSymbols; /// Provides the profile information for constants. const StaticDataProfileInfo *SDPI = nullptr; @@ -332,9 +332,9 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass { /// to emit them as well, return the whole set. ArrayRef getAddrLabelSymbolToEmit(const BasicBlock *BB); - /// Creates a new symbol to be used for the beginning of a callsite at the - /// specified basic block. - MCSymbol *createCallsiteSymbol(const MachineBasicBlock &MBB); + /// Creates a new symbol to be used for the end of a callsite at the specified + /// basic block. + MCSymbol *createCallsiteEndSymbol(const MachineBasicBlock &MBB); /// If the specified function has had any references to address-taken blocks /// generated, but the block got deleted, return the symbol now so we can diff --git a/llvm/include/llvm/Object/ELFTypes.h b/llvm/include/llvm/Object/ELFTypes.h index f22b376c3ab5b..5a26e2fc31458 100644 --- a/llvm/include/llvm/Object/ELFTypes.h +++ b/llvm/include/llvm/Object/ELFTypes.h @@ -832,7 +832,7 @@ struct BBAddrMap { bool BrProb : 1; bool MultiBBRange : 1; bool OmitBBEntries : 1; - bool CallsiteOffsets : 1; + bool CallsiteEndOffsets : 1; bool hasPGOAnalysis() const { return FuncEntryCount || BBFreq || BrProb; } @@ -845,7 +845,7 @@ struct BBAddrMap { (static_cast(BrProb) << 2) | (static_cast(MultiBBRange) << 3) | (static_cast(OmitBBEntries) << 4) | - (static_cast(CallsiteOffsets) << 5); + (static_cast(CallsiteEndOffsets) << 5); } // Decodes from minimum bit width representation and validates no @@ -864,10 +864,10 @@ struct BBAddrMap { bool operator==(const Features &Other) const { return std::tie(FuncEntryCount, BBFreq, BrProb, MultiBBRange, - OmitBBEntries, CallsiteOffsets) == + OmitBBEntries, CallsiteEndOffsets) == std::tie(Other.FuncEntryCount, Other.BBFreq, Other.BrProb, Other.MultiBBRange, Other.OmitBBEntries, - Other.CallsiteOffsets); + Other.CallsiteEndOffsets); } }; @@ -918,20 +918,19 @@ struct BBAddrMap { uint32_t Size = 0; // Size of the basic block. Metadata MD = {false, false, false, false, false}; // Metdata for this basic block. - // Offsets of callsites (beginning of call instructions), relative to the - // basic block start. - SmallVector CallsiteOffsets; + // Offsets of end of call instructions, relative to the basic block start. + SmallVector CallsiteEndOffsets; BBEntry(uint32_t ID, uint32_t Offset, uint32_t Size, Metadata MD, - SmallVector CallsiteOffsets) + SmallVector CallsiteEndOffsets) : ID(ID), Offset(Offset), Size(Size), MD(MD), - CallsiteOffsets(std::move(CallsiteOffsets)) {} + CallsiteEndOffsets(std::move(CallsiteEndOffsets)) {} UniqueBBID getID() const { return {ID, 0}; } bool operator==(const BBEntry &Other) const { return ID == Other.ID && Offset == Other.Offset && Size == Other.Size && - MD == Other.MD && CallsiteOffsets == Other.CallsiteOffsets; + MD == Other.MD && CallsiteEndOffsets == Other.CallsiteEndOffsets; } bool hasReturn() const { return MD.HasReturn; } diff --git a/llvm/include/llvm/ObjectYAML/ELFYAML.h b/llvm/include/llvm/ObjectYAML/ELFYAML.h index 3bf8c29c99581..c90591d009dcd 100644 --- a/llvm/include/llvm/ObjectYAML/ELFYAML.h +++ b/llvm/include/llvm/ObjectYAML/ELFYAML.h @@ -162,7 +162,7 @@ struct BBAddrMapEntry { llvm::yaml::Hex64 AddressOffset; llvm::yaml::Hex64 Size; llvm::yaml::Hex64 Metadata; - std::optional> CallsiteOffsets; + std::optional> CallsiteEndOffsets; }; uint8_t Version; llvm::yaml::Hex8 Feature; @@ -183,14 +183,14 @@ struct BBAddrMapEntry { } // Returns if any BB entries have non-empty callsite offsets. - bool hasAnyCallsiteOffsets() const { + bool hasAnyCallsiteEndOffsets() const { if (!BBRanges) return false; for (const ELFYAML::BBAddrMapEntry::BBRangeEntry &BBR : *BBRanges) { if (!BBR.BBEntries) continue; for (const ELFYAML::BBAddrMapEntry::BBEntry &BBE : *BBR.BBEntries) - if (BBE.CallsiteOffsets && !BBE.CallsiteOffsets->empty()) + if (BBE.CallsiteEndOffsets && !BBE.CallsiteEndOffsets->empty()) return true; } return false; diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 23a3543e9ebec..cd14a4f57f760 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1432,7 +1432,7 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) { MCSection *BBAddrMapSection = getObjFileLowering().getBBAddrMapSection(*MF.getSection()); assert(BBAddrMapSection && ".llvm_bb_addr_map section is not initialized."); - bool HasCalls = !CurrentFnCallsiteSymbols.empty(); + bool HasCalls = !CurrentFnCallsiteEndSymbols.empty(); const MCSymbol *FunctionSymbol = getFunctionBegin(); @@ -1497,13 +1497,13 @@ void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) { emitLabelDifferenceAsULEB128(MBBSymbol, PrevMBBEndSymbol); const MCSymbol *CurrentLabel = MBBSymbol; if (HasCalls) { - auto CallsiteSymbols = CurrentFnCallsiteSymbols.lookup(&MBB); + auto CallsiteEndSymbols = CurrentFnCallsiteEndSymbols.lookup(&MBB); OutStreamer->AddComment("number of callsites"); - OutStreamer->emitULEB128IntValue(CallsiteSymbols.size()); - for (const MCSymbol *CallsiteSymbol : CallsiteSymbols) { + OutStreamer->emitULEB128IntValue(CallsiteEndSymbols.size()); + for (const MCSymbol *CallsiteEndSymbol : CallsiteEndSymbols) { // Emit the callsite offset. - emitLabelDifferenceAsULEB128(CallsiteSymbol, CurrentLabel); - CurrentLabel = CallsiteSymbol; + emitLabelDifferenceAsULEB128(CallsiteEndSymbol, CurrentLabel); + CurrentLabel = CallsiteEndSymbol; } } // Emit the offset to the end of the block, which can be used to compute @@ -1941,8 +1941,6 @@ void AsmPrinter::emitFunctionBody() { !MI.isDebugInstr()) { HasAnyRealCode = true; } - if (MI.isCall() && MF->getTarget().Options.BBAddrMap) - OutStreamer->emitLabel(createCallsiteSymbol(MBB)); // If there is a pre-instruction symbol, emit a label for it here. if (MCSymbol *S = MI.getPreInstrSymbol()) @@ -2064,6 +2062,9 @@ void AsmPrinter::emitFunctionBody() { break; } + if (MI.isCall() && MF->getTarget().Options.BBAddrMap) + OutStreamer->emitLabel(createCallsiteEndSymbol(MBB)); + if (TM.Options.EmitCallGraphSection && MI.isCall()) emitIndirectCalleeLabels(FuncInfo, CallSitesInfoMap, MI); @@ -2897,11 +2898,11 @@ MCSymbol *AsmPrinter::getMBBExceptionSym(const MachineBasicBlock &MBB) { return Res.first->second; } -MCSymbol *AsmPrinter::createCallsiteSymbol(const MachineBasicBlock &MBB) { +MCSymbol *AsmPrinter::createCallsiteEndSymbol(const MachineBasicBlock &MBB) { MCContext &Ctx = MF->getContext(); MCSymbol *Sym = Ctx.createTempSymbol("BB" + Twine(MF->getFunctionNumber()) + "_" + Twine(MBB.getNumber()) + "_CS"); - CurrentFnCallsiteSymbols[&MBB].push_back(Sym); + CurrentFnCallsiteEndSymbols[&MBB].push_back(Sym); return Sym; } @@ -2939,7 +2940,7 @@ void AsmPrinter::SetupMachineFunction(MachineFunction &MF) { CurrentFnBegin = nullptr; CurrentFnBeginLocal = nullptr; CurrentSectionBeginSym = nullptr; - CurrentFnCallsiteSymbols.clear(); + CurrentFnCallsiteEndSymbols.clear(); MBBSectionRanges.clear(); MBBSectionExceptionSyms.clear(); bool NeedsLocalForSize = MAI->needsLocalForSize(); diff --git a/llvm/lib/Object/ELF.cpp b/llvm/lib/Object/ELF.cpp index 788c6020a7f99..53699ce0d4fcf 100644 --- a/llvm/lib/Object/ELF.cpp +++ b/llvm/lib/Object/ELF.cpp @@ -847,7 +847,7 @@ decodeBBAddrMapImpl(const ELFFile &EF, if (!FeatEnableOrErr) return FeatEnableOrErr.takeError(); FeatEnable = *FeatEnableOrErr; - if (FeatEnable.CallsiteOffsets && Version < 3) + if (FeatEnable.CallsiteEndOffsets && Version < 3) return createError("version should be >= 3 for SHT_LLVM_BB_ADDR_MAP when " "callsite offsets feature is enabled: version = " + Twine(static_cast(Version)) + @@ -890,22 +890,22 @@ decodeBBAddrMapImpl(const ELFFile &EF, uint32_t ID = readULEB128As(Data, Cur, ULEBSizeErr); uint32_t Offset = readULEB128As(Data, Cur, ULEBSizeErr); // Read the callsite offsets. - uint32_t LastCallsiteOffset = 0; - SmallVector CallsiteOffsets; - if (FeatEnable.CallsiteOffsets) { + uint32_t LastCallsiteEndOffset = 0; + SmallVector CallsiteEndOffsets; + if (FeatEnable.CallsiteEndOffsets) { uint32_t NumCallsites = readULEB128As(Data, Cur, ULEBSizeErr); - CallsiteOffsets.reserve(NumCallsites); + CallsiteEndOffsets.reserve(NumCallsites); for (uint32_t CallsiteIndex = 0; !ULEBSizeErr && Cur && (CallsiteIndex < NumCallsites); ++CallsiteIndex) { - LastCallsiteOffset += + LastCallsiteEndOffset += readULEB128As(Data, Cur, ULEBSizeErr); - CallsiteOffsets.push_back(LastCallsiteOffset); + CallsiteEndOffsets.push_back(LastCallsiteEndOffset); } } uint32_t Size = readULEB128As(Data, Cur, ULEBSizeErr) + - LastCallsiteOffset; + LastCallsiteEndOffset; uint32_t MD = readULEB128As(Data, Cur, ULEBSizeErr); Expected MetadataOrErr = BBAddrMap::BBEntry::Metadata::decode(MD); @@ -914,7 +914,7 @@ decodeBBAddrMapImpl(const ELFFile &EF, break; } BBEntries.push_back({ID, Offset + PrevBBEndOffset, Size, - *MetadataOrErr, CallsiteOffsets}); + *MetadataOrErr, CallsiteEndOffsets}); PrevBBEndOffset += Offset + Size; } TotalNumBlocks += BBEntries.size(); diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp index bc5c68d08d11f..2386a2e3e6c49 100644 --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -1487,8 +1487,8 @@ void ELFState::writeSectionContent( if (!E.BBRanges) continue; uint64_t TotalNumBlocks = 0; - bool EmitCallsiteOffsets = - FeatureOrErr->CallsiteOffsets || E.hasAnyCallsiteOffsets(); + bool EmitCallsiteEndOffsets = + FeatureOrErr->CallsiteEndOffsets || E.hasAnyCallsiteEndOffsets(); for (const ELFYAML::BBAddrMapEntry::BBRangeEntry &BBR : *E.BBRanges) { // Write the base address of the range. CBA.write(BBR.BaseAddress, ELFT::Endianness); @@ -1506,12 +1506,12 @@ void ELFState::writeSectionContent( if (Section.Type == llvm::ELF::SHT_LLVM_BB_ADDR_MAP && E.Version > 1) SHeader.sh_size += CBA.writeULEB128(BBE.ID); SHeader.sh_size += CBA.writeULEB128(BBE.AddressOffset); - if (EmitCallsiteOffsets) { - size_t NumCallsiteOffsets = - BBE.CallsiteOffsets ? BBE.CallsiteOffsets->size() : 0; - SHeader.sh_size += CBA.writeULEB128(NumCallsiteOffsets); - if (BBE.CallsiteOffsets) { - for (uint32_t Offset : *BBE.CallsiteOffsets) + if (EmitCallsiteEndOffsets) { + size_t NumCallsiteEndOffsets = + BBE.CallsiteEndOffsets ? BBE.CallsiteEndOffsets->size() : 0; + SHeader.sh_size += CBA.writeULEB128(NumCallsiteEndOffsets); + if (BBE.CallsiteEndOffsets) { + for (uint32_t Offset : *BBE.CallsiteEndOffsets) SHeader.sh_size += CBA.writeULEB128(Offset); } } diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp index c27339de67efd..62d80a24f4787 100644 --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1884,7 +1884,7 @@ void MappingTraits::mapping( IO.mapRequired("AddressOffset", E.AddressOffset); IO.mapRequired("Size", E.Size); IO.mapRequired("Metadata", E.Metadata); - IO.mapOptional("CallsiteOffsets", E.CallsiteOffsets); + IO.mapOptional("CallsiteEndOffsets", E.CallsiteEndOffsets); } void MappingTraits::mapping( diff --git a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test index 5d7bc8baa9b25..fd1492fa091a8 100644 --- a/llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test +++ b/llvm/test/tools/llvm-readobj/ELF/bb-addr-map.test @@ -49,7 +49,7 @@ # CHECK-NEXT: { # CHECK-NEXT: ID: 2 # CHECK-NEXT: Offset: 0x3 -# CHECK-NEXT: Callsite Offsets: [1, 3] +# CHECK-NEXT: Callsite End Offsets: [1, 3] # CHECK-NEXT: Size: 0x7 # CHECK-NEXT: HasReturn: Yes # CHECK-NEXT: HasTailCall: No @@ -159,7 +159,7 @@ Sections: AddressOffset: 0x3 Size: 0x4 Metadata: 0x15 - CallsiteOffsets: [ 0x1 , 0x2 ] + CallsiteEndOffsets: [ 0x1 , 0x2 ] - Version: 2 BBRanges: - BaseAddress: 0x22222 diff --git a/llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml b/llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml index 861cb94692947..dc14025689b60 100644 --- a/llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml +++ b/llvm/test/tools/obj2yaml/ELF/bb-addr-map.yaml @@ -39,7 +39,7 @@ # VALID-NEXT: AddressOffset: 0xA # VALID-NEXT: Size: 0xB # VALID-NEXT: Metadata: 0xC -# VALID-NEXT: CallsiteOffsets: [ 0x1, 0x2 ] +# VALID-NEXT: CallsiteEndOffsets: [ 0x1, 0x2 ] --- !ELF FileHeader: @@ -79,7 +79,7 @@ Sections: AddressOffset: 0xA Size: 0xB Metadata: 0xC - CallsiteOffsets: [ 0x1, 0x2 ] + CallsiteEndOffsets: [ 0x1, 0x2 ] ## Check obj2yaml can dump empty .llvm_bb_addr_map sections. diff --git a/llvm/test/tools/yaml2obj/ELF/bb-addr-map.yaml b/llvm/test/tools/yaml2obj/ELF/bb-addr-map.yaml index 9fd0577b1e0fd..418f90ff8bafe 100644 --- a/llvm/test/tools/yaml2obj/ELF/bb-addr-map.yaml +++ b/llvm/test/tools/yaml2obj/ELF/bb-addr-map.yaml @@ -66,7 +66,7 @@ # CHECK-NEXT: 0000: 030800 # CHECK-NEXT: ) -# Case 9: Specify empty CallsiteOffsets. +# Case 9: Specify empty CallsiteEndOffsets. # CHECK: Name: .llvm_bb_addr_map (1) # CHECK: SectionData ( # CHECK-NEXT: 0000: 03202000 00000000 0000010E 01000203 @@ -113,7 +113,7 @@ Sections: AddressOffset: 0x00000001 Size: 0x00000002 Metadata: 0x00000003 - CallsiteOffsets: [0x1, 0x2] + CallsiteEndOffsets: [0x1, 0x2] ## 5) When specifying the description with Entries, the 'Address' field will be ## zero when omitted. @@ -174,7 +174,7 @@ Sections: AddressOffset: 0x00000001 Size: 0x00000002 Metadata: 0x00000003 - CallsiteOffsets: [] + CallsiteEndOffsets: [] ## Check we can't use Entries at the same time as either Content or Size. # RUN: not yaml2obj --docnum=2 -DCONTENT="00" %s 2>&1 | FileCheck %s --check-prefix=INVALID diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp index ade025fd3d892..a440bad130f4c 100644 --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -8106,8 +8106,8 @@ void LLVMELFDumper::printBBAddrMaps(bool PrettyPGOAnalysis) { DictScope BBED(W); W.printNumber("ID", BBE.ID); W.printHex("Offset", BBE.Offset); - if (!BBE.CallsiteOffsets.empty()) - W.printList("Callsite Offsets", BBE.CallsiteOffsets); + if (!BBE.CallsiteEndOffsets.empty()) + W.printList("Callsite End Offsets", BBE.CallsiteEndOffsets); W.printHex("Size", BBE.Size); W.printBoolean("HasReturn", BBE.hasReturn()); W.printBoolean("HasTailCall", BBE.hasTailCall()); diff --git a/llvm/tools/obj2yaml/elf2yaml.cpp b/llvm/tools/obj2yaml/elf2yaml.cpp index ab15553df1e84..ef4552f734736 100644 --- a/llvm/tools/obj2yaml/elf2yaml.cpp +++ b/llvm/tools/obj2yaml/elf2yaml.cpp @@ -935,19 +935,19 @@ ELFDumper::dumpBBAddrMapSection(const Elf_Shdr *Shdr) { ++BlockIndex) { uint32_t ID = Version >= 2 ? Data.getULEB128(Cur) : BlockIndex; uint64_t Offset = Data.getULEB128(Cur); - std::optional> CallsiteOffsets; - if (FeatureOrErr->CallsiteOffsets) { + std::optional> CallsiteEndOffsets; + if (FeatureOrErr->CallsiteEndOffsets) { uint32_t NumCallsites = Data.getULEB128(Cur); - CallsiteOffsets = std::vector(NumCallsites, 0); + CallsiteEndOffsets = std::vector(NumCallsites, 0); for (uint32_t CallsiteIndex = 0; Cur && CallsiteIndex < NumCallsites; ++CallsiteIndex) { - (*CallsiteOffsets)[CallsiteIndex] = Data.getULEB128(Cur); + (*CallsiteEndOffsets)[CallsiteIndex] = Data.getULEB128(Cur); } } uint64_t Size = Data.getULEB128(Cur); uint64_t Metadata = Data.getULEB128(Cur); BBEntries.push_back( - {ID, Offset, Size, Metadata, std::move(CallsiteOffsets)}); + {ID, Offset, Size, Metadata, std::move(CallsiteEndOffsets)}); } TotalNumBlocks += BBEntries.size(); BBRanges.push_back({BaseAddress, /*NumBlocks=*/{}, BBEntries}); diff --git a/llvm/unittests/Object/ELFObjectFileTest.cpp b/llvm/unittests/Object/ELFObjectFileTest.cpp index 25b390758f172..17d9f5076b73f 100644 --- a/llvm/unittests/Object/ELFObjectFileTest.cpp +++ b/llvm/unittests/Object/ELFObjectFileTest.cpp @@ -680,7 +680,7 @@ TEST(ELFObjectFileTest, InvalidDecodeBBAddrMap) { - AddressOffset: 0x0 Size: 0x1 Metadata: 0x2 - CallsiteOffsets: [ 0x1 ] + CallsiteEndOffsets: [ 0x1 ] )"; { @@ -713,7 +713,7 @@ TEST(ELFObjectFileTest, ReadBBAddrMap) { AddressOffset: 0x0 Size: 0x1 Metadata: 0x2 - CallsiteOffsets: [ 0x1 , 0x1 ] + CallsiteEndOffsets: [ 0x1 , 0x1 ] - Name: .llvm_bb_addr_map_2 Type: SHT_LLVM_BB_ADDR_MAP Link: 1