Skip to content
thvnxPublic

About

A compiler experiment to demonstrate that Libadalang can be used as a frontend to feed LLVM through MLIR!

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

LALVM: Ada to LLVM Compiler

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.

Disclaimer

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.

Building

Dependencies

  • LLVM/MLIR.
  • AdaCore's Libadalang, version 26 or newer, for Ada parsing, semantic analysis and name resolution.
  • lit and FileCheck for running tests (from llvm-22-tools).
  • doxygen and graphviz (optional, for API documentation with call graphs).

Setting up a development environment

LALVM is currently based on LLVM/MLIR version 22. To install all LLVM dependencies, use llvm.sh's (on Debian or Ubuntu):

wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 22
sudo apt-get install -y cmake ninja-build mold clang-22 \
  llvm-22-dev llvm-22-tools libmlir-22-dev mlir-22-tools

Install the GNAT toolchain and Libadalang from Alire. Build Libadalang as a shared library (LALVM links libadalang.so):

alr toolchain --select gnat_native gprbuild
LIBRARY_TYPE=relocatable alr get --build libadalang

Some execution tests need the GNAT compiler on PATH. Running eval "$(alr printenv)" from the crate directory sets it.

Configuration

Get the directory where Libadalang has been built, this will be the directory to use as LIBADALANG_DIR:

alr get --dirname libadalang

Copy the example user presets file and fill in the paths for your environment:

cp CMakeUserPresets.json.example CMakeUserPresets.json
# edit CMakeUserPresets.json: set LIBADALANG_DIR, MLIR_DIR, compilers

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:

cmake --preset=debug
cmake --build --preset=debug

Optionally, install it with:

cmake --install build --prefix /opt/lalvm

Usage

lalvm [--emit=<action>] [-P <project.gpr>] [-o <file>] [options] <input-file>
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, …).

Example

-- compute.adb
function Compute (X : Integer) return Integer is
   function Double (N : Integer) return Integer is
   begin
      return N + N;
   end Double;
   Result : Integer := Double (X);
begin
   return Result;
end Compute;

Running lalvm --emit=mlir -O1 compute.adb, procudes the following MLIR output:

module @compute {
  ada.type @standard.integer : i32 = #ada.int_info<range -2147483648 to 2147483647>
  ada.subp @compute(%x: !ada.qual<i32, @standard.integer>) -> !ada.qual<i32, @standard.integer> {
    %0 = ada.call @compute.double(%x) : (!ada.qual<i32, @standard.integer>) -> !ada.qual<i32, @standard.integer>
    ada.decls {
      ada.subp private @compute.double(%n: !ada.qual<i32, @standard.integer>) -> !ada.qual<i32, @standard.integer> {
        %1 = ada.binop "+" %n, %n checks<overflow> : (!ada.qual<i32, @standard.integer>, !ada.qual<i32, @standard.integer>) -> !ada.qual<i32, @standard.integer>
        ada.return %1 : !ada.qual<i32, @standard.integer>
      }
    }
    ada.return %0 : !ada.qual<i32, @standard.integer>
  }
}

See compute.ll for the corresponding LLVM IR produced by lalvm --emit=llvm -O1 compute.adb.

Generating and executing machine code

LALVM can run the LLVM backend itself to emit target assembly or an object file for the host target, for example:

lalvm --emit=asm compute.adb       # generates compute.s
lalvm --emit=obj compute.adb       # generates compute.o
lalvm --emit=asm compute.adb -o -  # prints assembly to stdout

To target a non-host architecture, pipe the LLVM IR to llc (for now, LALVM itself only emits for the host):

lalvm --emit=llvm add.adb | llc -mtriple=aarch64-linux-gnu -o add.s

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:

lalvm --emit=obj main.adb
lalvm --bind main.adb
cc main.o b_main.o build/lib/libgnatstub.a -o main

Note that --bind composes with --emit. For example, --emit=llvm dumps the bind module as LLVM IR.

About

A compiler experiment to demonstrate that Libadalang can be used as a frontend to feed LLVM through MLIR!

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages