From 4a0dfe775f5be2be82095e869ffdd1a3164ac7d6 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:18:41 +0900 Subject: [PATCH 1/7] Add zero function --- melior/src/dialect/llvm.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index daea704533..0af6325bfa 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -123,14 +123,19 @@ 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. +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) From 97c8ae8b3c0bcf6ef87fb9f1eb79d1ad53719cce Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:20:45 +0900 Subject: [PATCH 2/7] Fix --- melior/src/dialect/llvm.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index 0af6325bfa..fc9c05060b 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -586,6 +586,39 @@ 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)], 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(); From 9656c15aaefc05518f7e3bb2c44cbc205eeceb7f Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:21:07 +0900 Subject: [PATCH 3/7] Fix --- melior/src/dialect/llvm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index fc9c05060b..09f327a010 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -603,7 +603,7 @@ mod tests { let operation = block.append_operation(zero(integer_type, location)); - block.append_operation(func::r#return(&[operation.result(0)], location)); + block.append_operation(func::r#return(&[operation.result(0).unwrap()], location)); let region = Region::new(); region.append_block(block); From 1f41f57baf5a5e1b29117eb1fcb5ebef8c8e3c6a Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:21:44 +0900 Subject: [PATCH 4/7] Fix --- melior/src/dialect/llvm.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index 09f327a010..9426f525b7 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -603,7 +603,10 @@ mod tests { let operation = block.append_operation(zero(integer_type, location)); - block.append_operation(func::r#return(&[operation.result(0).unwrap()], location)); + block.append_operation(func::r#return( + &[operation.result(0).unwrap().into()], + location, + )); let region = Region::new(); region.append_block(block); From 579bc8d8d7f8225fe8cf85c73cb55d70ae6fae6f Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:24:59 +0900 Subject: [PATCH 5/7] Fix --- melior/src/dialect/llvm.rs | 36 +++++++++++++++++++ ...r__dialect__llvm__tests__compile_zero.snap | 10 ++++++ 2 files changed, 46 insertions(+) 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 9426f525b7..e8dbffcd22 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -622,6 +622,42 @@ mod tests { 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_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 + } +} From 5e9c21c9003c25cb0cb859e212f4278da0c49349 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:25:15 +0900 Subject: [PATCH 6/7] Fix --- ...melior__dialect__llvm__tests__compile_null_ptr.snap | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 melior/src/dialect/snapshots/melior__dialect__llvm__tests__compile_null_ptr.snap 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 + } +} From 3e0a0230580fd17088b271c87f16ac46bc7da7e8 Mon Sep 17 00:00:00 2001 From: Yota Toyama Date: Sat, 4 May 2024 17:25:46 +0900 Subject: [PATCH 7/7] Fix --- melior/src/dialect/llvm.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/melior/src/dialect/llvm.rs b/melior/src/dialect/llvm.rs index e8dbffcd22..0a61f34735 100644 --- a/melior/src/dialect/llvm.rs +++ b/melior/src/dialect/llvm.rs @@ -132,6 +132,7 @@ pub fn zero<'c>(r#type: Type<'c>, location: Location<'c>) -> Operation<'c> { } /// Creates a null pointer. +#[deprecated] pub fn nullptr<'c>(ptr_type: Type<'c>, location: Location<'c>) -> Operation<'c> { zero(ptr_type, location) }