From 0d7ab095313b549bf3eef49cc11ee1a42d500257 Mon Sep 17 00:00:00 2001 From: Edgar Luque Date: Fri, 9 Feb 2024 09:03:27 +0100 Subject: [PATCH] a --- Cargo.lock | 112 +++++++----------- lib/edlang_codegen_mlir/Cargo.toml | 3 +- lib/edlang_codegen_mlir/src/codegen.rs | 153 +++++++++++-------------- 3 files changed, 108 insertions(+), 160 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index df271deb3..af63c1a88 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,9 +28,9 @@ dependencies = [ [[package]] name = "anstream" -version = "0.6.9" +version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3fde6067df7359f2d6335ec1a50c1f8f825801687d10da0cc4c6b08e3f6afd15" +checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5" dependencies = [ "anstyle", "anstyle-parse", @@ -42,9 +42,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.4" +version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87" +checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc" [[package]] name = "anstyle-parse" @@ -177,9 +177,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "4.4.18" +version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e578d6ec4194633722ccf9544794b71b1385c3c027efe0c55db226fc880865c" +checksum = "80c21025abd42669a92efc996ef13cfb2c5c627858421ea58d5c3b331a6c134f" dependencies = [ "clap_builder", "clap_derive", @@ -187,9 +187,9 @@ dependencies = [ [[package]] name = "clap_builder" -version = "4.4.18" +version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4df4df40ec50c46000231c914968278b1eb05098cf8f1b3a518a95030e71d1c7" +checksum = "458bf1f341769dfcf849846f65dffdf9146daa56bcd2a47cb4e1de9915567c99" dependencies = [ "anstream", "anstyle", @@ -199,9 +199,9 @@ dependencies = [ [[package]] name = "clap_derive" -version = "4.4.7" +version = "4.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf9804afaaf59a91e75b022a30fb7229a7901f60c755489cc61c9b423b836442" +checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47" dependencies = [ "heck", "proc-macro2", @@ -211,9 +211,9 @@ dependencies = [ [[package]] name = "clap_lex" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1" +checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce" [[package]] name = "color-eyre" @@ -333,6 +333,7 @@ dependencies = [ "edlang_ir", "edlang_parser", "edlang_session", + "edlang_span", "inkwell", "llvm-sys", "tracing", @@ -379,7 +380,7 @@ version = "0.1.0" dependencies = [ "ariadne", "edlang_ast", - "itertools 0.12.0", + "itertools 0.12.1", "lalrpop", "lalrpop-util", "logos", @@ -418,21 +419,11 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" -[[package]] -name = "errno" -version = "0.3.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" -dependencies = [ - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "eyre" -version = "0.6.11" +version = "0.6.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b6267a1fa6f59179ea4afc8e50fd8612a3cc60bc858f786ff877a4a8cb042799" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" dependencies = [ "indenter", "once_cell", @@ -481,9 +472,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" -version = "0.3.4" +version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5d3d0e0f38255e7fa3cf31335b3a56f05febd18025f4db5ef7a0cfb4f8da651f" +checksum = "d0c62115964e08cb8039170eb33c1d0e2388a256930279edca206fff675f82c3" [[package]] name = "indenter" @@ -493,9 +484,9 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" [[package]] name = "indexmap" -version = "2.1.0" +version = "2.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f" +checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520" dependencies = [ "equivalent", "hashbrown", @@ -503,8 +494,8 @@ dependencies = [ [[package]] name = "inkwell" -version = "0.2.0" -source = "git+https://github.com/TheDan64/inkwell?rev=0cdaa34bd612464f310b3fdefa00a75d1324687d#0cdaa34bd612464f310b3fdefa00a75d1324687d" +version = "0.4.0" +source = "git+https://github.com/TheDan64/inkwell?rev=e0cc92dc28eb684684e450ca28daedc52e613962#e0cc92dc28eb684684e450ca28daedc52e613962" dependencies = [ "either", "inkwell_internals", @@ -516,8 +507,8 @@ dependencies = [ [[package]] name = "inkwell_internals" -version = "0.8.0" -source = "git+https://github.com/TheDan64/inkwell?rev=0cdaa34bd612464f310b3fdefa00a75d1324687d#0cdaa34bd612464f310b3fdefa00a75d1324687d" +version = "0.9.0" +source = "git+https://github.com/TheDan64/inkwell?rev=e0cc92dc28eb684684e450ca28daedc52e613962#e0cc92dc28eb684684e450ca28daedc52e613962" dependencies = [ "proc-macro2", "quote", @@ -526,12 +517,12 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.10" +version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0bad00257d07be169d870ab665980b06cdb366d792ad690bf2e76876dc503455" +checksum = "fe8f25ce1159c7740ff0b9b2f5cdf4a8428742ba7c112b9f20f22cd5219c7dab" dependencies = [ "hermit-abi", - "rustix", + "libc", "windows-sys 0.52.0", ] @@ -546,9 +537,9 @@ dependencies = [ [[package]] name = "itertools" -version = "0.12.0" +version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "25db6b064527c5d482d0423354fcd07a89a2dfe07b67892e62411946db7f07b0" +checksum = "ba291022dbbd398a455acf126c1e341954079855bc60dfdda641363bd6922569" dependencies = [ "either", ] @@ -593,9 +584,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libredox" @@ -608,12 +599,6 @@ dependencies = [ "redox_syscall", ] -[[package]] -name = "linux-raw-sys" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" - [[package]] name = "llvm-sys" version = "170.0.1" @@ -693,9 +678,9 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "miniz_oxide" -version = "0.7.1" +version = "0.7.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7" +checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7" dependencies = [ "adler", ] @@ -805,9 +790,9 @@ checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ "unicode-ident", ] @@ -843,13 +828,13 @@ dependencies = [ [[package]] name = "regex" -version = "1.10.2" +version = "1.10.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343" +checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.4.3", + "regex-automata 0.4.5", "regex-syntax 0.8.2", ] @@ -864,9 +849,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.4.3" +version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f" +checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" dependencies = [ "aho-corasick", "memchr", @@ -903,19 +888,6 @@ version = "0.1.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" -[[package]] -name = "rustix" -version = "0.38.30" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "322394588aaf33c24007e8bb3238ee3e4c5c09c084ab32bc73890b99ff326bca" -dependencies = [ - "bitflags 2.4.2", - "errno", - "libc", - "linux-raw-sys", - "windows-sys 0.52.0", -] - [[package]] name = "rustversion" version = "1.0.14" @@ -970,9 +942,9 @@ dependencies = [ [[package]] name = "strsim" -version = "0.10.0" +version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01" [[package]] name = "syn" diff --git a/lib/edlang_codegen_mlir/Cargo.toml b/lib/edlang_codegen_mlir/Cargo.toml index d0ef97099..36f599bce 100644 --- a/lib/edlang_codegen_mlir/Cargo.toml +++ b/lib/edlang_codegen_mlir/Cargo.toml @@ -16,8 +16,9 @@ edlang_ir = { version = "0.1.0", path = "../edlang_ir" } edlang_parser = { version = "0.1.0", path = "../edlang_parser" } edlang_session = { version = "0.1.0", path = "../edlang_session" } llvm-sys = "170.0.1" -inkwell = { git = "https://github.com/TheDan64/inkwell", rev = "0cdaa34bd612464f310b3fdefa00a75d1324687d", features = ["llvm17-0"] } +inkwell = { git = "https://github.com/TheDan64/inkwell", rev = "e0cc92dc28eb684684e450ca28daedc52e613962", features = ["llvm17-0"] } tracing = { workspace = true } +edlang_span = { version = "0.1.0", path = "../edlang_span" } [build-dependencies] cc = "1.0.83" diff --git a/lib/edlang_codegen_mlir/src/codegen.rs b/lib/edlang_codegen_mlir/src/codegen.rs index 2761ab03e..f4df370de 100644 --- a/lib/edlang_codegen_mlir/src/codegen.rs +++ b/lib/edlang_codegen_mlir/src/codegen.rs @@ -3,12 +3,13 @@ use std::{collections::HashMap, error::Error, path::PathBuf}; use edlang_ir as ir; use edlang_ir::DefId; use edlang_session::Session; +use edlang_span::Span; use inkwell::{ builder::{Builder, BuilderError}, context::Context, debug_info::{ - AsDIScope, DICompileUnit, DIFile, DIFlagsConstants, DIScope, DISubprogram, DIType, - DebugInfoBuilder, + AsDIScope, DICompileUnit, DIFile, DIFlagsConstants, DILexicalBlock, DILocation, DIScope, + DISubprogram, DIType, DebugInfoBuilder, }, module::Module, targets::{InitializationConfig, Target, TargetData, TargetMachine}, @@ -41,6 +42,19 @@ impl<'ctx, 'm> ModuleCompileCtx<'ctx, 'm> { pub fn get_module_body(&self) -> &ModuleBody { self.ctx.program.modules.get(&self.module_id).unwrap() } + + pub fn set_debug_loc(&self, scope: DIScope<'ctx>, span: Span) -> DILocation<'ctx> { + let (_, line, column) = self.ctx.session.source.get_offset_line(span.lo).unwrap(); + let debug_loc = self.di_builder.create_debug_location( + self.ctx.context, + line as u32 + 1, + column as u32 + 1, + scope, + None, + ); + self.builder.set_current_debug_location(debug_loc); + debug_loc + } } pub fn compile(session: &Session, program: &ProgramBody) -> Result> { @@ -194,7 +208,7 @@ fn compile_fn_signature(ctx: &ModuleCompileCtx<'_, '_>, fn_id: DefId) { inkwell::attributes::AttributeLoc::Function, ctx.ctx.context.create_enum_attribute(37, 0), ); - let (_, line, col) = ctx + let (_, line, _col) = ctx .ctx .session .source @@ -234,56 +248,71 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> let fn_value = ctx.module.get_function(&body.name).unwrap(); let di_program = fn_value.get_subprogram().unwrap(); - let (_, line, column) = ctx - .ctx - .session - .source - .get_offset_line(body.fn_span.lo) - .unwrap(); + let mut debug_loc = ctx.set_debug_loc(di_program.as_debug_info_scope(), body.fn_span); let mut lexical_block = ctx.di_builder.create_lexical_block( - di_program.as_debug_info_scope(), + debug_loc.get_scope(), ctx.di_unit.get_file(), - line as u32 + 1, - column as u32 + 1, + debug_loc.get_line(), + debug_loc.get_column(), ); - let debug_loc = ctx.di_builder.create_debug_location( - ctx.ctx.context, - line as u32 + 1, - column as u32 + 1, - lexical_block.as_debug_info_scope(), - None, - ); - ctx.builder.set_current_debug_location(debug_loc); + debug_loc = ctx.set_debug_loc(lexical_block.as_debug_info_scope(), body.fn_span); let block = ctx.ctx.context.append_basic_block(fn_value, "entry"); ctx.builder.position_at_end(block); let mut locals = HashMap::new(); + let mut di_locals = HashMap::new(); let mut ret_local = None; let mut arg_counter = 0; for (index, local) in body.locals.iter().enumerate() { + if let Some(span) = local.span { + debug_loc = ctx.set_debug_loc(debug_loc.get_scope(), span); + } + match local.kind { ir::LocalKind::Temp => { if let ir::TypeKind::Unit = &local.ty.kind { } else { - let ptr = ctx - .builder - .build_alloca(compile_basic_type(ctx, &local.ty), &index.to_string())?; + let ptr = ctx.builder.build_alloca( + compile_basic_type(ctx, &local.ty), + local.debug_name.as_deref().unwrap_or(&index.to_string()), + )?; locals.insert(index, ptr); } } ir::LocalKind::Arg => { if let ir::TypeKind::Unit = &local.ty.kind { } else { - let ptr = ctx - .builder - .build_alloca(compile_basic_type(ctx, &local.ty), &index.to_string())?; - ctx.builder - .build_store(ptr, fn_value.get_nth_param(arg_counter).unwrap())?; + let ty = compile_basic_type(ctx, &local.ty); + let debug_ty = compile_debug_type(ctx, &local.ty); + let name = local.debug_name.as_ref().unwrap(); + let ptr = ctx.builder.build_alloca(ty, name)?; + let value = fn_value.get_nth_param(arg_counter).unwrap(); + ctx.builder.build_store(ptr, value)?; + + let di_local = ctx.di_builder.create_parameter_variable( + debug_loc.get_scope(), + local.debug_name.as_ref().unwrap(), + arg_counter, + ctx.di_unit.get_file(), + debug_loc.get_line(), + debug_ty, + true, + 0, + ); + ctx.di_builder.insert_dbg_value_before( + value, + di_local, + None, + debug_loc, + block.get_first_instruction().unwrap(), + ); + arg_counter += 1; locals.insert(index, ptr); + di_locals.insert(index, di_local); } } ir::LocalKind::ReturnPointer => { @@ -292,7 +321,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> ret_local = Some(index); let ptr = ctx .builder - .build_alloca(compile_basic_type(ctx, &local.ty), &index.to_string())?; + .build_alloca(compile_basic_type(ctx, &local.ty), "return_ptr")?; locals.insert(index, ptr); } } @@ -300,7 +329,6 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> } let mut blocks = Vec::with_capacity(body.blocks.len()); - let mut di_locals = HashMap::new(); for (index, _block) in body.blocks.iter().enumerate() { let llvm_block = ctx @@ -315,24 +343,9 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> for (block, llvm_block) in body.blocks.iter().zip(&blocks) { info!("compiling block"); ctx.builder.position_at_end(*llvm_block); - let mut arg_no = 0; for stmt in &block.statements { if let Some(span) = stmt.span { - let (_, line, column) = ctx.ctx.session.source.get_offset_line(span.lo).unwrap(); - lexical_block = ctx.di_builder.create_lexical_block( - di_program.as_debug_info_scope(), - ctx.di_unit.get_file(), - line as u32 + 1, - column as u32 + 1, - ); - let debug_loc = ctx.di_builder.create_debug_location( - ctx.ctx.context, - line as u32 + 1, - column as u32 + 1, - lexical_block.as_debug_info_scope(), - None, - ); - ctx.builder.set_current_debug_location(debug_loc); + debug_loc = ctx.set_debug_loc(debug_loc.get_scope(), span); } info!("compiling stmt"); @@ -345,21 +358,7 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> .builder .build_store(*locals.get(&place.local).unwrap(), value)?; - if let Some(debug_name) = &local.debug_name { - let (_, line, column) = ctx - .ctx - .session - .source - .get_offset_line(local.span.unwrap().lo) - .unwrap(); - let debug_loc = ctx.di_builder.create_debug_location( - ctx.ctx.context, - line as u32, - column as u32, - lexical_block.as_debug_info_scope(), // todo correct scope - None, - ); - ctx.builder.set_current_debug_location(debug_loc); + if let Some(_debug_name) = &local.debug_name { let di_local = di_locals.get(&place.local).unwrap(); ctx.di_builder.insert_dbg_value_before( value, @@ -374,47 +373,23 @@ fn compile_fn(ctx: &ModuleCompileCtx, fn_id: DefId) -> Result<(), BuilderError> let local = &body.locals[*local_idx]; if local.debug_name.is_some() { - let (_, line, column) = ctx - .ctx - .session - .source - .get_offset_line(local.span.unwrap().lo) - .unwrap(); - let debug_loc = ctx.di_builder.create_debug_location( - ctx.ctx.context, - line as u32 + 1, - column as u32 + 1, - lexical_block.as_debug_info_scope(), - None, - ); ctx.builder.set_current_debug_location(debug_loc); let ty = compile_debug_type(ctx, &local.ty); let var = match local.kind { LocalKind::Temp => ctx.di_builder.create_auto_variable( - lexical_block.as_debug_info_scope(), + debug_loc.get_scope(), local.debug_name.as_ref().unwrap(), ctx.di_unit.get_file(), - line as u32, + debug_loc.get_line(), ty, true, 0, ty.get_align_in_bits(), ), LocalKind::Arg => { - let cur_arg_no = arg_no; - arg_no += 1; - ctx.di_builder.create_parameter_variable( - lexical_block.as_debug_info_scope(), - local.debug_name.as_ref().unwrap(), - cur_arg_no, - ctx.di_unit.get_file(), - line as u32 + 1, - ty, - true, - 0, - ) + unreachable!() } - LocalKind::ReturnPointer => todo!(), + LocalKind::ReturnPointer => unreachable!(), }; di_locals.insert(*local_idx, var); ctx.di_builder.insert_declare_at_end(