|
lalvm
Ada to LLVM IR compiler via MLIR
|
LALVM is a prototype Ada-to-LLVM compiler using AdaCore's Libadalang as the Ada frontend, MLIR as an intermediate representation, and LLVM for the backend.
This README covers building and day-to-day use. See the developer documentation for testing and development tools.
This project is an experiment to demonstrate that Libadalang can be used as a frontend and directly feed LLVM through MLIR. It is in no way a substitute for any available Ada compiler, and is far from being usable today. Many limitations remain to be addressed before the Ada language could be fully supported.
Note: design choices in this project are mine, but most of the code was generated by AI. Therefore, the code carries a fair amount of AI slop, which is being cleaned up step by step as the files are revisited.
lit and FileCheck for running tests (from llvm-22-tools).doxygen and graphviz (optional, for API documentation with call graphs).LALVM is currently based on LLVM/MLIR version 22. To install all LLVM dependencies, use llvm.sh's (on Debian or Ubuntu):
Install the GNAT toolchain and Libadalang from Alire. Build Libadalang as a shared library (LALVM links libadalang.so):
Some execution tests need the GNAT compiler on PATH. Running eval "$(alr
printenv)" from the crate directory sets it.
Get the directory where Libadalang has been built, this will be the directory to use as LIBADALANG_DIR:
Copy the example user presets file and fill in the paths for your environment:
Note: LIBADALANG_DIR could point at a Libadalang build tree (the build/ directory of a source checkout), a crate directory alr get --build libadalang produces, or at an installation prefix.
Then configure and build:
Optionally, install it with:
| Main options | Description |
|---|---|
--emit {ast,mlir,llvm,obj,asm} | Output format (default: obj) |
-o <file> | Output file name (default to stdout for text dumps or the input basename with a .o/.s extension for obj/asm formats) |
-P <project.gpr> | Load a GPR project so withed units resolve through its unit provider (otherwise the single file is parsed alone) |
--bind | Emit the bind module of the given main unit |
-g | Generate DWARF debug info (off by default, best to use at O0) |
Run lalvm --help for the full list of options. Standard LLVM codegen flags can be used for obj/asm formats (-mcpu, -mattr, --relocation-model, …).
Running lalvm --emit=mlir -O1 compute.adb, procudes the following MLIR output:
See compute.ll for the corresponding LLVM IR produced by lalvm --emit=llvm -O1 compute.adb.
LALVM can run the LLVM backend itself to emit target assembly or an object file for the host target, for example:
To target a non-host architecture, pipe the LLVM IR to llc (for now, LALVM itself only emits for the host):
A program needs a main for the C runtime startup code to call. lalvm --bind emits it for the program whose main unit is given: main calls the main subprogram and returns the exit status. The main unit must be the body of a parameterless library-level subprogram, a procedure or a function returning Integer.
Compile the unit, bind it, then link both objects with the stub through the C compiler, which brings in the startup files and the C library:
Note that --bind composes with --emit. For example, --emit=llvm dumps the bind module as LLVM IR.