Note: this page is somewhat of a work-in-progress. Please feel free to edit with additions, clarifications, better formatting, etc.

Getting started

1. Skim this document to get a feeling for IR: http://llvm.org/docs/LangRef.html

Look at the table of contents to get the basic idea. Check out the "Type System" section, and browse some of the instructions.

2. Learn to read IR, and then IR-generating functions. First focus on the IR (most codegen functions have sample IR output at the top of the implementation), then how the IR is built.

Some examples to get started:

3. Review the Adding a builtin function to Impala documentation for the relatively easy-to-understand UDF use case.

4. Cross-compiled functions (TODO) 


I do not recommend plowing through the LLVM tutorial. It has some useful info, but is mostly useless for our purposes.

Debugging

Most codegen bugs will manifest by Impala crashing. Here are things to look for to help diagnose:

Here are some more things you can do to debug:

Common pitfalls:

Misc. tips

Code Overview

The planner divides a query into fragments, the BE runs multiple fragment instances. Each instance is represented by FragmentInstance in fragment-instance-state.cc. The Open() method checks if code generation should be done, and if so, triggers the codegen work by calling the CodeGen() method on on the set of exec nodes (operators), which will call CodeGen() on expressions (for those nodes that have expressions.)

Consider an expression in the SELECT clause. Impala is row-based, with each row represented by TupleRow (tuple.h). TupleRow::MaterializeExprs() iterates over the expressions to be used to materialize a row, which will include either a slot reference or a scalar expr (ScalarExpr). For each, GetValue() is called, which is either a code generated function or an interpreted function. This function itself can be code generated as the MaterializeExprs function.

Useful APIs and references

https://github.com/apache/impala/blob/master/be/src/codegen/llvm-codegen.h: the main entry point for generating IR

https://github.com/apache/incubator-impala/blob/master/be/src/codegen/codegen-anyval.h: internal API for handling Expr output, grep for CreateCallUnwrapped for example usage

http://llvm.org/docs/LangRef.html: IR reference

http://llvm.org/docs/ProgrammersManual.html: Guide for writing LLVM C++ code. Not super applicable but has some useful patterns (e.g. iterating through a BasicBlock)

LLVM hosts generated docs for each class, which are indispensable as the C++ API is very large. You can usually just google "llvm foo" where foo is the class you're interested in. Some important ones:

http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilder.html

http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilderBase.html: most of what you want is in IRBuilder, but IRBuilderBase has a few unexpected gems)

http://llvm.org/docs/doxygen/html/classllvm_1_1Value.html

http://llvm.org/docs/doxygen/html/classllvm_1_1Function.html

http://llvm.org/docs/doxygen/html/classllvm_1_1Type.html

http://adriansampson.net/blog/llvm.html: LLVM for Grad Students; gives a good and simple introduction to LLVM