• Stars
    star
    279
  • Rank 147,098 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created over 2 years ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The rustic MLIR bindings in Rust

Melior

GitHub Action Crate License

Melior is the MLIR bindings for Rust. It aims to provide a simple, safe, and complete API for MLIR with a reasonably sane ownership model represented by the type system in Rust.

This crate is a wrapper of the MLIR C API.

Safety

Although Melior aims to be completely type safe, some part of the current API is not.

  • Access to operations, types, or attributes that belong to dialects not loaded in contexts can lead to runtime errors or segmentation faults in the worst case.
    • Fix plan: Load all dialects by default on creation of contexts, and provide unsafe constructors of contexts for advanced users.
  • IR object references returned from functions that move ownership of arguments might get invalidated later.
    • This is because we need to borrow &self rather than &mut self to return such references.
    • e.g. Region::append_block()
    • Fix plan: Use dynamic check, such as RefCell, for the objects.

Examples

Building a function to add integers

use melior::{
    Context,
    dialect::{arith, DialectRegistry, func},
    ir::{*, attribute::{StringAttribute, TypeAttribute}, r#type::FunctionType},
    utility::register_all_dialects,
};

let registry = DialectRegistry::new();
register_all_dialects(&registry);

let context = Context::new();
context.append_dialect_registry(&registry);
context.load_all_available_dialects();

let location = Location::unknown(&context);
let module = Module::new(location);

let index_type = Type::index(&context);

module.body().append_operation(func::func(
    &context,
    StringAttribute::new(&context, "add"),
    TypeAttribute::new(FunctionType::new(&context, &[index_type, index_type], &[index_type]).into()),
    {
        let block = Block::new(&[(index_type, location), (index_type, location)]);

        let sum = block.append_operation(arith::addi(
            block.argument(0).unwrap().into(),
            block.argument(1).unwrap().into(),
            location
        ));

        block.append_operation(func::r#return(&[sum.result(0).unwrap().into()], location));

        let region = Region::new();
        region.append_block(block);
        region
    },
    &[],
    location,
));

assert!(module.as_operation().verify());

Install

cargo add melior

Dependencies

LLVM/MLIR 16 needs to be installed on your system. On Linux and macOS, you can install it via Homebrew.

brew install llvm@16

Documentation

On GitHub Pages.

Contribution

Contribution is welcome! But, Melior is still in the alpha stage as well as the MLIR C API. Note that the API is unstable and can have breaking changes in the future.

Technical notes

  • We always use &T for MLIR objects instead of &mut T to mitigate the intricacy of representing a loose ownership model of the MLIR C API in Rust.
  • Only UTF-8 is supported as string encoding.
    • Most string conversion between Rust and C is cached internally.

Naming conventions

  • Mlir<X> objects are named <X> if they have no destructor. Otherwise, they are named <X> for owned objects and <X>Ref for borrowed references.
  • mlir<X>Create functions are renamed as <X>::new.
  • mlir<X>Get<Y> functions are renamed as follows:
    • If the resulting objects refer to &self, they are named <X>::as_<Y>.
    • Otherwise, they are named just <X>::<Y> and may have arguments, such as position indices.

References

License

Apache 2.0

More Repositories

1

muffet

Fast website link checker in Go
Go
2,071
star
2

hamt

Immutable and Memory-Efficient Maps and Sets in Go
Go
231
star
3

liche

Fast Link Checker for Markdown and HTML in Go
Go
125
star
4

tensorflow-font2char2word2sent2doc

TensorFlow implementation of Hierarchical Attention Networks for Document Classification and some extension
Python
94
star
5

turtle-build

Ninja-compatible build system for high-level programming languages written in Rust
Rust
79
star
6

tensorflow-qnd

Quick and Dirty TensorFlow command framework to train and evaluate models and make inference
Python
57
star
7

begin-with-init.vim

Minimal neovim distribution as init.vim
Vim Script
33
star
8

oneRPC

The router-less serverless RPC framework for TypeScript
TypeScript
26
star
9

gherkin2markdown

Gherkin to Markdown converter
Go
24
star
10

rnm

Yet another codemod alternative
Go
20
star
11

ssf

Structurally-typed strict functional core language
Rust
19
star
12

block-is-array

Block is Array (See also https://github.com/raviqqe/block-is-hash)
Ruby
19
star
13

fmm

The uncurried minimal functional programming language for CPS transformation
Rust
18
star
14

bdwgc-alloc

impl GlobalAlloc for Boehm GC
Rust
18
star
15

schemat

Code formatter for Scheme, Lisp, and any S-expressions
Rust
15
star
16

code2d

Productivity tool for software engineers
TypeScript
11
star
17

lazy-ein

JSON meets Haskell.
Go
11
star
18

hamt-rs

HAMT implementation whose sub-trees can be shared over threads
Rust
11
star
19

dotfiles

My dotfiles for Linux / macOS
Shell
8
star
20

tasks

Simple task management app
TypeScript
8
star
21

self-talk

Talk to yourself
TypeScript
8
star
22

stak

No-std and no-alloc R7RS Scheme implementation in Rust
Rust
8
star
23

arachne

Rust
5
star
24

autophagy

Yet another AOT compiler for Rust
Rust
5
star
25

vim-nonblank

Delete trailing whitespaces on every write
Vim Script
5
star
26

dockerfile-dsl.rb

Dockerfile DSL in Ruby
Ruby
5
star
27

block-is-hash

Block is Hash (See also https://github.com/raviqqe/block-is-array)
Ruby
5
star
28

nginx-conf.rb

nginx.conf generator in Ruby
Ruby
4
star
29

bstie

Summon Beastie!
Go
3
star
30

language-design

3
star
31

til-old

Today I learned.
PHP
3
star
32

pomodoro

Pomodoro timer with performance tracking
TypeScript
3
star
33

stg

STG Machine in Go
Go
3
star
34

tensorflow-extenteten

TensorFlow extension library
Python
3
star
35

type-inference

Type inference algorithm with mutable data structures in Go
Go
2
star
36

flame

Freeze the world.
Rust
2
star
37

ein-design

2
star
38

tensorflow-entrec

Python
1
star
39

eslint-config

TypeScript
1
star
40

hm-type-inference

Rust
1
star
41

llir.rs

Rust
1
star
42

plist-rs

Persistent list in Rust
Rust
1
star
43

til

Today I learned
Rust
1
star
44

imleak

Rust
1
star
45

llvm-experiments

LLVM
1
star
46

simple-arc-rs

Simple Arc implementation in Rust
Rust
1
star
47

shakyo

type and learn
Python
1
star
48

wasm-examples

WebAssembly
1
star
49

infrastructure

HCL
1
star
50

parallel-go

Go
1
star
51

rpm

RPM (Requests Per Minute) reporter in Go
Go
1
star
52

react-scripts

TypeScript
1
star
53

xir

Rust
1
star
54

pir

Rust
1
star
55

mfmt

Meta formatter library in Rust
Rust
1
star
56

ewm

Rust
1
star
57

hidash

Lodash-like library for the Web API
TypeScript
1
star
58

all-yours

Yes, it's all yours.
Elm
1
star
59

marksvg

Haskell
1
star
60

freebsd_installation_script

Application installation script after the first boot (DEPRECATED, this project is succeeded to by https://github.com/raviqqe/dotfiles/)
C
1
star
61

llvm-all-the-things

Shell
1
star
62

parcom

Parser combinators in Go
Go
1
star
63

typetod

typeing game to let you type everything forever (DEPRECATED, this project is succeeded to by https://github.com/raviqqe/shakyo/)
Python
1
star
64

enable-homebrew

GitHub Action to enable Homebrew in Ubuntu images
JavaScript
1
star
65

immix-rust

Copy of gitlab.anu.edu.au/mu/immix-rust
Rust
1
star
66

gherkin-format

Gherkin code formatter
Go
1
star
67

loscore

Yet another utility library in TypeScript.
TypeScript
1
star