Getting stack size of compiled functions from LLVM

Hi all,
I'm trying to get the amount of stack memory used by the functions I am JIT compiling with LLVM.
I have a host C++ program, and I want to be able to access the stack size from the host C++ program.

I see in PrologEpilogInserter.cpp that the computed stack size is read from the MachineFunction corresponding to a Function, in order to issue a warning if the stack size is too large.
However I don't see a way to access this information from my host C++ program.

One approach I have investigated is writing a MachineFunctionPass that could access the MachineFunctions and hence the stack size. However I have not been able to get my MachineFunctionPass to run
without hitting assert failures.

Is it possible to add/schedule a MachineFunctionPass to run without modifying the LLVM libraries themselves? I would prefer not to have to modify LLVM.
If it is the case that the code to schedule the MachineFunctionPass must be added to LLVM, then it's not clear to me how to return the stack size information to the host C++ program either.

Any help would be appreciated.
Thanks,
Nick C.

Hi all,
I'm trying to get the amount of stack memory used by the functions I am JIT compiling with LLVM.
I have a host C++ program, and I want to be able to access the stack size from the host C++ program.

I see in PrologEpilogInserter.cpp that the computed stack size is read from the MachineFunction corresponding to a Function, in order to issue a warning if the stack size is too large.
However I don't see a way to access this information from my host C++ program.

You can retrieve those warnings from your host program; if you call LLVMContext::setDiagnosticHandler, the DiagnosticInfoStackSize gets passed to your code instead of getting dumped to stderr.

One approach I have investigated is writing a MachineFunctionPass that could access the MachineFunctions and hence the stack size. However I have not been able to get my MachineFunctionPass to run
without hitting assert failures.

Is it possible to add/schedule a MachineFunctionPass to run without modifying the LLVM libraries themselves? I would prefer not to have to modify LLVM.
If it is the case that the code to schedule the MachineFunctionPass must be added to LLVM, then it's not clear to me how to return the stack size information to the host C++ program either.

There currently aren't any hooks for this, but it probably wouldn't be hard to implement, if you're interested in contributing upstream. (See PassManagerBuilder::addExtension for IR optimization passes.)

-Eli

If you don't want to modify LLVM, you can consider inserting a "dummy"
patchpoint or statepoint call in your function. Then the JIT
generated in-memory object file will contain a __llvm_stackmaps
section that will contain the stack sizes of all the functions in the
module that have a compile-time constant sized stack. You could also
look into adding a flag that would ask LLVM to always produce this
__llvm_stackmaps section, even if the module did not have any
statepoint or patchpoint calls.

See https://llvm.org/docs/StackMaps.html

-- Sanjoy