Code Monkey home page Code Monkey logo

declarative-mlir-compiler's Introduction

Declarative MLIR Compilers

Design document

Building DMC

Build requirements:

  • cmake >= 3.10
  • python >= 3.6

Arch Linux:

sudo pacman -Sy cmake python

MacOS:

brew install cmake python3

Clone the repo and its submodules. Configure CMake with whatever generator you prefer and then build the target gen.

cmake --build $BINDIR --target gen

Using DMC

Let's define a dead simple dialect. Make sure the shared library is built with cmake --build $BINDIR -t mlir, and that $BINDIR/lib/Python is added to your PYTHONPATH.

// toy.mlir
Dialect @toy {
  Op @add3(one: !dmc.Any, two: !dmc.Any, three: !dmc.Any) -> (res: !dmc.Any)
    traits [@SameType<"one", "two", "three", "res">]
}

Our Python script will need to load the dialect.

from mlir import *

dialects = registerDynamicDialects(parseSourceFile("toy.mlir"))
toy = dialects[0]

Okay, let's generate a simple program.

m = ModuleOp()
func = FuncOp("add3_impl", FunctionType([I64Type()] * 3, [I64Type()]))
m.append(func)

entry = func.addEntryBlock()
args = entry.args
b = Builder()
b.insertAtStart(entry)
add3 = b.create(toy.add3, one=args[0], two=args[1], three=args[2],
                res=I64Type())
b.create(ReturnOp, operands=[add3.res()])
verify(m)
print(m)

The output should be

module {
  func @main(%arg0: i64, %arg1: i64, %arg2: i64) -> i64 {
    %0 = "toy.add3"(%arg0, %arg1, %arg2) : (i64, i64, i64) -> i64
    return %0 : i64
  }
}

Building the Lua Compiler

The Lua compile requires antlr >= 4. On Arch, install the Pacman package antlr4. On MacOS, install the Homebrew package antlr.

First, build the CMake targets mlir, mlir-translate, and lua-parser.

cmake --build $BINDIR -t mlir -t mlir-translate -t lua-parser

The Python shared library and autogenerated Lua parser must be added to your PYTHONPATH. These can be found under $BINDIR/lua/parser and $BINDIR/lib/Python.

export PATH=$PATH:$BINDIR/tools:$BINDIR/llvm-project/llvm/bin:$BINDIR/lua
export PYTHONPATH=$BINDIR/lua/parser:$BINDIR/lib/Python

Using the Lua Compiler

Let's define a basic Lua file.

-- example.lua
function fib(n)
  if n == 0 then return 0 end
  if n == 1 then return 1 end
  return fib(n-1) + fib(n-2)
end

print("fib(5) = ", fib(5))

The Python script lua/luac.py calls the ANTLR parser and lowers the IR to LLVM IR. The resulting file must be compiled (-O2 strongly recommended) and linked against a runtime lua/impl.cpp and the builtins lua/builtins.cpp.

python3 lua/luac.py example.lua > example.mlir
mlir-translate --mlir-to-llvmir example.mlir -o example.ll
clang++ example.ll lua/impl.cpp lua/builtins.cpp -O2 -std=c++17 -o example

Running (hopefully) produces the correct output:

me$ ./example
fib(5) = 5

declarative-mlir-compiler's People

Contributors

mogball avatar

Watchers

 avatar

Forkers

codes1gn

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.