Reg: Adding Intrinsic at start of every function

Hello Everyone
I have create a new intrinsic in RISCV and I am trying to insert it at the start of every function based on Command-Line argument which determines the list of functions to omit. I expect this intrinsic to be inserted at the start of function as first instruction. Is this possible with a normal Module pass using opt command? Or, should I look into MachineFunction pass?
Please guide me in this.

Thanking you


I’m probably not the best person to answer this, but I’ll say something in case no one else does b/c something is better than nothing. I noticed you re-asked your question, so I’m replying on the older one in case someone more qualified sees the more recent one.

I’m going to assume by “intrinsic” you mean an llvm intrinsic function. If you’re asking your question here, it sounds like you probably already tried inserting your intrinsic function as the first IR instruction, looked at the assembly and noticed that your instruction/instruction sequence isn’t the first one in the function. It’s possible that there’s a way to do it in IR, but if there is, idk how to do it.

If I were to do it, I’d use a machine function pass like you suggest and instead of an intrinsic create and insert the instruction as part of the last pass in the codegen process (end of preEmit2 in x86). That way you have total control where the instruction goes and know nothing in the codegen process is going to move it.

MachineFunction passes sound scary, but here’s a pretty pain-free link for how to write your first one: It’s for x86, but I’m sure you could do the same thing for RISC-V. There’s one typo: " In the pass’s .cpp file “createX86MachineInstrPrinterPass” is defined, and in the other files “createX86MachineInstrPrinter” is used." They just need to be the same.

All the best.