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 a75d47d commit 1809a5e
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 64 deletions.
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::BlockApi, Block, Location, Type};
use melior::ir::{block::BlockLike, Block, Location, Type};
use utility::*;

melior_macro::dialect! {
Expand Down
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
99 changes: 50 additions & 49 deletions melior/src/ir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,54 @@ use std::{
ops::Deref,
};

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

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

/// Returns a reference to the first operation.
fn first_operation(&self) -> Option<OperationRef<'c, 'v>>;
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, 'v>>;
fn first_operation_mut(self) -> Option<OperationRefMut<'c, 'a>>;

/// Returns a reference to a terminator operation.
fn terminator(&self) -> Option<OperationRef<'c, 'v>>;
fn terminator(self) -> Option<OperationRef<'c, 'a>>;
/// Returns a mutable reference to a terminator operation.
fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, 'v>>;
fn terminator_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, 'v>>;
fn parent_region(self) -> Option<RegionRef<'c, 'a>>;
/// Returns a parent operation.
fn parent_operation(&self) -> Option<OperationRef<'c, 'v>>;
fn parent_operation(self) -> Option<OperationRef<'c, 'a>>;

/// Adds an argument.
fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v>;
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, 'v>;
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, 'v>;
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, 'v>,
self,
one: OperationRef<'c, 'a>,
other: Operation<'c>,
) -> OperationRef<'c, 'v>;
) -> OperationRef<'c, 'a>;
/// Inserts an operation before another.
fn insert_operation_before(
&self,
one: OperationRef<'c, 'v>,
self,
one: OperationRef<'c, 'a>,
other: Operation<'c>,
) -> OperationRef<'c, 'v>;
) -> OperationRef<'c, 'a>;

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

/// A block.
Expand Down Expand Up @@ -143,8 +144,8 @@ impl<'c> Block<'c> {
}
}

impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'v>, Error> {
impl<'c, 'v> BlockLike<'c, 'v> for &'v Block<'c> {
fn argument(self, index: usize) -> Result<BlockArgument<'c, 'v>, Error> {
unsafe {
if index < self.argument_count() {
Ok(BlockArgument::from_raw(mlirBlockGetArgument(
Expand All @@ -161,35 +162,35 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}
}

fn argument_count(&self) -> usize {
fn argument_count(self) -> usize {
unsafe { mlirBlockGetNumArguments(self.raw) as usize }
}

fn first_operation(&self) -> Option<OperationRef<'c, 'v>> {
fn first_operation(self) -> Option<OperationRef<'c, 'v>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetFirstOperation(self.raw)) }
}

fn first_operation_mut(&mut self) -> Option<OperationRefMut<'c, 'v>> {
fn first_operation_mut(self) -> Option<OperationRefMut<'c, 'v>> {
unsafe { OperationRefMut::from_option_raw(mlirBlockGetFirstOperation(self.raw)) }
}

fn terminator(&self) -> Option<OperationRef<'c, 'v>> {
fn terminator(self) -> Option<OperationRef<'c, 'v>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetTerminator(self.raw)) }
}

fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, 'v>> {
fn terminator_mut(self) -> Option<OperationRefMut<'c, 'v>> {
unsafe { OperationRefMut::from_option_raw(mlirBlockGetTerminator(self.raw)) }
}

fn parent_region(&self) -> Option<RegionRef<'c, 'v>> {
fn parent_region(self) -> Option<RegionRef<'c, 'v>> {
unsafe { RegionRef::from_option_raw(mlirBlockGetParentRegion(self.raw)) }
}

fn parent_operation(&self) -> Option<OperationRef<'c, 'v>> {
fn parent_operation(self) -> Option<OperationRef<'c, 'v>> {
unsafe { OperationRef::from_option_raw(mlirBlockGetParentOperation(self.raw)) }
}

fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v> {
fn add_argument(self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v> {
unsafe {
Value::from_raw(mlirBlockAddArgument(
self.raw,
Expand All @@ -199,7 +200,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}
}

fn append_operation(&self, operation: Operation<'c>) -> OperationRef<'c, 'v> {
fn append_operation(self, operation: Operation<'c>) -> OperationRef<'c, 'v> {
unsafe {
let operation = operation.into_raw();

Expand All @@ -209,7 +210,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}
}

fn insert_operation(&self, position: usize, operation: Operation<'c>) -> OperationRef<'c, 'v> {
fn insert_operation(self, position: usize, operation: Operation<'c>) -> OperationRef<'c, 'v> {
unsafe {
let operation = operation.into_raw();

Expand All @@ -220,7 +221,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}

fn insert_operation_after(
&self,
self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v> {
Expand All @@ -234,7 +235,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}

fn insert_operation_before(
&self,
self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v> {
Expand All @@ -247,7 +248,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for &'v Block<'c> {
}
}

fn next_in_region(&self) -> Option<BlockRef<'c, 'v>> {
fn next_in_region(self) -> Option<BlockRef<'c, 'v>> {
unsafe { BlockRef::from_option_raw(mlirBlockGetNextInRegion(self.raw)) }
}
}
Expand Down Expand Up @@ -324,86 +325,86 @@ impl BlockRef<'_, '_> {
}
}

impl<'c, 'v> BlockApi<'c, 'v> for BlockRef<'c, 'v> {
fn argument(&self, index: usize) -> Result<BlockArgument<'c, 'v>, Error> {
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);
Block::into_raw(block);
result
}

fn argument_count(&self) -> usize {
fn argument_count(self) -> usize {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.argument_count();
Block::into_raw(block);
result
}

fn first_operation(&self) -> Option<OperationRef<'c, 'v>> {
fn first_operation(self) -> Option<OperationRef<'c, 'v>> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.first_operation();
Block::into_raw(block);
result
}

fn first_operation_mut(&mut self) -> Option<OperationRefMut<'c, 'v>> {
fn first_operation_mut(self) -> Option<OperationRefMut<'c, 'v>> {
let mut block = unsafe { Block::from_raw(self.raw) };
let result = block.first_operation_mut();
Block::into_raw(block);
result
}

fn terminator(&self) -> Option<OperationRef<'c, 'v>> {
fn terminator(self) -> Option<OperationRef<'c, 'v>> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.terminator();
Block::into_raw(block);
result
}

fn terminator_mut(&mut self) -> Option<OperationRefMut<'c, 'v>> {
fn terminator_mut(self) -> Option<OperationRefMut<'c, 'v>> {
let mut block = unsafe { Block::from_raw(self.raw) };
let result = block.terminator_mut();
Block::into_raw(block);
result
}

fn parent_region(&self) -> Option<RegionRef<'c, 'v>> {
fn parent_region(self) -> Option<RegionRef<'c, 'v>> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.parent_region();
Block::into_raw(block);
result
}

fn parent_operation(&self) -> Option<OperationRef<'c, 'v>> {
fn parent_operation(self) -> Option<OperationRef<'c, 'v>> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.parent_operation();
Block::into_raw(block);
result
}

fn add_argument(&self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v> {
fn add_argument(self, r#type: Type<'c>, location: Location<'c>) -> Value<'c, 'v> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.add_argument(r#type, location);
Block::into_raw(block);
result
}

fn append_operation(&self, operation: Operation<'c>) -> OperationRef<'c, 'v> {
fn append_operation(self, operation: Operation<'c>) -> OperationRef<'c, 'v> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.append_operation(operation);
Block::into_raw(block);
result
}

fn insert_operation(&self, position: usize, operation: Operation<'c>) -> OperationRef<'c, 'v> {
fn insert_operation(self, position: usize, operation: Operation<'c>) -> OperationRef<'c, 'v> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.insert_operation(position, operation);
Block::into_raw(block);
result
}

fn insert_operation_after(
&self,
self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v> {
Expand All @@ -414,7 +415,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for BlockRef<'c, 'v> {
}

fn insert_operation_before(
&self,
self,
one: OperationRef<'c, 'v>,
other: Operation<'c>,
) -> OperationRef<'c, 'v> {
Expand All @@ -424,7 +425,7 @@ impl<'c, 'v> BlockApi<'c, 'v> for BlockRef<'c, 'v> {
result
}

fn next_in_region(&self) -> Option<BlockRef<'c, 'v>> {
fn next_in_region(self) -> Option<BlockRef<'c, 'v>> {
let block = unsafe { Block::from_raw(self.raw) };
let result = block.next_in_region();
Block::into_raw(block);
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
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
Loading

0 comments on commit 1809a5e

Please sign in to comment.