[cfe-dev] A question on register allocation

James Y Knight via cfe-dev cfe-dev at lists.llvm.org
Sun Oct 4 17:48:16 PDT 2015


I don't think it'd actually be terribly difficult: every target already has a list of target-specific reserved registers that are setup by TargetRegisterInfo::getReservedRegs(const MachineFunction &). Merging that builtin set with an set of "extra" reserved registers attached to a MachineFunction seems like it'd be easy enough.

That seems sufficient to remove the register from normal register allocation, but of course if the target hard-codes the use of a register, or some asm clobbers the register, or whatever, this wouldn't prevent it from being clobbered. That caveat applies to GCC's feature as well (see docs <https://gcc.gnu.org/onlinedocs/gcc/Global-Reg-Vars.html>).

Of note for the use-case given here: there are no callee-saved xmm registers in the sysV ABI. So, unless you re-compile *everything* linked into your binary with the reserved register (including libc, dynamic linker, compiler runtime support library, etc.) and validate that no asm code in any of those clobbers it either, you can't be guaranteed that the value will always be preserved -- even if clang has this feature.

James

On Oct 4, 2015, at 2:06 PM, mats petersson via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> Your problem here is that you also need to "remove" your chosen register from the list of registers the compiler may use for other purposes. As far as I understand, there's no infrastructure to "remove this register ALWAYS", so you'll most likely need to do a fair bit of changes - I'm not familiar with this particular bit of code, but it's obviously an ugly/difficult enough solution that for the "make Linux compile with Clang" was solved by "remove use of global registers", except for "stack pointer" (Stack pointer is OK, since compiler will not use that register for anything other than "as stack pointer"). 
> 
> --
> Mats
> 
> On 4 October 2015 at 13:42, Viswesh Narayanan via cfe-dev <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> OK. I am also willing to change the source code (clang and/or LLVM) to
> get the desired behavior.
> So if some expert can point me to right areas in source code, it would
> be great!
> 
> On Sun, Oct 4, 2015 at 8:00 PM, Goncalo Carvalho <glslang at gmail.com <mailto:glslang at gmail.com>> wrote:
> > Clang doesn't support global register variables like GCC afaik so it needs
> > to be emulated if really needed by forcing the register to the intended
> > value.
> >
> > In general it can be clobbered by whatever else but such is the way without
> > backend support.
> >
> > But someone more knowledgeable about Clang's backend may provide a better
> > insight.
> >
> >
> >
> >
> >
> > On 4 October 2015 at 12:31, Viswesh Narayanan <visweshn92 at gmail.com <mailto:visweshn92 at gmail.com>> wrote:
> >>
> >> I think you have got my question wrong (Or perhaps my writing was bad).
> >>
> >> I want that register (say xmm15) to hold value 100 throughout the
> >> execution of program.
> >> And LLVM should lock down xmm15 for me and should always allocate some
> >> other register for other functions.
> >>
> >> The solution provided applies only to context of main() and is not
> >> generic if my source code grows bigger.
> >>
> >>
> >>
> >> On Sun, Oct 4, 2015 at 7:24 PM, Goncalo Carvalho <glslang at gmail.com <mailto:glslang at gmail.com>>
> >> wrote:
> >> > I don't think clang supports global register variables in general
> >> > although
> >> > 'sp' appears to work from clang-3.7 onwards. However it doesn't appear
> >> > to
> >> > understand 'r13' on arm which is equivalent.
> >> >
> >> > You can always do something like
> >> >
> >> > asm ("movq %0, %%xmm15"::"x"(100):"xmm15");
> >> >
> >> > although it's not strictly equivalent.
> >> >
> >> > int main() {
> >> >
> >> >     asm ("movq %0, %%xmm15"::"x"(100):"xmm15");
> >> >
> >> >     return 0;
> >> >
> >> > }
> >> >
> >> > will generate
> >> >
> >> >        movl    $0, -4(%rbp)
> >> >
> >> >         movss   .LCPI0_0(%rip), %xmm0
> >> >
> >> >         #APP
> >> >
> >> >         movq    %xmm0, %xmm15
> >> >
> >> >         #NO_APP
> >> >
> >> >         xorl    %eax, %eax
> >> >
> >> >         popq    %rbp
> >> >
> >> >         retq
> >> >
> >> >
> >> > So you could just have that as a replacement everytime 'A' is written
> >> > but is
> >> > not the same I appreciate that. The above also lacks context of the
> >> > actual
> >> > application of what is that you're trying to do so it's possible better
> >> > (and
> >> > faster) solutions exist!
> >> >
> >> >
> >> > On 4 October 2015 at 07:44, Viswesh Narayanan via cfe-dev
> >> > <cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>> wrote:
> >> >>
> >> >> My use case is to store a global value permanently in a register (say
> >> >> xmm15) using LLVM
> >> >>
> >> >> The following piece of code works as expected in GCC while it is not
> >> >> supported in LLVM. It complains with "fatal error: error in backend:
> >> >> Invalid register name global variable".
> >> >>
> >> >> Can any expert here provide me a solution to this in Clang/LLVM?
> >> >>
> >> >> Code:
> >> >> #include <stdio.h>
> >> >> volatile register int A asm ("xmm15");
> >> >> int main() {
> >> >>     A = 100;
> >> >>     return A;
> >> >> }
> >> >> _______________________________________________
> >> >> cfe-dev mailing list
> >> >> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
> >> >> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
> >> >
> >> >
> >> >
> >> >
> >> >
> >
> >
> >
> >
> > --
> > http://www.cryogenicgraphics.com <http://www.cryogenicgraphics.com/>
> > http://www.flickr.com/photos/hdrflow <http://www.flickr.com/photos/hdrflow>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org <mailto:cfe-dev at lists.llvm.org>
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev>
> 
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20151004/c0cdb858/attachment.html>


More information about the cfe-dev mailing list