Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
raviqqe committed Oct 18, 2023
1 parent cb33aab commit 2de7997
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 35 deletions.
4 changes: 3 additions & 1 deletion melior/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,20 @@ impl<'a> Display for Diagnostic<'a> {
#[cfg(test)]
mod tests {
use crate::{ir::Module, Context};
use std::ffi::CString;

#[test]
fn handle_diagnostic() {
let mut message = None;
let source = CString::new("foo").unwrap();
let context = Context::new();

context.attach_diagnostic_handler(|diagnostic| {
message = Some(diagnostic.to_string());
true
});

Module::parse(&context, "foo");
Module::parse(&context, &source);

assert_eq!(
message.unwrap(),
Expand Down
16 changes: 8 additions & 8 deletions melior/src/execution_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ impl Drop for ExecutionEngine {
mod tests {
use super::*;
use crate::{pass, test::create_test_context};
use std::ffi::CString;

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

let mut module = Module::parse(
&context,
let source = CString::new(
r#"
module {
func.func @add(%arg0 : i32) -> i32 attributes { llvm.emit_c_interface } {
Expand All @@ -115,6 +113,9 @@ mod tests {
"#,
)
.unwrap();
let context = create_test_context();

let mut module = Module::parse(&context, &source).unwrap();

let pass_manager = pass::PassManager::new(&context);
pass_manager.add_pass(pass::conversion::create_func_to_llvm());
Expand Down Expand Up @@ -150,10 +151,7 @@ mod tests {

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

let mut module = Module::parse(
&context,
let source = CString::new(
r#"
module {
func.func @add(%arg0 : i32) -> i32 {
Expand All @@ -164,7 +162,9 @@ mod tests {
"#,
)
.unwrap();
let context = create_test_context();

let mut module = Module::parse(&context, &source).unwrap();
let pass_manager = pass::PassManager::new(&context);
pass_manager.add_pass(pass::conversion::create_func_to_llvm());

Expand Down
16 changes: 10 additions & 6 deletions melior/src/ir/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use mlir_sys::{
mlirModuleCreateEmpty, mlirModuleCreateParse, mlirModuleDestroy, mlirModuleFromOperation,
mlirModuleGetBody, mlirModuleGetContext, mlirModuleGetOperation, MlirModule,
};
use std::{ffi::CString, marker::PhantomData};
use std::{ffi::CStr, marker::PhantomData};

/// A module.
#[derive(Debug)]
Expand All @@ -23,9 +23,8 @@ impl<'c> Module<'c> {
}

/// Parses a module.
pub fn parse(context: &Context, source: &str) -> Option<Self> {
let source = CString::new(source).unwrap();
let source = StringRef::from_c_str(&source);
pub fn parse(context: &Context, source: &'c CStr) -> Option<Self> {
let source = StringRef::from_c_str(source);

unsafe { Self::from_option_raw(mlirModuleCreateParse(context.to_raw(), source.to_raw())) }
}
Expand Down Expand Up @@ -94,6 +93,7 @@ mod tests {
ir::{operation::OperationBuilder, Block, Region},
test::create_test_context,
};
use std::ffi::CString;

#[test]
fn new() {
Expand All @@ -107,12 +107,16 @@ mod tests {

#[test]
fn parse() {
assert!(Module::parse(&Context::new(), "module{}").is_some());
let source = CString::new("module{}").unwrap();

assert!(Module::parse(&Context::new(), &source).is_some());
}

#[test]
fn parse_none() {
assert!(Module::parse(&Context::new(), "module{").is_none());
let source = CString::new("module{").unwrap();

assert!(Module::parse(&Context::new(), &source).is_none());
}

#[test]
Expand Down
37 changes: 17 additions & 20 deletions melior/src/pass/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ mod tests {
};
use indoc::indoc;
use pretty_assertions::assert_eq;
use std::ffi::CString;

#[test]
fn new() {
Expand Down Expand Up @@ -130,20 +131,18 @@ mod tests {

#[test]
fn run_on_function() {
let source = CString::new(indoc!(
"
func.func @foo(%arg0 : i32) -> i32 {
%res = arith.addi %arg0, %arg0 : i32
return %res : i32
}
"
))
.unwrap();
let context = create_test_context();

let mut module = Module::parse(
&context,
indoc!(
"
func.func @foo(%arg0 : i32) -> i32 {
%res = arith.addi %arg0, %arg0 : i32
return %res : i32
}
"
),
)
.unwrap();
let mut module = Module::parse(&context, &source).unwrap();

let manager = PassManager::new(&context);
manager.add_pass(pass::transform::create_print_op_stats());
Expand All @@ -153,12 +152,8 @@ mod tests {

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

let mut module = Module::parse(
&context,
indoc!(
"
let source = CString::new(indoc!(
"
func.func @foo(%arg0 : i32) -> i32 {
%res = arith.addi %arg0, %arg0 : i32
return %res : i32
Expand All @@ -171,9 +166,11 @@ mod tests {
}
}
"
),
)
))
.unwrap();
let context = create_test_context();

let mut module = Module::parse(&context, &source).unwrap();

let manager = PassManager::new(&context);
manager
Expand Down

0 comments on commit 2de7997

Please sign in to comment.