Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BlockApi trait. #635

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion macro/tests/operand.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utility;

use melior::ir::{Block, Location, Type};
use melior::ir::{block::BlockLike, Block, Location, Type};
use utility::*;

melior_macro::dialect! {
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ mod tests {
dialect::func,
ir::{
attribute::{StringAttribute, TypeAttribute},
block::BlockLike,
r#type::FunctionType,
Attribute, Block, Location, Module, Region, Type,
},
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
attribute::{
DenseElementsAttribute, DenseI32ArrayAttribute, IntegerAttribute, StringAttribute,
},
block::BlockLike,
operation::OperationBuilder,
r#type::RankedTensorType,
Block, Identifier, Location, Operation, Type, Value,
Expand Down
2 changes: 1 addition & 1 deletion melior/src/dialect/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn r#return<'c>(operands: &[Value<'c, '_>], location: Location<'c>) -> Opera
mod tests {
use super::*;
use crate::{
ir::{Block, Module, Type},
ir::{block::BlockLike, Block, Module, Type},
test::create_test_context,
};

Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ mod tests {
dialect::func,
ir::{
attribute::{StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type,
},
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ mod tests {
},
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Module, Region,
},
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/memref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ mod tests {
dialect::{func, index},
ir::{
attribute::{DenseElementsAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType, RankedTensorType},
Block, Module, Region, Type,
},
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ mod tests {
dialect,
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type,
},
Expand Down
1 change: 1 addition & 0 deletions melior/src/dialect/scf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod tests {
dialect::{arith, func},
ir::{
attribute::{FloatAttribute, IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType, Type},
Attribute, Block, Module,
},
Expand Down
2 changes: 1 addition & 1 deletion melior/src/helpers/builtin.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
ir::{Block, Operation, Value},
ir::{block::BlockLike, Block, Operation, Value},
Error,
};

Expand Down
1 change: 1 addition & 0 deletions melior/src/helpers/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
attribute::{
DenseI32ArrayAttribute, DenseI64ArrayAttribute, IntegerAttribute, TypeAttribute,
},
block::BlockLike,
r#type::IntegerType,
Attribute, Block, Location, Type, Value, ValueLike,
},
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod value;
pub use self::{
affine_map::AffineMap,
attribute::{Attribute, AttributeLike},
block::{Block, BlockRef},
block::{Block, BlockLike, BlockRef},
identifier::Identifier,
location::Location,
module::Module,
Expand Down
161 changes: 20 additions & 141 deletions melior/src/ir/block.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
//! Blocks.

mod argument;
mod block_like;

pub use self::argument::BlockArgument;
use super::{
operation::OperationRefMut, Location, Operation, OperationRef, RegionRef, Type, TypeLike, Value,
};
use crate::{context::Context, utility::print_callback, Error};
pub use self::block_like::BlockLike;
use super::{Location, Type, TypeLike, Value};
use crate::{context::Context, utility::print_callback};
use mlir_sys::{
mlirBlockAddArgument, mlirBlockAppendOwnedOperation, mlirBlockCreate, mlirBlockDestroy,
mlirBlockDetach, mlirBlockEqual, mlirBlockGetArgument, mlirBlockGetFirstOperation,
mlirBlockGetNextInRegion, mlirBlockGetNumArguments, mlirBlockGetParentOperation,
mlirBlockGetParentRegion, mlirBlockGetTerminator, mlirBlockInsertOwnedOperation,
mlirBlockInsertOwnedOperationAfter, mlirBlockInsertOwnedOperationBefore, mlirBlockPrint,
MlirBlock,
mlirBlockCreate, mlirBlockDestroy, mlirBlockDetach, mlirBlockEqual, mlirBlockPrint, MlirBlock,
};
use std::{
ffi::c_void,
Expand Down Expand Up @@ -50,130 +45,6 @@ impl<'c> Block<'c> {
}
}

/// Returns an argument at a position.
pub fn argument(&self, index: usize) -> Result<BlockArgument<'c, '_>, Error> {
unsafe {
if index < self.argument_count() {
Ok(BlockArgument::from_raw(mlirBlockGetArgument(
self.raw,
index as isize,
)))
} else {
Err(Error::PositionOutOfBounds {
name: "block argument",
value: self.to_string(),
index,
})
}
}
}

/// Returns a number of arguments.
pub fn argument_count(&self) -> usize {
unsafe { mlirBlockGetNumArguments(self.raw) as usize }
}

/// Returns a reference to the first operation.
pub fn first_operation(&self) -> Option<OperationRef<'c, '_>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetFirstOperation(self.raw)) }
}

/// Returns a mutable reference to the first operation.
pub fn first_operation_mut(&mut self) -> Option<OperationRefMut<'c, '_>> {
unsafe { OperationRefMut::from_option_raw(mlirBlockGetFirstOperation(self.raw)) }
}

/// Returns a reference to a terminator operation.
pub fn terminator(&self) -> Option<OperationRef<'c, '_>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetTerminator(self.raw)) }
}

/// Returns a mutable reference to a terminator operation.
pub fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, '_>> {
unsafe { OperationRefMut::from_option_raw(mlirBlockGetTerminator(self.raw)) }
}

/// Returns a parent region.
// TODO Store lifetime of regions in blocks, or create another type like
// `InsertedBlockRef`?
pub fn parent_region(&self) -> Option<RegionRef<'c, '_>> {
unsafe { RegionRef::from_option_raw(mlirBlockGetParentRegion(self.raw)) }
}

/// Returns a parent operation.
pub fn parent_operation(&self) -> Option<OperationRef<'c, '_>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetParentOperation(self.raw)) }
}

/// Adds an argument.
pub fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, '_> {
unsafe {
Value::from_raw(mlirBlockAddArgument(
self.raw,
r#type.to_raw(),
location.to_raw(),
))
}
}

/// Appends an operation.
pub fn append_operation(&self, operation: Operation<'c>) -> OperationRef<'c, '_> {
unsafe {
let operation = operation.into_raw();

mlirBlockAppendOwnedOperation(self.raw, operation);

OperationRef::from_raw(operation)
}
}

/// Inserts an operation.
// TODO How can we make those update functions take `&mut self`?
// TODO Use cells?
pub fn insert_operation(
&self,
position: usize,
operation: Operation<'c>,
) -> OperationRef<'c, '_> {
unsafe {
let operation = operation.into_raw();

mlirBlockInsertOwnedOperation(self.raw, position as isize, operation);

OperationRef::from_raw(operation)
}
}

/// Inserts an operation after another.
pub fn insert_operation_after(
&self,
one: OperationRef<'c, '_>,
other: Operation<'c>,
) -> OperationRef<'c, '_> {
unsafe {
let other = other.into_raw();

mlirBlockInsertOwnedOperationAfter(self.raw, one.to_raw(), other);

OperationRef::from_raw(other)
}
}

/// Inserts an operation before another.
pub fn insert_operation_before(
&self,
one: OperationRef<'c, '_>,
other: Operation<'c>,
) -> OperationRef<'c, '_> {
unsafe {
let other = other.into_raw();

mlirBlockInsertOwnedOperationBefore(self.raw, one.to_raw(), other);

OperationRef::from_raw(other)
}
}

/// Detaches a block from a region and assumes its ownership.
///
/// # Safety
Expand All @@ -191,11 +62,6 @@ impl<'c> Block<'c> {
}
}

/// Returns a next block in a region.
pub fn next_in_region(&self) -> Option<BlockRef<'c, '_>> {
unsafe { BlockRef::from_option_raw(mlirBlockGetNextInRegion(self.raw)) }
}

/// Creates a block from a raw object.
///
/// # Safety
Expand Down Expand Up @@ -223,6 +89,12 @@ impl<'c> Block<'c> {
}
}

impl<'c, 'a> BlockLike<'c, 'a> for &'a Block<'c> {
fn to_raw(self) -> MlirBlock {
self.raw
}
}

impl Drop for Block<'_> {
fn drop(&mut self) {
unsafe { mlirBlockDestroy(self.raw) };
Expand Down Expand Up @@ -295,8 +167,14 @@ impl BlockRef<'_, '_> {
}
}

impl<'a> Deref for BlockRef<'_, 'a> {
type Target = Block<'a>;
impl<'c, 'a> BlockLike<'c, 'a> for BlockRef<'c, 'a> {
fn to_raw(self) -> MlirBlock {
self.raw
}
}

impl<'c> Deref for BlockRef<'c, '_> {
type Target = Block<'c>;

fn deref(&self) -> &Self::Target {
unsafe { transmute(self) }
Expand Down Expand Up @@ -329,6 +207,7 @@ mod tests {
use crate::{
ir::{operation::OperationBuilder, r#type::IntegerType, Module, Region, ValueLike},
test::create_test_context,
Error,
};
use pretty_assertions::assert_eq;

Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/block/argument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ mod tests {
use super::*;
use crate::{
context::Context,
ir::{Block, Location},
ir::{block::BlockLike, Block, Location},
};

#[test]
Expand Down
Loading
Loading