Index: include/llvm/CodeGen/AsmPrinter.h =================================================================== --- include/llvm/CodeGen/AsmPrinter.h +++ include/llvm/CodeGen/AsmPrinter.h @@ -295,6 +295,8 @@ void emitFrameAlloc(const MachineInstr &MI); + void emitStackSizeSection(const MachineFunction &MF); + enum CFIMoveType { CFI_M_None, CFI_M_EH, CFI_M_Debug }; CFIMoveType needsCFIMoves() const; Index: include/llvm/CodeGen/CommandFlags.h =================================================================== --- include/llvm/CodeGen/CommandFlags.h +++ include/llvm/CodeGen/CommandFlags.h @@ -274,6 +274,10 @@ clEnumValN(DebuggerKind::SCE, "sce", "SCE targets (e.g. PS4)"))); +cl::opt EnableStackSizeSection( + "stack-size-section", + cl::desc("Emit a section containing stack size metadata"), cl::init(false)); + // Common utility function tightly tied to the options listed here. Initializes // a TargetOptions object with CodeGen flags and returns it. static inline TargetOptions InitTargetOptionsFromCodeGenFlags() { @@ -300,6 +304,7 @@ Options.UniqueSectionNames = UniqueSectionNames; Options.EmulatedTLS = EmulatedTLS; Options.ExceptionModel = ExceptionModel; + Options.EmitStackSizeSection = EnableStackSizeSection; Options.MCOptions = InitMCTargetOptionsFromFlags(); Index: include/llvm/MC/MCObjectFileInfo.h =================================================================== --- include/llvm/MC/MCObjectFileInfo.h +++ include/llvm/MC/MCObjectFileInfo.h @@ -154,6 +154,9 @@ /// It is initialized on demand so it can be overwritten (with uniquing). MCSection *EHFrameSection; + /// Section containing metadata on function stack sizes. + MCSection *StackSizesSection; + // ELF specific sections. MCSection *DataRelROSection; MCSection *MergeableConst4Section; @@ -287,6 +290,8 @@ MCSection *getStackMapSection() const { return StackMapSection; } MCSection *getFaultMapSection() const { return FaultMapSection; } + MCSection *getStackSizesSection() const { return StackSizesSection; } + // ELF specific sections. MCSection *getDataRelROSection() const { return DataRelROSection; } const MCSection *getMergeableConst4Section() const { Index: include/llvm/Target/TargetOptions.h =================================================================== --- include/llvm/Target/TargetOptions.h +++ include/llvm/Target/TargetOptions.h @@ -108,7 +108,7 @@ DisableIntegratedAS(false), RelaxELFRelocations(false), FunctionSections(false), DataSections(false), UniqueSectionNames(true), TrapUnreachable(false), EmulatedTLS(false), - EnableIPRA(false) {} + EnableIPRA(false), EmitStackSizeSection(false) {} /// PrintMachineCode - This flag is enabled when the -print-machineinstrs /// option is specified on the command line, and should enable debugging @@ -216,6 +216,9 @@ /// This flag enables InterProcedural Register Allocation (IPRA). unsigned EnableIPRA : 1; + /// Emit section containing metadata on function stack sizes. + unsigned EmitStackSizeSection : 1; + /// FloatABIType - This setting is set by -float-abi=xxx option is specfied /// on the command line. This setting may either be Default, Soft, or Hard. /// Default selects the target's default behavior. Soft selects the ABI for Index: lib/CodeGen/AsmPrinter/AsmPrinter.cpp =================================================================== --- lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -964,6 +964,26 @@ MCConstantExpr::create(FrameOffset, OutContext)); } +void AsmPrinter::emitStackSizeSection(const MachineFunction &MF) { + if (!MF.getTarget().Options.EmitStackSizeSection) + return; + + MCSection *StackSizeSection = getObjFileLowering().getStackSizesSection(); + if (!StackSizeSection) + return; + + OutStreamer->PushSection(); + OutStreamer->SwitchSection(StackSizeSection); + + const MCSymbol *FunctionSymbol = getSymbol(MF.getFunction()); + uint64_t StackSize = MF.getFrameInfo().getStackSize(); + OutStreamer->EmitValue(MCSymbolRefExpr::create(FunctionSymbol, OutContext), + /* size = */ 8); + OutStreamer->EmitULEB128IntValue(StackSize); + + OutStreamer->PopSection(); +} + static bool needFuncLabelsForEHOrDebugInfo(const MachineFunction &MF, MachineModuleInfo *MMI) { if (!MF.getLandingPads().empty() || MF.hasEHFunclets() || MMI->hasDebugInfo()) @@ -1135,6 +1155,9 @@ HI.Handler->endFunction(MF); } + // Emit section containing stack size metadata. + emitStackSizeSection(*MF); + if (isVerbose()) OutStreamer->GetCommentOS() << "-- End function\n"; Index: lib/MC/MCObjectFileInfo.cpp =================================================================== --- lib/MC/MCObjectFileInfo.cpp +++ lib/MC/MCObjectFileInfo.cpp @@ -594,6 +594,8 @@ EHFrameSection = Ctx->getELFSection(".eh_frame", EHSectionType, EHSectionFlags); + + StackSizesSection = Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, 0); } void MCObjectFileInfo::initCOFFMCObjectFileInfo(const Triple &T) { Index: test/CodeGen/X86/stack-size-section.ll =================================================================== --- /dev/null +++ test/CodeGen/X86/stack-size-section.ll @@ -0,0 +1,26 @@ +; RUN: llc < %s -mtriple=x86_64-linux -stack-size-section | FileCheck %s + +; CHECK-LABEL: func1: +; CHECK: .section .stack_sizes,"",@progbits +; CHECK-NEXT: .quad func1 +; CHECK-NEXT: .byte 8 + +; CHECK-LABEL: func2: +; CHECK: .section .stack_sizes,"",@progbits +; CHECK-NEXT: .quad func2 +; CHECK-NEXT: .byte 24 + +define void @func1(i32, i32) #0 { + alloca i32, align 4 + alloca i32, align 4 + ret void +} + +define void @func2() #1 { + alloca i32, align 4 + call void @func1(i32 1, i32 2) + ret void +} + +attributes #0 = { "no-frame-pointer-elim"="true" } +attributes #1 = { "no-frame-pointer-elim"="true" }