Skip to content

mocusez/mlir-hello-world

Repository files navigation

MLIR: Hello World

Print "Hello World!" from an MLIR Program.

Features

  • Custom out-of-tree hello dialect with TableGen-defined operations
  • hello.world op — prints "Hello, World!" via printf
  • hello.dict type — dictionary with create, put, get, delete, free operations
  • Lowering pass from hello dialect to LLVM dialect
  • JIT compiler (helloworld) — builds and executes MLIR programs in-memory
  • Opt tool (hello-opt) — reads .mlir files and emits MLIR LLVM dialect or LLVM IR
  • Cross-platform support: macOS (arm64/x86_64), Linux (x86_64/aarch64)

How to use

git clone https://github.com/mocusez/mlir-hello-world
cd mlir-hello-world

Setup Environment

Install pixi to manage the MLIR/LLVM toolchain:

curl -fsSL https://pixi.sh/install.sh | sh
pixi install

Build

pixi run cmake -B build -DCMAKE_BUILD_TYPE=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -G Ninja
pixi run cmake --build build

Run

./build/bin/helloworld

Output:

Hello, World!

Using .mlir Files

Output LLVM dialect

./build/bin/hello-opt test/hello_world.mlir -emit=mlir-llvm

Result:

module {
  llvm.mlir.global internal constant @hello_word_string("Hello, World! \0A\00") {addr_space = 0 : i32}
  llvm.func @printf(!llvm.ptr, ...) -> i32
  llvm.func @main() {
    %0 = llvm.mlir.addressof @hello_word_string : !llvm.ptr
    %1 = llvm.mlir.constant(0 : index) : i64
    %2 = llvm.getelementptr %0[%1, %1] : (!llvm.ptr, i64, i64) -> !llvm.ptr, !llvm.array<16 x i8>
    %3 = llvm.call @printf(%2) vararg(!llvm.func<i32 (ptr, ...)>) : (!llvm.ptr) -> i32
    llvm.return
  }
}

Output LLVM IR

./build/bin/hello-opt test/hello_world.mlir -emit=llvm

Result:

; ModuleID = 'LLVMDialectModule'
source_filename = "LLVMDialectModule"

@hello_word_string = internal constant [16 x i8] c"Hello, World! \0A\00"

declare i32 @printf(ptr, ...)

define void @main() {
  %1 = getelementptr [16 x i8], ptr @hello_word_string, i64 0, i64 0
  %2 = call i32 (ptr, ...) @printf(ptr %1)
  ret void
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors