I am working on the assembly printer for RISC-V, more specifically on the AsmPrinter class.
I altered the RISCV Backend to print C code instead of Assembly, interpreted by libraries... (but that's not important)
My problem is that, for my application to work, I need to treat my functions in the order they are in the original C file.
I discovered that these functions are not treated in this order.
My question is : Is there a way to force llvm to treat the functions in the order given in the original C file ? and how?
I treat each function in the EmitFunctionBody funtion of the AsmPrinter class, and the function is passed through the MachineFunction passed trhough the runOnMachineFunction function. I didn't find where this last function is called.
So I wish maybe to alter the way this function is called for each Function to call it in the "right" order...
The LLVM backend should generally print functions in the same order they appear in the IR. This is an implementation detail, not a guarantee, but there currently aren't any backend passes that rearrange functions.
That said, clang doesn't really try to emit functions in source order: it will delay emitting functions for various reasons. If that's truly necessary for your use-case, you'll probably have to modify clang somehow. Or maybe -femit-all-decls is close enough if your input is pure C?
From: llvm-dev [mailto:llvm-dev-bounces@lists.llvm.org] On Behalf Of
Friedman, Eli via llvm-dev
Sent: Tuesday, August 07, 2018 1:58 PM
To: 'Irenee GROZ IG255340'; llvm-dev@lists.llvm.org
Subject: Re: [llvm-dev] Risc-v Assembly printer function order
> Hello,
>
> I am working on the assembly printer for RISC-V, more specifically on
> the AsmPrinter class.
> I altered the RISCV Backend to print C code instead of Assembly,
> interpreted by libraries... (but that's not important)
> My problem is that, for my application to work, I need to treat my
> functions in the order they are in the original C file.
> I discovered that these functions are not treated in this order.
>
> My question is : Is there a way to force llvm to treat the functions
> in the order given in the original C file ? and how?
The LLVM backend should generally print functions in the same order they
appear in the IR. This is an implementation detail, not a guarantee,
but there currently aren't any backend passes that rearrange functions.
You could probably write a Module pass that sorted the Functions by source
location; that's the only solution that comes to mind. You'd need to
define the ordering across files (e.g., the source location of a function
defined in a header; this happens a lot in C++, not so much in C).
--paulr