[clang] [llvm] [RISCV] Add stack clash protection (PR #117612)

Craig Topper via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 25 15:52:51 PST 2024


================
@@ -22156,3 +22156,25 @@ namespace llvm::RISCVVIntrinsicsTable {
 #include "RISCVGenSearchableTables.inc"
 
 } // namespace llvm::RISCVVIntrinsicsTable
+
+bool RISCVTargetLowering::hasInlineStackProbe(const MachineFunction &MF) const {
+
+  // If the function specifically requests inline stack probes, emit them.
+  if (MF.getFunction().hasFnAttribute("probe-stack"))
+    return MF.getFunction().getFnAttribute("probe-stack").getValueAsString() ==
+           "inline-asm";
+
+  return false;
+}
+
+unsigned RISCVTargetLowering::getStackProbeSize(const MachineFunction &MF,
+                                                unsigned StackAlign) const {
+  // The default stack probe size is 4096 if the function has no
+  // stack-probe-size attribute.
+  const Function &Fn = MF.getFunction();
+  unsigned StackProbeSize =
+      Fn.getFnAttributeAsParsedInteger("stack-probe-size", 4096);
+  // Round down to the stack alignment.
+  StackProbeSize &= ~(StackAlign - 1);
----------------
topperc wrote:

Use `alignDown` from MathExtras.h?

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


More information about the cfe-commits mailing list