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.

Examples

Building a function to add integers

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

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

let context = Context::new(); context.appenddialectregistry(&registry); context.loadallavailable_dialects();

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

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

module.body().appendoperation(func::func( &context, StringAttribute::new(&context, "add"), TypeAttribute::new(FunctionType::new(&context, &[indextype, indextype], &[indextype]).into()), { let block = Block::new(&[(indextype, location), (indextype, 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

sh cargo add melior

Dependencies

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

sh 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

Naming conventions

References

License

Apache 2.0