Skip to content

Commit

Permalink
Test zero operation (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe authored May 4, 2024
1 parent 8614ccd commit 8b5c4a9
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 3 deletions.
84 changes: 81 additions & 3 deletions melior/src/dialect/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,20 @@ pub fn poison<'c>(result_type: Type<'c>, location: Location<'c>) -> Operation<'c
.expect("valid operation")
}

/// Creates a null pointer.
pub fn nullptr<'c>(ptr_type: Type<'c>, location: Location<'c>) -> Operation<'c> {
/// Creates a zero value.
pub fn zero<'c>(r#type: Type<'c>, location: Location<'c>) -> Operation<'c> {
OperationBuilder::new("llvm.mlir.zero", location)
.add_results(&[ptr_type])
.add_results(&[r#type])
.build()
.expect("valid operation")
}

/// Creates a null pointer.
#[deprecated]
pub fn nullptr<'c>(ptr_type: Type<'c>, location: Location<'c>) -> Operation<'c> {
zero(ptr_type, location)
}

/// Creates a `llvm.unreachable` operation.
pub fn unreachable(location: Location) -> Operation {
OperationBuilder::new("llvm.unreachable", location)
Expand Down Expand Up @@ -581,6 +587,78 @@ mod tests {
insta::assert_snapshot!(module.as_operation());
}

#[test]
fn compile_zero() {
let context = create_test_context();

let location = Location::unknown(&context);
let mut module = Module::new(location);
let integer_type = IntegerType::new(&context, 64).into();

module.body().append_operation(func::func(
&context,
StringAttribute::new(&context, "foo"),
TypeAttribute::new(FunctionType::new(&context, &[], &[integer_type]).into()),
{
let block = Block::new(&[]);

let operation = block.append_operation(zero(integer_type, location));

block.append_operation(func::r#return(
&[operation.result(0).unwrap().into()],
location,
));

let region = Region::new();
region.append_block(block);
region
},
&[],
location,
));

convert_module(&context, &mut module);

assert!(module.as_operation().verify());
insta::assert_snapshot!(module.as_operation());
}

#[test]
fn compile_null_ptr() {
let context = create_test_context();

let location = Location::unknown(&context);
let mut module = Module::new(location);
let ptr_type = r#type::pointer(&context, 0);

module.body().append_operation(func::func(
&context,
StringAttribute::new(&context, "foo"),
TypeAttribute::new(FunctionType::new(&context, &[], &[ptr_type]).into()),
{
let block = Block::new(&[]);

let operation = block.append_operation(zero(ptr_type, location));

block.append_operation(func::r#return(
&[operation.result(0).unwrap().into()],
location,
));

let region = Region::new();
region.append_block(block);
region
},
&[],
location,
));

convert_module(&context, &mut module);

assert!(module.as_operation().verify());
insta::assert_snapshot!(module.as_operation());
}

#[test]
fn compile_undefined() {
let context = create_test_context();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @foo() -> !llvm.ptr {
%0 = llvm.mlir.zero : !llvm.ptr
llvm.return %0 : !llvm.ptr
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: melior/src/dialect/llvm.rs
expression: module.as_operation()
---
module {
llvm.func @foo() -> i64 {
%0 = llvm.mlir.zero : i64
llvm.return %0 : i64
}
}

0 comments on commit 8b5c4a9

Please sign in to comment.