From 8b5c4a9dc092ed1c8a6ecfd73cb5c2d45ca0903d Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:30:28 +0900 Subject: [PATCH] Test `zero` operation (#536) --- melior/src/dialect/llvm.rs | 84 ++++++++++++++++++- ...ialect__llvm__tests__compile_null_ptr.snap | 10 +++ ...r__dialect__llvm__tests__compile_zero.snap | 10 +++ 3 files changed, 101 insertions(+), 3 deletions(-) create mode 100644 melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_null_ptr.snap create mode 100644 melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_zero.snap diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index daea704533..0a61f34735 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -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) @@ -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(); diff --git a/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_null_ptr.snap b/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_null_ptr.snap new file mode 100644 index 0000000000..e10974f1dc --- /dev/null +++ b/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_null_ptr.snap @@ -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 + } +} diff --git a/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_zero.snap b/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_zero.snap new file mode 100644 index 0000000000..7b156261c1 --- /dev/null +++ b/melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_zero.snap @@ -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 + } +}