[LLVMdev] Is it possible to write a llvm pass in OCaml?

Gordon Henriksen gordonhenriksen at mac.com
Wed Apr 30 16:50:20 PDT 2008


On Apr 30, 2008, at 12:56, proguan wrote:

> Is it possible to write a llvm pass in OCaml? Is there any API such  
> as runOnModule() in ModulePass, runOnFunction() in FunctionPass in  
> OCaml?

This would be a welcome addition to the bindings if you're interested.  
Implementing this would require deriving some C++ "glue pass" from  
each of the abstract base classes in the pass hierarchy (e.g.  
GlueFunctionPass, GlueModulePass, GlueSCCPass), then implementing  
runOn* to to invoke a callback.

Note also that while existent, the introspection and modification  
features of the bindings (both C and Ocaml) are immature. Additional  
bindings would be necessary for almost any analysis or transformation  
of the IR.

> I have looked through the tutorial, http://llvm.org/docs/tutorial/OCamlLangImpl1.html 
> , it looks like just a tutorial about "Implementing a language with  
> LLVM" in Objective Caml.


That's correct. At present, the bindings are heavily oriented toward  
authoring a front-end compiler.

As a cautionary note, I would like to point out that the nature of the  
bindings will prevent you from fully utilizing Objective Caml and  
functional algorithms. The bindings reflect directly onto the C++  
object model using opaque values. Therefore, it isn't possible to use  
pattern matches to conveniently deconstruct IR objects as you might in  
a pure Objective Caml program. Nor is it possible to write pure  
functional transformations upon the IR, since values are not garbage  
collected. As an example, the ocamlopt compiler uses algorithms that  
are infeasible with the bindings: a top-down pattern matching  
instruction selector based on mutually recursive matches. Rather, your  
transformation algorithms will have to be essentially imperative. To  
write purely functional transformation algorithms, it would be  
necessary to re-implement the IR in Objective Caml.

— Gordon





More information about the llvm-dev mailing list