[PATCH] D98689: [X86] [split-stack] Add an option to allocate extra space for stack split functions

Chuanqi Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 16 02:29:35 PDT 2021


ChuanqiXu created this revision.
ChuanqiXu added reviewers: cherry, thanm, wmi, MaskRay, echristo.
Herald added subscribers: pengfei, hiraditya.
ChuanqiXu requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

As the title describes, this patch add an option to allocate extra space for stack split functions. And if user specify extra spaces, then we can loose the constraint about the varargs of split-stack functions.

This idea comes from a patch from libgcc: patch <https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565096.html>. In this patch, the author tries to allocate extra space for each stack split functions to avoid frequent allocations. Then in the following reply: reply <https://gcc.gnu.org/pipermail/gcc-patches/2021-February/565123.html>, the reviewer suggests it could be a good idea to make it as compiler option. So I think it is a good idea to add it in clang.

Test-Plan: check-all


https://reviews.llvm.org/D98689

Files:
  llvm/lib/Target/X86/X86FrameLowering.cpp
  llvm/test/CodeGen/X86/split-stack-extra-space.ll


Index: llvm/test/CodeGen/X86/split-stack-extra-space.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/split-stack-extra-space.ll
@@ -0,0 +1,42 @@
+; RUN: llc -extra-space-for-more-stack-expansion=100 < %s | FileCheck %s
+source_filename = "/disk2/workspace.xuchuanqi/alibaba-llvm-11.x-for-work/clang/test/CodeGen/split-stacks.c"
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: movabsq	$108, %r10
+; CHECK: movabsq	$0, %r11
+; CHECK: callq	__morestack
+
+; CHECK: movabsq	$124, %r10
+; CHECK: movabsq	$0, %r11
+; CHECK: callq	__morestack
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @foo() #0 {
+entry:
+  ret i32 0
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @nosplit() #1 {
+entry:
+  ret i32 0
+}
+
+; Function Attrs: noinline nounwind optnone uwtable
+define dso_local i32 @main() #0 {
+entry:
+  %retval = alloca i32, align 4
+  store i32 0, i32* %retval, align 4
+  %call = call i32 @foo()
+  ret i32 %call
+}
+
+attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "split-stack" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{!"clang version 11.1.0 (git at gitlab.alibaba-inc.com:clang-llvm/alibaba-llvm-project.git 74aa28fe18b93f7a68bd02d039a60e858d016d7e)"}
\ No newline at end of file
Index: llvm/lib/Target/X86/X86FrameLowering.cpp
===================================================================
--- llvm/lib/Target/X86/X86FrameLowering.cpp
+++ llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -40,6 +40,11 @@
 STATISTIC(NumFrameExtraProbe,
           "Number of extra stack probes generated in prologue");
 
+static llvm::cl::opt<unsigned> ExtraSpaceForMoreStackExpansion(
+    "extra-space-for-more-stack-expansion", cl::Hidden,
+    llvm::cl::desc("allocate extra space for morestack expansion"),
+    llvm::cl::init(0));
+
 using namespace llvm;
 
 X86FrameLowering::X86FrameLowering(const X86Subtarget &STI,
@@ -2647,8 +2652,6 @@
   assert(!MF.getRegInfo().isLiveIn(ScratchReg) &&
          "Scratch register is live-in");
 
-  if (MF.getFunction().isVarArg())
-    report_fatal_error("Segmented stacks do not support vararg functions.");
   if (!STI.isTargetLinux() && !STI.isTargetDarwin() && !STI.isTargetWin32() &&
       !STI.isTargetWin64() && !STI.isTargetFreeBSD() &&
       !STI.isTargetDragonFly())
@@ -2659,6 +2662,12 @@
   // prologue.
   StackSize = MFI.getStackSize();
 
+  if (ExtraSpaceForMoreStackExpansion.getNumOccurrences())
+    StackSize += ExtraSpaceForMoreStackExpansion;
+  else if (MF.getFunction().isVarArg())
+    report_fatal_error("Segmented stacks do not support vararg functions if "
+                       "user don't specify extra spaces for split stacks.");
+
   // Do not generate a prologue for leaf functions with a stack of size zero.
   // For non-leaf functions we have to allow for the possibility that the
   // callis to a non-split function, as in PR37807. This function could also


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98689.330906.patch
Type: text/x-patch
Size: 4082 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210316/357dc2c0/attachment.bin>


More information about the llvm-commits mailing list