[LLVMdev] Fibonacci example in OCaml

Gordon Henriksen gordonhenriksen at mac.com
Mon Nov 26 12:05:53 PST 2007

On Nov 26, 2007, at 14:18, Jon Harrop wrote:

> On Monday 26 November 2007 16:21, Gordon Henriksen wrote:
>> Unfortunately, even if the bindings were more strongly typed, it  
>> would still be structurally possible to build invalid LLVM code, so  
>> you've just got to take care not to violate the invariants, then  
>> use the verifier as a cross-check.
> I suspect the OCaml bindings to LLVM could express more of these  
> constraints in the static type system. Has anyone tried to leverage  
> this?

Any specific ideas?

>>> I assume I need a real type system that can handle both ints and
>>> function pointers?
> As I understand it, the simplest approach is to box all values,  
> which means that every value is stored as a struct containing the  
> type (an enum) and the value itself (inline if it is no bigger than  
> a pointer or as a pointer to a larger data structure otherwise).
> I'd rather box everything rather than tag ints to start with. I'll  
> relegate that to a potential optimization.

You could go about it that way, sure. Memory pressure will be very high.

> What of this can LLVM's optimizer optimize away for me?

Not much. LLVM won't really change your memory layout. The features  
which did perform dramatic memory reorganization were excised from the  
source tree due to patent infringement issues. (They remain in the  
repository on a branch for UIUC research.) These were also targeted  
toward manual memory management.

It should be possible for LLVM to perform intra- or interprocedural  
escape analysis and lower heap allocations to stack allocations, but I  
don't believe this is implemented. Garbage collection would also  
complicate it, since LLVM would need to recognize GC allocator  
functions as such. This would be an excellent project if you are  
interested in contributing to LLVM itself.

> So I have to work out how to generate IR that handles those data  
> structures.

Should be straightforward. Read the getelementptr FAQ. Unions are  
handled with pointer bitcasts.


— Gordon

More information about the llvm-dev mailing list