[llvm-dev] How to add assembly instructions in CodeGen

Dean Michael Berris via llvm-dev llvm-dev at lists.llvm.org
Sun May 6 22:20:26 PDT 2018


On Sun, May 6, 2018 at 7:26 AM Soham Sinha via llvm-dev <
llvm-dev at lists.llvm.org> wrote:

> Hello,

> I want to add assembly instructions at certain points in a function. This
is X86 specific. So I am working in the lib/Target/X86 folder. I create a
`MachineFunctionPass` in that folder. I register it in the
X86TargetMachine.cpp in addPreEmitPass(). I use BuildMI to insert my own
assembly instructions in the MachineFunctionPass. This works and my
assembly instructions are inserted at desired places. However, this breaks
the alignment. So when I run the generated code, I get segmentation fault
(precisely in printf with XMM registers). Where should I add my pass?


It sounds like you're running into stack alignment issues. If you're adding
data to the stack, you may need to work a little harder with maintaining
the state of the stack. This is not trivial to do especially if you're
emitting the assembly by the time you're at a MachineFunctionPass (because
register spilling and/or stack alignment information would have already
been done by the time you're in machine instruction lowering). What you may
need to do here is to either:

- hook into the preamble and stack re-alignment code specifically in X86
that would look at information from your pass. This is not trivial and I
don't recommend going down this path (I tried, but I lost the patience to
do it properly).

- when emitting the assembly instructions that involve pushing/popping from
the stack, that you're keeping track of the alignment of the stack
variables. This is what we do with XRay, when we're lowering the custom
event sleds.

- use pseudo-instructions and preserving those until lowering, where the
lowering

> My pass depends on the MachineBasicBlock information as well. Therefore,
I cannot add my pass too early in LLVM IR. What is the proper pass to add
my custom MachineFunctionPass? I tried addPreRegAlloc, but it failed due to
insufficient register allocation error or something on that line.

> Can anybody please help me write a MachineFunctionPass where I can insert
assembly instruction without breaking the alignment? I am doing this for
X86_64.


You can look at the XRay lowering for the PATCHABLE_EVENT_CALL lowering in
X86AsmPrinter as a guide for the lowering, but you might also want to see
how we're inserting these pseudo-instructions from the

I don't remember having to specify where the pass is defined, since it's
already in the assembly printing. So you might consider inserting these
pseudo-instructions a the MachineFunctionPass, which gets lowered
appropriately in the assembly printer. Unfortunately I don't think there's
a generic way of doing this (yet) with the X86 back-end. There might be a
good case for making this easier, but right now these kinds of things
haven't been too important to fix yet.

Hope this helps!
-- 
Dean


More information about the llvm-dev mailing list