-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
164 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use crate::ir::{operation::Builder, Location, Operation, Value}; | ||
|
||
pub fn addi<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.addi", lhs, rhs, location) | ||
} | ||
|
||
pub fn subi<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.subi", lhs, rhs, location) | ||
} | ||
|
||
pub fn muli<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.muli", lhs, rhs, location) | ||
} | ||
|
||
pub fn divi<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.divi", lhs, rhs, location) | ||
} | ||
|
||
pub fn addf<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.addf", lhs, rhs, location) | ||
} | ||
|
||
pub fn subf<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.subf", lhs, rhs, location) | ||
} | ||
|
||
pub fn mulf<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.mulf", lhs, rhs, location) | ||
} | ||
|
||
pub fn divf<'c>(lhs: Value, rhs: Value, location: Location<'c>) -> Operation<'c> { | ||
binary_operator("arith.divf", lhs, rhs, location) | ||
} | ||
|
||
fn binary_operator<'c>( | ||
name: &str, | ||
lhs: Value, | ||
rhs: Value, | ||
location: Location<'c>, | ||
) -> Operation<'c> { | ||
Builder::new(name, location) | ||
.add_operands(&[lhs, rhs]) | ||
.enable_result_type_inference() | ||
.build() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
use crate::{ | ||
ir::{operation::Builder, Attribute, Identifier, Location, Operation, Region, Value}, | ||
Context, | ||
}; | ||
|
||
pub fn func<'c>( | ||
context: &'c Context, | ||
name: Attribute<'c>, | ||
r#type: Attribute<'c>, | ||
region: Region, | ||
location: Location<'c>, | ||
) -> Operation<'c> { | ||
Builder::new("func.func", location) | ||
.add_attributes(&[ | ||
(Identifier::new(context, "sym_name"), name), | ||
(Identifier::new(context, "function_type"), r#type), | ||
]) | ||
.add_regions(vec![region]) | ||
.build() | ||
} | ||
|
||
pub fn r#return<'c>(operands: &[Value], location: Location<'c>) -> Operation<'c> { | ||
Builder::new("func.return", location) | ||
.add_operands(operands) | ||
.build() | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use crate::{ | ||
dialect::arith::addi, | ||
ir::{Attribute, Block, Module, Type}, | ||
test::load_all_dialects, | ||
Context, | ||
}; | ||
|
||
#[test] | ||
fn run_on_function_in_nested_module() { | ||
let context = Context::new(); | ||
load_all_dialects(&context); | ||
|
||
let location = Location::unknown(&context); | ||
let module = Module::new(location); | ||
|
||
let integer_type = Type::integer(&context, 64); | ||
|
||
let function = { | ||
let region = Region::new(); | ||
let block = Block::new(&[(integer_type, location), (integer_type, location)]); | ||
|
||
let sum = block.append_operation(addi( | ||
block.argument(0).unwrap().into(), | ||
block.argument(1).unwrap().into(), | ||
location, | ||
)); | ||
|
||
block.append_operation(r#return(&[sum.result(0).unwrap().into()], location)); | ||
|
||
region.append_block(block); | ||
|
||
func( | ||
&context, | ||
Attribute::parse(&context, "\"add\"").unwrap(), | ||
Attribute::parse(&context, "(i64, i64) -> i64").unwrap(), | ||
region, | ||
Location::unknown(&context), | ||
) | ||
}; | ||
|
||
module.body().append_operation(function); | ||
|
||
assert!(module.as_operation().verify()); | ||
insta::assert_display_snapshot!(module.as_operation()); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/dialect/snapshots/melior__dialect__func__tests__run_on_function_in_nested_module.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
--- | ||
source: src/dialect/func.rs | ||
expression: module.as_operation() | ||
--- | ||
module { | ||
func.func @add(%arg0: i64, %arg1: i64) -> i64 { | ||
%0 = arith.addi %arg0, %arg1 : i64 | ||
return %0 : i64 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::{dialect::Registry, utility::register_all_dialects, Context}; | ||
|
||
pub fn load_all_dialects(context: &Context) { | ||
let registry = Registry::new(); | ||
register_all_dialects(®istry); | ||
context.append_dialect_registry(®istry); | ||
context.load_all_available_dialects(); | ||
} |