Skip to content

Commit

Permalink
add segfaulting ods test (#335)
Browse files Browse the repository at this point in the history
this is about #334 to give you a easy branch to check that this indeed
segfaults, not meant to be merged if u dont want

feel free to push here too if needed

---------

Co-authored-by: Yota Toyama <[email protected]>
  • Loading branch information
edg-l and raviqqe authored Sep 30, 2023
1 parent b0c2c17 commit dfa3036
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 3 deletions.
6 changes: 3 additions & 3 deletions macro/src/dialect/operation/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'o> OperationBuilder<'o> {

quote! {
&[(
::melior::ir::Identifier::new(self.context, #name_string),
::melior::ir::Identifier::new(unsafe { self.context.to_ref() }, #name_string),
#name.into(),
)]
}
Expand Down Expand Up @@ -142,7 +142,7 @@ impl<'o> OperationBuilder<'o> {
#[doc = #doc]
pub struct #builder_ident <'c, #(#iter_arguments),* > {
builder: ::melior::ir::operation::OperationBuilder<'c>,
context: &'c ::melior::Context,
context: ::melior::ContextRef<'c>,
#(#phantom_fields),*
}

Expand Down Expand Up @@ -182,7 +182,7 @@ impl<'o> OperationBuilder<'o> {
impl<'c> #builder_ident<'c, #(#arguments),*> {
pub fn new(location: ::melior::ir::Location<'c>) -> Self {
Self {
context: unsafe { location.context().to_ref() },
context: location.context(),
builder: ::melior::ir::operation::OperationBuilder::new(#name, location),
#(#phantoms),*
}
Expand Down
120 changes: 120 additions & 0 deletions melior/src/dialect/ods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,123 @@ melior_macro::dialect! {
name: "vector",
tablegen: r#"include "mlir/Dialect/Vector/IR/VectorOps.td""#
}

#[cfg(test)]
mod tests {
use super::*;
use crate::{
dialect::{self, func},
ir::{
attribute::{IntegerAttribute, StringAttribute, TypeAttribute},
r#type::{FunctionType, IntegerType},
Block, Location, Module, Region, Type,
},
pass::{self, PassManager},
test::create_test_context,
Context,
};

fn convert_module<'c>(context: &'c Context, module: &mut Module<'c>) {
let pass_manager = PassManager::new(context);

pass_manager.add_pass(pass::conversion::create_func_to_llvm());
pass_manager
.nested_under("func.func")
.add_pass(pass::conversion::create_arith_to_llvm());
pass_manager
.nested_under("func.func")
.add_pass(pass::conversion::create_index_to_llvm());
pass_manager.add_pass(pass::conversion::create_scf_to_control_flow());
pass_manager.add_pass(pass::conversion::create_control_flow_to_llvm());
pass_manager.add_pass(pass::conversion::create_finalize_mem_ref_to_llvm());

assert_eq!(pass_manager.run(module), Ok(()));
assert!(module.as_operation().verify());
}

fn test_operation<'c>(
name: &str,
context: &'c Context,
argument_types: &[Type<'c>],
callback: impl FnOnce(&Block<'c>),
) {
let location = Location::unknown(&context);
let mut module = Module::new(location);

module.body().append_operation(func::func(
&context,
StringAttribute::new(&context, "foo"),
TypeAttribute::new(FunctionType::new(&context, argument_types, &[]).into()),
{
let block = Block::new(
&argument_types
.iter()
.copied()
.map(|r#type| (r#type, location))
.collect::<Vec<_>>(),
);

callback(&block);

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

convert_module(&context, &mut module);

assert!(module.as_operation().verify());
insta::assert_display_snapshot!(name, module.as_operation());
}

#[test]
fn compile_llvm_alloca() {
let context = create_test_context();
let location = Location::unknown(&context);
let integer_type = IntegerType::new(&context, 64).into();

test_operation("alloc", &context, &[integer_type], |block| {
let alloca_size = block.argument(0).unwrap().into();
let i64_type = IntegerType::new(&context, 64);

block.append_operation(
llvm::alloca(
dialect::llvm::r#type::pointer(i64_type.into(), 0).into(),
alloca_size,
location,
)
.into(),
);

block.append_operation(func::r#return(&[], location));
});
}

#[test]
fn compile_llvm_alloca_builder() {
let context = create_test_context();
let location = Location::unknown(&context);
let integer_type = IntegerType::new(&context, 64).into();
let ptr_type = dialect::llvm::r#type::opaque_pointer(&context);

test_operation("alloc_builder", &context, &[integer_type], |block| {
let alloca_size = block.argument(0).unwrap().into();
let i64_type = IntegerType::new(&context, 64);

block.append_operation(
llvm::AllocaOpBuilder::new(location)
.alignment(IntegerAttribute::new(8, i64_type.into()))
.elem_type(TypeAttribute::new(i64_type.into()))
.array_size(alloca_size)
.res(ptr_type)
.build()
.into(),
);

block.append_operation(func::r#return(&[], location));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: melior/src/dialect/ods.rs
expression: module.as_operation()
---
module attributes {llvm.data_layout = ""} {
llvm.func @foo(%arg0: i64) {
%0 = llvm.alloca %arg0 x i64 : (i64) -> !llvm.ptr<i64>
llvm.return
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
source: melior/src/dialect/ods.rs
expression: module.as_operation()
---
module attributes {llvm.data_layout = ""} {
llvm.func @foo(%arg0: i64) {
%0 = llvm.alloca %arg0 x i64 {alignment = 8 : i64} : (i64) -> !llvm.ptr
llvm.return
}
}

0 comments on commit dfa3036

Please sign in to comment.