[LLVMdev] request for help writing a register allocator

Lang Hames lhames at gmail.com
Wed Oct 21 17:18:46 PDT 2009


Hi Susan,

> 1. I tried running the PBQP allocator (as a dynamic pass), but that didn't
>   work....



 Can you tell from this what I'm doing wrong?
>

The PBQP allocator is built into the LLVM CodeGen library, so the
"-regalloc=pbqp" option is already available in llc. If you've built a copy
of the PBQP allocator in a separate library it will try to re-register that
same option, triggering the assertion you're seeing. You can probably get
around this by just changing the option in your copy to "pbqp2".

2. I tried writing a very simple register allocator.  It works as follows...


<snip>


> This allocator works on very simple C code, but fails (with an
> uninformative segmentation fault) as soon as I include a call to scanf, or a
> loop, or an if-else.
>

There are any number of things that can go wrong in register allocation, so
it's hard for me to guess without seeing your code.

Possible issues:

1) Some machine instructions have implicit register operands. If you are not
including these in your calculations you will probably have some errors. My
apologies for not mentioning this earlier.

You can find the set of implicit uses and defs by querying the
TargetInstDesc object for each MachineInstr (call MachineInstr::getDesc() to
get it, and then the TargetInstrDesc::getImplicitUses() and
TargetInstrDesc::getImplicitDefs() methods to find the regs used/definied by
this instr).

2) How are you making sure that interfering virtregs never receive the same
physreg? If you're using the LiveIntervals analysis (and the
LiveInterval::overlaps(LiveInterval &) test) you should be ok, since it gets
a lot of testing (though bugs are not unheard of). If you've written your
own liveness analysis that would be one of the first places I'd check for
correctness.


> Do you see any obvious errors in the approach outlined above?
>

No obvious errors. My concern would be the liveness issue mentioned above.
In addition, what do you do if/when you encounter an instruction with two or
more operands (in the same class) that have been spilled?


> If not, can you suggest a way to find out what is going wrong?
>

The LLVM bugpoint tool is very handy if your allocator itself is crashing.
It sounds like your allocator is running fine though, but miscompiling
programs. Unfortunately in this case there aren't many tools to help you
(that I know of). I usually just work by inspection. Add some debugging
output to your allocator, or fire llc up under gdb and dump information
(many objects in LLVM have a handy "dump()" method to print out their
state). Try to find a minimal failing test case (if it breaks on if tests
then that's a great start), and just look to see where the allocation goes
wrong.


> Thanks for any help you can give me!!


No problem at all. Writing allocators is non-trivial (took me quite a while
to get my first one going). If you get really stuck try posting your
allocator and a failing test case.

Good luck!

- Lang.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20091021/28be3dc1/attachment.html>


More information about the llvm-dev mailing list