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

feat: add location method for the operation structure #580

Merged
merged 1 commit into from
Aug 25, 2024
Merged
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
33 changes: 26 additions & 7 deletions melior/src/ir/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod result;
pub use self::{
builder::OperationBuilder, printing_flags::OperationPrintingFlags, result::OperationResult,
};
use super::{Attribute, AttributeLike, BlockRef, Identifier, RegionRef, Value};
use super::{Attribute, AttributeLike, BlockRef, Identifier, Location, RegionRef, Value};
use crate::{
context::{Context, ContextRef},
utility::{print_callback, print_string_callback},
Expand All @@ -20,12 +20,12 @@ use core::{
use mlir_sys::{
mlirOperationClone, mlirOperationDestroy, mlirOperationDump, mlirOperationEqual,
mlirOperationGetAttribute, mlirOperationGetAttributeByName, mlirOperationGetBlock,
mlirOperationGetContext, mlirOperationGetName, mlirOperationGetNextInBlock,
mlirOperationGetNumAttributes, mlirOperationGetNumOperands, mlirOperationGetNumRegions,
mlirOperationGetNumResults, mlirOperationGetNumSuccessors, mlirOperationGetOperand,
mlirOperationGetParentOperation, mlirOperationGetRegion, mlirOperationGetResult,
mlirOperationGetSuccessor, mlirOperationPrint, mlirOperationPrintWithFlags,
mlirOperationRemoveAttributeByName, mlirOperationRemoveFromParent,
mlirOperationGetContext, mlirOperationGetLocation, mlirOperationGetName,
mlirOperationGetNextInBlock, mlirOperationGetNumAttributes, mlirOperationGetNumOperands,
mlirOperationGetNumRegions, mlirOperationGetNumResults, mlirOperationGetNumSuccessors,
mlirOperationGetOperand, mlirOperationGetParentOperation, mlirOperationGetRegion,
mlirOperationGetResult, mlirOperationGetSuccessor, mlirOperationPrint,
mlirOperationPrintWithFlags, mlirOperationRemoveAttributeByName, mlirOperationRemoveFromParent,
mlirOperationSetAttributeByName, mlirOperationVerify, MlirOperation,
};
use std::{
Expand Down Expand Up @@ -130,6 +130,11 @@ impl<'c> Operation<'c> {
(0..self.region_count()).map(|index| self.region(index).expect("valid result index"))
}

/// Gets the location of the operation.
pub fn location(&self) -> Location<'c> {
unsafe { Location::from_raw(mlirOperationGetLocation(self.raw)) }
}

/// Returns the number of successors.
pub fn successor_count(&self) -> usize {
unsafe { mlirOperationGetNumSuccessors(self.raw) as usize }
Expand Down Expand Up @@ -644,6 +649,20 @@ mod tests {
);
}

#[test]
fn location() {
let context = create_test_context();
context.set_allow_unregistered_dialects(true);
let location = Location::new(&context, "test", 1, 1);

let operation = OperationBuilder::new("foo", location)
.add_regions([Region::new()])
.build()
.unwrap();

assert_eq!(operation.location(), location);
}

#[test]
fn attribute() {
let context = create_test_context();
Expand Down