[llvm] [clang] [AArch64] Stack probing for function prologues (PR #66524)

Momchil Velikov via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 21 16:41:14 PST 2023


================
@@ -26262,3 +26262,37 @@ bool AArch64TargetLowering::preferScalarizeSplat(SDNode *N) const {
   }
   return true;
 }
+
+bool AArch64TargetLowering::hasInlineStackProbe(
+    const MachineFunction &MF) const {
+  // If the function specifically requests inline stack probes, emit them.
+  if (MF.getFunction().hasFnAttribute("probe-stack")) {
+    if (MF.getFunction().getFnAttribute("probe-stack").getValueAsString() ==
+        "inline-asm")
+      return true;
+    else
+      llvm_unreachable("Unsupported stack probing method");
+  }
+
+  return false;
+}
+
+unsigned
+AArch64TargetLowering::getStackProbeSize(const MachineFunction &MF) const {
+  const TargetFrameLowering *TFI = Subtarget->getFrameLowering();
+  unsigned StackAlign = TFI->getStackAlignment();
+  assert(StackAlign >= 1 && isPowerOf2_32(StackAlign) &&
+         "Unexpected stack alignment");
+  // The default stack probe size is 4096 if the function has no
+  // stack-probe-size attribute. This is a safe default because it is the
+  // smallest possible guard page size.
+  unsigned StackProbeSize = 4096;
+  const Function &Fn = MF.getFunction();
+  if (Fn.hasFnAttribute("stack-probe-size"))
----------------
momchil-velikov wrote:

The rounding to the stack alignment size is enough. Rounding down is the safer choice, zero is handled, there's no requirement other than be a multiple of stack alignment. Some choices might not be appropriate for certain platforms, e.g. 
5k or 8k probe size with 4k guard page size, but that's not something that can be validated here. Values, greater than 64k might also be legit, e.g. a specific platform allocates 2 guard pages (128k) at the top of the stack to limit probing overhead.

https://github.com/llvm/llvm-project/pull/66524


More information about the cfe-commits mailing list