diff --git a/compiler/gen/src/llvm/build.rs b/compiler/gen/src/llvm/build.rs index 7edf07c819..bdda4756e0 100644 --- a/compiler/gen/src/llvm/build.rs +++ b/compiler/gen/src/llvm/build.rs @@ -56,6 +56,30 @@ const PRINT_FN_VERIFICATION_OUTPUT: bool = true; #[cfg(not(debug_assertions))] const PRINT_FN_VERIFICATION_OUTPUT: bool = false; +#[macro_export] +macro_rules! debug_info_init { + ($env:expr, $function_value:expr) => {{ + use inkwell::debug_info::AsDIScope; + + let func_scope = $function_value.get_subprogram().unwrap(); + let lexical_block = $env.dibuilder.create_lexical_block( + /* scope */ func_scope.as_debug_info_scope(), + /* file */ $env.compile_unit.get_file(), + /* line_no */ 0, + /* column_no */ 0, + ); + + let loc = $env.dibuilder.create_debug_location( + $env.context, + /* line */ 0, + /* column */ 0, + /* current_scope */ lexical_block.as_debug_info_scope(), + /* inlined_at */ None, + ); + $env.builder.set_current_debug_location(&$env.context, loc); + }}; +} + #[derive(Debug, Clone, Copy)] pub enum OptLevel { Normal, @@ -2656,22 +2680,7 @@ fn expose_function_to_host_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = c_function.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - env.context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(env.context, loc); + debug_info_init!(env, c_function); // drop the final argument, which is the pointer we write the result into let args = c_function.get_params(); @@ -2713,22 +2722,7 @@ fn expose_function_to_host_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = size_function.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - env.context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(env.context, loc); + debug_info_init!(env, size_function); let size: BasicValueEnum = return_type.size_of().unwrap().into(); builder.build_return(Some(&size)); @@ -2940,22 +2934,7 @@ fn make_exception_catching_wrapper<'a, 'ctx, 'env>( let basic_block = context.append_basic_block(wrapper_function, "entry"); builder.position_at_end(basic_block); - let func_scope = wrapper_function.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - env.context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(env.context, loc); + debug_info_init!(env, wrapper_function); let result = invoke_and_catch( env, @@ -3355,23 +3334,7 @@ pub fn build_proc<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = fn_val.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - - builder.set_current_debug_location(&context, loc); + debug_info_init!(env, fn_val); // Add args to scope for (arg_val, (layout, arg_symbol)) in fn_val.get_param_iter().zip(args) { diff --git a/compiler/gen/src/llvm/build_dict.rs b/compiler/gen/src/llvm/build_dict.rs index 13dbfd0fc7..30d955e867 100644 --- a/compiler/gen/src/llvm/build_dict.rs +++ b/compiler/gen/src/llvm/build_dict.rs @@ -1,9 +1,10 @@ +use crate::debug_info_init; use crate::llvm::build::{ call_bitcode_fn, call_void_bitcode_fn, complex_bitcast, load_symbol, load_symbol_and_layout, set_name, Env, Scope, }; -use crate::llvm::convert::{basic_type_from_layout, collection}; -use inkwell::types::{BasicType, BasicTypeEnum}; +use crate::llvm::convert::basic_type_from_layout; +use inkwell::types::BasicType; use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, StructValue}; use inkwell::AddressSpace; use roc_builtins::bitcode; @@ -39,29 +40,6 @@ impl Alignment { } } -macro_rules! debug_info_init { - ($env:expr, $function_value:expr) => {{ - use inkwell::debug_info::AsDIScope; - - let func_scope = $function_value.get_subprogram().unwrap(); - let lexical_block = $env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ $env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = $env.dibuilder.create_debug_location( - $env.context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - $env.builder.set_current_debug_location(&$env.context, loc); - }}; -} - pub fn dict_len<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, scope: &Scope<'a, 'ctx>, @@ -111,7 +89,7 @@ pub fn dict_empty<'a, 'ctx, 'env>( pub fn dict_insert<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, layout_ids: &mut LayoutIds<'a>, - scope: &Scope<'a, 'ctx>, + _scope: &Scope<'a, 'ctx>, dict: BasicValueEnum<'ctx>, key: BasicValueEnum<'ctx>, key_layout: &Layout<'a>, @@ -191,10 +169,7 @@ fn build_hash_wrapper<'a, 'ctx, 'env>( let function_value = match env.module.get_function(fn_name.as_str()) { Some(function_value) => function_value, None => { - let arena = env.arena; - let seed_type = env.context.i64_type(); - let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic); let function_value = crate::llvm::refcounting::build_header_help( @@ -258,8 +233,6 @@ fn build_eq_wrapper<'a, 'ctx, 'env>( let function_value = match env.module.get_function(fn_name.as_str()) { Some(function_value) => function_value, None => { - let arena = env.arena; - let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic); let function_value = crate::llvm::refcounting::build_header_help( @@ -304,8 +277,6 @@ fn build_rc_wrapper<'a, 'ctx, 'env>( let function_value = match env.module.get_function(fn_name.as_str()) { Some(function_value) => function_value, None => { - let arena = env.arena; - let arg_type = env.context.i8_type().ptr_type(AddressSpace::Generic); let function_value = crate::llvm::refcounting::build_header_help( @@ -333,18 +304,6 @@ fn build_rc_wrapper<'a, 'ctx, 'env>( function_value } -fn dict_symbol_to_i128<'a, 'ctx, 'env>( - env: &Env<'a, 'ctx, 'env>, - scope: &Scope<'a, 'ctx>, - symbol: Symbol, -) -> IntValue<'ctx> { - let dict = load_symbol(scope, &symbol); - - let i128_type = env.context.i128_type().into(); - - complex_bitcast(&env.builder, dict, i128_type, "dict_to_i128").into_int_value() -} - fn dict_symbol_to_zig_dict<'a, 'ctx, 'env>( env: &Env<'a, 'ctx, 'env>, scope: &Scope<'a, 'ctx>, diff --git a/compiler/gen/src/llvm/build_hash.rs b/compiler/gen/src/llvm/build_hash.rs index 4c212911e5..a5445e1a39 100644 --- a/compiler/gen/src/llvm/build_hash.rs +++ b/compiler/gen/src/llvm/build_hash.rs @@ -1,3 +1,4 @@ +use crate::debug_info_init; use crate::llvm::build::Env; use crate::llvm::build::{ call_bitcode_fn, cast_block_of_memory_to_tag, complex_bitcast, set_name, FAST_CALL_CONV, @@ -236,28 +237,8 @@ fn build_hash_struct_help<'a, 'ctx, 'env>( field_layouts: &[Layout<'a>], ) { let ctx = env.context; - let builder = env.builder; - { - use inkwell::debug_info::AsDIScope; - - let func_scope = parent.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&ctx, loc); - } + debug_info_init!(env, parent); // Add args to scope let mut it = parent.get_param_iter(); @@ -404,28 +385,8 @@ fn build_hash_tag_help<'a, 'ctx, 'env>( union_layout: &UnionLayout<'a>, ) { let ctx = env.context; - let builder = env.builder; - { - use inkwell::debug_info::AsDIScope; - - let func_scope = parent.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&ctx, loc); - } + debug_info_init!(env, parent); // Add args to scope let mut it = parent.get_param_iter(); @@ -707,28 +668,8 @@ fn build_hash_list_help<'a, 'ctx, 'env>( element_layout: &Layout<'a>, ) { let ctx = env.context; - let builder = env.builder; - { - use inkwell::debug_info::AsDIScope; - - let func_scope = parent.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&ctx, loc); - } + debug_info_init!(env, parent); // Add args to scope let mut it = parent.get_param_iter(); diff --git a/compiler/gen/src/llvm/refcounting.rs b/compiler/gen/src/llvm/refcounting.rs index a89271911b..428e3d6047 100644 --- a/compiler/gen/src/llvm/refcounting.rs +++ b/compiler/gen/src/llvm/refcounting.rs @@ -1,3 +1,4 @@ +use crate::debug_info_init; use crate::llvm::build::{ cast_basic_basic, cast_block_of_memory_to_tag, set_name, Env, FAST_CALL_CONV, LLVM_SADD_WITH_OVERFLOW_I64, @@ -8,7 +9,6 @@ use crate::llvm::convert::{ }; use bumpalo::collections::Vec; use inkwell::context::Context; -use inkwell::debug_info::AsDIScope; use inkwell::module::Linkage; use inkwell::types::{AnyTypeEnum, BasicType, BasicTypeEnum}; use inkwell::values::{BasicValueEnum, FunctionValue, IntValue, PointerValue, StructValue}; @@ -192,23 +192,7 @@ impl<'ctx> PointerToRefcount<'ctx> { let entry = ctx.append_basic_block(parent, "entry"); builder.position_at_end(entry); - let subprogram = parent.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ subprogram.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - &ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - - env.builder.set_current_debug_location(&ctx, loc); + debug_info_init!(env, parent); let refcount_ptr = { let raw_refcount_ptr = parent.get_nth_param(0).unwrap(); @@ -576,22 +560,7 @@ fn modify_refcount_list_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = fn_val.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&ctx, loc); + debug_info_init!(env, fn_val); // Add args to scope let arg_symbol = Symbol::ARG_1; @@ -683,22 +652,7 @@ fn modify_refcount_str_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = fn_val.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - ctx, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&ctx, loc); + debug_info_init!(env, fn_val); // Add args to scope let arg_symbol = Symbol::ARG_1; @@ -869,22 +823,7 @@ fn build_rec_union_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = fn_val.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&context, loc); + debug_info_init!(env, fn_val); // Add args to scope let arg_symbol = Symbol::ARG_1; @@ -940,8 +879,6 @@ fn build_rec_union_help<'a, 'ctx, 'env>( // next, make a jump table for all possible values of the tag_id let mut cases = Vec::with_capacity_in(tags.len(), env.arena); - builder.set_current_debug_location(&context, loc); - for (tag_id, field_layouts) in tags.iter().enumerate() { // if none of the fields are or contain anything refcounted, just move on if !field_layouts @@ -1174,22 +1111,7 @@ fn modify_refcount_union_help<'a, 'ctx, 'env>( builder.position_at_end(entry); - let func_scope = fn_val.get_subprogram().unwrap(); - let lexical_block = env.dibuilder.create_lexical_block( - /* scope */ func_scope.as_debug_info_scope(), - /* file */ env.compile_unit.get_file(), - /* line_no */ 0, - /* column_no */ 0, - ); - - let loc = env.dibuilder.create_debug_location( - context, - /* line */ 0, - /* column */ 0, - /* current_scope */ lexical_block.as_debug_info_scope(), - /* inlined_at */ None, - ); - builder.set_current_debug_location(&context, loc); + debug_info_init!(env, fn_val); // Add args to scope let arg_symbol = Symbol::ARG_1;