[LLVMdev] Getting To Native Code

John Criswell criswell at cs.uiuc.edu
Wed Nov 12 09:23:01 PST 2003


Reid Spencer wrote:
> Suppose I wanted to, say, write glibc for LLVM (fat chance! :) I would 
> need at some point to write (hopefully a small amount) of native code 
> to, say, access specific registers, handle interrupts, or generate 
> operating system traps.

	Funny you should mention that; getting a C library compiled to LLVM 
code is one of the tasks on my plate.	:)

> 
>  From my (somewhat cursory) review of AsmParser, it seems like this 
> can't be done with LLVM right now. There is nothing in AsmParser or the 
> rest of LLVM that would allow native assembly instructions to be passed 
> through to the back end.
> 
> Is my finding correct or can this be done currently with LLVM in some 
> way I haven't found?

	You are correct that the LLVM assembly language cannot accept native 
assembly instructions or access machine specific features directly. 
However, there is hope:

	Long term, one of our projects is to port the Linux kernel to LLVM. 
This project has the same problem that you have encountered; how to 
access machine specific services.

	Our current plan is to add a set of intrinsic functions that will give 
us access to the hardware facilities that we need.  For example, we will 
probably have an intrinsic that registers a function as a system call 
handler, another intrinsic that performs I/O, another intrinsic that 
tells us what hardware is connected to the machine, etc, etc.

	In this way, the actual hardware can be abstracted away so that LLVM 
bytecode programs don't need to know about it.  All they see is a set of 
intrinsic functions for the particular piece of hardware/OS upon which 
they run.

	Please note that the Linux kernel project is still in its infancy and 
won't be done for awhile.	:)

	In the meantime, there are still things you can do.  Please see below...

> 
> What is the alternative? Write a library function in C and have it 
> called by LLVM?

	First, I should probably mention that an LLVM program has access to 
just about all OS services offered to a regular native code program. 
This is because undefined symbols in an LLVM program are resolved when 
it is generated to native code.

	Specifically, if you JIT a program using lli, the JIT looks the symbol 
up in its own address space.  That is how LLVM programs find the system 
calls and other library functions that are not compiled to LLVM bytecode.

	For native code, the symbols are resolved at native code link time 
(i.e. you run llc on the bytecode and then assemble it and link it).

	For writing code that does special stuff (accesses machine registers, 
etc), there are several alternatives:

	1) As you suggested, you could write a library in C and compile it to 
native code.  You would then be able to link this library with LLVM code 
(either by loading it into the JIT with -load or by using llc to 
generate native code and linking to your library).

	2) You could add an intrinsic for it.  I'd only recommend this if it's 
something that would be useful as a generic primitive; an intrinsic to 
read performance registers would be okay; an intrinsic for creating 
prime numbers would not.

> 
> The reason I'm asking is that I'd like as much code as possible to be 
> open to optimization by LLVM. If the core of my runtime library can't be 
> expressed in LLVM then it can't be optimized by it either.  Ideally, 
> everything except the glue for system traps, interrupts and the like 
> should be expressible in LLVIS.
> 

	That is where we are headed; we just have a way to go before we're there.

	If you have any further questions, please feel free to ask.  I realize 
I'm being pretty verbose here.

Regards,

-- John T.

-- 
*********************************************************************
* John T. Criswell                         Email: criswell at uiuc.edu *
* Research Programmer                                               *
* University of Illinois at Urbana-Champaign                        *
*                                                                   *
* "It's today!" said Piglet. "My favorite day," said Pooh.          *
*********************************************************************





More information about the llvm-dev mailing list