[clang] [llvm] Fixed stack size estimates in the function for the -fstack-usage option (PR #173181)
Aleksandr Ryabikov via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 5 23:56:37 PST 2026
https://github.com/rsashka updated https://github.com/llvm/llvm-project/pull/173181
>From 307629b7aa506746d4f0bcc5ac2513d7038c7abc Mon Sep 17 00:00:00 2001
From: Aleksandr Ryabikov <rsashka at mail.ru>
Date: Sun, 21 Dec 2025 11:56:03 +0300
Subject: [PATCH] The return address size has been added to the function stack
size on the x86 platform, and a check that takes into account compiler
optimization settings has been added to the -fstack_usage compiler option
tests.
---
clang/test/CodeGen/stack-usage.c | 21 +++++++++++++++++----
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 9 +++++++--
2 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/clang/test/CodeGen/stack-usage.c b/clang/test/CodeGen/stack-usage.c
index c9f495605fefd..e1778b0f62d5f 100644
--- a/clang/test/CodeGen/stack-usage.c
+++ b/clang/test/CodeGen/stack-usage.c
@@ -1,11 +1,24 @@
// REQUIRES: aarch64-registered-target
+// REQUIRES: x86-registered-target
// RUN: rm -rf %t && split-file %s %t && cd %t
-// RUN: %clang_cc1 -triple aarch64-unknown -I . -stack-usage-file a.su -emit-obj a.c -o a.o
-// RUN: FileCheck %s < a.su
+// RUN: %clang_cc1 -triple aarch64-unknown -O0 -I . -stack-usage-file a-O0.su -emit-obj a.c -o a-O0.o
+// RUN: FileCheck %s < a-O0.su -check-prefix=O0
+// RUN: %clang_cc1 -triple aarch64-unknown -O3 -I . -stack-usage-file a-O3.su -emit-obj a.c -o a-O3.o
+// RUN: FileCheck %s < a-O3.su -check-prefix=O3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -O3 -I . -stack-usage-file a-x86.su -emit-obj a.c -o a-x86.o
+// RUN: FileCheck %s < a-x86.su -check-prefix=x86
+
+// O0: {{.*}}x.inc:1:bar [[#]] dynamic
+// O0: a.c:2:foo [[#]] static
+
+// O3: {{.*}}x.inc:1:bar [[#]] static
+// O3: a.c:2:foo [[#]] static
+
+// For x86 stack usage must be greater than zero
+// x86: {{.*}}x.inc:1:bar {{[1-9][0-9]*}} static
+// x86: a.c:2:foo {{[1-9][0-9]*}} static
-// CHECK: {{.*}}x.inc:1:bar [[#]] dynamic
-// CHECK: a.c:2:foo [[#]] static
//--- a.c
#include "x.inc"
int foo() {
diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
index 4d2992456f6f8..1ded0211e465c 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -1679,9 +1679,14 @@ void AsmPrinter::emitStackUsage(const MachineFunction &MF) {
return;
const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
- uint64_t StackSize =
+ uint64_t StackSize =
FrameInfo.getStackSize() + FrameInfo.getUnsafeStackSize();
-
+
+ // Increase the stack size for the return address on x86
+ if(MF.getTarget().getTargetTriple().isX86()){
+ StackSize += MF.getTarget().getAllocaPointerSize();
+ }
+
if (StackUsageStream == nullptr) {
std::error_code EC;
StackUsageStream =
More information about the llvm-commits
mailing list