From 0a0db23b1751c0b7021cc1b3af3329b5d93cf9da Mon Sep 17 00:00:00 2001 From: gingerBill Date: Tue, 22 Nov 2022 15:49:27 +0000 Subject: Remove copy elision code --- src/llvm_backend_proc.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 6e2345436..968a702e2 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -882,7 +882,7 @@ lbValue lb_emit_conjugate(lbProcedure *p, lbValue val, Type *type) { return lb_emit_load(p, res); } -lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining, bool use_copy_elision_hint) { +lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, ProcInlining inlining) { lbModule *m = p->module; Type *pt = base_type(value.type); @@ -981,11 +981,6 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, Type *rt = reduce_tuple_to_single_type(results); if (return_by_pointer) { lbValue return_ptr = {}; - if (use_copy_elision_hint && p->copy_elision_hint.ptr.value != nullptr) { - if (are_types_identical(type_deref(p->copy_elision_hint.ptr.type), rt)) { - return_ptr = lb_consume_copy_elision_hint(p); - } - } if (return_ptr.value == nullptr) { lbAddr r = lb_add_local_generated(p, rt, true); return_ptr = r.addr; @@ -3032,7 +3027,7 @@ lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { } } - return lb_emit_call(p, value, args, ce->inlining, p->copy_elision_hint.ast == expr); + return lb_emit_call(p, value, args, ce->inlining); } isize arg_index = 0; @@ -3213,6 +3208,6 @@ lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { } auto call_args = array_slice(args, 0, final_count); - return lb_emit_call(p, value, call_args, ce->inlining, p->copy_elision_hint.ast == expr); + return lb_emit_call(p, value, call_args, ce->inlining); } -- cgit v1.2.3 From 7ab591667a1c647926fe79fda18efec8ce706198 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 16:25:09 +0000 Subject: Basic support for new ABI experiment on Win64 --- src/llvm_abi.cpp | 111 +++++++++++++++++++++++++++++++++++-------- src/llvm_backend_general.cpp | 35 ++++++-------- src/llvm_backend_proc.cpp | 93 +++++++++++++++++++++++++++++------- src/llvm_backend_stmt.cpp | 24 ++++++++-- src/llvm_backend_utility.cpp | 2 +- 5 files changed, 202 insertions(+), 63 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index 298041aa6..31773e1a2 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -1,3 +1,5 @@ +#define ALLOW_SPLIT_MULTI_RETURNS true + enum lbArgKind { lbArg_Direct, lbArg_Indirect, @@ -48,8 +50,16 @@ struct lbFunctionType { ProcCallingConvention calling_convention; Array args; lbArgType ret; + + LLVMTypeRef multiple_return_original_type; // nullptr if not used + isize original_arg_count; }; +gbAllocator lb_function_type_args_allocator(void) { + return heap_allocator(); +} + + i64 llvm_align_formula(i64 off, i64 a) { return (off + a - 1) / a * a; } @@ -100,7 +110,9 @@ LLVMTypeRef lb_function_type_to_llvm_raw(lbFunctionType *ft, bool is_var_arg) { } args[arg_index++] = arg_type; } else if (arg->kind == lbArg_Indirect) { - GB_ASSERT(!lb_is_type_kind(arg->type, LLVMPointerTypeKind)); + if (ft->multiple_return_original_type == nullptr || i < ft->original_arg_count) { + GB_ASSERT(!lb_is_type_kind(arg->type, LLVMPointerTypeKind)); + } args[arg_index++] = LLVMPointerType(arg->type, 0); } else if (arg->kind == lbArg_Ignore) { // ignore @@ -147,6 +159,13 @@ void lb_add_function_type_attributes(LLVMValueRef fn, lbFunctionType *ft, ProcCa LLVMAddAttributeAtIndex(fn, arg_index+1, arg->align_attribute); } + if (ft->multiple_return_original_type) { + if (ft->original_arg_count <= i) { + LLVMAddAttributeAtIndex(fn, arg_index+1, noalias_attr); + LLVMAddAttributeAtIndex(fn, arg_index+1, nonnull_attr); + } + } + arg_index++; } @@ -307,7 +326,7 @@ i64 lb_alignof(LLVMTypeRef type) { } -#define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, ProcCallingConvention calling_convention) +#define LB_ABI_INFO(name) lbFunctionType *name(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple, ProcCallingConvention calling_convention) typedef LB_ABI_INFO(lbAbiInfoType); @@ -353,7 +372,7 @@ namespace lbAbi386 { } Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { - auto args = array_make(heap_allocator(), arg_count); + auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef t = arg_types[i]; @@ -392,19 +411,19 @@ namespace lbAbi386 { namespace lbAbiAmd64Win64 { Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count); - + lbArgType compute_return_type(lbFunctionType *ft, LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple); LB_ABI_INFO(abi_info) { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; ft->args = compute_arg_types(c, arg_types, arg_count); - ft->ret = lbAbi386::compute_return_type(c, return_type, return_is_defined); + ft->ret = compute_return_type(ft, c, return_type, return_is_defined, return_is_tuple); ft->calling_convention = calling_convention; return ft; } Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { - auto args = array_make(heap_allocator(), arg_count); + auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef t = arg_types[i]; @@ -428,6 +447,45 @@ namespace lbAbiAmd64Win64 { } return args; } + + lbArgType compute_return_type(lbFunctionType *ft, LLVMContextRef c, LLVMTypeRef return_type, bool return_is_defined, bool return_is_tuple) { + if (!return_is_defined) { + return lb_arg_type_direct(LLVMVoidTypeInContext(c)); + } else if (lb_is_type_kind(return_type, LLVMStructTypeKind) || lb_is_type_kind(return_type, LLVMArrayTypeKind)) { + i64 sz = lb_sizeof(return_type); + switch (sz) { + case 1: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 8), nullptr, nullptr); + case 2: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 16), nullptr, nullptr); + case 4: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 32), nullptr, nullptr); + case 8: return lb_arg_type_direct(return_type, LLVMIntTypeInContext(c, 64), nullptr, nullptr); + } + + if (return_is_tuple) { + GB_ASSERT(lb_is_type_kind(return_type, LLVMStructTypeKind)); + unsigned field_count = LLVMCountStructElementTypes(return_type); + + if (field_count > 1) { + ft->original_arg_count = ft->args.count; + ft->multiple_return_original_type = return_type; + + for (unsigned i = 0; i < field_count-1; i++) { + LLVMTypeRef field_type = LLVMStructGetTypeAtIndex(return_type, i); + LLVMTypeRef field_pointer_type = LLVMPointerType(field_type, 0); + lbArgType ret_partial = lb_arg_type_direct(field_pointer_type); + array_add(&ft->args, ret_partial); + } + + // override the return type for the last field + LLVMTypeRef new_return_type = LLVMStructGetTypeAtIndex(return_type, field_count-1); + return compute_return_type(ft, c, new_return_type, true, false); + } + } + + LLVMAttributeRef attr = lb_create_enum_attribute_with_type(c, "sret", return_type); + return lb_arg_type_indirect(return_type, attr); + } + return lbAbi386::non_struct(c, return_type, true); + } }; // NOTE(bill): I hate `namespace` in C++ but this is just because I don't want to prefix everything @@ -490,7 +548,7 @@ namespace lbAbiAmd64SysV { ft->ctx = c; ft->calling_convention = calling_convention; - ft->args = array_make(heap_allocator(), arg_count); + ft->args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { ft->args[i] = amd64_type(c, arg_types[i], Amd64TypeAttribute_ByVal, calling_convention); } @@ -1056,7 +1114,7 @@ namespace lbAbiArm64 { } Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { - auto args = array_make(heap_allocator(), arg_count); + auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef type = arg_types[i]; @@ -1188,7 +1246,7 @@ namespace lbAbiWasm { Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count) { - auto args = array_make(heap_allocator(), arg_count); + auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef t = arg_types[i]; @@ -1266,7 +1324,7 @@ namespace lbAbiArm32 { } Array compute_arg_types(LLVMContextRef c, LLVMTypeRef *arg_types, unsigned arg_count, ProcCallingConvention calling_convention) { - auto args = array_make(heap_allocator(), arg_count); + auto args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { LLVMTypeRef t = arg_types[i]; @@ -1307,14 +1365,14 @@ namespace lbAbiArm32 { }; -LB_ABI_INFO(lb_get_abi_info) { +LB_ABI_INFO(lb_get_abi_info_internal) { switch (calling_convention) { case ProcCC_None: case ProcCC_InlineAsm: { lbFunctionType *ft = gb_alloc_item(permanent_allocator(), lbFunctionType); ft->ctx = c; - ft->args = array_make(heap_allocator(), arg_count); + ft->args = array_make(lb_function_type_args_allocator(), arg_count); for (unsigned i = 0; i < arg_count; i++) { ft->args[i] = lb_arg_type_direct(arg_types[i]); } @@ -1328,32 +1386,43 @@ LB_ABI_INFO(lb_get_abi_info) { } case ProcCC_Win64: GB_ASSERT(build_context.metrics.arch == TargetArch_amd64); - return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); case ProcCC_SysV: GB_ASSERT(build_context.metrics.arch == TargetArch_amd64); - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } switch (build_context.metrics.arch) { case TargetArch_amd64: if (build_context.metrics.os == TargetOs_windows || build_context.metrics.abi == TargetABI_Win64) { - return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } else if (build_context.metrics.abi == TargetABI_SysV) { - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } else { - return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } case TargetArch_i386: - return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbi386::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); case TargetArch_arm32: - return lbAbiArm32::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiArm32::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); case TargetArch_arm64: - return lbAbiArm64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiArm64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); case TargetArch_wasm32: case TargetArch_wasm64: - return lbAbiWasm::abi_info(c, arg_types, arg_count, return_type, return_is_defined, calling_convention); + return lbAbiWasm::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } GB_PANIC("Unsupported ABI"); return {}; } + + +LB_ABI_INFO(lb_get_abi_info) { + lbFunctionType *ft = lb_get_abi_info_internal(c, arg_types, arg_count, return_type, return_is_defined, ALLOW_SPLIT_MULTI_RETURNS && return_is_tuple, calling_convention); + if (calling_convention == ProcCC_Odin) { + // append the `context` pointer + lbArgType context_param = lb_arg_type_direct(LLVMPointerType(LLVMInt8TypeInContext(c), 0)); + array_add(&ft->args, context_param); + } + return ft; +} diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 050168e8e..382c01042 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -1499,9 +1499,6 @@ LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { } unsigned param_count = 0; - if (type->Proc.calling_convention == ProcCC_Odin) { - param_count += 1; - } if (type->Proc.param_count != 0) { GB_ASSERT(type->Proc.params->kind == Type_Tuple); @@ -1519,21 +1516,23 @@ LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { m->internal_type_level += 1; defer (m->internal_type_level -= 1); + bool return_is_tuple = false; LLVMTypeRef ret = nullptr; LLVMTypeRef *params = gb_alloc_array(permanent_allocator(), LLVMTypeRef, param_count); bool *params_by_ptr = gb_alloc_array(permanent_allocator(), bool, param_count); if (type->Proc.result_count != 0) { Type *single_ret = reduce_tuple_to_single_type(type->Proc.results); - if (is_type_proc(single_ret)) { + if (is_type_proc(single_ret)) { single_ret = t_rawptr; } ret = lb_type(m, single_ret); - if (ret != nullptr) { - if (is_type_boolean(single_ret) && - is_calling_convention_none(type->Proc.calling_convention) && - type_size_of(single_ret) <= 1) { - ret = LLVMInt1TypeInContext(m->ctx); - } + if (is_type_tuple(single_ret)) { + return_is_tuple = true; + } + if (is_type_boolean(single_ret) && + is_calling_convention_none(type->Proc.calling_convention) && + type_size_of(single_ret) <= 1) { + ret = LLVMInt1TypeInContext(m->ctx); } } @@ -1571,12 +1570,8 @@ LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { params[param_index++] = param_type; } } - if (param_index < param_count) { - params[param_index++] = lb_type(m, t_rawptr); - } GB_ASSERT(param_index == param_count); - - lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, type->Proc.calling_convention); + lbFunctionType *ft = lb_get_abi_info(m->ctx, params, param_count, ret, ret != nullptr, return_is_tuple, type->Proc.calling_convention); { for_array(j, ft->args) { auto arg = ft->args[j]; @@ -1593,10 +1588,10 @@ LLVMTypeRef lb_type_internal_for_procedures_raw(lbModule *m, Type *type) { LLVMPrintTypeToString(ft->ret.type), LLVMGetTypeContext(ft->ret.type), ft->ctx, LLVMGetGlobalContext()); } - for_array(j, ft->args) { - if (params_by_ptr[j]) { + for (unsigned i = 0; i < param_count; i++) { + if (params_by_ptr[i]) { // NOTE(bill): The parameter needs to be passed "indirectly", override it - ft->args[j].kind = lbArg_Indirect; + ft->args[i].kind = lbArg_Indirect; } } @@ -2161,11 +2156,11 @@ LLVMTypeRef lb_type(lbModule *m, Type *type) { return llvm_type; } -lbFunctionType *lb_get_function_type(lbModule *m, lbProcedure *p, Type *pt) { +lbFunctionType *lb_get_function_type(lbModule *m, Type *pt) { lbFunctionType **ft_found = nullptr; ft_found = map_get(&m->function_type_map, pt); if (!ft_found) { - LLVMTypeRef llvm_proc_type = lb_type(p->module, pt); + LLVMTypeRef llvm_proc_type = lb_type(m, pt); gb_unused(llvm_proc_type); ft_found = map_get(&m->function_type_map, pt); } diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index 968a702e2..b47168541 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -501,6 +501,14 @@ void lb_begin_procedure_body(lbProcedure *p) { // NOTE(bill): this must be parameter 0 String name = str_lit("agg.result"); + if (ft->multiple_return_original_type && + p->type->Proc.has_named_results) { + auto const &variables = p->type->Proc.results->Tuple.variables; + Entity *e = variables[variables.count-1]; + if (!is_blank_ident(e->token)) { + name = e->token.string; + } + } Type *ptr_type = alloc_type_pointer(reduce_tuple_to_single_type(p->type->Proc.results)); Entity *e = alloc_entity_param(nullptr, make_token_ident(name), ptr_type, false, false); @@ -580,14 +588,31 @@ void lb_begin_procedure_body(lbProcedure *p) { if (e->token.string != "") { GB_ASSERT(!is_blank_ident(e->token)); - // NOTE(bill): Don't even bother trying to optimize this with the return ptr value - // This will violate the defer rules if you do: - // foo :: proc() -> (x, y: T) { - // defer x = ... // defer is executed after the `defer` - // return // the values returned should be zeroed - // } - // NOTE(bill): REALLY, don't even bother. - lbAddr res = lb_add_local(p, e->type, e); + lbAddr res = {}; + + lbFunctionType *ft = p->abi_function_type; + if (ft->multiple_return_original_type && + i < results->variables.count-1) { + isize ret_offset = param_offset + ft->original_arg_count + i; + lbValue ptr = {}; + ptr.value = LLVMGetParam(p->value, cast(unsigned)ret_offset); + ptr.type = alloc_type_pointer(e->type); + res = lb_addr(ptr); + + lb_add_entity(p->module, e, ptr); + lb_add_debug_local_variable(p, ptr.value, e->type, e->token); + } else { + + // NOTE(bill): Don't even bother trying to optimize this with the return ptr value + // This will violate the defer rules if you do: + // foo :: proc() -> (x, y: T) { + // defer x = ... // defer is executed after the `defer` + // return // the values returned should be zeroed + // } + // NOTE(bill): REALLY, don't even bother. + res = lb_add_local(p, e->type, e); + } + if (e->Variable.param_value.kind != ParameterValue_Invalid) { lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos); lb_addr_store(p, res, c); @@ -734,6 +759,7 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, if (return_ptr.value != nullptr) { args[arg_index++] = return_ptr.value; } + for_array(i, processed_args) { lbValue arg = processed_args[i]; if (is_type_proc(arg.type)) { @@ -741,16 +767,23 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, } args[arg_index++] = arg.value; } + if (context_ptr.addr.value != nullptr) { LLVMValueRef cp = context_ptr.addr.value; cp = LLVMBuildPointerCast(p->builder, cp, lb_type(p->module, t_rawptr), ""); args[arg_index++] = cp; } + + GB_ASSERT(arg_index == arg_count); + LLVMBasicBlockRef curr_block = LLVMGetInsertBlock(p->builder); GB_ASSERT(curr_block != p->decl_block->block); { - LLVMTypeRef fnp = lb_type_internal_for_procedures_raw(p->module, value.type); + Type *proc_type = base_type(value.type); + GB_ASSERT(proc_type->kind == Type_Proc); + + LLVMTypeRef fnp = lb_type_internal_for_procedures_raw(p->module, proc_type); LLVMTypeRef ftp = LLVMPointerType(fnp, 0); LLVMValueRef fn = value.value; if (!lb_is_type_kind(LLVMTypeOf(value.value), LLVMFunctionTypeKind)) { @@ -775,10 +808,11 @@ lbValue lb_emit_call_internal(lbProcedure *p, lbValue value, lbValue return_ptr, // LLVMTypeKind arg_kind = LLVMGetTypeKind(arg_type); GB_ASSERT_MSG( arg_type == param_type, - "Parameter types do not match: %s != %s, argument: %s", + "Parameter types do not match: %s != %s, argument: %s\n\t%s", LLVMPrintTypeToString(arg_type), LLVMPrintTypeToString(param_type), - LLVMPrintValueToString(args[i]) + LLVMPrintValueToString(args[i]), + LLVMPrintTypeToString(fnp) ); } } @@ -915,8 +949,9 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, bool is_odin_cc = is_calling_convention_odin(pt->Proc.calling_convention); - lbFunctionType *ft = lb_get_function_type(m, p, pt); + lbFunctionType *ft = lb_get_function_type(m, pt); bool return_by_pointer = ft->ret.kind == lbArg_Indirect; + bool split_returns = ft->multiple_return_original_type != nullptr; unsigned param_index = 0; for (isize i = 0; i < param_count; i++) { @@ -979,13 +1014,19 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, } Type *rt = reduce_tuple_to_single_type(results); - if (return_by_pointer) { - lbValue return_ptr = {}; - if (return_ptr.value == nullptr) { - lbAddr r = lb_add_local_generated(p, rt, true); - return_ptr = r.addr; + Type *original_rt = rt; + if (split_returns) { + GB_ASSERT(rt->kind == Type_Tuple); + for (isize j = 0; j < rt->Tuple.variables.count-1; j++) { + Type *partial_return_type = rt->Tuple.variables[j]->type; + lbValue partial_return_ptr = lb_add_local_generated(p, partial_return_type, true).addr; + array_add(&processed_args, partial_return_ptr); } - GB_ASSERT(is_type_pointer(return_ptr.type)); + rt = reduce_tuple_to_single_type(rt->Tuple.variables[rt->Tuple.variables.count-1]->type); + } + + if (return_by_pointer) { + lbValue return_ptr = lb_add_local_generated(p, rt, true).addr; lb_emit_call_internal(p, value, return_ptr, processed_args, nullptr, context_ptr, inlining); result = lb_emit_load(p, return_ptr); } else if (rt != nullptr) { @@ -1005,6 +1046,22 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, lb_emit_call_internal(p, value, {}, processed_args, nullptr, context_ptr, inlining); } + if (original_rt != rt) { + GB_ASSERT(split_returns); + GB_ASSERT(original_rt->kind == Type_Tuple); + lbValue result_ptr = lb_add_local_generated(p, original_rt, false).addr; + isize ret_count = original_rt->Tuple.variables.count; + isize offset = ft->original_arg_count; + for (isize j = 0; j < ret_count-1; j++) { + lbValue ret_arg_ptr = processed_args[offset + j]; + lbValue ret_arg = lb_emit_load(p, ret_arg_ptr); + lb_emit_store(p, lb_emit_struct_ep(p, result_ptr, cast(i32)j), ret_arg); + } + lb_emit_store(p, lb_emit_struct_ep(p, result_ptr, cast(i32)(ret_count-1)), result); + + result = lb_emit_load(p, result_ptr); + } + } Entity **found = map_get(&p->module->procedure_values, value.value); diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 9159f7550..1d28ea1c6 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -1576,9 +1576,27 @@ void lb_build_assignment(lbProcedure *p, Array &lvals, Slice cons } } -void lb_build_return_stmt_internal(lbProcedure *p, lbValue const &res) { - lbFunctionType *ft = lb_get_function_type(p->module, p, p->type); +void lb_build_return_stmt_internal(lbProcedure *p, lbValue res) { + lbFunctionType *ft = lb_get_function_type(p->module, p->type); bool return_by_pointer = ft->ret.kind == lbArg_Indirect; + bool split_returns = ft->multiple_return_original_type != nullptr; + + if (split_returns) { + GB_ASSERT(res.value != nullptr); + Type *res_type = res.type; + GB_ASSERT(is_type_tuple(res_type)); + isize res_count = res_type->Tuple.variables.count; + + isize param_offset = return_by_pointer ? 1 : 0; + param_offset += ft->original_arg_count; + for (isize i = 0; i < res_count-1; i++) { + lbValue ret_ptr = {}; + ret_ptr.value = LLVMGetParam(p->value, cast(unsigned)(param_offset + i)); + ret_ptr.type = alloc_type_pointer(res_type->Tuple.variables[i]->type); + lb_emit_store(p, ret_ptr, lb_emit_struct_ev(p, res, cast(i32)i)); + } + res = lb_emit_struct_ev(p, res, cast(i32)(res_count-1)); + } if (return_by_pointer) { if (res.value != nullptr) { @@ -1617,7 +1635,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice const &return_results) { isize return_count = p->type->Proc.result_count; isize res_count = return_results.count; - lbFunctionType *ft = lb_get_function_type(p->module, p, p->type); + lbFunctionType *ft = lb_get_function_type(p->module, p->type); bool return_by_pointer = ft->ret.kind == lbArg_Indirect; if (return_count == 0) { diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index 42f84ade0..fa8433a36 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -436,7 +436,7 @@ lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue c } void lb_build_return_stmt(lbProcedure *p, Slice const &return_results); -void lb_build_return_stmt_internal(lbProcedure *p, lbValue const &res); +void lb_build_return_stmt_internal(lbProcedure *p, lbValue res); lbValue lb_emit_or_return(lbProcedure *p, Ast *arg, TypeAndValue const &tv) { lbValue lhs = {}; -- cgit v1.2.3 From aef8b25a8e8787759839eb6f02fea61390dc25bc Mon Sep 17 00:00:00 2001 From: gingerBill Date: Wed, 23 Nov 2022 23:54:12 +0000 Subject: Listen to past Bill's wisdom --- src/llvm_backend_proc.cpp | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index b47168541..d330c90a5 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -588,30 +588,16 @@ void lb_begin_procedure_body(lbProcedure *p) { if (e->token.string != "") { GB_ASSERT(!is_blank_ident(e->token)); - lbAddr res = {}; - - lbFunctionType *ft = p->abi_function_type; - if (ft->multiple_return_original_type && - i < results->variables.count-1) { - isize ret_offset = param_offset + ft->original_arg_count + i; - lbValue ptr = {}; - ptr.value = LLVMGetParam(p->value, cast(unsigned)ret_offset); - ptr.type = alloc_type_pointer(e->type); - res = lb_addr(ptr); - - lb_add_entity(p->module, e, ptr); - lb_add_debug_local_variable(p, ptr.value, e->type, e->token); - } else { - - // NOTE(bill): Don't even bother trying to optimize this with the return ptr value - // This will violate the defer rules if you do: - // foo :: proc() -> (x, y: T) { - // defer x = ... // defer is executed after the `defer` - // return // the values returned should be zeroed - // } - // NOTE(bill): REALLY, don't even bother. - res = lb_add_local(p, e->type, e); - } + // NOTE(bill): Don't even bother trying to optimize this with the return ptr value + // This will violate the defer rules if you do: + // foo :: proc() -> (x, y: T) { + // defer x = ... // defer is executed after the `defer` + // return // the values returned should be zeroed + // } + // NOTE(bill): REALLY, don't even bother. + // + // IMPORTANT NOTE(bill): REALLY, don't even bother!!!!!! + lbAddr res = lb_add_local(p, e->type, e); if (e->Variable.param_value.kind != ParameterValue_Invalid) { lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos); -- cgit v1.2.3 From 0befadde1dc9229526f0d200e423ed6360b1c1a4 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 24 Nov 2022 01:27:39 +0000 Subject: Basic copy elision support for multiple return values --- src/llvm_backend.hpp | 6 +++ src/llvm_backend_proc.cpp | 60 +++++++++++++++------ src/llvm_backend_stmt.cpp | 48 ++++++++--------- src/llvm_backend_utility.cpp | 122 ++++++++++++++++++++++++++++++------------- 4 files changed, 160 insertions(+), 76 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 4b5b24b71..59881735d 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -254,6 +254,10 @@ struct lbTargetList { }; +struct lbTupleFix { + Slice values; +}; + enum lbProcedureFlag : u32 { lbProcedureFlag_WithoutMemcpyPass = 1<<0, lbProcedureFlag_DebugAllocaCopy = 1<<1, @@ -306,6 +310,7 @@ struct lbProcedure { PtrMap selector_values; PtrMap selector_addr; + PtrMap tuple_fix_map; }; @@ -360,6 +365,7 @@ lbValue lb_emit_epi(lbModule *m, lbValue const &value, isize index); lbValue lb_emit_array_epi(lbModule *m, lbValue s, isize index); lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index); lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index); +lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index); lbValue lb_emit_array_epi(lbProcedure *p, lbValue value, isize index); lbValue lb_emit_array_ep(lbProcedure *p, lbValue s, lbValue index); lbValue lb_emit_deep_field_gep(lbProcedure *p, lbValue e, Selection sel); diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index d330c90a5..a518e501f 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -123,6 +123,7 @@ lbProcedure *lb_create_procedure(lbModule *m, Entity *entity, bool ignore_body) p->scope_stack.allocator = a; map_init(&p->selector_values, a, 0); map_init(&p->selector_addr, a, 0); + map_init(&p->tuple_fix_map, a, 0); if (p->is_foreign) { lb_add_foreign_library_path(p->module, entity->Procedure.foreign_library); @@ -711,15 +712,8 @@ Array lb_value_to_array(lbProcedure *p, lbValue value) { if (t == nullptr) { // Do nothing } else if (is_type_tuple(t)) { - GB_ASSERT(t->kind == Type_Tuple); - auto *rt = &t->Tuple; - if (rt->variables.count > 0) { - array = array_make(permanent_allocator(), rt->variables.count); - for_array(i, rt->variables) { - lbValue elem = lb_emit_struct_ev(p, value, cast(i32)i); - array[i] = elem; - } - } + array = array_make(permanent_allocator(), 0, t->Tuple.variables.count); + lb_append_tuple_values(p, &array, value); } else { array = array_make(permanent_allocator(), 1); array[0] = value; @@ -1034,18 +1028,43 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, if (original_rt != rt) { GB_ASSERT(split_returns); - GB_ASSERT(original_rt->kind == Type_Tuple); + GB_ASSERT(is_type_tuple(original_rt)); + + // IMPORTANT NOTE(bill, 2022-11-24) + // result_ptr is a dummy value which is only used to reference a tuple + // value for the "tuple-fix" + // + // The reason for the fake stack allocation is to have a unique pointer + // for the value to be used as a key within the procedure itself + lbValue result_ptr = lb_add_local_generated(p, original_rt, false).addr; isize ret_count = original_rt->Tuple.variables.count; + + auto tuple_fix_values = slice_make(permanent_allocator(), ret_count); + auto tuple_geps = slice_make(permanent_allocator(), ret_count); + isize offset = ft->original_arg_count; for (isize j = 0; j < ret_count-1; j++) { lbValue ret_arg_ptr = processed_args[offset + j]; lbValue ret_arg = lb_emit_load(p, ret_arg_ptr); - lb_emit_store(p, lb_emit_struct_ep(p, result_ptr, cast(i32)j), ret_arg); + tuple_fix_values[j] = ret_arg; } - lb_emit_store(p, lb_emit_struct_ep(p, result_ptr, cast(i32)(ret_count-1)), result); + tuple_fix_values[ret_count-1] = result; + + #if 0 + for (isize j = 0; j < ret_count; j++) { + tuple_geps[j] = lb_emit_struct_ep(p, result_ptr, cast(i32)j); + } + for (isize j = 0; j < ret_count; j++) { + lb_emit_store(p, tuple_geps[j], tuple_fix_values[j]); + } + #endif result = lb_emit_load(p, result_ptr); + + lbTupleFix tf = {tuple_fix_values}; + map_set(&p->tuple_fix_map, result_ptr.value, tf); + map_set(&p->tuple_fix_map, result.value, tf); } } @@ -2338,7 +2357,7 @@ lbValue lb_build_builtin_proc(lbProcedure *p, Ast *expr, TypeAndValue const &tv, ); LLVMSetWeak(value, weak); - if (tv.type->kind == Type_Tuple) { + if (is_type_tuple(tv.type)) { Type *fix_typed = alloc_type_tuple(); slice_init(&fix_typed->Tuple.variables, permanent_allocator(), 2); fix_typed->Tuple.variables[0] = tv.type->Tuple.variables[0]; @@ -3082,7 +3101,7 @@ lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { GB_ASSERT_MSG(tav.mode != Addressing_Invalid, "%s %s %d", expr_to_string(arg), expr_to_string(expr), tav.mode); GB_ASSERT_MSG(tav.mode != Addressing_ProcGroup, "%s", expr_to_string(arg)); Type *at = tav.type; - if (at->kind == Type_Tuple) { + if (is_type_tuple(at)) { arg_count += at->Tuple.variables.count; } else { arg_count++; @@ -3122,9 +3141,16 @@ lbValue lb_build_call_expr_internal(lbProcedure *p, Ast *expr) { lbValue a = lb_build_expr(p, arg); Type *at = a.type; if (at->kind == Type_Tuple) { - for_array(i, at->Tuple.variables) { - lbValue v = lb_emit_struct_ev(p, a, cast(i32)i); - args[arg_index++] = v; + lbTupleFix *tf = map_get(&p->tuple_fix_map, a.value); + if (tf) { + for_array(j, tf->values) { + args[arg_index++] = tf->values[j]; + } + } else { + for_array(j, at->Tuple.variables) { + lbValue v = lb_emit_struct_ev(p, a, cast(i32)j); + args[arg_index++] = v; + } } } else { args[arg_index++] = a; diff --git a/src/llvm_backend_stmt.cpp b/src/llvm_backend_stmt.cpp index 949ceed7d..d026d2ecd 100644 --- a/src/llvm_backend_stmt.cpp +++ b/src/llvm_backend_stmt.cpp @@ -698,13 +698,13 @@ void lb_build_range_tuple(lbProcedure *p, Ast *expr, Type *val0_type, Type *val1 i32 tuple_count = cast(i32)tuple->Tuple.variables.count; i32 cond_index = tuple_count-1; - lbValue cond = lb_emit_struct_ev(p, tuple_value, cond_index); + lbValue cond = lb_emit_tuple_ev(p, tuple_value, cond_index); lb_emit_if(p, cond, body, done); lb_start_block(p, body); - if (val0_) *val0_ = lb_emit_struct_ev(p, tuple_value, 0); - if (val1_) *val1_ = lb_emit_struct_ev(p, tuple_value, 1); + if (val0_) *val0_ = lb_emit_tuple_ev(p, tuple_value, 0); + if (val1_) *val1_ = lb_emit_tuple_ev(p, tuple_value, 1); if (loop_) *loop_ = loop; if (done_) *done_ = done; } @@ -1543,6 +1543,24 @@ void lb_build_static_variables(lbProcedure *p, AstValueDecl *vd) { lb_add_member(p->module, mangled_name, global_val); } } +void lb_append_tuple_values(lbProcedure *p, Array *dst_values, lbValue src_value) { + Type *t = src_value.type; + if (t->kind == Type_Tuple) { + lbTupleFix *tf = map_get(&p->tuple_fix_map, src_value.value); + if (tf) { + for_array(j, tf->values) { + array_add(dst_values, tf->values[j]); + } + } else { + for_array(i, t->Tuple.variables) { + lbValue v = lb_emit_tuple_ev(p, src_value, cast(i32)i); + array_add(dst_values, v); + } + } + } else { + array_add(dst_values, src_value); + } +} void lb_build_assignment(lbProcedure *p, Array &lvals, Slice const &values) { @@ -1554,18 +1572,8 @@ void lb_build_assignment(lbProcedure *p, Array &lvals, Slice cons for_array(i, values) { Ast *rhs = values[i]; - if (is_type_tuple(type_of_expr(rhs))) { - lbValue init = lb_build_expr(p, rhs); - Type *t = init.type; - GB_ASSERT(t->kind == Type_Tuple); - for_array(i, t->Tuple.variables) { - lbValue v = lb_emit_struct_ev(p, init, cast(i32)i); - array_add(&inits, v); - } - } else { - lbValue init = lb_build_expr(p, rhs); - array_add(&inits, init); - } + lbValue init = lb_build_expr(p, rhs); + lb_append_tuple_values(p, &inits, init); } GB_ASSERT(lvals.count == inits.count); @@ -1655,15 +1663,7 @@ void lb_build_return_stmt(lbProcedure *p, Slice const &return_results) { if (res_count != 0) { for (isize res_index = 0; res_index < res_count; res_index++) { lbValue res = lb_build_expr(p, return_results[res_index]); - Type *t = res.type; - if (t->kind == Type_Tuple) { - for_array(i, t->Tuple.variables) { - lbValue v = lb_emit_struct_ev(p, res, cast(i32)i); - array_add(&results, v); - } - } else { - array_add(&results, res); - } + lb_append_tuple_values(p, &results, res); } } else { for (isize res_index = 0; res_index < return_count; res_index++) { diff --git a/src/llvm_backend_utility.cpp b/src/llvm_backend_utility.cpp index fa8433a36..3ae9aba8f 100644 --- a/src/llvm_backend_utility.cpp +++ b/src/llvm_backend_utility.cpp @@ -339,16 +339,16 @@ void lb_emit_try_lhs_rhs(lbProcedure *p, Ast *arg, TypeAndValue const &tv, lbVal if (is_type_tuple(value.type)) { i32 n = cast(i32)(value.type->Tuple.variables.count-1); if (value.type->Tuple.variables.count == 2) { - lhs = lb_emit_struct_ev(p, value, 0); + lhs = lb_emit_tuple_ev(p, value, 0); } else { lbAddr lhs_addr = lb_add_local_generated(p, tv.type, false); lbValue lhs_ptr = lb_addr_get_ptr(p, lhs_addr); for (i32 i = 0; i < n; i++) { - lb_emit_store(p, lb_emit_struct_ep(p, lhs_ptr, i), lb_emit_struct_ev(p, value, i)); + lb_emit_store(p, lb_emit_struct_ep(p, lhs_ptr, i), lb_emit_tuple_ev(p, value, i)); } lhs = lb_addr_load(p, lhs_addr); } - rhs = lb_emit_struct_ev(p, value, n); + rhs = lb_emit_tuple_ev(p, value, n); } else { rhs = value; } @@ -943,6 +943,54 @@ char const *llvm_type_kinds[] = { "LLVMBFloatTypeKind", }; +gb_internal lbValue lb_emit_struct_ep_internal(lbProcedure *p, lbValue s, i32 index, Type *result_type) { + Type *t = base_type(type_deref(s.type)); + + i32 original_index = index; + index = lb_convert_struct_index(p->module, t, index); + + if (lb_is_const(s)) { + // NOTE(bill): this cannot be replaced with lb_emit_epi + lbModule *m = p->module; + lbValue res = {}; + LLVMValueRef indices[2] = {llvm_zero(m), LLVMConstInt(lb_type(m, t_i32), index, false)}; + res.value = LLVMConstGEP2(lb_type(m, type_deref(s.type)), s.value, indices, gb_count_of(indices)); + res.type = alloc_type_pointer(result_type); + return res; + } else { + lbValue res = {}; + LLVMTypeRef st = lb_type(p->module, type_deref(s.type)); + // gb_printf_err("%s\n", type_to_string(s.type)); + // gb_printf_err("%s\n", LLVMPrintTypeToString(LLVMTypeOf(s.value))); + // gb_printf_err("%d\n", index); + GB_ASSERT_MSG(LLVMGetTypeKind(st) == LLVMStructTypeKind, "%s", llvm_type_kinds[LLVMGetTypeKind(st)]); + unsigned count = LLVMCountStructElementTypes(st); + GB_ASSERT_MSG(count >= cast(unsigned)index, "%u %d %d", count, index, original_index); + + res.value = LLVMBuildStructGEP2(p->builder, st, s.value, cast(unsigned)index, ""); + res.type = alloc_type_pointer(result_type); + return res; + } +} + +lbValue lb_emit_tuple_ep(lbProcedure *p, lbValue ptr, i32 index) { + Type *t = type_deref(ptr.type); + GB_ASSERT(is_type_tuple(t)); + Type *result_type = t->Tuple.variables[index]->type; + + lbValue res = {}; + lbTupleFix *tf = map_get(&p->tuple_fix_map, ptr.value); + if (tf) { + res = tf->values[index]; + GB_ASSERT(are_types_identical(res.type, result_type)); + res = lb_address_from_load_or_generate_local(p, res); + } else { + res = lb_emit_struct_ep_internal(p, ptr, index, result_type); + } + return res; +} + + lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { GB_ASSERT(is_type_pointer(s.type)); Type *t = base_type(type_deref(s.type)); @@ -958,8 +1006,7 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { GB_ASSERT(index == -1); return lb_emit_union_tag_ptr(p, s); } else if (is_type_tuple(t)) { - GB_ASSERT(t->Tuple.variables.count > 0); - result_type = t->Tuple.variables[index]->type; + return lb_emit_tuple_ep(p, s, index); } else if (is_type_complex(t)) { Type *ft = base_complex_elem_type(t); switch (index) { @@ -1024,34 +1071,45 @@ lbValue lb_emit_struct_ep(lbProcedure *p, lbValue s, i32 index) { GB_ASSERT_MSG(result_type != nullptr, "%s %d", type_to_string(t), index); - i32 original_index = index; - index = lb_convert_struct_index(p->module, t, index); - - if (lb_is_const(s)) { - // NOTE(bill): this cannot be replaced with lb_emit_epi - lbModule *m = p->module; - lbValue res = {}; - LLVMValueRef indices[2] = {llvm_zero(m), LLVMConstInt(lb_type(m, t_i32), index, false)}; - res.value = LLVMConstGEP2(lb_type(m, type_deref(s.type)), s.value, indices, gb_count_of(indices)); - res.type = alloc_type_pointer(result_type); - return res; + return lb_emit_struct_ep_internal(p, s, index, result_type); +} + +lbValue lb_emit_tuple_ev(lbProcedure *p, lbValue value, i32 index) { + Type *t = value.type; + GB_ASSERT(is_type_tuple(t)); + Type *result_type = t->Tuple.variables[index]->type; + + lbValue res = {}; + lbTupleFix *tf = map_get(&p->tuple_fix_map, value.value); + if (tf) { + res = tf->values[index]; + GB_ASSERT(are_types_identical(res.type, result_type)); } else { - lbValue res = {}; - LLVMTypeRef st = lb_type(p->module, type_deref(s.type)); - // gb_printf_err("%s\n", type_to_string(s.type)); - // gb_printf_err("%s\n", LLVMPrintTypeToString(LLVMTypeOf(s.value))); - // gb_printf_err("%d\n", index); - GB_ASSERT_MSG(LLVMGetTypeKind(st) == LLVMStructTypeKind, "%s", llvm_type_kinds[LLVMGetTypeKind(st)]); - unsigned count = LLVMCountStructElementTypes(st); - GB_ASSERT_MSG(count >= cast(unsigned)index, "%u %d %d", count, index, original_index); - - res.value = LLVMBuildStructGEP2(p->builder, st, s.value, cast(unsigned)index, ""); - res.type = alloc_type_pointer(result_type); - return res; + if (t->Tuple.variables.count == 1) { + GB_ASSERT(index == 0); + // value.type = result_type; + return value; + } + if (LLVMIsALoadInst(value.value)) { + lbValue res = {}; + res.value = LLVMGetOperand(value.value, 0); + res.type = alloc_type_pointer(value.type); + lbValue ptr = lb_emit_struct_ep(p, res, index); + return lb_emit_load(p, ptr); + } + + res.value = LLVMBuildExtractValue(p->builder, value.value, cast(unsigned)index, ""); + res.type = result_type; } + return res; } lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { + Type *t = base_type(s.type); + if (is_type_tuple(t)) { + return lb_emit_tuple_ev(p, s, index); + } + if (LLVMIsALoadInst(s.value)) { lbValue res = {}; res.value = LLVMGetOperand(s.value, 0); @@ -1060,7 +1118,6 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { return lb_emit_load(p, ptr); } - Type *t = base_type(s.type); Type *result_type = nullptr; switch (t->kind) { @@ -1113,12 +1170,7 @@ lbValue lb_emit_struct_ev(lbProcedure *p, lbValue s, i32 index) { GB_PANIC("lb_emit_union_tag_value"); case Type_Tuple: - GB_ASSERT(t->Tuple.variables.count > 0); - result_type = t->Tuple.variables[index]->type; - if (t->Tuple.variables.count == 1) { - return s; - } - break; + return lb_emit_tuple_ev(p, s, index); case Type_Slice: switch (index) { case 0: result_type = alloc_type_pointer(t->Slice.elem); break; -- cgit v1.2.3 From 615eccb6d193e9eb2575cfb28b1366c648296c31 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Thu, 24 Nov 2022 14:26:45 +0000 Subject: Correct return ptr semantics for split returns --- src/llvm_backend_proc.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index a518e501f..f14fc8a8c 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -511,7 +511,14 @@ void lb_begin_procedure_body(lbProcedure *p) { } } - Type *ptr_type = alloc_type_pointer(reduce_tuple_to_single_type(p->type->Proc.results)); + Type *return_ptr_type = reduce_tuple_to_single_type(p->type->Proc.results); + bool split_returns = ft->multiple_return_original_type != nullptr; + if (split_returns) { + GB_ASSERT(is_type_tuple(return_ptr_type)); + auto const &variables = return_ptr_type->Tuple.variables; + return_ptr_type = variables[variables.count-1]->type; + } + Type *ptr_type = alloc_type_pointer(return_ptr_type); Entity *e = alloc_entity_param(nullptr, make_token_ident(name), ptr_type, false, false); e->flags |= EntityFlag_NoAlias; -- cgit v1.2.3 From d88b052d2d9aa8fa012be314bd29d7ae311fc941 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Fri, 25 Nov 2022 23:57:55 +0000 Subject: Naïve optimization of named _split_ multiple return valued when `defer` is never used MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is a naïve optimization but it helps a lot in the general case where callee temporary stack variables are not allocated to represent the named return values by using that specific memory. In the future, try to check if a specific named return value is ever used a `defer` within a procedure or not, or is ever passed to a nested procedure call (e.g. possibly escapes). --- src/check_decl.cpp | 5 +++ src/check_expr.cpp | 3 ++ src/check_stmt.cpp | 3 ++ src/checker.hpp | 2 ++ src/llvm_abi.cpp | 4 ++- src/llvm_backend.hpp | 2 +- src/llvm_backend_expr.cpp | 2 +- src/llvm_backend_general.cpp | 4 +-- src/llvm_backend_proc.cpp | 76 +++++++++++++++++++++++++++++++++++++------- 9 files changed, 84 insertions(+), 17 deletions(-) (limited to 'src/llvm_backend_proc.cpp') diff --git a/src/check_decl.cpp b/src/check_decl.cpp index bb56749af..0e41dbbb5 100644 --- a/src/check_decl.cpp +++ b/src/check_decl.cpp @@ -1544,8 +1544,12 @@ void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *ty // NOTE(bill): Don't err here } + GB_ASSERT(decl->defer_use_checked == false); + check_stmt_list(ctx, bs->stmts, Stmt_CheckScopeDecls); + decl->defer_use_checked = true; + for_array(i, bs->stmts) { Ast *stmt = bs->stmts[i]; if (stmt->kind == Ast_ValueDecl) { @@ -1580,6 +1584,7 @@ void check_proc_body(CheckerContext *ctx_, Token token, DeclInfo *decl, Type *ty } } } + } check_close_scope(ctx); diff --git a/src/check_expr.cpp b/src/check_expr.cpp index c7585b51c..9846199f8 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -6763,6 +6763,9 @@ ExprKind check_call_expr(CheckerContext *c, Operand *operand, Ast *call, Ast *pr if (initial_entity != nullptr && initial_entity->kind == Entity_Procedure) { if (initial_entity->Procedure.deferred_procedure.entity != nullptr) { call->viral_state_flags |= ViralStateFlag_ContainsDeferredProcedure; + if (c->decl) { + c->decl->defer_used += 1; + } } } diff --git a/src/check_stmt.cpp b/src/check_stmt.cpp index 9cacb4a35..502eed57e 100644 --- a/src/check_stmt.cpp +++ b/src/check_stmt.cpp @@ -2018,6 +2018,9 @@ void check_stmt_internal(CheckerContext *ctx, Ast *node, u32 flags) { ctx->in_defer = true; check_stmt(ctx, ds->stmt, 0); ctx->in_defer = out_in_defer; + if (ctx->decl) { + ctx->decl->defer_used += 1; + } } case_end; diff --git a/src/checker.hpp b/src/checker.hpp index badcd93d5..37232ea95 100644 --- a/src/checker.hpp +++ b/src/checker.hpp @@ -158,6 +158,8 @@ struct DeclInfo { bool is_using; bool where_clauses_evaluated; bool proc_checked; + isize defer_used; + bool defer_use_checked; CommentGroup *comment; CommentGroup *docs; diff --git a/src/llvm_abi.cpp b/src/llvm_abi.cpp index c35affad5..166fcb3ee 100644 --- a/src/llvm_abi.cpp +++ b/src/llvm_abi.cpp @@ -1426,7 +1426,9 @@ LB_ABI_INFO(lb_get_abi_info_internal) { switch (build_context.metrics.arch) { case TargetArch_amd64: - if (build_context.metrics.os == TargetOs_windows || build_context.metrics.abi == TargetABI_Win64) { + if (build_context.metrics.os == TargetOs_windows) { + return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); + } else if (build_context.metrics.abi == TargetABI_Win64) { return lbAbiAmd64Win64::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); } else if (build_context.metrics.abi == TargetABI_SysV) { return lbAbiAmd64SysV::abi_info(c, arg_types, arg_count, return_type, return_is_defined, return_is_tuple, calling_convention); diff --git a/src/llvm_backend.hpp b/src/llvm_backend.hpp index 59881735d..911c915a8 100644 --- a/src/llvm_backend.hpp +++ b/src/llvm_backend.hpp @@ -398,7 +398,7 @@ lbContextData *lb_push_context_onto_stack_from_implicit_parameter(lbProcedure *p lbAddr lb_add_global_generated(lbModule *m, Type *type, lbValue value={}, Entity **entity_=nullptr); -lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, i32 param_index=0, bool force_no_init=false); +lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e=nullptr, bool zero_init=true, bool force_no_init=false); void lb_add_foreign_library_path(lbModule *m, Entity *e); diff --git a/src/llvm_backend_expr.cpp b/src/llvm_backend_expr.cpp index 389bfb151..2c1bfecd6 100644 --- a/src/llvm_backend_expr.cpp +++ b/src/llvm_backend_expr.cpp @@ -134,7 +134,7 @@ lbValue lb_emit_unary_arith(lbProcedure *p, TokenKind op, lbValue x, Type *type) Type *elem_type = base_array_type(type); // NOTE(bill): Doesn't need to be zero because it will be initialized in the loops - lbAddr res_addr = lb_add_local(p, type, nullptr, false, 0, true); + lbAddr res_addr = lb_add_local(p, type, nullptr, false, true); lbValue res = lb_addr_get_ptr(p, res_addr); bool inline_array_arith = lb_can_try_to_inline_array_arith(type); diff --git a/src/llvm_backend_general.cpp b/src/llvm_backend_general.cpp index 9fef5b05e..777755794 100644 --- a/src/llvm_backend_general.cpp +++ b/src/llvm_backend_general.cpp @@ -2868,7 +2868,7 @@ lbValue lb_build_cond(lbProcedure *p, Ast *cond, lbBlock *true_block, lbBlock *f } -lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, i32 param_index, bool force_no_init) { +lbAddr lb_add_local(lbProcedure *p, Type *type, Entity *e, bool zero_init, bool force_no_init) { GB_ASSERT(p->decl_block != p->curr_block); LLVMPositionBuilderAtEnd(p->builder, p->decl_block->block); @@ -2922,7 +2922,7 @@ lbAddr lb_add_local_generated(lbProcedure *p, Type *type, bool zero_init) { } lbAddr lb_add_local_generated_temp(lbProcedure *p, Type *type, i64 min_alignment) { - lbAddr res = lb_add_local(p, type, nullptr, false, 0, true); + lbAddr res = lb_add_local(p, type, nullptr, false, true); lb_try_update_alignment(res.addr, cast(unsigned)min_alignment); return res; } diff --git a/src/llvm_backend_proc.cpp b/src/llvm_backend_proc.cpp index f14fc8a8c..f8dffa9b6 100644 --- a/src/llvm_backend_proc.cpp +++ b/src/llvm_backend_proc.cpp @@ -1,4 +1,3 @@ - LLVMValueRef lb_call_intrinsic(lbProcedure *p, const char *name, LLVMValueRef* args, unsigned arg_count, LLVMTypeRef* types, unsigned type_count) { unsigned id = LLVMLookupIntrinsicID(name, gb_strlen(name)); @@ -596,16 +595,69 @@ void lb_begin_procedure_body(lbProcedure *p) { if (e->token.string != "") { GB_ASSERT(!is_blank_ident(e->token)); - // NOTE(bill): Don't even bother trying to optimize this with the return ptr value - // This will violate the defer rules if you do: - // foo :: proc() -> (x, y: T) { - // defer x = ... // defer is executed after the `defer` - // return // the values returned should be zeroed - // } - // NOTE(bill): REALLY, don't even bother. - // - // IMPORTANT NOTE(bill): REALLY, don't even bother!!!!!! - lbAddr res = lb_add_local(p, e->type, e); + lbAddr res = {}; + if (p->entity && p->entity->decl_info && + p->entity->decl_info->defer_use_checked && + p->entity->decl_info->defer_used == 0) { + + // NOTE(bill): this is a bodge to get around the issue of the problem BELOW + // We check to see if we ever use a defer statement ever within a procedure and if it + // if it never happens, see if you can possibly do take the return value pointer + // + // NOTE(bill): this could be buggy in that I have missed a case where `defer` was used + // + // TODO(bill): This could be optimized to check to see where a `defer` only uses + // the variable in question + + bool has_return_ptr = p->return_ptr.addr.value != nullptr; + lbValue ptr = {}; + + if (ft->multiple_return_original_type != nullptr) { + isize the_offset = -1; + if (i+1 < results->variables.count) { + the_offset = cast(isize)param_offset + ft->original_arg_count + i; + } else if (has_return_ptr) { + GB_ASSERT(i+1 == results->variables.count); + the_offset = 0; + } + if (the_offset >= 0) { + lbValue ptr = {}; + ptr.value = LLVMGetParam(p->value, cast(unsigned)the_offset); + ptr.type = alloc_type_pointer(e->type); + + + } + } else if (has_return_ptr) { + lbValue ptr = p->return_ptr.addr; + + if (results->variables.count > 1) { + ptr = lb_emit_tuple_ep(p, ptr, cast(i32)i); + } + GB_ASSERT(is_type_pointer(ptr.type)); + GB_ASSERT(are_types_identical(type_deref(ptr.type), e->type)); + } + + if (ptr.value != nullptr) { + lb_add_entity(p->module, e, ptr); + lb_add_debug_local_variable(p, ptr.value, e->type, e->token); + // NOTE(bill): no need to zero on the callee side as it is zeroed on the caller side + + res = lb_addr(ptr); + } + } + + if (res.addr.type == nullptr) { + // NOTE(bill): Don't even bother trying to optimize this with the return ptr value + // This will violate the defer rules if you do: + // foo :: proc() -> (x, y: T) { + // defer x = ... // defer is executed after the `defer` + // return // the values returned should be zeroed + // } + // NOTE(bill): REALLY, don't even bother. + // + // IMPORTANT NOTE(bill): REALLY, don't even bother!!!!!! + res = lb_add_local(p, e->type, e); + } if (e->Variable.param_value.kind != ParameterValue_Invalid) { lbValue c = lb_handle_param_value(p, e->type, e->Variable.param_value, e->token.pos); @@ -1006,7 +1058,7 @@ lbValue lb_emit_call(lbProcedure *p, lbValue value, Array const &args, GB_ASSERT(rt->kind == Type_Tuple); for (isize j = 0; j < rt->Tuple.variables.count-1; j++) { Type *partial_return_type = rt->Tuple.variables[j]->type; - lbValue partial_return_ptr = lb_add_local_generated(p, partial_return_type, true).addr; + lbValue partial_return_ptr = lb_add_local(p, partial_return_type, nullptr, true, false).addr; array_add(&processed_args, partial_return_ptr); } rt = reduce_tuple_to_single_type(rt->Tuple.variables[rt->Tuple.variables.count-1]->type); -- cgit v1.2.3