Can a normal compiler include a MachineFunctionPass?

Hi all, first I wanna say thank you for everything you all do! LLVM has made possible my dream of creating my own programming language, and you all are a huge part of that.

My ultimate goal is to be able to measure the stack space needed for a function and all of its callees (excluding recursive calls and indirect function pointer calls), to know how much space I need to allocate for a virtual thread, as part of Vale’s concurrency plans.

This 2013 email thread says:

If you want something that includes stack-spill slots and the like, then you’d need to write a MachineFunctionPass and examine the generated machine instructions. Alternatively, there might be a way in a MachineFunctionPass to get a pointer to a MachineFrame object and to query its size.

However, this 2015 email thread says:

A MachineFunctionPass is run by the code generator, so you can only use
it in tools like llc, clang, and libLTO.

Does this mean that I can’t make a MachineFunctionPass and invoke it via a PassManager inside my compiler, after the optimization passes?

I was able to get a regular FunctionPass to run from inside my compiler this way (well, can’t anymore, some “typeinfo for llvm::FunctionPass” undefined symbols seem to be appearing now) but MachineFunctionPass never seemed to work.

Are we really not able to make a MachineFunctionPass inside a compiler?

Thank you so much!

There is currently no extension mechanism to add a MachineFunctionPass to existing target without modifying the LLVM source code. If you think there’s some reason to add such a mechanism, proposals welcome. (The relevant code in LLVM is TargetPassConfig::addMachinePasses.)

Thank you! I’ll switch my efforts towards modifying the LLVM source code. You’ve saved me much wandering and struggle, thank you so much!

Note for anyone else with the same ultimate goal as I had: there’s actually an llc flag named -stack-size-section which will emit the stack sizes for every function. No MachineFunctionPass needed!