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

RegionLike trait #668

Merged
merged 13 commits into from
Jan 28, 2025
Merged
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/region.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utility;

use melior::ir::{Block, Location, Region};
use melior::ir::{Block, Location, Region, RegionLike};
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 @@ -176,7 +176,7 @@ mod tests {
attribute::{StringAttribute, TypeAttribute},
block::BlockLike,
r#type::FunctionType,
Attribute, Block, Location, Module, Region, Type,
Attribute, Block, Location, Module, Region, RegionLike, Type,
},
test::load_all_dialects,
Context,
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 @@ -155,7 +155,7 @@ mod tests {
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
r#type::{FunctionType, IntegerType, Type},
Block, Module, Region,
Block, Module, Region, RegionLike,
},
test::load_all_dialects,
Context,
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::BlockLike, Block, Module, Type},
ir::{block::BlockLike, Block, Module, RegionLike, 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 @@ -77,7 +77,7 @@ mod tests {
attribute::{StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type,
Block, Location, Module, Region, RegionLike, Type,
},
test::load_all_dialects,
Context,
Expand Down
5 changes: 2 additions & 3 deletions melior/src/dialect/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,6 @@ pub fn zext<'c>(

#[cfg(test)]
mod tests {
use tests::r#type::pointer;

use super::*;
use crate::{
dialect::{
Expand All @@ -383,11 +381,12 @@ mod tests {
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Module, Region,
Block, Module, Region, RegionLike,
},
pass::{self, PassManager},
test::create_test_context,
};
use tests::r#type::pointer;

fn convert_module<'c>(context: &'c Context, module: &mut Module<'c>) {
let pass_manager = PassManager::new(context);
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 @@ -326,7 +326,7 @@ mod tests {
attribute::{DenseElementsAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType, RankedTensorType},
Block, Module, Region, Type,
Block, Module, Region, RegionLike, Type,
},
test::create_test_context,
};
Expand Down
3 changes: 1 addition & 2 deletions melior/src/dialect/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,8 @@ mod tests {
dialect,
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type,
Block, BlockLike, Location, Module, Region, RegionLike, Type,
},
pass::{self, PassManager},
test::create_test_context,
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 @@ -116,7 +116,7 @@ mod tests {
attribute::{FloatAttribute, IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
r#type::{FunctionType, IntegerType, Type},
Attribute, Block, Module,
Attribute, Block, Module, RegionLike,
},
test::load_all_dialects,
Context,
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ pub use self::{
module::Module,
operation::{Operation, OperationRef},
r#type::{ShapedTypeLike, Type, TypeLike},
region::{Region, RegionRef},
region::{Region, RegionLike, RegionRef},
value::{Value, ValueLike},
};
2 changes: 1 addition & 1 deletion melior/src/ir/attribute/attribute_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use mlir_sys::{
MlirAttribute,
};

/// Trait for attribute-like types.
/// A trait for attribute-like types.
pub trait AttributeLike<'c> {
/// Converts a attribute into a raw object.
fn to_raw(&self) -> MlirAttribute;
Expand Down
4 changes: 3 additions & 1 deletion melior/src/ir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,9 @@ impl Debug for BlockRef<'_, '_> {
mod tests {
use super::*;
use crate::{
ir::{operation::OperationBuilder, r#type::IntegerType, Module, Region, ValueLike},
ir::{
operation::OperationBuilder, r#type::IntegerType, Module, Region, RegionLike, ValueLike,
},
test::create_test_context,
Error,
};
Expand Down
1 change: 1 addition & 0 deletions melior/src/ir/block/block_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use mlir_sys::{
mlirBlockInsertOwnedOperationBefore, MlirBlock,
};

/// A trait for block-like types.
// TODO Split this trait into `BlockLike` and `BlockLikeMut`.
pub trait BlockLike<'c, 'a>: Copy + Display {
/// Converts a block into a raw object.
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl Drop for Module<'_> {
mod tests {
use super::*;
use crate::{
ir::{attribute::StringAttribute, operation::OperationBuilder, Block, Region},
ir::{attribute::StringAttribute, operation::OperationBuilder, Block, Region, RegionLike},
test::create_test_context,
};

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::BlockLike, Block, Location, Region, Type},
ir::{attribute::StringAttribute, Block, BlockLike, Location, Region, RegionLike, Type},
test::create_test_context,
};
use pretty_assertions::assert_eq;
Expand Down
71 changes: 18 additions & 53 deletions melior/src/ir/region.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use super::{Block, BlockRef};
use mlir_sys::{
mlirRegionAppendOwnedBlock, mlirRegionCreate, mlirRegionDestroy, mlirRegionEqual,
mlirRegionGetFirstBlock, mlirRegionInsertOwnedBlockAfter, mlirRegionInsertOwnedBlockBefore,
MlirRegion,
};
mod region_like;

pub use self::region_like::RegionLike;
use super::Block;
use mlir_sys::{mlirRegionCreate, mlirRegionDestroy, mlirRegionEqual, MlirRegion};
use std::{
marker::PhantomData,
mem::{forget, transmute},
Expand All @@ -17,7 +16,7 @@ pub struct Region<'c> {
_block: PhantomData<Block<'c>>,
}

impl<'c> Region<'c> {
impl Region<'_> {
/// Creates a region.
pub fn new() -> Self {
Self {
Expand All @@ -26,52 +25,6 @@ impl<'c> Region<'c> {
}
}

/// Returns the first block in a region.
pub fn first_block(&self) -> Option<BlockRef<'c, '_>> {
unsafe {
let block = mlirRegionGetFirstBlock(self.raw);

if block.ptr.is_null() {
None
} else {
Some(BlockRef::from_raw(block))
}
}
}

/// Inserts a block after another block.
pub fn insert_block_after(&self, one: BlockRef<'c, '_>, other: Block<'c>) -> BlockRef<'c, '_> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());

mlirRegionInsertOwnedBlockAfter(self.raw, one.to_raw(), other.into_raw());

r#ref
}
}

/// Inserts a block before another block.
pub fn insert_block_before(&self, one: BlockRef<'c, '_>, other: Block<'c>) -> BlockRef<'c, '_> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());

mlirRegionInsertOwnedBlockBefore(self.raw, one.to_raw(), other.into_raw());

r#ref
}
}

/// Appends a block.
pub fn append_block(&self, block: Block<'c>) -> BlockRef<'c, '_> {
unsafe {
let r#ref = BlockRef::from_raw(block.to_raw());

mlirRegionAppendOwnedBlock(self.raw, block.into_raw());

r#ref
}
}

/// Converts a region into a raw object.
pub const fn into_raw(self) -> mlir_sys::MlirRegion {
let region = self.raw;
Expand All @@ -82,6 +35,12 @@ impl<'c> Region<'c> {
}
}

impl<'c> RegionLike<'c, '_> for &Region<'c> {
fn to_raw(self) -> MlirRegion {
self.raw
}
}

impl Default for Region<'_> {
fn default() -> Self {
Self::new()
Expand Down Expand Up @@ -136,6 +95,12 @@ impl RegionRef<'_, '_> {
}
}

impl<'c, 'a> RegionLike<'c, 'a> for RegionRef<'c, 'a> {
fn to_raw(self) -> MlirRegion {
self.raw
}
}

impl<'c> Deref for RegionRef<'c, '_> {
type Target = Region<'c>;

Expand Down
57 changes: 57 additions & 0 deletions melior/src/ir/region/region_like.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::ir::{Block, BlockRef};
use mlir_sys::{
mlirRegionAppendOwnedBlock, mlirRegionGetFirstBlock, mlirRegionInsertOwnedBlockAfter,
mlirRegionInsertOwnedBlockBefore, MlirRegion,
};

/// A region-like trait.
pub trait RegionLike<'c, 'a>: Copy {
/// Converts a region into a raw object.
fn to_raw(self) -> MlirRegion;

/// Returns the first block in a region.
fn first_block(self) -> Option<BlockRef<'c, 'a>> {
unsafe {
let block = mlirRegionGetFirstBlock(self.to_raw());

if block.ptr.is_null() {
None
} else {
Some(BlockRef::from_raw(block))
}
}
}

/// Inserts a block after another block.
fn insert_block_after(self, one: BlockRef<'c, 'a>, other: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());

mlirRegionInsertOwnedBlockAfter(self.to_raw(), one.to_raw(), other.into_raw());

r#ref
}
}

/// Inserts a block before another block.
fn insert_block_before(self, one: BlockRef<'c, 'a>, other: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(other.to_raw());

mlirRegionInsertOwnedBlockBefore(self.to_raw(), one.to_raw(), other.into_raw());

r#ref
}
}

/// Appends a block.
fn append_block(self, block: Block<'c>) -> BlockRef<'c, 'a> {
unsafe {
let r#ref = BlockRef::from_raw(block.to_raw());

mlirRegionAppendOwnedBlock(self.to_raw(), block.into_raw());

r#ref
}
}
}
2 changes: 1 addition & 1 deletion melior/src/ir/type/type_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::TypeId;
use crate::{context::ContextRef, dialect::Dialect};
use mlir_sys::{mlirTypeDump, mlirTypeGetContext, mlirTypeGetDialect, mlirTypeGetTypeID, MlirType};

/// Trait for type-like types.
/// A trait for type-like types.
pub trait TypeLike<'c> {
/// Converts a type into a raw object.
fn to_raw(&self) -> MlirType;
Expand Down
2 changes: 1 addition & 1 deletion melior/src/ir/value/value_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use mlir_sys::{
mlirValueDump, mlirValueGetType, mlirValueIsABlockArgument, mlirValueIsAOpResult, MlirValue,
};

/// Trait for value-like types.
/// A trait for value-like types.
pub trait ValueLike<'c> {
/// Converts a value into a raw value.
fn to_raw(&self) -> MlirValue;
Expand Down
3 changes: 1 addition & 2 deletions melior/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ mod tests {
dialect::{self, arith, func, scf},
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
block::BlockLike,
operation::OperationBuilder,
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type, Value,
Block, BlockLike, Location, Module, Region, RegionLike, Type, Value,
},
test::load_all_dialects,
};
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 @@ -218,7 +218,7 @@ mod tests {
attribute::{StringAttribute, TypeAttribute},
block::BlockLike,
r#type::FunctionType,
Block, Identifier, Location, Module, Region,
Block, Identifier, Location, Module, Region, RegionLike,
},
pass::PassManager,
test::create_test_context,
Expand Down
Loading