From c83aa2b10570726f058803d65e96fcbd3ca0f8bc Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Tue, 10 Dec 2024 17:00:12 +0900 Subject: [PATCH] Rename `BlockLike` trait --- melior/src/dialect/arith.rs | 2 +- melior/src/dialect/cf.rs | 2 +- melior/src/dialect/func.rs | 2 +- melior/src/dialect/index.rs | 2 +- melior/src/dialect/llvm.rs | 2 +- melior/src/dialect/memref.rs | 2 +- melior/src/dialect/ods.rs | 2 +- melior/src/dialect/scf.rs | 2 +- melior/src/helpers/builtin.rs | 2 +- melior/src/helpers/llvm.rs | 2 +- melior/src/ir.rs | 2 +- melior/src/ir/block.rs | 55 ++------------------------ melior/src/ir/block/argument.rs | 2 +- melior/src/ir/block/block_like.rs | 62 ++++++++++++++++++++++++++++++ melior/src/ir/operation.rs | 2 +- melior/src/ir/operation/builder.rs | 2 +- melior/src/ir/operation/result.rs | 2 +- melior/src/ir/value.rs | 2 +- melior/src/lib.rs | 2 +- melior/src/pass/external.rs | 2 +- 20 files changed, 84 insertions(+), 69 deletions(-) create mode 100644 melior/src/ir/block/block_like.rs diff --git a/melior/src/dialect/arith.rs b/melior/src/dialect/arith.rs index deac34b89c..c55b888eb1 100644 --- a/melior/src/dialect/arith.rs +++ b/melior/src/dialect/arith.rs @@ -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, }, diff --git a/melior/src/dialect/cf.rs b/melior/src/dialect/cf.rs index e4d39336c0..03b6b06b80 100644 --- a/melior/src/dialect/cf.rs +++ b/melior/src/dialect/cf.rs @@ -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, diff --git a/melior/src/dialect/func.rs b/melior/src/dialect/func.rs index 7c5c26d4c2..97d45d3a5f 100644 --- a/melior/src/dialect/func.rs +++ b/melior/src/dialect/func.rs @@ -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, }; diff --git a/melior/src/dialect/index.rs b/melior/src/dialect/index.rs index 2d6e8c0333..88994129d2 100644 --- a/melior/src/dialect/index.rs +++ b/melior/src/dialect/index.rs @@ -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, }, diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index eef4f9b649..fff8c120a0 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -381,7 +381,7 @@ mod tests { }, ir::{ attribute::{IntegerAttribute, StringAttribute, TypeAttribute}, - block::BlockApi, + block::BlockLike, r#type::{FunctionType, IntegerType}, Block, Module, Region, }, diff --git a/melior/src/dialect/memref.rs b/melior/src/dialect/memref.rs index c710e77c48..a0e1eeaa8f 100644 --- a/melior/src/dialect/memref.rs +++ b/melior/src/dialect/memref.rs @@ -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, }, diff --git a/melior/src/dialect/ods.rs b/melior/src/dialect/ods.rs index cc0c64f18b..05d741071d 100644 --- a/melior/src/dialect/ods.rs +++ b/melior/src/dialect/ods.rs @@ -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, }, diff --git a/melior/src/dialect/scf.rs b/melior/src/dialect/scf.rs index c8e62a8bc3..7744250daf 100644 --- a/melior/src/dialect/scf.rs +++ b/melior/src/dialect/scf.rs @@ -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, }, diff --git a/melior/src/helpers/builtin.rs b/melior/src/helpers/builtin.rs index d2dc259883..717b0f7d6e 100644 --- a/melior/src/helpers/builtin.rs +++ b/melior/src/helpers/builtin.rs @@ -1,5 +1,5 @@ use crate::{ - ir::{block::BlockApi, Block, Operation, Value}, + ir::{block::BlockLike, Block, Operation, Value}, Error, }; diff --git a/melior/src/helpers/llvm.rs b/melior/src/helpers/llvm.rs index 61f5971a47..5b32c50381 100644 --- a/melior/src/helpers/llvm.rs +++ b/melior/src/helpers/llvm.rs @@ -6,7 +6,7 @@ use crate::{ attribute::{ DenseI32ArrayAttribute, DenseI64ArrayAttribute, IntegerAttribute, TypeAttribute, }, - block::BlockApi, + block::BlockLike, r#type::IntegerType, Attribute, Block, Location, Type, Value, ValueLike, }, diff --git a/melior/src/ir.rs b/melior/src/ir.rs index 77f1d8dffe..7da7c8ad44 100644 --- a/melior/src/ir.rs +++ b/melior/src/ir.rs @@ -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, diff --git a/melior/src/ir/block.rs b/melior/src/ir/block.rs index a902960f70..770621e3c8 100644 --- a/melior/src/ir/block.rs +++ b/melior/src/ir/block.rs @@ -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, }; @@ -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, Error>; - /// Returns a number of arguments. - fn argument_count(&self) -> usize; - - /// Returns a reference to the first operation. - fn first_operation(&self) -> Option>; - /// Returns a mutable reference to the first operation. - fn first_operation_mut(&mut self) -> Option>; - - /// Returns a reference to a terminator operation. - fn terminator(&self) -> Option>; - /// Returns a mutable reference to a terminator operation. - fn terminator_mut(&mut self) -> Option>; - - /// Returns a parent region. - // TODO Store lifetime of regions in blocks, or create another type like - // `InsertedBlockRef`? - fn parent_region(&self) -> Option>; - /// Returns a parent operation. - fn parent_operation(&self) -> Option>; - - /// 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>; -} - /// A block. pub struct Block<'c> { raw: MlirBlock, @@ -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, Error> { unsafe { if index < self.argument_count() { @@ -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, Error> { let block = unsafe { Block::from_raw(self.raw) }; let result = block.argument(index); diff --git a/melior/src/ir/block/argument.rs b/melior/src/ir/block/argument.rs index 4218c79a2c..fe175bb62d 100644 --- a/melior/src/ir/block/argument.rs +++ b/melior/src/ir/block/argument.rs @@ -71,7 +71,7 @@ mod tests { use super::*; use crate::{ context::Context, - ir::{block::BlockApi, Block, Location}, + ir::{block::BlockLike, Block, Location}, }; #[test] diff --git a/melior/src/ir/block/block_like.rs b/melior/src/ir/block/block_like.rs new file mode 100644 index 0000000000..d478467ff1 --- /dev/null +++ b/melior/src/ir/block/block_like.rs @@ -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, Error>; + + /// Returns a number of arguments. + fn argument_count(&self) -> usize; + + /// Returns a reference to the first operation. + fn first_operation(&self) -> Option>; + /// Returns a mutable reference to the first operation. + fn first_operation_mut(&mut self) -> Option>; + + /// Returns a reference to a terminator operation. + fn terminator(&self) -> Option>; + + /// Returns a mutable reference to a terminator operation. + fn terminator_mut(&mut self) -> Option>; + + /// Returns a parent region. + // TODO Store lifetime of regions in blocks, or create another type like + // `InsertedBlockRef`? + fn parent_region(&self) -> Option>; + + /// Returns a parent operation. + fn parent_operation(&self) -> Option>; + + /// 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>; +} diff --git a/melior/src/ir/operation.rs b/melior/src/ir/operation.rs index 02f70536e0..09a28159a9 100644 --- a/melior/src/ir/operation.rs +++ b/melior/src/ir/operation.rs @@ -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; diff --git a/melior/src/ir/operation/builder.rs b/melior/src/ir/operation/builder.rs index 0429bafb1a..efacc898b4 100644 --- a/melior/src/ir/operation/builder.rs +++ b/melior/src/ir/operation/builder.rs @@ -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, }; diff --git a/melior/src/ir/operation/result.rs b/melior/src/ir/operation/result.rs index e92a2e5b0b..c3eec9bd25 100644 --- a/melior/src/ir/operation/result.rs +++ b/melior/src/ir/operation/result.rs @@ -61,7 +61,7 @@ impl<'c, 'a> TryFrom> 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, }; diff --git a/melior/src/ir/value.rs b/melior/src/ir/value.rs index 444e1c71a5..c4291bfae8 100644 --- a/melior/src/ir/value.rs +++ b/melior/src/ir/value.rs @@ -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, diff --git a/melior/src/lib.rs b/melior/src/lib.rs index c4e2ccf03d..9b9d2e0ace 100644 --- a/melior/src/lib.rs +++ b/melior/src/lib.rs @@ -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, diff --git a/melior/src/pass/external.rs b/melior/src/pass/external.rs index 218292d878..b01f7645fb 100644 --- a/melior/src/pass/external.rs +++ b/melior/src/pass/external.rs @@ -216,7 +216,7 @@ mod tests { dialect::func, ir::{ attribute::{StringAttribute, TypeAttribute}, - block::BlockApi, + block::BlockLike, r#type::FunctionType, Block, Identifier, Location, Module, Region, },