Skip to content

Commit

Permalink
Rename BlockLike trait
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Dec 10, 2024
1 parent 0582e97 commit c83aa2b
Show file tree
Hide file tree
Showing 20 changed files with 84 additions and 69 deletions.
2 changes: 1 addition & 1 deletion melior/src/dialect/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ mod tests {
dialect::func,
ir::{
attribute::{StringAttribute, TypeAttribute},
block::BlockApi,
block::BlockLike,
r#type::FunctionType,
Attribute, Block, Location, Module, Region, Type,
},
Expand Down
2 changes: 1 addition & 1 deletion melior/src/dialect/cf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
attribute::{
DenseElementsAttribute, DenseI32ArrayAttribute, IntegerAttribute, StringAttribute,
},
block::BlockApi,
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::BlockApi, Block, Module, Type},
ir::{block::BlockLike, Block, Module, Type},
test::create_test_context,
};

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

Expand Down
2 changes: 1 addition & 1 deletion melior/src/helpers/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
attribute::{
DenseI32ArrayAttribute, DenseI64ArrayAttribute, IntegerAttribute, TypeAttribute,
},
block::BlockApi,
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, BlockApi, BlockRef},
block::{Block, BlockLike, BlockRef},
identifier::Identifier,
location::Location,
module::Module,
Expand Down
55 changes: 4 additions & 51 deletions melior/src/ir/block.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Blocks.
mod argument;
mod block_like;

pub use self::argument::BlockArgument;
pub use self::block_like::BlockLike;
use super::{
operation::OperationRefMut, Location, Operation, OperationRef, RegionRef, Type, TypeLike, Value,
};
Expand All @@ -23,55 +25,6 @@ use std::{
ops::Deref,
};

pub trait BlockApi<'c, 'v> {
/// Returns an argument at a position.
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'v>, Error>;
/// Returns a number of arguments.
fn argument_count(&self) -> usize;

/// Returns a reference to the first operation.
fn first_operation(&self) -> Option<OperationRef<'c, 'v>>;
/// Returns a mutable reference to the first operation.
fn first_operation_mut(&mut self) -> Option<OperationRefMut<'c, 'v>>;

/// Returns a reference to a terminator operation.
fn terminator(&self) -> Option<OperationRef<'c, 'v>>;
/// Returns a mutable reference to a terminator operation.
fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, 'v>>;

/// Returns a parent region.
// TODO Store lifetime of regions in blocks, or create another type like
// `InsertedBlockRef`?
fn parent_region(&self) -> Option<RegionRef<'c, 'v>>;
/// Returns a parent operation.
fn parent_operation(&self) -> Option<OperationRef<'c, 'v>>;

/// Adds an argument.
fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v>;

/// Appends an operation.
fn append_operation(&self, operation: Operation<'c>) -> OperationRef<'c, 'v>;
/// Inserts an operation.
// TODO How can we make those update functions take `&mut self`?
// TODO Use cells?
fn insert_operation(&self, position: usize, operation: Operation<'c>) -> OperationRef<'c, 'v>;
/// Inserts an operation after another.
fn insert_operation_after(
&self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v>;
/// Inserts an operation before another.
fn insert_operation_before(
&self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v>;

/// Returns a next block in a region.
fn next_in_region(&self) -> Option<BlockRef<'c, 'v>>;
}

/// A block.
pub struct Block<'c> {
raw: MlirBlock,
Expand Down Expand Up @@ -143,7 +96,7 @@ impl<'c> Block<'c> {
}
}

impl<'c, 'v> BlockApi<'c, 'v> for Block<'c> {
impl<'c, 'v> BlockLike<'c, 'v> for Block<'c> {
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'v>, Error> {
unsafe {
if index < self.argument_count() {
Expand Down Expand Up @@ -324,7 +277,7 @@ impl BlockRef<'_, '_> {
}
}

impl<'c, 'v> BlockApi<'c, 'v> for BlockRef<'c, 'v> {
impl<'c, 'v> BlockLike<'c, 'v> for BlockRef<'c, 'v> {
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'v>, Error> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.argument(index);
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::BlockApi, Block, Location},
ir::{block::BlockLike, Block, Location},
};

#[test]
Expand Down
62 changes: 62 additions & 0 deletions melior/src/ir/block/block_like.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use std::panic::Location;

use super::{BlockArgument, BlockRef};
use crate::{
ir::{operation::OperationRefMut, Operation, OperationRef, RegionRef, Type, Value},
Error,
};

pub trait BlockLike<'c, 'a> {
/// Returns an argument at a position.
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'a>, Error>;

/// Returns a number of arguments.
fn argument_count(&self) -> usize;

/// Returns a reference to the first operation.
fn first_operation(&self) -> Option<OperationRef<'c, 'a>>;
/// Returns a mutable reference to the first operation.
fn first_operation_mut(&mut self) -> Option<OperationRefMut<'c, 'a>>;

/// Returns a reference to a terminator operation.
fn terminator(&self) -> Option<OperationRef<'c, 'a>>;

/// Returns a mutable reference to a terminator operation.
fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, 'a>>;

/// Returns a parent region.
// TODO Store lifetime of regions in blocks, or create another type like
// `InsertedBlockRef`?
fn parent_region(&self) -> Option<RegionRef<'c, 'a>>;

/// Returns a parent operation.
fn parent_operation(&self) -> Option<OperationRef<'c, 'a>>;

/// Adds an argument.
fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'a>;

/// Appends an operation.
fn append_operation(&self, operation: Operation<'c>) -> OperationRef<'c, 'a>;

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

/// Inserts an operation after another.
fn insert_operation_after(
&self,
one: OperationRef<'c, 'a>,
other: Operation<'c>,
) -> OperationRef<'c, 'a>;

/// Inserts an operation before another.
fn insert_operation_before(
&self,
one: OperationRef<'c, 'a>,
other: Operation<'c>,
) -> OperationRef<'c, 'a>;

/// Returns a next block in a region.
fn next_in_region(&self) -> Option<BlockRef<'c, 'a>>;
}
2 changes: 1 addition & 1 deletion melior/src/ir/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ mod tests {
use super::*;
use crate::{
context::Context,
ir::{attribute::StringAttribute, block::BlockApi, Block, Location, Region, Type},
ir::{attribute::StringAttribute, block::BlockLike, Block, Location, Region, Type},
test::create_test_context,
};
use pretty_assertions::assert_eq;
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/operation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl<'c> OperationBuilder<'c> {
mod tests {
use super::*;
use crate::{
ir::{block::BlockApi, Block, ValueLike},
ir::{block::BlockLike, Block, ValueLike},
test::create_test_context,
};

Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/operation/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'c, 'a> TryFrom<Value<'c, 'a>> for OperationResult<'c, 'a> {
#[cfg(test)]
mod tests {
use crate::{
ir::{block::BlockApi, operation::OperationBuilder, Block, Location, Type},
ir::{block::BlockLike, operation::OperationBuilder, Block, Location, Type},
test::create_test_context,
};

Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ mod tests {
use super::*;
use crate::{
ir::{
block::BlockApi, operation::OperationBuilder, Attribute, Block, Identifier, Location,
block::BlockLike, operation::OperationBuilder, Attribute, Block, Identifier, Location,
},
test::create_test_context,
Context,
Expand Down
2 changes: 1 addition & 1 deletion melior/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ mod tests {
dialect::{self, arith, func, scf},
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockApi,
block::BlockLike,
operation::OperationBuilder,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type, Value,
Expand Down
2 changes: 1 addition & 1 deletion melior/src/pass/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod tests {
dialect::func,
ir::{
attribute::{StringAttribute, TypeAttribute},
block::BlockApi,
block::BlockLike,
r#type::FunctionType,
Block, Identifier, Location, Module, Region,
},
Expand Down

0 comments on commit c83aa2b

Please sign in to comment.