From 9333638933e286e59c1e6057a36433b441466067 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Sat, 20 Jan 2024 16:44:34 +0100 Subject: [PATCH] ok --- .clangd | 2 ++ lib/edlang_codegen_mlir/src/codegen.rs | 36 ++++++++++++++++++-------- lib/edlang_codegen_mlir/src/lib.rs | 14 +++++++++- lib/edlang_driver/src/lib.rs | 16 ++++++++++++ 4 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 .clangd diff --git a/.clangd b/.clangd new file mode 100644 index 000000000..325dfe423 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: # Tweak the parse settings + Add: [-std=c++17, -xc++, -Wall, -I/home/edgar/storage/llvm-17/include] diff --git a/lib/edlang_codegen_mlir/src/codegen.rs b/lib/edlang_codegen_mlir/src/codegen.rs index d5bf3b361..4aca2c7cc 100644 --- a/lib/edlang_codegen_mlir/src/codegen.rs +++ b/lib/edlang_codegen_mlir/src/codegen.rs @@ -160,6 +160,22 @@ fn get_location<'c>(context: &'c MeliorContext, session: &Session, offset: usize ) } +fn get_di_file<'c>(context: &'c MeliorContext, session: &Session) -> Attribute<'c> { + Attribute::parse( + context, + &format!(r#"#llvm.di_file<"{}">"#, session.file_path.display()), + ) + .unwrap() +} + +fn get_di<'c>(context: &'c MeliorContext, session: &Session) -> Attribute<'c> { + Attribute::parse( + context, + &format!(r#"#llvm.di_file<"{}">"#, session.file_path.display()), + ) + .unwrap() +} + fn compile_function_def<'ctx, 'parent>( session: &Session, context: &'ctx MeliorContext, @@ -171,7 +187,6 @@ fn compile_function_def<'ctx, 'parent>( let region = Region::new(); let location = get_location(context, session, info.name.span.lo); - let location = Location::name(context, &info.name.name, location); let mut args = Vec::with_capacity(info.params.len()); let mut fn_args_types = Vec::with_capacity(info.params.len()); @@ -179,7 +194,7 @@ fn compile_function_def<'ctx, 'parent>( for param in &info.params { let param_type = scope_ctx.resolve_type(context, ¶m.arg_type)?; let loc = get_location(context, session, param.name.span.lo); - let loc = Location::name(context, ¶m.name.name, loc); + // let loc = Location::name(context, ¶m.name.name, loc); args.push((param_type, loc)); fn_args_types.push(param_type); } @@ -223,11 +238,7 @@ fn compile_function_def<'ctx, 'parent>( if final_block.terminator().is_none() { final_block.append_operation(func::r#return( &[], - Location::name( - context, - "return", - get_location(context, session, info.span.hi), - ), + get_location(context, session, info.span.hi), )); } } @@ -383,7 +394,6 @@ fn compile_return<'ctx, 'parent: 'ctx>( ) -> Result<(), Box> { tracing::debug!("compiling return"); let location = get_location(context, session, info.span.lo); - let location = Location::name(context, "return", location); let ret_type = scope_ctx.function.and_then(|x| x.return_type.clone()); @@ -471,7 +481,6 @@ fn compile_expression<'ctx, 'parent: 'ctx>( .expect("local not found"); let location = get_location(context, session, path.first.span.lo); - let location = Location::name(context, &path.first.name, location); if local.is_alloca { let k0 = block @@ -638,13 +647,18 @@ fn compile_fn_call<'ctx, 'parent: 'ctx>( ) -> Result, Box> { let mut args = Vec::with_capacity(info.params.len()); let location = get_location(context, session, info.name.span.lo); + /* let location_callee = Location::name(context, &info.name.name, location); let location_caller = Location::name( context, &info.name.name, get_location(context, session, scope_ctx.function.unwrap().span.lo), ); - let location = Location::call_site(location_callee, location_caller); + */ + let location = Location::call_site( + location, + get_location(context, session, scope_ctx.function.unwrap().span.lo), + ); let target_fn = scope_ctx .functions @@ -718,7 +732,7 @@ fn compile_if_stmt<'c, 'this: 'c>( else_successor, &[], &[], - Location::name(context, "if", location), + location, )); let mut then_successor = then_successor; diff --git a/lib/edlang_codegen_mlir/src/lib.rs b/lib/edlang_codegen_mlir/src/lib.rs index ab8867428..8e8a42a14 100644 --- a/lib/edlang_codegen_mlir/src/lib.rs +++ b/lib/edlang_codegen_mlir/src/lib.rs @@ -24,7 +24,7 @@ use llvm_sys::{ LLVMTargetRef, }, }; -use melior::ir::Module as MeliorModule; +use melior::ir::{operation::OperationPrintingFlags, Module as MeliorModule}; use crate::ffi::mlirTranslateModuleToLLVMIR; @@ -33,6 +33,18 @@ mod context; mod ffi; pub mod linker; +pub fn compile_mlir( + session: &Session, + program: &Module, +) -> Result> { + let context = Context::new(); + let mlir_module = context.compile(session, program)?; + + Ok(mlir_module + .as_operation() + .to_string_with_flags(OperationPrintingFlags::new().enable_debug_info(true, false))?) +} + pub fn compile(session: &Session, program: &Module) -> Result> { let context = Context::new(); let mlir_module = context.compile(session, program)?; diff --git a/lib/edlang_driver/src/lib.rs b/lib/edlang_driver/src/lib.rs index 8555f3ba6..4d7499f23 100644 --- a/lib/edlang_driver/src/lib.rs +++ b/lib/edlang_driver/src/lib.rs @@ -18,6 +18,12 @@ pub struct CompilerArgs { /// Build as a library. #[arg(short, long, default_value_t = false)] library: bool, + + #[arg(long, default_value_t = false)] + mlir: bool, + + #[arg(long, default_value_t = false)] + ast: bool, } pub fn main() -> Result<(), Box> { @@ -70,6 +76,16 @@ pub fn main() -> Result<(), Box> { }; tracing::debug!("Compiling with session: {:#?}", session); + if args.ast { + println!("{:#?}", module); + return Ok(()); + } + + if args.mlir { + println!("{}", edlang_codegen_mlir::compile_mlir(&session, &module)?); + return Ok(()); + } + let object_path = edlang_codegen_mlir::compile(&session, &module)?; if session.library {