[LLVMdev] How to extend llvm IR and frontend?

Frits van Bommel fvbommel at gmail.com
Fri Jan 21 10:23:52 PST 2011


On Fri, Jan 21, 2011 at 4:32 PM, Aaron Myles Landwehr <snaphat at gmail.com> wrote:
> Hypothetically, suppose I have a generic system with multiple address spaces
> such that each address space is accessed using different instructions.
> Now suppose, I wanted to add a new keywords 'foo' and 'bar' to the front of
> c variables and function return types such that the following would be
> valid:
> foo void* a;
> foo void* somefunc(){...}
> bar int b;
> int somefunc2(bar int*){...}

How about putting

  #define foo __attribute__((address_space(256)))
  #define bar __attribute__((address_space(257)))

in some header? (Or on the command line, or in clang's default #defines, ...)
Though maybe they'd need to be in a slightly different place; the
example in the documentation
(http://clang.llvm.org/docs/LanguageExtensions.html) is

  #define GS_RELATIVE __attribute__((address_space(256)))
  int foo(int GS_RELATIVE *P) {
    return *P;
  }

so maybe it won't work if they're in front of the declaration.

And of course, they're not technically keywords...

> Furthermore, if I wanted this keyword to be added as part of the LLVM IR in
> some manner such that the during the translation of LLVM IR to some target
> machine ASM it could be used to switch which address spaces are used (e.g.
> if I specify one of the keywords, I get some instruction or group of
> instructions to access a certain address space), what would be the
> correct/easiest place(s) to add this functionality, and would there be
> existing code that does something similar that can be used as a starting
> point?

The x86 frontend does something like this for fs and gs segment
specifiers, but those just add a prefix to the instruction so they
might not actually be different instructions (I'm not sure).



More information about the llvm-dev mailing list