Skip to content

woxjro/lltz

Repository files navigation

LLTZ: Compiler from MLIR to Michelson

github build status license
lltz-logo

[WIP] LLTZ is a compiler from MLIR to Michelson, VM of blockchain Tezos.

Table of contents

Features

  • Reusable and readable intermediate representation Michelson Dialect
  • Compilation method with easy analysis and practical gas consumption
  • World's first compiler from MLIR to a blockchain VM
lltz_overview

Motivation

A Domain-Specific Language (DSL) is generally necessary to write smart contracts. These DSLs have contributed to the advancement of blockchain technology. However, these impose a high learning cost on developers, becoming a barrier to entry into blockchain ecosystems such as Tezos.

To address this issue and enable smart contract development using general-purpose programming languages, I've developed LLTZ as a first step. This compiler compiles MLIR, an intermediate representation defined by the LLVM compiler infrastructure, into Michelson. Unlike LLVM IR, JVM and WebAssembly, MLIR provides a higher level of abstraction with control structures and types and allows the definition of custom intermediate representations for specific domains through a feature called Dialect.

If smart contracts can be written in a general-purpose programming language, a single program can run both off-chain and on-chain.

Prerequisites

  • Working C and C++ toolchains(compiler, linker)
  • cmake
  • make or ninja
  • tezos-client: Only required for running tests.

Supported OS

This software has been tested and verified to work correctly on the following operating systems:

  • Ubuntu 22.04.2 LTS

Clone LLTZ

git clone --recursive https://github.com/woxjro/lltz
cd lltz

Build & Usage

Build LLVM and MLIR:

$ cd llvm-project
$ mkdir build && cd build
$ cmake -G Ninja ../llvm \
    -DCMAKE_BUILD_TYPE=Release \
    -DLLVM_ENABLE_PROJECTS=mlir \
    -DLLVM_BUILD_EXAMPLES=ON \
    -DLLVM_TARGETS_TO_BUILD=X86 \
    -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON \
    -DCMAKE_EXPORT_COMPILE_COMMANDS=YES \
    -DLLVM_CCACHE_BUILD=ON \
    -DLLVM_CCACHE_DIR=$CCACHE_DIR \
    -DCMAKE_C_COMPILER=clang-15 \
    -DCMAKE_CXX_COMPILER=clang++-15
$ cmake --build . --parallel 7 --target check-mlir

Build michelson-mlir-opt and michelson-mlir-lsp-server:

$ pwd
<path/to/lltz>
$ cd mlir && mkdir build && cd build
$ cmake -G Ninja .. \
    -DMLIR_DIR=$PWD/../../llvm-project/build/lib/cmake/mlir \
    -DCMAKE_EXPORT_COMPILE_COMMANDS=YES
$ cmake --build .

There are sample and test code under the examples directory.

You can compile them by the following command.

$ cargo run --bin cli -- --input ./examples/mlir/boomerang.mlir

To show details, run the following command.

$ cargo run --bin cli -- --help

Contributing

This project is currently in the Proof of Concept (PoC) stage. Contributions are kindly requested to be postponed until further notice.