How can I get the size of function

Now in my project I need to do certain operations on functions with size less than 8 bytes, so I need to know the size of the function, but the llvm::Function class doesn’t seem to provide such an interface.
I tried to use llvm::Function::size() but all results are zero.
Can anyone tell me what to do?

1 Like

Since you want a size in bytes, presumably you mean you want the size of the final codegen’d function, which isn’t known until, well, after final codegen. llvm::Function operates on IR and doesn’t know what that size would be.

how about MachineFunction? through InstructionCount. LLVM::MachineFunction has a method called getMachineFunction().

And, since the linker can change the size of the function (by converting 32-bit constants into 16-bit constants,…) you often don’t know this until after linking.

However, many LLVM compilers leave some syntactic sugar after the function::

.size	grid_2d, .Lfunc_end0-grid_2d
                                    ; -- End function"

You have a way to get ahold of this number should you need it–just not at compile time.

Why have a size function at all then?

llvm::Function is about IR, so its size() function likely tells you something about the IR attached to the function.

Specifically, it tells you how many basic blocks are in the function, since it behaves like a list of basic blocks (like how std::vector<T>::size() tells you how many elements it has). Similarly a basic block’s size tells you how many IR instructions it has, since a basic block is a list of instructions.

1 Like