diff --git a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h --- a/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h +++ b/llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h @@ -166,6 +166,11 @@ MCSection *getSectionForJumpTable(const Function &F, const TargetMachine &TM) const override; + MCSection * + getSectionForMachineBasicBlock(const Function &F, + const MachineBasicBlock &MBB, + const TargetMachine &TM) const override; + /// Emit Obj-C garbage collection and linker options. void emitModuleMetadata(MCStreamer &Streamer, Module &M) const override; diff --git a/llvm/lib/CodeGen/BasicBlockSections.cpp b/llvm/lib/CodeGen/BasicBlockSections.cpp --- a/llvm/lib/CodeGen/BasicBlockSections.cpp +++ b/llvm/lib/CodeGen/BasicBlockSections.cpp @@ -81,12 +81,12 @@ // Placing the cold clusters in a separate section mitigates against poor // profiles and allows optimizations such as hugepage mapping to be applied at a -// section granularity. Defaults to ".text.split." which is recognized by lld -// via the `-z keep-text-section-prefix` flag. +// section granularity. Defaults to "split" which is recognized by lld via the +// `-z keep-text-section-prefix` flag. cl::opt llvm::BBSectionsColdTextPrefix( "bbsections-cold-text-prefix", cl::desc("The text prefix to use for cold basic block clusters"), - cl::init(".text.split."), cl::Hidden); + cl::init("split"), cl::Hidden); namespace { diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp --- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp +++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp @@ -910,7 +910,9 @@ // name, or a unique ID for the section. SmallString<128> Name; if (MBB.getSectionID() == MBBSectionID::ColdSectionID) { + Name += ".text."; Name += BBSectionsColdTextPrefix; + Name += "."; Name += MBB.getParent()->getName(); } else if (MBB.getSectionID() == MBBSectionID::ExceptionSectionID) { Name += ".text.eh."; @@ -1644,6 +1646,25 @@ COFF::IMAGE_COMDAT_SELECT_ASSOCIATIVE, UniqueID); } +/// Returns a unique section for the given machine basic block. +MCSection *TargetLoweringObjectFileCOFF::getSectionForMachineBasicBlock( + const Function &F, const MachineBasicBlock &MBB, + const TargetMachine &TM) const { + assert(MBB.isBeginSection() && "Basic block does not start a section!"); + assert(MBB.getSectionID() == MBBSectionID::ColdSectionID); + SmallString<128> Name, GroupName; + Name += ".text$"; + Name += BBSectionsColdTextPrefix; + GroupName += MBB.getParent()->getName(); + GroupName += BBSectionsColdTextPrefix; + return getContext().getCOFFSection(Name, + COFF::IMAGE_SCN_CNT_CODE | + COFF::IMAGE_SCN_MEM_EXECUTE | + COFF::IMAGE_SCN_MEM_READ, + SectionKind::getText(), GroupName, + COFF::IMAGE_COMDAT_SELECT_NODUPLICATES); +} + void TargetLoweringObjectFileCOFF::emitModuleMetadata(MCStreamer &Streamer, Module &M) const { emitLinkerDirectives(Streamer, M); diff --git a/llvm/test/CodeGen/X86/basic-block-sections-cold.ll b/llvm/test/CodeGen/X86/basic-block-sections-cold.ll --- a/llvm/test/CodeGen/X86/basic-block-sections-cold.ll +++ b/llvm/test/CodeGen/X86/basic-block-sections-cold.ll @@ -3,7 +3,7 @@ ; RUN: echo '!_Z3bazb' > %t ; RUN: echo '!!0' >> %t ; RUN: llc < %s -mtriple=x86_64 -function-sections -basic-block-sections=%t -unique-basic-block-section-names | FileCheck %s -check-prefix=LINUX-SECTIONS -; RUN: llc < %s -mtriple=x86_64 -function-sections -basic-block-sections=%t -unique-basic-block-section-names -bbsections-cold-text-prefix=".text.unlikely." | FileCheck %s -check-prefix=LINUX-SPLIT +; RUN: llc < %s -mtriple=x86_64 -function-sections -basic-block-sections=%t -unique-basic-block-section-names -bbsections-cold-text-prefix="unlikely" | FileCheck %s -check-prefix=LINUX-SPLIT define void @_Z3bazb(i1 zeroext %0) nounwind { br i1 %0, label %2, label %4 diff --git a/llvm/test/CodeGen/X86/machine-function-splitter.ll b/llvm/test/CodeGen/X86/machine-function-splitter.ll --- a/llvm/test/CodeGen/X86/machine-function-splitter.ll +++ b/llvm/test/CodeGen/X86/machine-function-splitter.ll @@ -1,11 +1,15 @@ ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -split-machine-functions | FileCheck %s -check-prefix=MFS-DEFAULTS +; RUN: llc < %s -mtriple=x86_64-windows-msvc -split-machine-functions | FileCheck %s -check-prefix=MFS-DEFAULTS-MSVC ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -split-machine-functions -mfs-psi-cutoff=0 -mfs-count-threshold=2000 | FileCheck %s --dump-input=always -check-prefix=MFS-OPTS1 +; RUN: llc < %s -mtriple=x86_64-windows-msvc -split-machine-functions -mfs-psi-cutoff=0 -mfs-count-threshold=2000 | FileCheck %s --dump-input=always -check-prefix=MFS-OPTS1-MSVC ; RUN: llc < %s -mtriple=x86_64-unknown-linux-gnu -split-machine-functions -mfs-psi-cutoff=950000 | FileCheck %s -check-prefix=MFS-OPTS2 +; RUN: llc < %s -mtriple=x86_64-windows-msvc -split-machine-functions -mfs-psi-cutoff=950000 | FileCheck %s -check-prefix=MFS-OPTS2-MSVC define void @foo1(i1 zeroext %0) nounwind !prof !14 !section_prefix !15 { -;; Check that cold block is moved to .text.split. +;; Check that cold block is moved to .text.split for ELF or .text$split for COFF ; MFS-DEFAULTS-LABEL: foo1 ; MFS-DEFAULTS: .section .text.split.foo1 +; MFS-DEFAULTS-MSVC: .section .text$split ; MFS-DEFAULTS-NEXT: foo1.cold: ; MFS-DEFAULTS-NOT: callq bar ; MFS-DEFAULTS-NEXT: callq baz @@ -66,6 +70,7 @@ ;; Check that count threshold works. ; MFS-OPTS1-LABEL: foo4 ; MFS-OPTS1: .section .text.split.foo4 +; MFS-OPTS1-MSVC: .section .text$split ; MFS-OPTS1-NEXT: foo4.cold: ; MFS-OPTS1-NOT: callq bar ; MFS-OPTS1-NOT: callq baz @@ -100,6 +105,7 @@ ;; Check that profile summary info cutoff works. ; MFS-OPTS2-LABEL: foo5 ; MFS-OPTS2: .section .text.split.foo5 +; MFS-OPTS2-MSVC: .section .text$split ; MFS-OPTS2-NEXT: foo5.cold: ; MFS-OPTS2-NOT: callq bar ; MFS-OPTS2-NOT: callq baz @@ -153,6 +159,7 @@ ;; Check that cold ehpads are not split out. ; MFS-DEFAULTS-LABEL: foo7 ; MFS-DEFAULTS: .section .text.split.foo7,"ax",@progbits +; MFS-DEFAULTS-MSVC: .section .text$split,"xr" ; MFS-DEFAULTS-NEXT: foo7.cold: ; MFS-DEFAULTS-NOT: callq _Unwind_Resume ; MFS-DEFAULTS: callq baz