Started from Pauladam Smith's blog how to get started with llvm c api, then try to make it a comprehensive code guide about llvm c api
Typical cmake project, make sure LLVM in cmake search path
- simple function --
create_sum_fn
: Adopted Paladam'ssum
code to LLVM 14 and C++ - call function in libc --
create_out_fn
: Call functions from libc,puts
- recursive calls and TCE --
create_factorial_fn
: Recursive functionfactorial(n)
withLLVMAddTailCallEliminationPass
to eliminate recursive calls in IR - more complicated recursive calls and TCE --
create_fib_fn
: Naive recursive implementation offib(n)
, clearly only the lastfib
call infib(n-1) + fib(n-2)
got TCE optimized - if-the-else with PHI --
create_is_odd_fn
:if then else
pattern - while-do without PHI --
create_factorial_loop_fn
:while-do
without PHI. For factorial, this version and the recursive one optimized down to the same IR code. - array access with GEP --
create_ap_sum_fn
: Sum of an array of arithmetic progression numbers, I really hoped LLVM can do it in the way of(f + l) * n / 2
, but sadly no, still loops - Niubi LLVM optimization --
create_ap_simple_sum_fn
: Sum ofan array ofa sequence of arithmetic progression numbers, still not using that formula, maybe because of some optimization are missing, unlike pure -O3, maybe it is being done during re-write
In orcv2jit.cpp
file
- Found memory leaks in llvm official example, filed a bug report, fixed in 4246269
- In the same bug report, I mentioned
LLVMOrcIRTransformLayerSetTransform
causes memory leaks, fixed in bc062e0 cal_fact_4
: call to another function in a different module