diff --git a/docs/Extensions.rst b/docs/Extensions.rst index 0fca0684593..8111d2f0d85 100644 --- a/docs/Extensions.rst +++ b/docs/Extensions.rst @@ -395,6 +395,30 @@ the symbol that belongs to the partition. It may be constructed as follows: .. _partition: https://lld.llvm.org/Partitions.html +``SHT_LLVM_BB_ADDR_MAP`` Section (basic block address map) +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +This section stores the binary address of basic blocks along with other related +metadata. This information can be used to map binary profiles (like perf +profiles) directly to machine basic blocks. +This section is emitted with ``-basic-block-sections=labels`` and will contain +a BB address map table for every function which may be constructed as follows: + +.. code-block:: gas + + .section ".llvm_bb_addr_map","",@llvm_bb_addr_map + .quad .Lfunc_begin0 # address of the function + .byte 2 # number of basic blocks + # BB record for BB_0 + .uleb128 .Lfunc_beign0-.Lfunc_begin0 # BB_0 offset relative to function entry (always zero) + .uleb128 .LBB_END0_0-.Lfunc_begin0 # BB_0 size + .byte x # BB_0 metadata + # BB record for BB_1 + .uleb128 .LBB0_1-.Lfunc_begin0 # BB_1 offset relative to function entry + .uleb128 .LBB_END0_1-.Lfunc_begin0 # BB_1 size + .byte y # BB_1 metadata + +This creates a BB address map table for a function with two basic blocks. + CodeView-Dependent ------------------ diff --git a/include/llvm/BinaryFormat/ELF.h b/include/llvm/BinaryFormat/ELF.h index 8fa5646879c..32757a9fb4c 100644 --- a/include/llvm/BinaryFormat/ELF.h +++ b/include/llvm/BinaryFormat/ELF.h @@ -856,10 +856,11 @@ enum : unsigned { SHT_LLVM_ADDRSIG = 0x6fff4c03, // List of address-significant symbols // for safe ICF. SHT_LLVM_DEPENDENT_LIBRARIES = - 0x6fff4c04, // LLVM Dependent Library Specifiers. - SHT_LLVM_SYMPART = 0x6fff4c05, // Symbol partition specification. - SHT_LLVM_PART_EHDR = 0x6fff4c06, // ELF header for loadable partition. - SHT_LLVM_PART_PHDR = 0x6fff4c07, // Phdrs for loadable partition. + 0x6fff4c04, // LLVM Dependent Library Specifiers. + SHT_LLVM_SYMPART = 0x6fff4c05, // Symbol partition specification. + SHT_LLVM_PART_EHDR = 0x6fff4c06, // ELF header for loadable partition. + SHT_LLVM_PART_PHDR = 0x6fff4c07, // Phdrs for loadable partition. + SHT_LLVM_BB_ADDR_MAP = 0x6fff4c08, // LLVM Basic Block Address Map. // Android's experimental support for SHT_RELR sections. // https://android.googlesource.com/platform/bionic/+/b7feec74547f84559a1467aca02708ff61346d2a/libc/include/elf.h#512 SHT_ANDROID_RELR = 0x6fffff00, // Relocation entries; only offsets. diff --git a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index f45f8b7cb96..f5587c9ae7d 100644 --- a/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -1023,9 +1023,10 @@ void AsmPrinter::emitFrameAlloc(const MachineInstr &MI) { MCConstantExpr::create(FrameOffset, OutContext)); } -/// Returns the BB metadata to be emitted in the bb_addr_map section for a given -/// basic block. This can be used to capture more precise profile information. -/// We use the last 3 bits (LSBs) to ecnode the following information: +/// Returns the BB metadata to be emitted in the .llvm_bb_addr_map section for a +/// given basic block. This can be used to capture more precise profile +/// information. We use the last 3 bits (LSBs) to ecnode the following +/// information: /// * (1): set if return block (ret or tail call). /// * (2): set if ends with a tail call. /// * (3): set if exception handling (EH) landing pad. @@ -1040,7 +1041,7 @@ static unsigned getBBAddrMapMetadata(const MachineBasicBlock &MBB) { void AsmPrinter::emitBBAddrMapSection(const MachineFunction &MF) { MCSection *BBAddrMapSection = getObjFileLowering().getBBAddrMapSection(*MF.getSection()); - assert(BBAddrMapSection && ".bb_addr_map section is not initialized."); + assert(BBAddrMapSection && ".llvm_bb_addr_map section is not initialized."); const MCSymbol *FunctionSymbol = getFunctionBegin(); diff --git a/lib/CodeGen/BasicBlockSections.cpp b/lib/CodeGen/BasicBlockSections.cpp index adbe94a5dad..5c47481d45b 100644 --- a/lib/CodeGen/BasicBlockSections.cpp +++ b/lib/CodeGen/BasicBlockSections.cpp @@ -49,9 +49,9 @@ // ================== // // With -fbasic-block-sections=labels, we emit the offsets of BB addresses of -// every function into a .bb_addr_map section. Along with the function symbols, -// this allows for mapping of virtual addresses in PMU profiles back to the -// corresponding basic blocks. This logic is implemented in AsmPrinter. This +// every function into the .llvm_bb_addr_map section. Along with the function +// symbols, this allows for mapping of virtual addresses in PMU profiles back to +// the corresponding basic blocks. This logic is implemented in AsmPrinter. This // pass only assigns the BBSectionType of every function to ``labels``. // //===----------------------------------------------------------------------===// diff --git a/lib/MC/MCObjectFileInfo.cpp b/lib/MC/MCObjectFileInfo.cpp index ae7345c4e05..fd22d724290 100644 --- a/lib/MC/MCObjectFileInfo.cpp +++ b/lib/MC/MCObjectFileInfo.cpp @@ -1002,7 +1002,7 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const { Flags |= ELF::SHF_GROUP; } - return Ctx->getELFSection(".bb_addr_map", ELF::SHT_PROGBITS, Flags, 0, - GroupName, MCSection::NonUniqueID, + return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP, + Flags, 0, GroupName, MCSection::NonUniqueID, cast(TextSec.getBeginSymbol())); } diff --git a/lib/MC/MCParser/ELFAsmParser.cpp b/lib/MC/MCParser/ELFAsmParser.cpp index 41779d023a5..440d3a38191 100644 --- a/lib/MC/MCParser/ELFAsmParser.cpp +++ b/lib/MC/MCParser/ELFAsmParser.cpp @@ -626,6 +626,8 @@ EndStmt: Type = ELF::SHT_LLVM_DEPENDENT_LIBRARIES; else if (TypeName == "llvm_sympart") Type = ELF::SHT_LLVM_SYMPART; + else if (TypeName == "llvm_bb_addr_map") + Type = ELF::SHT_LLVM_BB_ADDR_MAP; else if (TypeName.getAsInteger(0, Type)) return TokError("unknown section type"); } diff --git a/lib/MC/MCSectionELF.cpp b/lib/MC/MCSectionELF.cpp index 7a155561826..27694cb1441 100644 --- a/lib/MC/MCSectionELF.cpp +++ b/lib/MC/MCSectionELF.cpp @@ -156,6 +156,8 @@ void MCSectionELF::PrintSwitchToSection(const MCAsmInfo &MAI, const Triple &T, OS << "llvm_dependent_libraries"; else if (Type == ELF::SHT_LLVM_SYMPART) OS << "llvm_sympart"; + else if (Type == ELF::SHT_LLVM_BB_ADDR_MAP) + OS << "llvm_bb_addr_map"; else report_fatal_error("unsupported type 0x" + Twine::utohexstr(Type) + " for section " + getName()); diff --git a/lib/Object/ELF.cpp b/lib/Object/ELF.cpp index 5290f8ce056..116856a4260 100644 --- a/lib/Object/ELF.cpp +++ b/lib/Object/ELF.cpp @@ -276,6 +276,7 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART); STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR); STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR); + STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH); STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef); diff --git a/test/CodeGen/X86/basic-block-sections-labels-functions-sections.ll b/test/CodeGen/X86/basic-block-sections-labels-functions-sections.ll index 1142a8a1ec1..d2a2a7bfd52 100644 --- a/test/CodeGen/X86/basic-block-sections-labels-functions-sections.ll +++ b/test/CodeGen/X86/basic-block-sections-labels-functions-sections.ll @@ -5,11 +5,11 @@ $_Z4fooTIiET_v = comdat any define dso_local i32 @_Z3barv() { ret i32 0 } -;; Check we add SHF_LINK_ORDER for .bb_addr_map and link it with the corresponding .text sections. +;; Check we add SHF_LINK_ORDER for .llvm_bb_addr_map and link it with the corresponding .text sections. ; CHECK: .section .text._Z3barv,"ax",@progbits ; CHECK-LABEL: _Z3barv: ; CHECK-NEXT: [[BAR_BEGIN:.Lfunc_begin[0-9]+]]: -; CHECK: .section .bb_addr_map,"o",@progbits,.text._Z3barv{{$}} +; CHECK: .section .llvm_bb_addr_map,"o",@llvm_bb_addr_map,.text._Z3barv{{$}} ; CHECK-NEXT: .quad [[BAR_BEGIN]] @@ -20,16 +20,16 @@ define dso_local i32 @_Z3foov() { ; CHECK: .section .text._Z3foov,"ax",@progbits ; CHECK-LABEL: _Z3foov: ; CHECK-NEXT: [[FOO_BEGIN:.Lfunc_begin[0-9]+]]: -; CHECK: .section .bb_addr_map,"o",@progbits,.text._Z3foov{{$}} +; CHECK: .section .llvm_bb_addr_map,"o",@llvm_bb_addr_map,.text._Z3foov{{$}} ; CHECK-NEXT: .quad [[FOO_BEGIN]] define linkonce_odr dso_local i32 @_Z4fooTIiET_v() comdat { ret i32 0 } -;; Check we add .bb_addr_map section to a COMDAT group with the corresponding .text section if such a COMDAT exists. +;; Check we add .llvm_bb_addr_map section to a COMDAT group with the corresponding .text section if such a COMDAT exists. ; CHECK: .section .text._Z4fooTIiET_v,"axG",@progbits,_Z4fooTIiET_v,comdat ; CHECK-LABEL: _Z4fooTIiET_v: ; CHECK-NEXT: [[FOOCOMDAT_BEGIN:.Lfunc_begin[0-9]+]]: -; CHECK: .section .bb_addr_map,"Go",@progbits,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v{{$}} +; CHECK: .section .llvm_bb_addr_map,"Go",@llvm_bb_addr_map,_Z4fooTIiET_v,comdat,.text._Z4fooTIiET_v{{$}} ; CHECK-NEXT: .quad [[FOOCOMDAT_BEGIN]] diff --git a/test/CodeGen/X86/basic-block-sections-labels.ll b/test/CodeGen/X86/basic-block-sections-labels.ll index 267132c92e9..026667bc32c 100644 --- a/test/CodeGen/X86/basic-block-sections-labels.ll +++ b/test/CodeGen/X86/basic-block-sections-labels.ll @@ -39,7 +39,7 @@ declare i32 @__gxx_personality_v0(...) ; CHECK-LABEL: .LBB_END0_3: ; CHECK-LABEL: .Lfunc_end0: -; CHECK: .section .bb_addr_map,"o",@progbits,.text +; CHECK: .section .llvm_bb_addr_map,"o",@llvm_bb_addr_map,.text ; CHECK-NEXT: .quad .Lfunc_begin0 ; CHECK-NEXT: .byte 4 ; CHECK-NEXT: .uleb128 .Lfunc_begin0-.Lfunc_begin0 diff --git a/test/MC/AsmParser/llvm_section_types.s b/test/MC/AsmParser/llvm_section_types.s new file mode 100644 index 00000000000..bbbb527f4ea --- /dev/null +++ b/test/MC/AsmParser/llvm_section_types.s @@ -0,0 +1,28 @@ +## Verify that LLVM-specific section types are correctly inferred from assembly input. +# RUN: llvm-mc -triple i386-pc-linux -filetype=obj -o %t %s +# RUN: llvm-readobj -S %t | FileCheck %s +.section .section1,"",@llvm_bb_addr_map +.byte 1 +.section .section2,"",@llvm_call_graph_profile +.byte 1 +.section .section3,"",@llvm_odrtab +.byte 1 +.section .section4,"",@llvm_linker_options +.byte 1 +.section .section5,"",@llvm_sympart +.byte 1 +.section .section6,"",@llvm_dependent_libraries +.byte 1 + +# CHECK: Name: .section1 +# CHECK-NEXT: Type: SHT_LLVM_BB_ADDR_MAP +# CHECK: Name: .section2 +# CHECK-NEXT: Type: SHT_LLVM_CALL_GRAPH_PROFILE +# CHECK: Name: .section3 +# CHECK-NEXT: Type: SHT_LLVM_ODRTAB +# CHECK: Name: .section4 +# CHECK-NEXT: Type: SHT_LLVM_LINKER_OPTIONS +# CHECK: Name: .section5 +# CHECK-NEXT: Type: SHT_LLVM_SYMPART +# CHECK: Name: .section6 +# CHECK-NEXT: Type: SHT_LLVM_DEPENDENT_LIBRARIES