The rustic MLIR bindings for Rust. Continued
This crate is a wrapper of the MLIR C API.
```rust use melior_next::{ Context, dialect, ir::, pass, utility::, ExecutionEngine };
let registry = dialect::Registry::new(); registeralldialects(®istry);
let context = Context::new(); context.appenddialectregistry(®istry); context.getorloaddialect("func"); registerallllvmtranslations(&context);
let location = Location::unknown(&context); let mut module = Module::new(location);
let integer_type = Type::integer(&context, 64);
let function = { let region = Region::new(); let block = Block::new(&[(integertype, location), (integertype, location)]);
let sum = block.append_operation(
operation::Builder::new("arith.addi", location)
.add_operands(&[block.argument(0).unwrap().into(), block.argument(1).unwrap().into()])
.add_results(&[integer_type])
.build(),
);
block.append_operation(
operation::Builder::new("func.return", Location::unknown(&context))
.add_operands(&[sum.result(0).unwrap().into()])
.build(),
);
region.append_block(block);
operation::Builder::new("func.func", Location::unknown(&context))
.add_attributes(&[
(
Identifier::new(&context, "function_type"),
Attribute::parse(&context, "(i64, i64) -> i64").unwrap(),
),
(
Identifier::new(&context, "sym_name"),
Attribute::parse(&context, "\"add\"").unwrap(),
),
(
Identifier::new(&context, "llvm.emit_c_interface"),
Attribute::parse(&context, r#"unit"#).unwrap(),
),
])
.add_regions(vec![region])
.build()
};
module.body().append_operation(function);
assert!(module.as_operation().verify());
let passmanager = pass::Manager::new(&context); registerallpasses(); passmanager.addpass(pass::conversion::convertscftocf()); passmanager.addpass(pass::conversion::convertcftollvm()); passmanager.addpass(pass::conversion::convertfunctollvm()); passmanager.addpass(pass::conversion::convertarithmetictollvm()); passmanager.enableverifier(true); passmanager.run(&mut module).unwrap();
let engine = ExecutionEngine::new(&module, 2, &[], false);
let mut argument1: i64 = 2; let mut argument2: i64 = 4; let mut result: i64 = -1;
unsafe { engine .invoke_packed( "add", &mut [ &mut argument1 as *mut i64 as *mut (), &mut argument2 as *mut i64 as *mut (), &mut result as *mut i64 as *mut () ]) .unwrap(); };
assert_eq!(result, 6); ```
Melior aims to provide a simple, safe, and complete API for MLIR with a reasonably sane ownership model represented by the type system in Rust.
sh
cargo add melior-next
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
On GitHub Pages.
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.
&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.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:
&self
, they are named <X>::as_<Y>
.<X>::<Y>
and may have arguments, such as position indices.This is a fork of https://github.com/raviqqe/melior