Skip to content

Add support for -fstack-usage#624

Open
ddunl wants to merge 1 commit intobazelbuild:mainfrom
ddunl:main
Open

Add support for -fstack-usage#624
ddunl wants to merge 1 commit intobazelbuild:mainfrom
ddunl:main

Conversation

@ddunl
Copy link

@ddunl ddunl commented Mar 8, 2026

This PR adds support for -fstack-usage by adding a generate_stack_usage feature. I roughly tried to follow the generate_linkmap feature as a template.

I left Windows support out because it seems like MSVC doesn't support this and I have no Windows machine to test on, but I can look into this if desired.

If this PR is roughly acceptable I would also like to contribute a similar PR for GCC's -fcallgraph-info. The motivation is for an embedded target I could compute necessary stack usage at build time and give each processor on my target exactly the amount of stack it needs to not waste any IRAM space which I only have tens of kilobytes of.

I'm not sure if this is the correct approach, or if something that solves this problem in a more general way (like a fix for #594) is preferred. Apologies if this is naive in some way, this is at (or past!) the limits of my bazel knowledge 😃

Example usage:

david@Davids-Mac-mini stack_usage_test % cat BUILD *.cc
# BUILD
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")

cc_library(
    name = "math",
    srcs = ["math.cc"],
    hdrs = ["math.h"],
    features = ["generate_stack_usage"],
)

cc_binary(
    name = "main",
    srcs = ["main.cc"],
    deps = [":math"],
    features = ["generate_stack_usage"],
)
// main.cc
#include <cstdio>
#include "math.h"

int main() {
    int x = add(3, 4);
    int y = multiply(x, 5);
    printf("%d\n", y);
    return 0;
}
// math.cc
#include "math.h"

int add(int a, int b) {
    int result = a + b;
    return result;
}

int multiply(int a, int b) {
    int result = a * b;
    return result;
}
david@Davids-Mac-mini stack_usage_test % bazelisk build //...
Starting local Bazel server (9.0.0) and connecting to it...
INFO: Analyzed 2 targets (86 packages loaded, 475 targets configured).
INFO: Found 2 targets...
INFO: Elapsed time: 3.237s, Critical Path: 0.30s
INFO: 9 processes: 5 action cache hit, 5 internal, 4 darwin-sandbox.
INFO: Build completed successfully, 9 total actions
david@Davids-Mac-mini stack_usage_test % cat bazel-bin/_objs/main/main.su bazel-bin/_objs/math/math.su
main.cc:5:main  48      static
math.cc:4:_Z3addii      16      static
math.cc:9:_Z8multiplyii 16      static

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant