From 6b1eb459ae2246706505d6486ff3ef9681041aa0 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:47 +0000 Subject: [PATCH 01/14] Use the runtime count for partially initialized multidimensional VLAs [GH #557] Partially initialized multidimensional GNU variable-length arrays (e.g., "E a[n][3] = {{E(3)}};") could be constructed and destroyed with the wrong number of elements, and some shapes caused an assertion failure: the repeat count of the remaining elements was taken from the total element count of the array, and the starting element was not scaled for rows of variable length. The repeat counts of such initializers are now encoded and decoded in one place (documented in il_def.h). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/decl_inits.c | 9 +- src/il_def.h | 9 +- src/lower_eh.c | 13 +- src/lower_il.h | 10 + src/lower_init.c | 333 ++++++++++++------ ...-vla-partial-init-runtime-repeat-count.txt | 10 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + ...ray-construction-vla-partial-throw.sft.cpp | 64 ++++ .../array-construction-vla-partial.sft.cpp | 268 ++++++++++++++ 14 files changed, 612 insertions(+), 110 deletions(-) create mode 100644 src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt create mode 100644 tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp create mode 100644 tests/tests/regressions/array-construction-vla-partial.sft.cpp diff --git a/src/decl_inits.c b/src/decl_inits.c index c077697a93..9e89b2f573 100644 --- a/src/decl_inits.c +++ b/src/decl_inits.c @@ -1983,7 +1983,7 @@ the position at which diagnostics should be issued. if (type_is(etype, tk_array)) { /* The element is a sub-array. Create a single potentially-repeated initializer for all array levels. */ - if (!etype->variant.array.is_vla) { + if (!is_vla_type(etype)) { count *= num_array_elements(etype); } else { /* is->variable_size_array should be TRUE, and therefore we should @@ -2037,8 +2037,11 @@ the position at which diagnostics should be issued. ck_repeat_init on top of it if needed. */ check_assertion(type_is(skip_typerefs(array_con->type), tk_array) && constant_is(array_con, ck_aggregate)); - if (is->variable_size_array && !is->non_top_level_aggregate) { - /* Something like "new T[x]{...}" where x is a run-time value. + if (is->variable_size_array && + (!is->non_top_level_aggregate || + is_vla_type(array_con->type))) { + /* Something like "new T[x]{...}" where x is a run-time value, or + a (possibly nested) VLA such as a row of "T a[2][n] = {...}". We don't know a priori how many default initializations are needed, but we have to represent it in some way so that lowering (or a back end) knows which routine to call. We use a zero count, diff --git a/src/il_def.h b/src/il_def.h index 7f5cc12d80..001461c24f 100644 --- a/src/il_def.h +++ b/src/il_def.h @@ -5353,7 +5353,14 @@ typedef struct a_constant { zero is used for new and delete of an array, and means "use the number of elements recorded along with the storage allocation". A similar case occurs - when initializing a variable-length array. */ + when initializing a variable-length array: a count + of zero (which can also appear in a nested + variable-length row of a multidimensional array) + initializes all the remaining elements, a number + known only at run time. In that case the repeated + constant always initializes leaf elements and the + ck_init_repeat is the last constant of its + aggregate. */ a_byte_boolean multidimensional_aggr_tail_not_repeated; /* This flag is used to indicate that the repeated diff --git a/src/lower_eh.c b/src/lower_eh.c index 0b96251195..1c213f5114 100644 --- a/src/lower_eh.c +++ b/src/lower_eh.c @@ -3406,7 +3406,9 @@ region description entry). The value put in the table is arbitrary, but use -1. */ elem_count = -1; elem_type = entity_type; - if (!ipdp->array_element_sequence) { + if (is_array_type(entity_type)) { + /* The count is in elements of the innermost array, both for a whole + VLA and for a sequence of array elements that are arrays. */ elem_type = underlying_array_element_type(entity_type); } /* if */ } else if (ipdp->array_element_sequence) { @@ -4098,6 +4100,12 @@ The region table variable is created if necessary. set_var_init_pos_descr(dip->variable->vla_element_count_variable, &ipd); make_handle_for_entity(&ipd, &vla_elem_count_handle, insert_location); } /* if */ + } else if (dedp->runtime_elem_count_var != NULL) { + /* A sequence of array elements whose number is known only at run time + is described like a VLA. */ + is_vla = TRUE; + set_var_init_pos_descr(dedp->runtime_elem_count_var, &ipd); + make_handle_for_entity(&ipd, &vla_elem_count_handle, insert_location); } /* if */ #endif /* ABI_COMPATIBILITY_VERSION < 306 */ dedp->next_in_region_table = next_dip; @@ -4207,7 +4215,8 @@ stop_before == NULL means stop at the beginning of the current lifetime. } else #endif /* !RUNTIME_VLA_DEALLOCATION_NEEDS_ELEMENT_COUNT */ /* Do not insert code here. */ - if (is_dynamic_init_for_vla(dip)) { + if (is_dynamic_init_for_vla(dip) || + dedp->runtime_elem_count_var != NULL) { /* The entry has a VLA element count entry, so clone that too. */ (void)clone_raw_region_table_entry(next_entry, ®ion_number); } /* if */ diff --git a/src/lower_il.h b/src/lower_il.h index 7989b2eb1b..8c81e488e9 100644 --- a/src/lower_il.h +++ b/src/lower_il.h @@ -337,6 +337,16 @@ typedef struct a_destructible_entity_descr { slightly different than the front end order (reflected by the dynamic init next_in_destruction_list pointer). */ + a_variable_ptr + runtime_elem_count_var; + /* When exceptions are enabled and the entity is a + sequence of array elements whose number is known + only at run time (e.g., the default-initialized + remainder of a row of a partially initialized + multidimensional variable-length array), points to + a size_t variable holding that number, which the + region table entry refers to like the element count + of a VLA. NULL otherwise. */ #endif /* GENERATE_EH_TABLES */ a_byte_boolean initialization_done; diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..1aa243a8bc 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -1251,6 +1251,7 @@ Clear the fields of a destructible entity description to default values. dedp->region_number = null_eh_region_number; dedp->region_table_entry = NULL; dedp->next_in_region_table = NULL; + dedp->runtime_elem_count_var = NULL; #endif /* GENERATE_EH_TABLES */ dedp->initialization_done = FALSE; dedp->needs_subobject_construction_vtbl = FALSE; @@ -1775,8 +1776,11 @@ initialization (when ipdp->array_element_sequence is TRUE). if (ipdp->array_element_count == 0) { num_elem_node = num_elem_node_if_array(ipdp); } /* if */ - /* Create a helper routine to do the initialization and call it. */ - insert_call_to_initialize_entity(type_from_init_pos_descr(ipdp), + /* Create a helper routine to do the initialization and call it. The + entity type is the type of each element of the sequence (the + position itself can be a variably-sized row of a multidimensional + array). */ + insert_call_to_initialize_entity(ipdp->array_element_type, have_complete_object, add_address_of_to_node(entity_node), num_elem_node, @@ -2771,8 +2775,8 @@ all dimensions. if (ipdp->variable != NULL && ipdp->variable->is_vla && !ipdp->indirect_through_variable && - (ipdp->modifiers == NULL || ipdp->array_element_sequence)) { - /* The entity being accessed is a variable-length array (VLA). */ + ipdp->modifiers == NULL) { + /* The entity being accessed is a whole variable-length array (VLA). */ a_variable_ptr num_elem_var; /* Get the variable that has been set to the number of elements in the VLA. For multi-dimensional arrays, it gives the total across all @@ -2810,8 +2814,24 @@ all dimensions. if (ipdp->array_element_count == 0 && ipdp->num_elem_node != NULL) { /* For variably-sized arrays, use the expression that has already been created to describe the number of elements in the array. */ + /* For a VLA variable, the expression was built (by + set_partial_initialization_start) from the VLA dimension + temporaries, which do not change once set, and it is not + otherwise evaluated, so it must not be replaced by an + assignment to a temporary. */ + a_boolean vars_can_change = !(ipdp->variable != NULL && + ipdp->variable->is_vla && + !ipdp->indirect_through_variable); num_elem_node = make_reusable_copy(ipdp->num_elem_node, - /*vars_can_change=*/TRUE); + vars_can_change); + num_elem_node = add_cast_if_necessary(num_elem_node, + integer_type( +#if IA64_ABI + targ_size_t_int_kind +#else /* !IA64_ABI */ + targ_runtime_elem_count_int_kind +#endif /* IA64_ABI */ + )); } else { num_elem_node = num_elem_node_from_count(array_element_count); } /* if */ @@ -6563,6 +6583,156 @@ to lower_dynamic_init_aggregate_constant; see their description there. } /* helper_routine_to_initialize_repeated_constant */ +static void set_partial_initialization_start(an_init_pos_descr_ptr ipdp, + a_type_ptr aggr_type, + a_targ_size_t curr_elem) +/* +ipdp describes the elements of the variably-sized array type aggr_type that +remain to be default initialized, beginning with element curr_elem, to +complete a partial initialization. Set ipdp->num_elem_node and +ipdp->partial_initialization_starting_element so that num_elem_node_if_array +yields the number of remaining elements, counted (like the total number of +elements) in elements of the innermost array. When the element type of +aggr_type is itself a VLA (e.g., "A a[2][n] = {{A()}};"), the size of an +element is known only at runtime, so the remaining count is computed as an +expression. +*/ +{ + a_type_ptr elem_type = array_element_type(aggr_type); + a_type_ptr count_type; + an_expr_node_ptr count_node, used_node; + + if (is_vla_type(aggr_type)) { + /* For VLA types (e.g., "A a[n] = {A()};"), determine the number + of elements in the array from the VLA type (which is assumed + to have been lowered already, if needed). */ + ipdp->num_elem_node = vla_dimension_expr_for_type(aggr_type); + } /* if */ + check_assertion(ipdp->num_elem_node != NULL); + ipdp->partial_initialization_starting_element = + (a_host_large_integer)curr_elem; + if (is_array_type(elem_type)) { + /* For the multi-dimensional array case, ensure that the starting + element takes into account all of the elements that have already + been initialized. */ + if (is_vla_type(elem_type)) { + if (curr_elem != 0) { + count_type = integer_type(targ_size_t_int_kind); + used_node = add_cast_if_necessary( + vla_dimension_expr_for_type(elem_type), + count_type); + used_node->next = node_for_host_large_integer( + (a_host_large_integer)curr_elem, + targ_size_t_int_kind); + used_node = make_operator_node((an_expr_operator_kind)eok_multiply, + count_type, used_node); + count_node = add_cast_if_necessary(ipdp->num_elem_node, count_type); + count_node->next = used_node; + ipdp->num_elem_node = + make_operator_node((an_expr_operator_kind)eok_subtract, + count_type, count_node); + } /* if */ + ipdp->partial_initialization_starting_element = 0; + } else { + ipdp->partial_initialization_starting_element *= + (a_host_large_integer)num_array_elements(elem_type); + } /* if */ + } /* if */ +} /* set_partial_initialization_start */ + + +static a_constant_ptr set_init_pos_for_init_repeat( + an_init_pos_descr_ptr ipdp, + an_init_pos_descr_ptr outer_ipdp, + a_type_ptr aggr_type, + a_targ_size_t curr_elem, + a_constant_ptr repeat_con, + a_boolean strip_nested_repeats) +/* +repeat_con is a ck_init_repeat constant that initializes elements of the +array type aggr_type, beginning with element curr_elem. outer_ipdp describes +the array and ipdp (a copy of it positioned at element curr_elem) is updated +to describe the sequence of elements initialized by repeat_con. This is the +only place where lowering decodes the repeat count (see il_def.h); all users +of the resulting sequence get the number of elements from +num_elem_node_if_array (or ipdp->array_element_count when that is nonzero). +A count of zero means that the remaining elements of a variably-sized array +(possibly a nested row of one) are initialized; the number is known only at +run time, and the repeated constant then initializes the innermost elements. +A leaf entity repeated with a nonzero count for an array whose rows have +variable length (as for "A a[2][n];") initializes the whole array; the count +then covers only the constant dimensions and the number of innermost elements +is likewise known only at run time. +If strip_nested_repeats is TRUE and the repeated constant is an aggregate +consisting of a single repeat (as for a multidimensional array), the nested +repeats are folded into the count. Return the constant to be repeated. +*/ +{ + a_constant_ptr repeated_con = repeat_con->variant.init_repeat.constant; + a_targ_size_t count = repeat_con->variant.init_repeat.count; + + ipdp->array_element_sequence = TRUE; + ipdp->array_element_type = repeated_con->type; + if (count == 0) { + /* This initialization completes a partial initialization of a + variably-sized array. Make a note of the starting element that + needs initialization (which could be zero, in cases like + "new A[n] {}"). */ + check_assertion(!is_array_type(repeated_con->type)); + set_partial_initialization_start(ipdp, aggr_type, curr_elem); + } else if (is_vla_type(array_element_type(aggr_type)) && + !is_array_type(repeated_con->type)) { + /* A leaf entity repeated for all the elements of an array with rows of + variable length (e.g., "A a[2][n];"). The count is the number of + leaf elements known at compile time, i.e., the product of the + constant dimensions; the actual number, which also depends on the + variable dimensions, is known only at run time. */ + a_type_ptr type; + a_targ_size_t const_elems = 1; + + check_assertion(!outer_ipdp->array_element_sequence && curr_elem == 0); + for (type = skip_typerefs(aggr_type); is_array_type(type); + type = skip_typerefs(array_element_type(type))) { + if (!type->variant.array.is_vla) { + const_elems *= type->variant.array.variant.number_of_elements; + } /* if */ + } /* for */ + check_assertion(const_elems == count); + ipdp->num_elem_node = add_cast_if_necessary( + vla_dimension_expr_for_type(aggr_type), + integer_type(targ_size_t_int_kind)); + ipdp->array_element_count = 0; + ipdp->partial_initialization_starting_element = 0; + } else { + if (outer_ipdp->array_element_sequence) { + /* Flatten multi-dimensional arrays. */ + ipdp->array_element_count = outer_ipdp->array_element_count * + (a_targ_ptrdiff_t)count; + } else { + ipdp->array_element_count = (a_targ_ptrdiff_t)count; + } /* if */ + if (strip_nested_repeats) { + /* See if the constant is a repeated aggregate (used to initialize + multi-dimensional arrays). If so, strip any applicable + ck_init_repeat constants and increase the count accordingly. */ + while (repeated_con->kind == ck_aggregate && + repeated_con->variant.aggregate.first_constant->kind == + ck_init_repeat && + repeated_con->variant.aggregate.first_constant == + repeated_con->variant.aggregate.last_constant) { + a_constant_ptr rcon = repeated_con->variant.aggregate.first_constant; + check_assertion(rcon->variant.init_repeat.count != 0); + ipdp->array_element_count *= + (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; + repeated_con = rcon->variant.init_repeat.constant; + ipdp->array_element_type = repeated_con->type; + } /* while */ + } /* if */ + } /* if */ + return repeated_con; +} /* set_init_pos_for_init_repeat */ + + static void lower_dynamic_init_aggregate_constant( a_constant_ptr aggr_const, an_init_pos_descr_ptr ipdp, @@ -6794,13 +6964,15 @@ dealt with). "lower_dynamic_init_aggregate_constant: repeat on non-array"); repeated_con = con_ptr->variant.init_repeat.constant; elem_type = array_element_type(aggr_type); - if (has_aggregate_with_dynamic(repeated_con) && + if (con_ptr->variant.init_repeat.count != 0 && + has_aggregate_with_dynamic(repeated_con) && !identical_types_ignoring_qualifiers(elem_type, repeated_con->type)) { /* As a shortcut, the IL allows a single "leaf" entity to be repeated for a multi-dimensional aggregate constant. Remove this shortcut and create IL that represents the structure of the - multi-dimensional array. */ + multi-dimensional array. (A run-time count, i.e., zero, is + instead handled as a flattened sequence of leaf entities.) */ handle_multidimensional_ck_init_repeat(con_ptr, elem_type); /* Update information about the revised repeated constant. */ check_assertion(con_ptr->kind == (a_constant_repr_kind)ck_init_repeat); @@ -6817,43 +6989,9 @@ dealt with). aggr_type->variant.array.bound_is_zero) { /* Nothing to initialize here. */ } else { - ipd.array_element_sequence = TRUE; - ipd.array_element_type = repeated_con->type; - if (con_ptr->variant.init_repeat.count == 0) { - /* If the repeat count is zero, this initialization is being used - to complete a partial-initialization of a variably-sized array. - Make a note of the starting element that needs initialization - (which could be zero, in cases like "new A[n] {}"). */ - if (is_vla_type(aggr_type)) { - /* For VLA types (e.g., "A a[n] = {A()};"), determine the number - of elements in the array from the VLA type (which is assumed - to have been lowered already, if needed). */ - ipd.num_elem_node = vla_dimension_expr_for_type(aggr_type); - } /* if */ - check_assertion(ipd.num_elem_node != NULL); - ipd.partial_initialization_starting_element = - (a_host_large_integer)ipmp->curr_elem; - if (!is_variably_modified_type(aggr_type) && - is_array_type(array_element_type(aggr_type))) { - /* For the multi-dimensional array case, ensure that the - starting element takes into account all of the elements - that have already been initialized. */ - a_host_large_integer num_elems = - (a_host_large_integer)num_array_elements( - array_element_type(aggr_type)); - - ipd.partial_initialization_starting_element *= num_elems; - } /* if */ - } else { - if (ipdp->array_element_sequence) { - /* Flatten multi-dimensional arrays. */ - ipd.array_element_count = ipdp->array_element_count * - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } else { - ipd.array_element_count = - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } /* if */ - } /* if */ + (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/FALSE); if (repeated_con->kind == (a_constant_repr_kind)ck_dynamic_init && repeated_con->variant.dynamic_init.ptr->kind != (a_dynamic_init_kind)dik_nonconstant_aggregate) { @@ -6881,10 +7019,9 @@ dealt with). /*using_as_dest=*/FALSE); if (con_ptr->variant.init_repeat.count == 0) { /* The repeat count isn't known at compilation time; use - ipd.num_elem_node for the count. */ - check_assertion(ipd.num_elem_node != NULL); - args->next = make_reusable_copy(ipd.num_elem_node, - /*vars_can_change=*/TRUE); + the run-time count of the remaining elements. */ + args->next = num_elem_node_if_array(&ipd); + check_assertion(args->next != NULL); } else { args->next = node_for_host_large_integer( (a_host_large_integer)con_ptr->variant.init_repeat.count, @@ -6912,65 +7049,21 @@ dealt with). } else { lower_constant(repeated_con); } /* if */ - if (ipd.indirect_through_variable) { + if (ipd.indirect_through_variable || + con_ptr->variant.init_repeat.count == 0) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant aggregate initialization. This can occur when initializing a (portion of a) variably-sized array with a constant value - (from a constexpr constructor). Generate executable statements - to perform the initialization. */ + (from a constexpr constructor). A run-time count (zero) cannot + be represented in a constant either, even for a simple + variable. Generate executable statements to perform the + initialization. */ an_expr_node_ptr entity_node; - ipd.array_element_sequence = TRUE; - ipd.array_element_type = repeated_con->type; - if (con_ptr->variant.init_repeat.count == 0) { - /* If the repeat count is zero, this initialization is being used - to complete a partial-initialization of a variably-sized array. - Make a note of the starting element that needs initialization - (which could be zero, in cases like "new A[n] {}"). */ - if (is_vla_type(aggr_type)) { - /* For VLA types (e.g., "A a[n] = {A()};"), determine the number - of elements in the array from the VLA type (which is assumed - to have been lowered already, if needed). */ - ipd.num_elem_node = vla_dimension_expr_for_type(aggr_type); - } /* if */ - check_assertion(ipd.num_elem_node != NULL); - ipd.partial_initialization_starting_element = - (a_host_large_integer)ipmp->curr_elem; - if (is_array_type(array_element_type(aggr_type))) { - /* For the multi-dimensional array case, ensure that the - starting element takes into account all of the elements - that have already been initialized. */ - a_host_large_integer num_elems = - (a_host_large_integer)num_array_elements( - array_element_type(aggr_type)); - - ipd.partial_initialization_starting_element *= num_elems; - } /* if */ - } else { - if (ipdp->array_element_sequence) { - /* Flatten multi-dimensional arrays. */ - ipd.array_element_count = ipdp->array_element_count * - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } else { - ipd.array_element_count = - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } /* if */ - /* See if the constant is a repeated aggregate (used to initialize - multi-dimensional arrays). If so, strip any applicable - ck_init_repeat constants and increase the count accordingly. */ - while (repeated_con->kind == ck_aggregate && - repeated_con->variant.aggregate.first_constant->kind == - ck_init_repeat && - repeated_con->variant.aggregate.first_constant == - repeated_con->variant.aggregate.last_constant) { - a_constant_ptr rcon = - repeated_con->variant.aggregate.first_constant; - ipd.array_element_count *= - (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; - repeated_con = rcon-> variant.init_repeat.constant; - ipd.array_element_type = repeated_con->type; - } /* while */ - } /* if */ + repeated_con = set_init_pos_for_init_repeat( + &ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/TRUE); entity_node = make_init_entity_node(&ipd, /*result_is_lvalue=*/TRUE, /*using_as_dest=*/TRUE); @@ -6981,6 +7074,11 @@ dealt with). insert_location, /*is_lambda_capture=*/FALSE, &ipd); + if (con_ptr->variant.init_repeat.count == 0) { + /* Remove the ck_init_repeat constant, in case the overall + aggregate is kept for the constant parts. */ + remove_constant = TRUE; + } /* if */ } else { /* Repeated non-dynamic constants are possible with designators and when constexpr default constructors are folded. Either way, leave @@ -7722,6 +7820,28 @@ Any code needed is inserted at *insert_location. context->latest_initialization = dip; if (exceptions_enabled) { #if GENERATE_EH_TABLES + if (ipdp->array_element_sequence && + ipdp->array_element_count == 0 && + ipdp->num_elem_node != NULL && + ipdp->variable != NULL && + ipdp->variable->is_vla && + !ipdp->indirect_through_variable && + !is_dynamic_init_for_vla(dip)) { + /* The entity is a sequence of elements of a variable-length array + (e.g., the default-initialized remainder of a row of + "A a[2][n] = {{A()}};") whose number is computed at run time. If + an exception occurs later, the runtime needs that number to destroy + the elements, so save it in a variable that the region table entry + can refer to. (For a new-expression, such a sequence is the last + part of the initialization, so its cleanup is done by the + construction routine itself.) */ + an_expr_node_ptr count_node = num_elem_node_if_array(ipdp); + count_node = add_cast_if_necessary(count_node, + integer_type(targ_size_t_int_kind)); + dedp->runtime_elem_count_var = make_lowered_temporary(count_node->type); + (void)insert_var_assignment_statement(dedp->runtime_elem_count_var, + count_node, insert_location); + } /* if */ /* Make a region table entry for the entity (and for its conditional flag, if it has one). */ make_dyn_init_region_table_entry(dip, @@ -10500,12 +10620,17 @@ C99 mode for the same reason. is_or_was_ptr_to_member_function_type(entity_type) || ipdp->array_element_sequence) { /* Aggregate. Use a runtime routine call to zero it. */ - a_targ_size_t array_element_count = 0; + a_targ_size_t array_element_count = 0; + an_expr_node_ptr num_elem_node = NULL; if (ipdp->array_element_sequence) { /* If this assertion fails the array element count is unknown and no call to initialize the entity can be formed. */ check_assertion(ipdp->array_element_count >= 0); array_element_count = (a_targ_size_t)ipdp->array_element_count; + if (array_element_count == 0) { + /* The count is determined at run time. */ + num_elem_node = num_elem_node_if_array(ipdp); + } /* if */ entity_type = ipdp->array_element_type; } /* if */ entity_node = make_address_of_init_entity_node(ipdp, @@ -10513,7 +10638,7 @@ C99 mode for the same reason. insert_call_to_zero_entity(entity_type, have_complete_object, entity_node, - (an_expr_node_ptr)NULL, + num_elem_node, array_element_count, eff_insert_location); } else { diff --git a/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt b/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt new file mode 100644 index 0000000000..b28ef212a0 --- /dev/null +++ b/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt @@ -0,0 +1,10 @@ +[GH #557] +Partially initialized multidimensional variable-length arrays + +A partially initialized multidimensional GNU variable-length array of a class +type, e.g., + + E a[3][n] = {{E(1)}, {}}; + +could be constructed and destroyed with the wrong number of elements (writing +past the end of the array), and some shapes caused an internal error. diff --git a/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp b/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp new file mode 100644 index 0000000000..8ee4266694 --- /dev/null +++ b/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp @@ -0,0 +1,64 @@ +//type:rp +//options_all:--c++11 --g++ --exceptions + +// When a default constructor throws while a partially initialized +// multidimensional GNU variable-length array is being constructed, exactly +// the elements already constructed must be destroyed. + +int constructed; +int destroyed; +int errors; +int throw_at; + +struct Element { + int live; + Element() : live(1) { + if (++constructed == throw_at) throw 1; + } + Element(int) : live(1) { ++constructed; } + ~Element() { + if (live != 1) ++errors; + live = 0; + ++destroyed; + } +}; + +static void touch(void *) {} + +void rows_variable(int n) { + Element a[n][3] = {{Element(1)}}; + touch(a); +} + +void columns_variable(int n) { + Element a[3][n] = {{Element(1)}, {}}; + touch(a); +} + +void both_variable(int n, int m) { + Element a[n][2][m] = {{{Element(1)}}}; + touch(a); +} + +static void run(void (*construct)(int, int), int n, int m, int elements) { + for (throw_at = 2; throw_at <= elements; ++throw_at) { + constructed = destroyed = 0; + try { + construct(n, m); + ++errors; + } catch (int) { + } + if (constructed != throw_at || destroyed != throw_at - 1) ++errors; + } +} + +static void rows(int n, int) { rows_variable(n); } +static void columns(int n, int) { columns_variable(n); } +static void both(int n, int m) { both_variable(n, m); } + +int main() { + run(rows, 3, 0, 9); + run(columns, 3, 0, 9); + run(both, 2, 3, 12); + return errors; +} diff --git a/tests/tests/regressions/array-construction-vla-partial.sft.cpp b/tests/tests/regressions/array-construction-vla-partial.sft.cpp new file mode 100644 index 0000000000..6f2face644 --- /dev/null +++ b/tests/tests/regressions/array-construction-vla-partial.sft.cpp @@ -0,0 +1,268 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Partially initialized multidimensional GNU variable-length arrays: each +// element must be constructed and destroyed exactly once, and elements +// without an explicit initializer must be default-initialized. This also +// covers elements whose default constructor is constexpr (the remaining +// elements are then initialized with a constant, repeated a number of times +// known only at run time). + +int constructed; +int destroyed; +int errors; + +struct Element { + int value; + int live; + Element() : value(-1), live(1) { ++constructed; } + Element(int v) : value(v), live(1) { ++constructed; } + ~Element() { + if (live != 1) ++errors; + live = 0; + ++destroyed; + } +}; + +struct Pair { + Element first; + Element second; +}; + +struct Folded { + int value; + constexpr Folded() : value(-1) {} + constexpr Folded(int v) : value(v) {} + ~Folded() { ++destroyed; } +}; + +struct Holder { + Folded folded; + int extra = 3; +}; + +static void expect(int value, int wanted) { + if (value != wanted) ++errors; +} + +static void finish(int elements) { + expect(constructed, elements); + expect(destroyed, elements); + constructed = destroyed = 0; +} + +void rows_variable(int n) { + { + Element a[n][3] = {{Element(1)}, {Element(2), Element(3)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 3; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + if (i == 1 && j == 1) wanted = 3; + expect(a[i][j].value, wanted); + } + } + } + finish(n * 3); +} + +void columns_variable(int n) { + { + Element a[3][n] = {{Element(1)}, {Element(2), Element(3)}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + if (i == 1 && j == 1) wanted = 3; + expect(a[i][j].value, wanted); + } + } + } + finish(3 * n); +} + +void both_variable(int n, int m) { + { + Element a[n][m] = {{Element(1)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != m; ++j) { + expect(a[i][j].value, i == 0 && j == 0 ? 1 : -1); + } + } + } + finish(n * m); +} + +void middle_variable(int n) { + { + Element a[2][n][3] = {{{Element(1)}}, {{}, {Element(2)}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != n; ++j) { + for (int k = 0; k != 3; ++k) { + int wanted = -1; + if (i == 0 && j == 0 && k == 0) wanted = 1; + if (i == 1 && j == 1 && k == 0) wanted = 2; + expect(a[i][j][k].value, wanted); + } + } + } + } + finish(2 * n * 3); +} + +void innermost_variable(int n) { + { + Element a[2][3][n] = {{{Element(1)}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != 3; ++j) { + for (int k = 0; k != n; ++k) { + expect(a[i][j][k].value, i == 0 && j == 0 && k == 0 ? 1 : -1); + } + } + } + } + finish(2 * 3 * n); +} + +void aggregate_elements(int n, int m) { + { + Pair a[n][2][m] = {{{{Element(1)}}}, {{}, {{Element(2)}}}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 2; ++j) { + for (int k = 0; k != m; ++k) { + int wanted = -1; + if (i == 0 && j == 0 && k == 0) wanted = 1; + if (i == 1 && j == 1 && k == 0) wanted = 2; + expect(a[i][j][k].first.value, wanted); + expect(a[i][j][k].second.value, -1); + } + } + } + } + finish(n * 2 * m * 2); +} + +void empty_initializer(int n, int m) { + { + Element a[n][m] = {}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != m; ++j) { + expect(a[i][j].value, -1); + } + } + } + finish(n * m); +} + +void constexpr_elements(int n) { + { + Folded a[n] = {Folded(1)}; + for (int i = 0; i != n; ++i) expect(a[i].value, i == 0 ? 1 : -1); + } + expect(destroyed, n); + destroyed = 0; + { + Folded a[n][3] = {{Folded(1)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 3; ++j) { + expect(a[i][j].value, i == 0 && j == 0 ? 1 : -1); + } + } + } + expect(destroyed, n * 3); + destroyed = 0; + { + Folded a[3][n] = {{Folded(1)}, {Folded(2)}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + expect(a[i][j].value, wanted); + } + } + } + expect(destroyed, 3 * n); + destroyed = 0; +} + +void constexpr_members(int n) { + { + Holder a[3][n] = {{Holder()}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + expect(a[i][j].folded.value, -1); + expect(a[i][j].extra, 3); + } + } + } + expect(destroyed, 3 * n); + destroyed = 0; + { + Holder a[2][n][2] = {{{Holder()}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != n; ++j) { + for (int k = 0; k != 2; ++k) { + expect(a[i][j][k].folded.value, -1); + expect(a[i][j][k].extra, 3); + } + } + } + } + expect(destroyed, 2 * n * 2); + destroyed = 0; +} + +void no_initializer(int n) { + // Default initialization of arrays with rows of variable length: the + // number of elements known at compile time covers only the constant + // dimensions. + { + Element a[2][n]; + expect(a[1][n - 1].value, -1); + } + finish(2 * n); + { + Element a[2][n][2]; + expect(a[1][n - 1][1].value, -1); + } + finish(2 * n * 2); + { + Element a[2][2][n]; + expect(a[1][1][n - 1].value, -1); + } + finish(2 * 2 * n); + { + Element a[2][n][n]; + expect(a[1][n - 1][n - 1].value, -1); + } + finish(2 * n * n); + { + typedef Element Row[n]; + Row a[3]; + expect(a[2][n - 1].value, -1); + } + finish(3 * n); +} + +int main() { + rows_variable(4); + columns_variable(4); + both_variable(3, 2); + both_variable(1, 1); + middle_variable(3); + innermost_variable(2); + aggregate_elements(3, 2); + empty_initializer(3, 2); + empty_initializer(0, 2); + empty_initializer(2, 0); + constexpr_elements(4); + constexpr_elements(1); + constexpr_members(3); + no_initializer(3); + no_initializer(1); + return errors; +} From cf8c82ec544245c9a9349228dff3ea3de47bcc31 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:49 +0000 Subject: [PATCH 02/14] Fix brace initialization of VLAs whose rows have variable length [GH #558] An initializer such as "int a[2][n] = {{1}, {2, 3}};" (or "= {}") can be neither kept as a C initializer nor copied from a static temporary, but it caused an internal error (class types) or generated C that used an undeclared temporary. Zero the whole array and then assign the elements individually; elements without an explicit initializer are zero (or default-initialized). Requires the runtime repeat count change for partially initialized multidimensional VLAs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_il.c | 7 +- src/lower_il.h | 9 ++ src/lower_init.c | 87 +++++++++- .../gh-558-vla-rows-brace-init.txt | 10 ++ .../.vla-rows-brace-init.rto/default.1.1.txt | 1 + .../.vla-rows-brace-init.rto/default.1.2.txt | 1 + .../.vla-rows-brace-init.rto/default.2.1.txt | 1 + .../.vla-rows-brace-init.rto/default.2.2.txt | 1 + .../regressions/vla-rows-brace-init.sft.cpp | 150 ++++++++++++++++++ 9 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 src/pending_changes/gh-558-vla-rows-brace-init.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt create mode 100644 tests/tests/regressions/vla-rows-brace-init.sft.cpp diff --git a/src/lower_il.c b/src/lower_il.c index 832fee1d98..cd2717435f 100644 --- a/src/lower_il.c +++ b/src/lower_il.c @@ -4578,7 +4578,12 @@ called in C mode. handles them correctly. If they are partially initialized, the first member is (at least partially) initialized, which is all we need here. */ - if (is_array_type(type) && contains_ptr_to_data_member(type)) { + if (is_vla_type(type)) { + /* A constant of VLA type (for an array with rows of variable length) + cannot be filled out; the whole entity is set to its zero value + before the constant's elements are assigned (see + lower_vla_rows_constant_init in lower_init.c). */ + } else if (is_array_type(type) && contains_ptr_to_data_member(type)) { a_targ_size_t elem = 0; /* Count the number of elements that have been explicitly initialized. Note that they have previously been run through diff --git a/src/lower_il.h b/src/lower_il.h index 8c81e488e9..3c47702b93 100644 --- a/src/lower_il.h +++ b/src/lower_il.h @@ -227,6 +227,15 @@ typedef struct an_init_pos_descr { lowered. Such an element cannot refer to that aggregate through an implicit "this" (see lower_param_ref). */ + a_byte_boolean + assign_constants; + /* TRUE if no part of the initialization can be kept + as a constant, so that constants must be assigned + by executable code even though the entity is a + simple variable. This is the case for an array + with rows of variable length, which can be neither + initialized by a C initializer nor copied from a + static constant. */ a_type_ptr base_type; /* Base entity type. */ an_init_pos_modifier_ptr diff --git a/src/lower_init.c b/src/lower_init.c index 1aa243a8bc..8a75179bf7 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -1133,6 +1133,7 @@ Clear an initialization position description entry to default values. ipdp->base_class_subobject = FALSE; ipdp->base_of_complete_object = FALSE; ipdp->not_this_candidate = FALSE; + ipdp->assign_constants = FALSE; ipdp->base_type = NULL; ipdp->modifiers = NULL; ipdp->array_element_count = 0; @@ -7049,7 +7050,7 @@ dealt with). } else { lower_constant(repeated_con); } /* if */ - if (ipd.indirect_through_variable || + if (ipd.indirect_through_variable || ipd.assign_constants || con_ptr->variant.init_repeat.count == 0) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant @@ -7098,7 +7099,10 @@ dealt with). in the initialization, but if that's not possible, rewrite the initialization with executable statements. */ mark_as_visited(con_ptr); - if (ipd.indirect_through_variable) { + if (ipd.assign_constants) { + /* The entire array has already been zeroed (see + lower_vla_rows_constant_init). */ + } else if (ipd.indirect_through_variable) { insert_call_to_zero_entity(con_ptr->type, /*have_complete_object=*/TRUE, make_address_of_init_entity_node(&ipd, @@ -7126,7 +7130,7 @@ dealt with). /* C++ mode. */ lower_constant(con_ptr); } /* if */ - if (ipd.indirect_through_variable) { + if (ipd.indirect_through_variable || ipd.assign_constants) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant aggregate initialization. This comes up with return value @@ -10127,6 +10131,51 @@ statement/expression lowering process (mostly by lower_dynamic_init). #endif /* LOWERING_REMOVES_UNNEEDED_CONSTRUCTIONS_AND_DESTRUCTIONS */ +static a_boolean has_variable_length_rows(a_type_ptr type) +/* +Return TRUE if type is an array whose element type is a VLA, i.e., an +array with rows of variable length (e.g., "int[2][n]" or "int[n][n]"). +*/ +{ + type = skip_typerefs(type); + return is_array_type(type) && + is_vla_type(type->variant.array.element_type); +} /* has_variable_length_rows */ + + +static void lower_vla_rows_constant_init(a_dynamic_init_ptr dip, + an_init_pos_descr_ptr ipdp, + an_insert_location_ptr + insert_location) +/* +The dik_constant dynamic initialization dip initializes the entity described +by ipdp, an array whose rows have variable length (e.g., "int a[2][n] = { +{1}, {2, 3} };"). The entity is always treated as partially initialized, so +it has already been zeroed in full (see +stretch_partial_initialization_if_necessary); insert assignments of the +individual element values at *insert_location. +*/ +{ + a_constant_ptr con = dip->variant.constant.ptr; + an_init_pos_descr ipd; + a_boolean keep_constant = FALSE; + + check_assertion(dip->is_partially_initialized && + has_variable_length_rows(type_from_init_pos_descr(ipdp))); + if (con->kind == (a_constant_repr_kind)ck_aggregate && + con->variant.aggregate.first_constant != NULL) { + ipd = *ipdp; + ipd.assign_constants = TRUE; + lower_dynamic_init_aggregate_constant(con, &ipd, /*dtor_case=*/FALSE, + (an_implied_copy_source *)NULL, + /*others_follow_in_aggr=*/FALSE, + insert_location, &keep_constant, + (a_lower_dynamic_init_options_set)0); + check_assertion(!keep_constant); + } /* if */ +} /* lower_vla_rows_constant_init */ + + static void stretch_partial_initialization_if_necessary( a_dynamic_init_ptr dip, an_init_pos_descr_ptr ipdp, @@ -10185,12 +10234,30 @@ un-initialized. } /* if */ } /* if */ if (needs_initializing) { - insert_call_to_zero_entity(entity_type, + a_type_ptr zero_type = entity_type; + an_expr_node_ptr num_elem_node = ipdp->num_elem_node; + a_targ_size_t array_element_count = 0; + if (ipdp->array_element_sequence) { + /* A sequence of array elements (e.g., the remaining elements of an + array with rows of variable length, "A a[2][n] = {{A()}};"): zero + that many elements of the sequence element type, as for dik_zero + in lower_dynamic_init. */ + check_assertion(ipdp->array_element_count >= 0); + array_element_count = (a_targ_size_t)ipdp->array_element_count; + num_elem_node = array_element_count != 0 ? (an_expr_node_ptr)NULL : + num_elem_node_if_array(ipdp); + zero_type = ipdp->array_element_type; + } else if (is_vla_type(entity_type)) { + /* insert_call_to_zero_entity gets the number of elements of a VLA + from the type itself. */ + num_elem_node = NULL; + } /* if */ + insert_call_to_zero_entity(zero_type, have_complete_object, make_address_of_init_entity_node(ipdp, /*using_as_dest=*/TRUE), - ipdp->num_elem_node, - (a_targ_size_t)0, + num_elem_node, + array_element_count, insert_location); } /* if */ } /* stretch_partial_initialization_if_necessary */ @@ -10673,6 +10740,14 @@ C99 mode for the same reason. have_complete_object, eff_insert_location); } /* if */ + if (has_variable_length_rows(type_from_init_pos_descr(ipdp))) { + /* The constant can be neither kept as an initializer nor copied from + a static temporary, because its layout differs from that of the + entity (whose rows have a length known only at run time); assign + its elements individually. */ + lower_vla_rows_constant_init(dip, ipdp, eff_insert_location); + break; + } /* if */ /* For the normal cases, go on and generate an assignment. */ goto do_assignment; case dik_expression: diff --git a/src/pending_changes/gh-558-vla-rows-brace-init.txt b/src/pending_changes/gh-558-vla-rows-brace-init.txt new file mode 100644 index 0000000000..c2f8436699 --- /dev/null +++ b/src/pending_changes/gh-558-vla-rows-brace-init.txt @@ -0,0 +1,10 @@ +[GH #558] +Brace initialization of variable-length arrays with variable-length rows + +An initializer for a GNU variable-length array whose rows have variable +length, e.g., + + int a[2][n] = {{1}, {2, 3}}; + D b[2][n] = {}; // D has a destructor + +generated C that used an undeclared temporary, or caused an internal error. diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/vla-rows-brace-init.sft.cpp b/tests/tests/regressions/vla-rows-brace-init.sft.cpp new file mode 100644 index 0000000000..6d8371c01e --- /dev/null +++ b/tests/tests/regressions/vla-rows-brace-init.sft.cpp @@ -0,0 +1,150 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Brace initialization of GNU variable-length arrays whose rows have +// variable length (e.g., "int a[2][n] = {{1}, {2, 3}};"). The initializer +// can be neither kept as a C initializer nor copied from a static temporary +// (the layout of the rows is known only at run time), so the elements are +// assigned individually after the whole array has been zeroed. Elements +// without an explicit initializer must be zero (or, for a class with a +// default member initializer or constexpr constructor, default-initialized). + +int errors; + +static void check(int got, int expected) { + if (got != expected) ++errors; +} + +struct P { int x, y; }; +struct CD { int v; constexpr CD() : v(-1) {} ~CD() {} }; +struct W { CD c; int i = 3; }; +struct DO { int v; ~DO() {} }; +struct S { int a, b; }; +struct Q { int S::*p; int v; }; + +static void empty_rows(int n) { + int a[2][n] = {}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) check(a[i][j], 0); +} + +static void partial_rows(int n) { + int a[3][n] = {{1}, {2, 3}, {}}; + for (int i = 0; i < 3; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void middle_variable_dim(int n) { + int a[2][n][2] = {{{1, 2}, {3}}, {{4}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + for (int k = 0; k < 2; k++) + check(a[i][j][k], i == 0 && j == 0 ? k + 1 : + i == 0 && j == 1 && k == 0 ? 3 : + i == 1 && j == 0 && k == 0 ? 4 : 0); +} + +static void inner_variable_dim(int n) { + int a[2][2][n] = {{{1}, {2, 3}}, {{}, {4}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + for (int k = 0; k < n; k++) + check(a[i][j][k], i == 0 && j == 0 && k == 0 ? 1 : + i == 0 && j == 1 && k == 0 ? 2 : + i == 0 && j == 1 && k == 1 ? 3 : + i == 1 && j == 1 && k == 0 ? 4 : 0); +} + +static void all_variable_dims(int n) { + // The initializer has the fixed type int[2][2] here. + int a[n][n] = {{1}, {2, 3}}; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void typedef_rows(int n) { + typedef int R[n]; + R a[2] = {{1}, {2, 3}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void nonconstant_element(int n) { + int x = n * 10; + int a[2][n] = {{x, 1}, {2}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? x : + i == 0 && j == 1 ? 1 : + i == 1 && j == 0 ? 2 : 0); +} + +static void string_rows(int n) { + char a[2][n] = {"ab", "c"}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 'a' : + i == 0 && j == 1 ? 'b' : + i == 1 && j == 0 ? 'c' : 0); +} + +static void struct_rows(int n) { + P a[2][n] = {{{1, 2}}, {{3}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].x, i == 0 && j == 0 ? 1 : i == 1 && j == 0 ? 3 : 0); + check(a[i][j].y, i == 0 && j == 0 ? 2 : 0); + } +} + +static void class_rows(int n) { + W w[2][n] = {}; + CD c[2][n] = {}; + DO d[2][n] = {{{5}}, {{6}, {7}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(w[i][j].i, 3); + check(w[i][j].c.v, -1); + check(c[i][j].v, -1); + check(d[i][j].v, i == 0 && j == 0 ? 5 : + i == 1 && j == 0 ? 6 : + i == 1 && j == 1 ? 7 : 0); + } +} + +static void ptr_to_member_rows(int n) { + // A null pointer to data member is not all-bits-zero in the IA-64 ABI. + Q a[2][n] = {{{&S::b, 5}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(a[i][j].v, i == 0 && j == 0 ? 5 : 0); + } +} + +int main() { + for (int n = 3; n <= 4; n++) { + empty_rows(n); + partial_rows(n); + middle_variable_dim(n); + inner_variable_dim(n); + all_variable_dims(n); + typedef_rows(n); + nonconstant_element(n); + string_rows(n); + struct_rows(n); + class_rows(n); + ptr_to_member_rows(n); + } + return errors != 0; +} From d015e679ff7444ed62d068d1ecb20fdebcab5bbc Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:54 +0000 Subject: [PATCH 03/14] Fix brace initialization of VLAs of a class with a destructor [GH #559] Without exceptions, such an initialization keeps the constant part of the initializer and leaves zeroing the remainder to the back end. That did not zero the array at all when no constant part was kept ("= {}"), and zeroing gives the wrong value for a null pointer to data member, which is not all-bits-zero in the IA-64 ABI. Requires the fix for brace initialization of VLAs whose rows have variable length. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 23 ++++-- .../gh-559-vla-init-class-dtor.txt | 7 ++ .../.vla-init-class-dtor.rto/default.1.1.txt | 1 + .../.vla-init-class-dtor.rto/default.1.2.txt | 1 + .../.vla-init-class-dtor.rto/default.2.1.txt | 1 + .../.vla-init-class-dtor.rto/default.2.2.txt | 1 + .../regressions/vla-init-class-dtor.sft.cpp | 77 +++++++++++++++++++ 7 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 src/pending_changes/gh-559-vla-init-class-dtor.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt create mode 100644 tests/tests/regressions/vla-init-class-dtor.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 8a75179bf7..88abd28b91 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -10968,6 +10968,8 @@ do_assignment:; break; case dik_nonconstant_aggregate: { + an_init_pos_descr vla_ipd; + an_init_pos_descr_ptr agg_ipdp; /* Initialization with a nonconstant aggregate constant. This is usually a whole-variable initialization, but can be used in a ctor-initializer or lambda capture to iterate over an array @@ -10976,12 +10978,21 @@ do_assignment:; latest_initialization_on_entry = eff_context->latest_initialization; } /* if */ keep_constant = FALSE; - if (dip->is_partially_initialized && - (variable == NULL || !is_vla_type(variable->type))) { + agg_ipdp = ipdp; + if (variable != NULL && is_vla_type(variable->type)) { + /* The back end cannot be relied on to complete the initialization + of a VLA: nothing zeroes it if no constant is kept, and zeroing + gives the wrong null pointer-to-data-member value for the + IA-64 ABI. Initialize the whole VLA here and assign (rather + than keep) the constant parts. */ + vla_ipd = *ipdp; + vla_ipd.assign_constants = TRUE; + agg_ipdp = &vla_ipd; + } /* if */ + if (dip->is_partially_initialized) { /* For cases where the initialization only partially covers the entity being initialized, initialize the remaining portion - of the entity if necessary. No need to stretch VLA - initialization; the back end will do that. */ + of the entity if necessary. */ stretch_partial_initialization_if_necessary(dip, ipdp, have_complete_object, eff_insert_location); @@ -11016,12 +11027,14 @@ do_assignment:; copy_node(init_entity_expr)); insert_expr(master_entry_assignment, eff_insert_location); } /* if */ - lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, ipdp, + lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, + agg_ipdp, /*dtor_case=*/FALSE, source_desc, others_follow_in_aggr, eff_insert_location, &keep_constant, options); + check_assertion(agg_ipdp == ipdp || !keep_constant); do_keep_constant: if (keep_constant) { /* There is a constant part of the initialization to be kept. */ diff --git a/src/pending_changes/gh-559-vla-init-class-dtor.txt b/src/pending_changes/gh-559-vla-init-class-dtor.txt new file mode 100644 index 0000000000..73b4c8f9a1 --- /dev/null +++ b/src/pending_changes/gh-559-vla-init-class-dtor.txt @@ -0,0 +1,7 @@ +[GH #559] +Zeroing of variable-length arrays of a class with a destructor + +With exceptions disabled, "D a[n] = {};" for a class D with a destructor left +the array uninitialized, and a pointer to data member element of such an array +was zeroed rather than set to the null member pointer value (which is not +all-bits-zero in the IA-64 ABI). diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/vla-init-class-dtor.sft.cpp b/tests/tests/regressions/vla-init-class-dtor.sft.cpp new file mode 100644 index 0000000000..e059885a45 --- /dev/null +++ b/tests/tests/regressions/vla-init-class-dtor.sft.cpp @@ -0,0 +1,77 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Brace initialization of GNU variable-length arrays of a class type with a +// nontrivial destructor. When exceptions are disabled, such an +// initialization is lowered by keeping the constant part of the initializer +// and leaving the zeroing of the remainder to the back end; that did not +// zero the array at all when no constant part was kept ("= {}"), and zeroing +// gives the wrong value for a null pointer to data member, which is not +// all-bits-zero in the IA-64 ABI. + +int errors; + +static void check(int got, int expected) { + if (got != expected) ++errors; +} + +// Leave nonzero garbage on the stack so that missing zeroing is detected. +static void scribble() { + volatile int junk[256]; + for (int i = 0; i < 256; i++) junk[i] = 0x5a5a5a5a; +} + +struct S { int a, b; }; +struct DO { int v; ~DO() {} }; +struct M { int S::*p; int v; ~M() {} }; +struct W { int k; M m; }; + +static int empty_init(int n) { + DO a[n] = {}; + DO b[2][n] = {}; + DO c[n][2] = {}; + DO d[n]{}; + int sum = 0; + for (int i = 0; i < n; i++) { + sum += a[i].v + d[i].v + c[i][0].v + c[i][1].v; + sum += b[0][i].v + b[1][i].v; + } + return sum; +} + +static void ptr_to_member(int n) { + M a[n] = {{&S::b, 5}}; + M b[n][2] = {{{&S::b, 5}}}; + M c[2][n] = {{{&S::b, 5}}}; + W w[n] = {{1, {&S::b, 5}}}; + M e[n + 1] = {{&S::b, 5}, {nullptr, 6}}; + for (int i = 0; i < n; i++) { + check(a[i].p == (i == 0 ? &S::b : nullptr), 1); + check(a[i].v, i == 0 ? 5 : 0); + for (int j = 0; j < 2; j++) { + check(b[i][j].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(b[i][j].v, i == 0 && j == 0 ? 5 : 0); + check(c[j][i].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(c[j][i].v, i == 0 && j == 0 ? 5 : 0); + } + check(w[i].k, i == 0 ? 1 : 0); + check(w[i].m.p == (i == 0 ? &S::b : nullptr), 1); + check(w[i].m.v, i == 0 ? 5 : 0); + } + check(e[0].p == &S::b, 1); + for (int i = 1; i <= n; i++) { + check(e[i].p == nullptr, 1); + check(e[i].v, i == 1 ? 6 : 0); + } +} + +int main() { + for (int n = 1; n <= 4; n++) { + scribble(); + check(empty_init(n), 0); + scribble(); + ptr_to_member(n); + } + return errors != 0; +} From 35fd9bc8c89367e3bd6562be357dcdc0fc759d17 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:56 +0000 Subject: [PATCH 04/14] Destroy constructed array elements when value-initialization throws [GH #554] For "G g{};" where G has a member "P a[4]" and P has a nontrivial default constructor and a destructor, the elements were initialized by a generated helper routine without cleanups for the elements already constructed (or with a cleanup for only one of them), cleanups of preceding members were lost, and the helper's exception-handling epilogue was missing. Also affected: default member initializers, multidimensional and nested arrays, and new G{}. Initialize such element sequences with the runtime array construction routine, using a helper that initializes one element and the element destructor, and add the return statements of generated helper routines to the return memo list. Requires the fix for brace initialization of VLAs of a class with a destructor (textual overlap only). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 212 ++++++++++++++++-- .../gh-554-value-init-array-member-throw.txt | 13 ++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../value-init-array-member-throw.sft.cpp | 55 +++++ 5 files changed, 263 insertions(+), 19 deletions(-) create mode 100644 src/pending_changes/gh-554-value-init-array-member-throw.txt create mode 100644 tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/value-init-array-member-throw.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 88abd28b91..f4c5cf72f0 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -167,6 +167,12 @@ static a_boolean recompute_partially_initialized_flag( a_constant_ptr aggr_con, a_type_ptr aggr_type); +static void stretch_partial_initialization_if_necessary( + a_dynamic_init_ptr dip, + an_init_pos_descr_ptr ipdp, + a_boolean have_complete_object, + an_insert_location_ptr insert_location); + static a_type_ptr make_function_type(a_type_ptr return_type, a_type_ptr param_1_type, a_type_ptr param_2_type) @@ -6514,7 +6520,7 @@ to lower_dynamic_init_aggregate_constant; see their description there. an_insert_location insert_location, loop_insert_location; a_generated_routine_context context; a_variable_ptr entity_var, count_var = NULL; - a_statement_ptr loop_stmt = NULL; + a_statement_ptr loop_stmt = NULL, return_stmt; an_expr_node_ptr ptr_increment, expr; a_param_type_ptr count_param_type = NULL; an_init_pos_descr ipd; @@ -6537,6 +6543,12 @@ to lower_dynamic_init_aggregate_constant; see their description there. scope = make_routine_definition(rp, /*make_return=*/TRUE, curr_il_region_number, &il_region); push_generated_routine_context(scope, il_region, &context); + /* Add the return statement at the end of the routine to the return memo + list, so that any exception handling epilogue is added before it. */ + return_stmt = scope->assoc_block->variant.block.statements; + check_assertion(return_stmt != NULL && + return_stmt->kind == (a_statement_kind)stmk_return); + add_to_return_memo_list(return_stmt); /* Create the parameters. */ entity_var = make_lowered_param_variable(rtsp->param_type_list->type); count_var = make_lowered_param_variable(count_param_type->type); @@ -6584,6 +6596,95 @@ to lower_dynamic_init_aggregate_constant; see their description there. } /* helper_routine_to_initialize_repeated_constant */ +static a_routine_ptr helper_routine_to_initialize_aggregate_element( + a_dynamic_init_ptr dip, + a_type_ptr type, + an_implied_copy_source *source_desc, + a_lower_dynamic_init_options_set options) +/* +Create, and return a pointer to, a "helper" routine that initializes one +element of a sequence of array elements with the dik_nonconstant_aggregate +dynamic initialization dip. The routine is passed as the "constructor" to +the runtime array construction routine (which loops over the elements and, +if an exception is thrown, destroys the elements already constructed), so +it has the generic constructor type (see make_ctor_type): + + static void helper(type *ptr) { + + } + +(returning ptr when constructors return a pointer). Only the +initialization of the element is done here; the destruction of the elements +is handled for the whole sequence by the caller. "type" is a pointer to the +type of the element. source_desc and options are passed through to +lower_dynamic_init_aggregate_constant; see their description there. +*/ +{ + a_routine_ptr rp; + a_type_ptr pointer_type, return_type; + a_memory_region_number il_region; + a_scope_ptr scope; + an_insert_location insert_location; + a_generated_routine_context context; + a_variable_ptr entity_var; + a_statement_ptr return_stmt; + an_init_pos_descr ipd; + a_boolean keep_constant = FALSE; + a_boolean return_ptr; + + check_assertion(dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate); + pointer_type = skip_typerefs(type); + check_assertion(is_pointer_type(pointer_type)); +#if IA64_ABI + return_ptr = ctors_return_this; +#else /* !IA64_ABI */ + return_ptr = TRUE; +#endif /* IA64_ABI */ + return_type = return_ptr ? pointer_type : void_type(); + rp = make_rout_entry((char *)NULL, (a_storage_class)sc_static, + return_type, pointer_type); + /* Build the definition of the routine. Note that the same memory region + is used (because lowering of the aggregate may involve IL that is + already in the current memory region). */ + scope = make_routine_definition(rp, /*make_return=*/TRUE, + curr_il_region_number, &il_region); + push_generated_routine_context(scope, il_region, &context); + /* Add the return statement at the end of the routine to the return memo + list, so that any exception handling epilogue is added before it. */ + return_stmt = scope->assoc_block->variant.block.statements; + check_assertion(return_stmt != NULL && + return_stmt->kind == (a_statement_kind)stmk_return); + add_to_return_memo_list(return_stmt); + entity_var = make_lowered_param_variable( + rp->type->variant.routine.extra_info->param_type_list-> + type); + scope->variant.routine.parameters = entity_var; + if (return_ptr) return_stmt->expr = var_rvalue_expr(entity_var); + set_block_start_insert_location(scope->assoc_block, &insert_location); + /* Mark the location where any generated stmk_init statements should go. */ + set_insert_location_mark(&insert_location); + /* Insert any generated stmk_inits at the previously marked location. */ + insert_pending_stmk_init_statements_at_mark(&insert_location); + /* Set ipd to point to the element being passed in. */ + set_var_indirect_init_pos_descr(entity_var, &ipd); + if (dip->is_partially_initialized) { + stretch_partial_initialization_if_necessary(dip, &ipd, + /*have_complete_object=*/TRUE, + &insert_location); + } /* if */ + lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, &ipd, + /*dtor_case=*/FALSE, source_desc, + /*others_follow_in_aggr=*/FALSE, + &insert_location, &keep_constant, + options); + check_assertion(!keep_constant); + /* Clean up. */ + pop_generated_routine_context(scope, il_region, &context); + return rp; +} /* helper_routine_to_initialize_aggregate_element */ + + static void set_partial_initialization_start(an_init_pos_descr_ptr ipdp, a_type_ptr aggr_type, a_targ_size_t curr_elem) @@ -6990,6 +7091,27 @@ dealt with). aggr_type->variant.array.bound_is_zero) { /* Nothing to initialize here. */ } else { + an_init_pos_descr leaf_ipd; + a_constant_ptr leaf_con; + a_dynamic_init_ptr leaf_dip; + a_boolean init_leaf_sequence = FALSE; + /* Determine the repeated leaf entity (stripping the repeats of + a multi-dimensional array), and whether it must be destroyed + if an exception occurs during the initialization of a later + element. */ + leaf_ipd = ipd; + leaf_con = set_init_pos_for_init_repeat(&leaf_ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/TRUE); + if (exceptions_enabled && !dtor_case && + leaf_con->kind == (a_constant_repr_kind)ck_dynamic_init) { + leaf_dip = leaf_con->variant.dynamic_init.ptr; + init_leaf_sequence = + leaf_dip->destructor != NULL && + (leaf_dip->kind == + (a_dynamic_init_kind)dik_nonconstant_aggregate || + leaf_dip->kind == (a_dynamic_init_kind)dik_constructor); + } /* if */ (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, ipmp->curr_elem, con_ptr, /*strip_nested_repeats=*/FALSE); @@ -7014,29 +7136,39 @@ dealt with). will be re-invoked to lower repeated_con, but in the context of a different function). */ an_expr_node_ptr args; - /* Call the routine: helper(ptr, count); */ check_assertion(con_ptr->variant.init_repeat.constant != NULL); - args = make_address_of_init_entity_node(&ipd, - /*using_as_dest=*/FALSE); - if (con_ptr->variant.init_repeat.count == 0) { - /* The repeat count isn't known at compilation time; use - the run-time count of the remaining elements. */ - args->next = num_elem_node_if_array(&ipd); - check_assertion(args->next != NULL); + if (init_leaf_sequence) { + /* The elements must be destroyed if the initialization of + a later element throws an exception. Lower the leaf + initialization for the whole (flattened) sequence of + elements; lower_dynamic_init uses the runtime array + construction routine for that, and arranges for the + destruction of the sequence. */ + lower_ck_dynamic_init(leaf_con, &leaf_ipd, dtor_case, + source_desc, others_follow, + insert_location, keep_constant, options); } else { - args->next = node_for_host_large_integer( + /* Call the routine: helper(ptr, count); */ + args = make_address_of_init_entity_node(&ipd, + /*using_as_dest=*/FALSE); + if (con_ptr->variant.init_repeat.count == 0) { + /* The repeat count isn't known at compilation time; use + the run-time count of the remaining elements. */ + args->next = num_elem_node_if_array(&ipd); + check_assertion(args->next != NULL); + } else { + args->next = node_for_host_large_integer( (a_host_large_integer)con_ptr->variant.init_repeat.count, targ_size_t_int_kind); + } /* if */ + make_call_statement( + helper_routine_to_initialize_repeated_constant( + repeated_con, args->type, + dtor_case, source_desc, + others_follow, + keep_constant, options), + args, (an_expr_node_ptr)NULL, insert_location); } /* if */ - make_call_statement(helper_routine_to_initialize_repeated_constant( - repeated_con, - args->type, - dtor_case, - source_desc, - others_follow, - keep_constant, - options), - args, (an_expr_node_ptr)NULL, insert_location); /* Remove the ck_init_repeat constant, in case the overall aggregate is kept for the constant parts. */ remove_constant = TRUE; @@ -10978,6 +11110,48 @@ do_assignment:; latest_initialization_on_entry = eff_context->latest_initialization; } /* if */ keep_constant = FALSE; + if (ipdp->array_element_sequence) { + /* A sequence of array elements, each initialized with the + aggregate (see lower_dynamic_init_aggregate_constant). Use the + runtime array construction routine, which destroys the elements + already constructed if an exception is thrown, with a helper + routine that initializes one element: + __cxa_vec_ctor(ptr, count, size, helper, dtor); + The destruction of the sequence is arranged below, as for an + array constructor call. */ + a_routine_ptr helper, dtor_routine = NULL; + an_expr_node_ptr num_elem_node; + if (exceptions_enabled && dip->destructor != NULL) { + dtor_routine = dip->destructor; +#if IA64_ABI + dtor_routine = alternate_entry_point(dtor_routine, + (a_ctor_or_dtor_kind)cdk_complete, + /*define_now=*/FALSE); +#endif /* IA64_ABI */ + } /* if */ + /* For a multi-dimensional array, the address is that of the first + row; convert it to a pointer to the first element. */ + entity_node = make_address_of_init_entity_node(ipdp, + /*using_as_dest=*/TRUE); + entity_node = add_cast_if_necessary( + entity_node, + make_pointer_type(ipdp->array_element_type)); + num_elem_node = num_elem_node_if_array(ipdp); + check_assertion(num_elem_node != NULL); + helper = helper_routine_to_initialize_aggregate_element( + dip, + entity_node->type, + source_desc, + options); + (void)insert_expr_statement_set_pos( + make_vec_new_call(entity_node, entity_node->type, + num_elem_node, helper, dtor_routine, + (a_routine_ptr)NULL, + (a_routine_ptr)NULL, + /*zero_storage=*/FALSE), + eff_insert_location); + break; + } /* if */ agg_ipdp = ipdp; if (variable != NULL && is_vla_type(variable->type)) { /* The back end cannot be relied on to complete the initialization diff --git a/src/pending_changes/gh-554-value-init-array-member-throw.txt b/src/pending_changes/gh-554-value-init-array-member-throw.txt new file mode 100644 index 0000000000..b2d596052a --- /dev/null +++ b/src/pending_changes/gh-554-value-init-array-member-throw.txt @@ -0,0 +1,13 @@ +[GH #554] +Array members not cleaned up when their value-initialization throws + +When a constructor called for the value-initialization of an array member +threw, the elements already constructed were not destroyed (and cleanups of +preceding members could be lost), e.g., + + struct P { int i; E e; }; // E has a constructor and a destructor + struct G { P a[4]; }; + G g{}; + +Default member initializers, multidimensional and nested arrays, and +"new G{}" were affected as well. diff --git a/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..06f761968e --- /dev/null +++ b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++17 --exceptions Test_name.c diff --git a/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/value-init-array-member-throw.sft.cpp b/tests/tests/regressions/value-init-array-member-throw.sft.cpp new file mode 100644 index 0000000000..eea6080f71 --- /dev/null +++ b/tests/tests/regressions/value-init-array-member-throw.sft.cpp @@ -0,0 +1,55 @@ +//type:rp +//options_all:--c++17 --exceptions + +// Value-/brace-initialization of an aggregate whose array member has a +// repeated initializer for a class with a destructor (e.g., "G g{}" where +// G has a member "P a[4]" and P has a nontrivial default constructor and +// destructor). When the constructor of one element throws, the elements +// that were already constructed must be destroyed exactly once. Before +// the fix, the generated per-element helper had no cleanup for earlier +// elements (or registered the cleanup of one element only), and cleanups +// of preceding array members were lost. + +int ctors, dtors, throw_at, live, errors; + +struct E { + int v; + E() : v(-1) { if (++ctors == throw_at) throw 1; ++live; } + ~E() { ++dtors; --live; } +}; +struct P { int i; E e; }; +struct Q { E e1; int i; E e2; }; +struct H { P a[4]; int tail = 9; }; +struct G { P a[4]; }; +struct GQ { Q a[3]; }; +struct G2 { P a[2][3]; }; +struct G3 { P a[2][2][2]; }; +struct N { G g[2]; }; +struct GV { int k = 5; Q a[2]; P b[2][2]; }; + +template static void check(int n, F f) { + for (throw_at = 1; throw_at <= n; ++throw_at) { + ctors = dtors = live = 0; + try { f(); } catch (int) {} + if (dtors != throw_at - 1 || live != 0) ++errors; + } +} + +int main() { + check(4, [] { H x{}; }); + check(4, [] { G x{}; }); + check(6, [] { GQ x{}; }); + check(6, [] { G2 x{}; }); + check(8, [] { G3 x{}; }); + check(8, [] { N x{}; }); + check(8, [] { GV x{}; }); + check(12, [] { G2 x[2]{}; }); + check(4, [] { static G x{}; }); + check(4, [] { (void)G{}; }); + check(4, [] { G *p = new G{}; delete p; }); + /* Success path: everything constructed is destroyed. */ + throw_at = 0; live = 0; + { G2 a{}; GV b{}; G c[2]{}; N d{}; } + if (live != 0) ++errors; + return errors != 0; +} From ead8944999c18ea0f490f201c91b4f5aa4e60ab2 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:00 +0000 Subject: [PATCH 05/14] Avoid allocation helpers for static array construction [GH #561] Generate an element construction loop when no zeroing or exception cleanup is needed. Retain the existing runtime path for cases requiring cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- doc/source/lower_il.rst | 10 +++-- .../gh-561-static-array-construction.txt | 7 ++++ src/lower_init.c | 23 +++++++++++ .../gh-561-static-array-construction.txt | 10 +++++ .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 ++ .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 ++ .../default.2.1.txt | 2 + .../default.2.matches.txt | 4 ++ .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 ++ .../default.2.1.txt | 2 + .../default.2.matches.txt | 4 ++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + .../array-construction-cleanup.sft.cpp | 17 ++++++++ .../array-construction-dynamic.sft.cpp | 15 +++++++ .../array-construction-no-allocation.sft.cpp | 25 ++++++++++++ .../array-construction-semantics.sft.cpp | 39 +++++++++++++++++++ 22 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 lib_src/pending_changes/gh-561-static-array-construction.txt create mode 100644 src/pending_changes/gh-561-static-array-construction.txt create mode 100644 tests/tests/regressions/.array-construction-cleanup.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-cleanup.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-dynamic.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-dynamic.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-dynamic.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-dynamic.rto/default.2.matches.txt create mode 100644 tests/tests/regressions/.array-construction-no-allocation.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-no-allocation.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-no-allocation.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-no-allocation.rto/default.2.matches.txt create mode 100644 tests/tests/regressions/.array-construction-semantics.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-semantics.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-semantics.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-semantics.rto/default.2.2.txt create mode 100644 tests/tests/regressions/array-construction-cleanup.sft.cpp create mode 100644 tests/tests/regressions/array-construction-dynamic.sft.cpp create mode 100644 tests/tests/regressions/array-construction-no-allocation.sft.cpp create mode 100644 tests/tests/regressions/array-construction-semantics.sft.cpp diff --git a/doc/source/lower_il.rst b/doc/source/lower_il.rst index fcf36e7daf..acee8b30b2 100644 --- a/doc/source/lower_il.rst +++ b/doc/source/lower_il.rst @@ -1991,8 +1991,10 @@ See ``lower_array_new`` and ``lower_array_delete``. Constructors and Destructors for Arrays --------------------------------------- -Construction or destruction of an entire (static or automatic) array is done by -calling runtime routines. For the cfront-like ABI, ``__vec_new`` or +For construction of an entire (static or automatic) array, lowering generates +a loop that calls the element constructor when no storage zeroing or exception +cleanup is needed. Other construction cases and array destruction call +runtime routines. For the cfront-like ABI, ``__vec_new`` or ``__vec_delete`` are used (see previous section). The address of the array is passed into ``__vec_new``, thus suppressing allocation, and ``0`` for the flag ``free`` is passed into ``__vec_delete``, thus suppressing freeing of the @@ -2011,8 +2013,8 @@ array address. [#f4]_ In the IA-64 ABI, the runtime routine is If the constructor involved has default arguments, a wrapper routine is created that requires no arguments other than the "``this``" parameter and calls the -constructor with the proper default arguments. The wrapper routine is then -passed to the runtime routine, which can call it like a normal constructor. +constructor with the proper default arguments. The generated loop or runtime +routine can then call the wrapper like a normal constructor. See diff --git a/lib_src/pending_changes/gh-561-static-array-construction.txt b/lib_src/pending_changes/gh-561-static-array-construction.txt new file mode 100644 index 0000000000..97fb30cc12 --- /dev/null +++ b/lib_src/pending_changes/gh-561-static-array-construction.txt @@ -0,0 +1,7 @@ +[GH #561] +Runtime support for constructing preallocated arrays + +The construction and destruction of arrays that are not allocated by array +new (__edg_vec_ctor, __edg_vec_dtor, and related routines) is now in +vec_preallocated.c, separate from the array new and delete support in +vec_newdel.c. Exception cleanup for such arrays always uses __edg_vec_dtor. diff --git a/src/lower_init.c b/src/lower_init.c index f4c5cf72f0..b44f26b3ad 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -5915,6 +5915,29 @@ must NOT already be lowered (see comment in default_version_of_routine). check_assertion(!dip->variant.constructor.value_initialization); call_node = make_vec_cctor_call(entity_node, source_node, ipdp, ctor_routine, dtor_routine); + } else if (!zero_storage && + (!exceptions_enabled || dtor_routine == NULL)) { + /* No cleanup is needed if a constructor throws. Construct the elements + directly without pulling in the allocating vector-new runtime. */ + an_insert_location helper_insert_location; + a_variable_ptr element_ptr; + a_type_ptr element_type = type_pointed_to(entity_node->type); + a_routine_ptr helper_routine; + an_expr_node_ptr ctor_call; + + num_elem_node = num_elem_node_if_array(ipdp); + check_assertion(num_elem_node != NULL); + helper_routine = helper_routine_to_loop_through_array_elements( + element_type, &element_ptr, + &helper_insert_location); + ctor_call = make_call_node(ctor_routine, var_rvalue_expr(element_ptr)); + (void)insert_expr_statement(ctor_call, &helper_insert_location); + entity_node = add_cast_if_necessary(entity_node, + make_pointer_type(element_type)); + entity_node->next = add_cast_if_necessary( + num_elem_node, + integer_type(targ_size_t_int_kind)); + call_node = make_call_node(helper_routine, entity_node); } else { /* Normal constructor case. */ /* Build a node for the number of array elements. */ diff --git a/src/pending_changes/gh-561-static-array-construction.txt b/src/pending_changes/gh-561-static-array-construction.txt new file mode 100644 index 0000000000..18a46145e6 --- /dev/null +++ b/src/pending_changes/gh-561-static-array-construction.txt @@ -0,0 +1,10 @@ +[GH #561] +Construction of arrays that are not allocated by new + +Arrays with static, automatic, or otherwise preallocated storage are now +constructed by generated loops when no exception cleanup is needed, and +otherwise by new runtime routines that do not depend on the array new and +delete support, so that programs that are compiled without exception +handling and do not use array new no longer pull in the allocating vector +runtime. Generated code that uses these routines requires the matching +runtime library. diff --git a/tests/tests/regressions/.array-construction-cleanup.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-cleanup.rto/default.1.1.txt new file mode 100644 index 0000000000..047fef4a4b --- /dev/null +++ b/tests/tests/regressions/.array-construction-cleanup.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +exception cleanup helper retained diff --git a/tests/tests/regressions/.array-construction-cleanup.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-cleanup.rto/default.1.matches.txt new file mode 100644 index 0000000000..d683df3d8f --- /dev/null +++ b/tests/tests/regressions/.array-construction-cleanup.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "exception cleanup helper retained": + <----------------------------------------------------------------------------- + exception cleanup helper retained + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-dynamic.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-dynamic.rto/default.1.1.txt new file mode 100644 index 0000000000..34226d7c92 --- /dev/null +++ b/tests/tests/regressions/.array-construction-dynamic.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +dynamic allocation helper retained diff --git a/tests/tests/regressions/.array-construction-dynamic.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-dynamic.rto/default.1.matches.txt new file mode 100644 index 0000000000..e6b09f2837 --- /dev/null +++ b/tests/tests/regressions/.array-construction-dynamic.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "dynamic allocation helper retained": + <----------------------------------------------------------------------------- + dynamic allocation helper retained + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-dynamic.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-dynamic.rto/default.2.1.txt new file mode 100644 index 0000000000..385e223678 --- /dev/null +++ b/tests/tests/regressions/.array-construction-dynamic.rto/default.2.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c +dynamic allocation helper retained diff --git a/tests/tests/regressions/.array-construction-dynamic.rto/default.2.matches.txt b/tests/tests/regressions/.array-construction-dynamic.rto/default.2.matches.txt new file mode 100644 index 0000000000..e6b09f2837 --- /dev/null +++ b/tests/tests/regressions/.array-construction-dynamic.rto/default.2.matches.txt @@ -0,0 +1,4 @@ +Matches for "dynamic allocation helper retained": + <----------------------------------------------------------------------------- + dynamic allocation helper retained + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.1.txt new file mode 100644 index 0000000000..080d8de48c --- /dev/null +++ b/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +array construction needs no allocation helper diff --git a/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.matches.txt new file mode 100644 index 0000000000..2444119ca7 --- /dev/null +++ b/tests/tests/regressions/.array-construction-no-allocation.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "array construction needs no allocation helper": + <----------------------------------------------------------------------------- + array construction needs no allocation helper + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.1.txt new file mode 100644 index 0000000000..89b13edbd2 --- /dev/null +++ b/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c +array construction needs no allocation helper diff --git a/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.matches.txt b/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.matches.txt new file mode 100644 index 0000000000..2444119ca7 --- /dev/null +++ b/tests/tests/regressions/.array-construction-no-allocation.rto/default.2.matches.txt @@ -0,0 +1,4 @@ +Matches for "array construction needs no allocation helper": + <----------------------------------------------------------------------------- + array construction needs no allocation helper + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-semantics.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-semantics.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-semantics.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-semantics.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-semantics.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-semantics.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-semantics.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-semantics.rto/default.2.1.txt new file mode 100644 index 0000000000..bfedef0468 --- /dev/null +++ b/tests/tests/regressions/.array-construction-semantics.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-semantics.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-semantics.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-semantics.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-construction-cleanup.sft.cpp b/tests/tests/regressions/array-construction-cleanup.sft.cpp new file mode 100644 index 0000000000..12082794cc --- /dev/null +++ b/tests/tests/regressions/array-construction-cleanup.sft.cpp @@ -0,0 +1,17 @@ +//type:fp +//options_all:--c++11 --exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; awk 'BEGIN { found = 0 } /__vec_new_eh|__cxa_vec_ctor/ { found = 1 } END { if (found) print "exception cleanup helper retained"; else print "missing exception cleanup helper" }' Test_name.int.c +//match_regex:exception cleanup helper retained + +struct Element { + Element(); + ~Element(); +}; + +struct Container { + Element elements[2]; + Container(); +}; + +Container::Container() {} diff --git a/tests/tests/regressions/array-construction-dynamic.sft.cpp b/tests/tests/regressions/array-construction-dynamic.sft.cpp new file mode 100644 index 0000000000..c0cc0877c4 --- /dev/null +++ b/tests/tests/regressions/array-construction-dynamic.sft.cpp @@ -0,0 +1,15 @@ +//type:fp +//options_all:--c++11 +//options:--exceptions:--no_exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; awk 'BEGIN { found = 0 } /__array_new|__vec_new|__cxa_vec_new/ { found = 1 } END { if (found) print "dynamic allocation helper retained"; else print "missing dynamic allocation helper" }' Test_name.int.c +//match_regex:dynamic allocation helper retained + +struct Element { + Element(); + ~Element(); +}; + +Element *allocate() { + return new Element[2]; +} diff --git a/tests/tests/regressions/array-construction-no-allocation.sft.cpp b/tests/tests/regressions/array-construction-no-allocation.sft.cpp new file mode 100644 index 0000000000..c252ceb98e --- /dev/null +++ b/tests/tests/regressions/array-construction-no-allocation.sft.cpp @@ -0,0 +1,25 @@ +//type:fp +//options_all:--c++11 +//options:--exceptions:--no_exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; awk 'BEGIN { found = 0 } /__vec_new|__cxa_vec_new|__cxa_vec_ctor/ { found = 1 } END { if (found) print "unexpected vector allocation helper"; else print "array construction needs no allocation helper" }' Test_name.int.c +//match_regex:array construction needs no allocation helper + +struct Element { + int value; + Element(int n = 7) : value(n) {} +#if TEST_NUMBER == 2 + ~Element(); +#endif +}; + +struct Container { + Element elements[2][3]; + Container(); +}; + +Container::Container() {} + +void construct() { + Container object; +} diff --git a/tests/tests/regressions/array-construction-semantics.sft.cpp b/tests/tests/regressions/array-construction-semantics.sft.cpp new file mode 100644 index 0000000000..9f9e58911b --- /dev/null +++ b/tests/tests/regressions/array-construction-semantics.sft.cpp @@ -0,0 +1,39 @@ +//type:rp +//options_all:--c++11 +//options:--exceptions:--no_exceptions + +int constructed; +int defaults; + +int next_value() { + return ++defaults; +} + +struct Element { + int value; + Element(int n = next_value()) : value(n) { + ++constructed; + } +}; + +struct Container { + Element elements[2][3]; + Container(); +}; + +Container::Container() {} + +int main() { + Container object; + if (constructed != 6 || defaults != 6) { + return 1; + } + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != 3; ++j) { + if (object.elements[i][j].value != i * 3 + j + 1) { + return 2; + } + } + } + return 0; +} From 29bcc56ad75a3037e0befed1715571e8adbf2b30 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:03 +0000 Subject: [PATCH 06/14] Align array construction changes with EDG style [GH #561] Simplify regression filters and clarify the lowering condition. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 13 +++++++------ .../regressions/array-construction-cleanup.sft.cpp | 2 +- .../regressions/array-construction-dynamic.sft.cpp | 2 +- .../array-construction-no-allocation.sft.cpp | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index b44f26b3ad..c40153a0d7 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -5917,8 +5917,9 @@ must NOT already be lowered (see comment in default_version_of_routine). ctor_routine, dtor_routine); } else if (!zero_storage && (!exceptions_enabled || dtor_routine == NULL)) { - /* No cleanup is needed if a constructor throws. Construct the elements - directly without pulling in the allocating vector-new runtime. */ + /* With exceptions disabled or no destructor to call, construct the + elements directly without pulling in the allocating vector-new + runtime. */ an_insert_location helper_insert_location; a_variable_ptr element_ptr; a_type_ptr element_type = type_pointed_to(entity_node->type); @@ -5928,15 +5929,15 @@ must NOT already be lowered (see comment in default_version_of_routine). num_elem_node = num_elem_node_if_array(ipdp); check_assertion(num_elem_node != NULL); helper_routine = helper_routine_to_loop_through_array_elements( - element_type, &element_ptr, - &helper_insert_location); + element_type, &element_ptr, + &helper_insert_location); ctor_call = make_call_node(ctor_routine, var_rvalue_expr(element_ptr)); (void)insert_expr_statement(ctor_call, &helper_insert_location); entity_node = add_cast_if_necessary(entity_node, make_pointer_type(element_type)); entity_node->next = add_cast_if_necessary( - num_elem_node, - integer_type(targ_size_t_int_kind)); + num_elem_node, + integer_type(targ_size_t_int_kind)); call_node = make_call_node(helper_routine, entity_node); } else { /* Normal constructor case. */ diff --git a/tests/tests/regressions/array-construction-cleanup.sft.cpp b/tests/tests/regressions/array-construction-cleanup.sft.cpp index 12082794cc..fe7de15eb0 100644 --- a/tests/tests/regressions/array-construction-cleanup.sft.cpp +++ b/tests/tests/regressions/array-construction-cleanup.sft.cpp @@ -1,7 +1,7 @@ //type:fp //options_all:--c++11 --exceptions //require:BACK_END_IS_C_GEN_BE 1 -//filter:cat; awk 'BEGIN { found = 0 } /__vec_new_eh|__cxa_vec_ctor/ { found = 1 } END { if (found) print "exception cleanup helper retained"; else print "missing exception cleanup helper" }' Test_name.int.c +//filter:cat; grep -Eq '__vec_new_eh|__cxa_vec_ctor' Test_name.int.c && echo 'exception cleanup helper retained' //match_regex:exception cleanup helper retained struct Element { diff --git a/tests/tests/regressions/array-construction-dynamic.sft.cpp b/tests/tests/regressions/array-construction-dynamic.sft.cpp index c0cc0877c4..54e586b094 100644 --- a/tests/tests/regressions/array-construction-dynamic.sft.cpp +++ b/tests/tests/regressions/array-construction-dynamic.sft.cpp @@ -2,7 +2,7 @@ //options_all:--c++11 //options:--exceptions:--no_exceptions //require:BACK_END_IS_C_GEN_BE 1 -//filter:cat; awk 'BEGIN { found = 0 } /__array_new|__vec_new|__cxa_vec_new/ { found = 1 } END { if (found) print "dynamic allocation helper retained"; else print "missing dynamic allocation helper" }' Test_name.int.c +//filter:cat; grep -Eq '__array_new|__vec_new|__cxa_vec_new' Test_name.int.c && echo 'dynamic allocation helper retained' //match_regex:dynamic allocation helper retained struct Element { diff --git a/tests/tests/regressions/array-construction-no-allocation.sft.cpp b/tests/tests/regressions/array-construction-no-allocation.sft.cpp index c252ceb98e..6292f00eaa 100644 --- a/tests/tests/regressions/array-construction-no-allocation.sft.cpp +++ b/tests/tests/regressions/array-construction-no-allocation.sft.cpp @@ -2,7 +2,7 @@ //options_all:--c++11 //options:--exceptions:--no_exceptions //require:BACK_END_IS_C_GEN_BE 1 -//filter:cat; awk 'BEGIN { found = 0 } /__vec_new|__cxa_vec_new|__cxa_vec_ctor/ { found = 1 } END { if (found) print "unexpected vector allocation helper"; else print "array construction needs no allocation helper" }' Test_name.int.c +//filter:cat; grep -Eq '__array_new|__vec_new|__cxa_vec_(new|ctor)' Test_name.int.c; [ $? = 1 ] && echo 'array construction needs no allocation helper' //match_regex:array construction needs no allocation helper struct Element { From e6ad6cfecd86058a4dfb37e7a3d2e4d24684dc30 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:05 +0000 Subject: [PATCH 07/14] Separate fixed-array cleanup from allocation [GH #561] Use nested construction loops and a nonallocating runtime for preallocated arrays. Preserve vector allocation entry points and exception cleanup behavior. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- doc/source/lower_il.rst | 29 +- lib_src/CMakeLists.txt | 2 +- lib_src/throw.c | 6 +- lib_src/vec_newdel.c | 162 +--------- lib_src/vec_newdel.h | 47 ++- lib_src/vec_preallocated.c | 281 ++++++++++++++++++ src/lower_init.c | 184 ++++++++++-- .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 + .../default.2.1.txt | 2 + .../default.2.matches.txt | 4 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + .../default.1.1.txt | 2 + .../default.1.matches.txt | 4 + .../default.2.1.txt | 2 + .../default.2.matches.txt | 4 + .../array-construction-cleanup.sft.cpp | 2 +- .../array-construction-copy-shape.sft.cpp | 17 ++ .../array-construction-copy.sft.cpp | 45 +++ .../array-construction-double-throw.sft.cpp | 40 +++ .../array-construction-nested-shape.sft.cpp | 15 + .../array-construction-nested.sft.cpp | 29 ++ ...array-construction-runtime-cleanup.sft.cpp | 108 +++++++ .../array-construction-throw.sft.cpp | 83 ++++++ .../array-construction-vla.sft.cpp | 23 ++ .../array-construction-zero-runtime.sft.cpp | 45 +++ .../array-construction-zero.sft.cpp | 24 ++ 48 files changed, 983 insertions(+), 209 deletions(-) create mode 100644 lib_src/vec_preallocated.c create mode 100644 tests/tests/regressions/.array-construction-copy-shape.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-copy-shape.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-copy.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-copy.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-double-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-double-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-nested-shape.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-nested-shape.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-nested-shape.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-nested-shape.rto/default.2.matches.txt create mode 100644 tests/tests/regressions/.array-construction-nested.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-nested.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-nested.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-nested.rto/default.2.2.txt create mode 100644 tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla.rto/default.2.2.txt create mode 100644 tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.2.txt create mode 100644 tests/tests/regressions/.array-construction-zero.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-zero.rto/default.1.matches.txt create mode 100644 tests/tests/regressions/.array-construction-zero.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-zero.rto/default.2.matches.txt create mode 100644 tests/tests/regressions/array-construction-copy-shape.sft.cpp create mode 100644 tests/tests/regressions/array-construction-copy.sft.cpp create mode 100644 tests/tests/regressions/array-construction-double-throw.sft.cpp create mode 100644 tests/tests/regressions/array-construction-nested-shape.sft.cpp create mode 100644 tests/tests/regressions/array-construction-nested.sft.cpp create mode 100644 tests/tests/regressions/array-construction-runtime-cleanup.sft.cpp create mode 100644 tests/tests/regressions/array-construction-throw.sft.cpp create mode 100644 tests/tests/regressions/array-construction-vla.sft.cpp create mode 100644 tests/tests/regressions/array-construction-zero-runtime.sft.cpp create mode 100644 tests/tests/regressions/array-construction-zero.sft.cpp diff --git a/doc/source/lower_il.rst b/doc/source/lower_il.rst index acee8b30b2..d385a6c626 100644 --- a/doc/source/lower_il.rst +++ b/doc/source/lower_il.rst @@ -1992,24 +1992,29 @@ Constructors and Destructors for Arrays --------------------------------------- For construction of an entire (static or automatic) array, lowering generates -a loop that calls the element constructor when no storage zeroing or exception -cleanup is needed. Other construction cases and array destruction call -runtime routines. For the cfront-like ABI, ``__vec_new`` or -``__vec_delete`` are used (see previous section). The address of the array is -passed into ``__vec_new``, thus suppressing allocation, and ``0`` for the flag -``free`` is passed into ``__vec_delete``, thus suppressing freeing of the -storage. For the IA-64 ABI, there are specific runtime routines for these -functions: +nested loops that call the element constructor when no exception cleanup is +needed, the array is not variably sized, and nested bounds are known and +nonzero. The loops zero each element first when value-initialization requires +it. When construction needs +exception cleanup, or for other construction cases handled by the runtime, +the cfront-like ABI calls ``__edg_vec_ctor`` for preallocated storage. +Array destruction uses ``__edg_vec_dtor``. These entry points and the +exception unwinder's array cleanup are implemented separately from the +allocation-capable vector runtime. Newly generated calls require a matching +runtime library; the existing vector entry points remain available for +previously generated code and dynamic allocation. + +For the IA-64 ABI, the standard runtime routines for these functions are: | ``__cxa_vec_ctor(``\ *array, num_elems, elem_size, ctor_routine,* *dtor_routine*\ ``)`` | ``__cxa_vec_dtor(``\ *array, num_elems, elem_size, dtor_routine*\ ``)`` Copying of an entire array using a copy constructor is done in the cfront-like -ABI by calling ``__vec_cctor``, passing the array address, the number of -elements, the element size, the address of the routine to call, and the source -array address. [#f4]_ In the IA-64 ABI, the runtime routine is -``__cxa_vec_cctor.`` +ABI by calling ``__vec_cctor``, or ``__edg_vec_cctor`` when exception cleanup +is needed. The call passes the array address, the number of elements, the +element size, the routine to call, and the source array address. [#f4]_ In +the IA-64 ABI, the runtime routine is ``__cxa_vec_cctor``. If the constructor involved has default arguments, a wrapper routine is created that requires no arguments other than the "``this``" parameter and calls the diff --git a/lib_src/CMakeLists.txt b/lib_src/CMakeLists.txt index f11512bf58..a579280190 100644 --- a/lib_src/CMakeLists.txt +++ b/lib_src/CMakeLists.txt @@ -58,7 +58,7 @@ set(BARE_FILE_NAMES array_del array_del_aligned array_new newnothrow newnothrow_aligned placedel placenew pure_virt rtti set_new sized_array_del sized_array_del_aligned sized_del sized_del_aligned static_init stdexcept thread_dtor throw - typeinfo vars vec_cctor vec_newdel vla_alloc) + typeinfo vars vec_cctor vec_newdel vec_preallocated vla_alloc) # Load the specified macro configuration for the runtime library (i.e., # cpprtlib). diff --git a/lib_src/throw.c b/lib_src/throw.c index c13fd82384..c4c8cd58df 100644 --- a/lib_src/throw.c +++ b/lib_src/throw.c @@ -832,9 +832,8 @@ requires cleanup. elements = (an_element_count)*element_addr; } /* if */ #ifndef __EDG_IA64_ABI - __vec_delete(obj_addr, (an_element_count_param)elements, - ehasp->element_size, dtor_ptr, /*delete_flag=*/FALSE, - /*unused_arg=*/0); + __edg_vec_dtor(obj_addr, (size_t)elements, + ehasp->element_size, dtor_ptr); #else /* ifdef __EDG_IA64_ABI */ ABI_NAMESPACE::__cxa_vec_dtor(obj_addr, elements, ehasp->element_size, dtor_ptr); @@ -2047,4 +2046,3 @@ built without exception handling support. This version does nothing. } /* __eh_exit_processing */ #endif /* EXCEPTION_HANDLING */ - diff --git a/lib_src/vec_newdel.c b/lib_src/vec_newdel.c index 5f4a496ce1..bd465626c7 100644 --- a/lib_src/vec_newdel.c +++ b/lib_src/vec_newdel.c @@ -59,50 +59,6 @@ EXTERN_C void _array_pointer_not_from_vec_new(); to one of the vector handling routines. */ #endif /* ifndef __EDG_IA64_ABI */ -struct an_array_alloc_eh_info { - void* array_ptr; - /* Pointer to the memory allocated for the array. */ - ptrdiff_t - number_of_elements; - /* Total number of elements in the array. */ - size_t - element_size; - /* Size of each element. */ - size_t - prefix_size; - /* Size of the array prefix. */ - size_t - elements_processed; - /* Number of elements constructed or destructed so - far. */ - a_boolean - is_vec_new; - /* TRUE if this is a vec_new operation, FALSE if this - is a vec_delete. */ - a_boolean - free_memory_on_cleanup; - /* TRUE if the memory for the array was allocated by - new and should be freed during object cleanup. */ - a_destructor_ptr - destructor; - /* Pointer to the destructor to be called for each - element of the array. */ - a_delete_ptr - delete_routine; - /* Pointer to the delete routine to be called to - deallocate an array. */ - - a_boolean is_two_arg; - /* TRUE if the delete routine is the two argument - form. FALSE if it is the single argument form. */ -#ifdef __EDG_IA64_ABI - a_boolean terminate_immediately; - /* TRUE if any exception during the destruction of the - array elements should result in termination. */ -#endif /* defined(__EDG_IA64_ABI) */ -}; - - /* Increment a void* pointer by a given value. */ @@ -132,6 +88,7 @@ operation that is in process. aaehip->destructor = NULL; aaehip->delete_routine = NULL; aaehip->is_two_arg = FALSE; + aaehip->free_array = NULL; } /* add_vec_new_or_delete_eh_stack_entry */ #endif /* EXCEPTION_HANDLING */ @@ -523,6 +480,7 @@ use. if (create_eh_stack_entry) { add_vec_new_or_delete_eh_stack_entry(&ehse, &aaehi, /*is_vec_new=*/TRUE); aaehi.free_memory_on_cleanup = free_memory_on_cleanup; + aaehi.free_array = free_array; aaehi.number_of_elements = number_of_elements; aaehi.element_size = element_size; aaehi.prefix_size = prefix_size; @@ -811,105 +769,7 @@ operators where the delete operator, if any, takes two arguments. } /* __cxa_vec_new3 */ -EXTERN_C void ABI_NAMESPACE::__cxa_vec_ctor( - void *array_ptr, - size_t number_of_elements, - size_t element_size, - a_constructor_ptr ctor, - a_destructor_ptr dtor) -/* -The entry point used for constructing an array of objects where the memory has -already been allocated. -*/ -{ - (void)(array_new_general(array_ptr, (ptrdiff_t)number_of_elements, - element_size, /*prefix_size=*/0, (void *)NULL, - ctor, dtor, (a_new_ptr)NULL, (a_delete_ptr)NULL, - /*is_two_arg=*/FALSE, /*zero_init=*/FALSE)); -} /* __cxa_vec_ctor */ - - -EXTERN_C void ABI_NAMESPACE::__cxa_vec_cctor( - void *array_ptr, - void *src_array_ptr, - size_t number_of_elements, - size_t element_size, - a_copy_constructor_ptr ctor, - a_destructor_ptr dtor) -/* -The entry point used for copying an array of objects. -*/ -{ - (void)(array_new_general(array_ptr, (ptrdiff_t)number_of_elements, - element_size, /*prefix_size=*/0, src_array_ptr, - (a_constructor_ptr)ctor, dtor, (a_new_ptr)NULL, - (a_delete_ptr)NULL, /*is_two_arg=*/FALSE, - /*zero_init=*/FALSE)); -} /* __cxa_vec_cctor */ - -#endif /* defined(__EDG_IA64_ABI) */ - -#if EXCEPTION_HANDLING -EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep) -/* -Called by the exception handling cleanup routine to do the cleanup -processing for a vec_new or vec_delete operation that was interrupted by -an exception. -*/ -{ - /* Call the destructor, if specified, on each element in the array, in - reverse order. */ - an_array_alloc_eh_info_ptr aaehip = ehsep->variant.array_alloc_eh_info; - a_destructor_ptr dtor = aaehip->destructor; - size_t number_of_elements; - size_t element_size; - void* arr_ptr; - void* array_ptr; - size_t i; - size_t first_element; - -#ifdef __EDG_IA64_ABI - if (aaehip->terminate_immediately) { - __call_terminate(); - } /* if */ #endif /* defined(__EDG_IA64_ABI) */ - array_ptr = (void *)aaehip->array_ptr; - element_size = aaehip->element_size; - if (aaehip->is_vec_new) { - /* Cleaning up a vec_new. Destroy the fully constructed elements of - the array in reverse order. */ - number_of_elements = aaehip->elements_processed; - first_element = number_of_elements - 1; - } else { - first_element = aaehip->number_of_elements - - aaehip->elements_processed - 1; - number_of_elements = first_element + 1; - } /* if */ - if (dtor != NULL) { - /* If there is a destructor, destroy the objects. */ - for (i = 0, - arr_ptr = (void *)(((char *)array_ptr) + - first_element * element_size); - i < number_of_elements; - i++, increment_ptr(arr_ptr, -(int)(element_size))) { -#ifndef __EDG_IA64_ABI - /* Call the destructor with 0x2 - whole object = TRUE - 0x1 - delete object = FALSE. */ - (*dtor)(arr_ptr, 0x2 /*whole object = TRUE, delete = FALSE*/); -#else /* ifdef __EDG_IA64_ABI */ - (*dtor)(arr_ptr); -#endif /* ifdef __EDG_IA64_ABI */ - } /* for */ - } /* if */ - if (aaehip->free_memory_on_cleanup) { - /* Call the routine to free the memory. */ - size_t size = element_size * aaehip->number_of_elements; - free_array(array_ptr, size, aaehip->prefix_size, - aaehip->delete_routine, aaehip->is_two_arg); - } /* if */ -} /* __cleanup_vec_new_or_delete */ -#endif /* EXCEPTION_HANDLING */ - #ifndef __EDG_IA64_ABI /*ARGSUSED*/ /* terminate_immediately is used only in the IA-64 ABI. */ @@ -952,6 +812,7 @@ elements will be destroyed and the exception will be rethrown. add_vec_new_or_delete_eh_stack_entry(&ehse, &aaehi, /*is_vec_new=*/FALSE); aaehi.free_memory_on_cleanup = delete_flag; + aaehi.free_array = free_array; aaehi.array_ptr = array_ptr; aaehi.number_of_elements = number_of_elements; aaehi.element_size = element_size; @@ -1055,22 +916,6 @@ requires two arguments. } /* __array_delete */ #endif /* ABI_CHANGES_FOR_ARRAY_NEW_AND_DELETE */ #else /* defined(__EDG_IA64_ABI) */ -EXTERN_C void ABI_NAMESPACE::__cxa_vec_dtor( - void *array_ptr, - size_t number_of_elements, - size_t element_size, - a_destructor_ptr dtor) -/* -Run the destructors for an array of objects. -*/ -{ - array_delete_general(array_ptr, number_of_elements, element_size, - /*prefix_size=*/0, dtor, /*delete_flag=*/FALSE, - (a_delete_ptr)NULL, /*is_two_arg=*/FALSE, - /*terminate_immediately=*/FALSE); -} /* __cxa_vec_dtor */ - - EXTERN_C void ABI_NAMESPACE::__cxa_vec_delete(void *array_ptr, size_t element_size, size_t prefix_size, @@ -1168,4 +1013,3 @@ The name is intended to describe the nature of the problem to the user __abort_execution(ec_array_not_from_vec_new); } #endif /* ifndef __EDG_IA64_ABI */ - diff --git a/lib_src/vec_newdel.h b/lib_src/vec_newdel.h index 9874b3e267..ddad8fa5d6 100644 --- a/lib_src/vec_newdel.h +++ b/lib_src/vec_newdel.h @@ -47,5 +47,50 @@ operations that are in process. */ typedef struct an_array_alloc_eh_info *an_array_alloc_eh_info_ptr; -#endif /* ifndef VEC_NEWDEL_H */ +struct an_array_alloc_eh_info { + void* array_ptr; /* Pointer to the memory allocated for the array. */ + ptrdiff_t number_of_elements; + /* Total number of elements in the array. */ + size_t element_size; /* Size of each element. */ + size_t prefix_size; /* Size of the array prefix. */ + size_t elements_processed; + /* Number of elements constructed or destructed. */ + a_boolean is_vec_new; /* TRUE for construction, FALSE for destruction. */ + a_boolean free_memory_on_cleanup; + /* TRUE if cleanup must free the array storage. */ + a_destructor_ptr destructor; + /* Destructor for each element of the array. */ + a_delete_ptr delete_routine; + /* Deallocation routine, or NULL for the default. */ + a_boolean is_two_arg; /* TRUE for a two-argument deallocation routine. */ + void (*free_array)(void *, size_t, size_t, a_delete_ptr, int); + /* Array deallocator supplied by the allocating + runtime, or NULL for preallocated arrays. */ +#ifdef __EDG_IA64_ABI + a_boolean terminate_immediately; + /* TRUE if a destructor throw requires termination. */ +#endif /* defined(__EDG_IA64_ABI) */ +}; + +EXTERN_C void __edg_vec_dtor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_destructor_ptr dtor); + +EXTERN_C void *__edg_vec_ctor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_constructor_ptr ctor, + a_destructor_ptr dtor, + int zero_init); +#ifndef __EDG_IA64_ABI +EXTERN_C void __edg_vec_cctor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_copy_constructor_ptr ctor, + void *src_array_ptr, + a_destructor_ptr dtor); +#endif /* ifndef __EDG_IA64_ABI */ + +#endif /* ifndef VEC_NEWDEL_H */ diff --git a/lib_src/vec_preallocated.c b/lib_src/vec_preallocated.c new file mode 100644 index 0000000000..32b9945858 --- /dev/null +++ b/lib_src/vec_preallocated.c @@ -0,0 +1,281 @@ +/* +Part of the EDG Compiler Project, under the Apache License v2.0 with LLVM +Exceptions. +See https://edgcpp.org/LICENSE.txt for license information. +SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +*/ + +#include "basics.h" +#include "runtime.h" +#include "eh.h" +#pragma hdrstop +#include "vec_newdel.h" +#include "memzero.h" + +#if EXCEPTION_HANDLING +static void add_preallocated_eh_entry(an_eh_stack_entry_ptr ehsep, + an_array_alloc_eh_info_ptr info, + void *array_ptr, + size_t count, + size_t element_size, + a_destructor_ptr dtor, + a_boolean is_vec_new) +/* +Link ehsep to the exception-handling stack and initialize info for the +preallocated array at array_ptr, which has count elements of element_size. +dtor is the element destructor, and is_vec_new indicates construction rather +than destruction. +*/ +{ + ehsep->next = __curr_eh_stack_entry; + __curr_eh_stack_entry = ehsep; + ehsep->kind = ehsek_vec_new_or_delete; + ehsep->variant.array_alloc_eh_info = info; + info->array_ptr = array_ptr; + info->number_of_elements = count; + info->element_size = element_size; + info->prefix_size = 0; + info->elements_processed = 0; + info->is_vec_new = is_vec_new; + info->free_memory_on_cleanup = FALSE; + info->destructor = dtor; + info->delete_routine = NULL; + info->is_two_arg = FALSE; + info->free_array = NULL; +#ifdef __EDG_IA64_ABI + info->terminate_immediately = FALSE; +#endif /* defined(__EDG_IA64_ABI) */ +} /* add_preallocated_eh_entry */ + + +EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep) +/* +Use ehsep to clean up an interrupted array construction or destruction. +Allocated arrays carry their deallocator in the stack entry; preallocated +arrays do not. +*/ +{ + an_array_alloc_eh_info_ptr info = ehsep->variant.array_alloc_eh_info; + size_t count; + size_t i; + +#ifdef __EDG_IA64_ABI + if (info->terminate_immediately) { + __call_terminate(); + } /* if */ +#endif /* defined(__EDG_IA64_ABI) */ + if (info->is_vec_new) { + count = info->elements_processed; + } else { + count = info->number_of_elements - info->elements_processed; + } /* if */ + if (info->destructor != NULL) { + for (i = count; i != 0; --i) { + void *element = (char *)info->array_ptr + (i - 1) * info->element_size; +#ifndef __EDG_IA64_ABI + (*info->destructor)(element, 0x2); +#else /* defined(__EDG_IA64_ABI) */ + (*info->destructor)(element); +#endif /* defined(__EDG_IA64_ABI) */ + } /* for */ + } /* if */ + if (info->free_memory_on_cleanup) { + check_assertion(info->free_array != NULL); + (*info->free_array)(info->array_ptr, + info->element_size * info->number_of_elements, + info->prefix_size, info->delete_routine, + info->is_two_arg); + } /* if */ +} /* __cleanup_vec_new_or_delete */ +#endif /* EXCEPTION_HANDLING */ + + +EXTERN_C void *__edg_vec_ctor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_constructor_ptr ctor, + a_destructor_ptr dtor, + int zero_init) +/* +Construct number_of_elements objects of element_size in preallocated storage +at array_ptr using ctor, zeroing the storage first if zero_init is TRUE. Use +dtor to clean up constructed elements if a constructor throws. Return +array_ptr. +*/ +{ + size_t i; +#if EXCEPTION_HANDLING + an_eh_stack_entry ehse; + an_array_alloc_eh_info info; +#endif /* EXCEPTION_HANDLING */ + + if (zero_init) __memzero(array_ptr, number_of_elements * element_size); +#if EXCEPTION_HANDLING + if (dtor != NULL) { + add_preallocated_eh_entry(&ehse, &info, array_ptr, number_of_elements, + element_size, dtor, /*is_vec_new=*/TRUE); + } /* if */ +#endif /* EXCEPTION_HANDLING */ + if (ctor != NULL) { + for (i = 0; i < number_of_elements; ++i) { + void *element = (char *)array_ptr + i * element_size; +#if CFRONT_COMPATIBILITY_MODE + a_cfront_constructor_ptr cfront_ctor = (a_cfront_constructor_ptr)ctor; + (*cfront_ctor)(element, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); +#else /* CFRONT_COMPATIBILITY_MODE */ + (*ctor)(element); +#endif /* CFRONT_COMPATIBILITY_MODE */ +#if EXCEPTION_HANDLING + if (dtor != NULL) ++info.elements_processed; +#endif /* EXCEPTION_HANDLING */ + } /* for */ + } /* if */ +#if EXCEPTION_HANDLING + if (dtor != NULL) __curr_eh_stack_entry = ehse.next; +#endif /* EXCEPTION_HANDLING */ + return array_ptr; +} /* __edg_vec_ctor */ + + +EXTERN_C void __edg_vec_dtor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_destructor_ptr dtor) +/* +Use dtor to destroy number_of_elements objects of element_size in preallocated +storage at array_ptr, in reverse order. +*/ +{ + size_t i; +#if EXCEPTION_HANDLING + an_eh_stack_entry ehse; + an_array_alloc_eh_info info; +#endif /* EXCEPTION_HANDLING */ + + if (array_ptr == NULL) return; +#if EXCEPTION_HANDLING + add_preallocated_eh_entry(&ehse, &info, array_ptr, number_of_elements, + element_size, dtor, /*is_vec_new=*/FALSE); +#endif /* EXCEPTION_HANDLING */ + if (dtor != NULL) { + for (i = number_of_elements; i != 0; --i) { + void *element = (char *)array_ptr + (i - 1) * element_size; +#if EXCEPTION_HANDLING + ++info.elements_processed; +#endif /* EXCEPTION_HANDLING */ +#ifndef __EDG_IA64_ABI + (*dtor)(element, 0x2); +#else /* defined(__EDG_IA64_ABI) */ + (*dtor)(element); +#endif /* defined(__EDG_IA64_ABI) */ + } /* for */ + } /* if */ +#if EXCEPTION_HANDLING + __curr_eh_stack_entry = ehse.next; +#endif /* EXCEPTION_HANDLING */ +} /* __edg_vec_dtor */ + + +static void copy_preallocated_array(void *array_ptr, + void *src_array_ptr, + size_t number_of_elements, + size_t element_size, + a_copy_constructor_ptr ctor, + a_destructor_ptr dtor) +/* +Copy number_of_elements objects of element_size from src_array_ptr to +preallocated storage at array_ptr using ctor. Use dtor to clean up +constructed elements if a copy constructor throws. +*/ +{ + size_t i; +#if EXCEPTION_HANDLING + an_eh_stack_entry ehse; + an_array_alloc_eh_info info; + + if (dtor != NULL) { + add_preallocated_eh_entry(&ehse, &info, array_ptr, number_of_elements, + element_size, dtor, /*is_vec_new=*/TRUE); + } /* if */ +#endif /* EXCEPTION_HANDLING */ + if (ctor != NULL) { + for (i = 0; i < number_of_elements; ++i) { + void *dest = (char *)array_ptr + i * element_size; + void *src = (char *)src_array_ptr + i * element_size; + (*ctor)(dest, src); +#if EXCEPTION_HANDLING + if (dtor != NULL) ++info.elements_processed; +#endif /* EXCEPTION_HANDLING */ + } /* for */ + } /* if */ +#if EXCEPTION_HANDLING + if (dtor != NULL) __curr_eh_stack_entry = ehse.next; +#endif /* EXCEPTION_HANDLING */ +} /* copy_preallocated_array */ + + +#ifndef __EDG_IA64_ABI +EXTERN_C void __edg_vec_cctor(void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_copy_constructor_ptr ctor, + void *src_array_ptr, + a_destructor_ptr dtor) +/* +Copy the array at src_array_ptr into preallocated storage at array_ptr. +The count, element size, copy constructor, and destructor are supplied by +the caller. +*/ +{ + copy_preallocated_array(array_ptr, src_array_ptr, number_of_elements, + element_size, ctor, dtor); +} /* __edg_vec_cctor */ +#else /* defined(__EDG_IA64_ABI) */ +EXTERN_C void ABI_NAMESPACE::__cxa_vec_cctor( + void *array_ptr, + void *src_array_ptr, + size_t number_of_elements, + size_t element_size, + a_copy_constructor_ptr ctor, + a_destructor_ptr dtor) +/* +Copy the array at src_array_ptr into preallocated storage at array_ptr. +The count, element size, copy constructor, and destructor are supplied by +the caller. +*/ +{ + copy_preallocated_array(array_ptr, src_array_ptr, number_of_elements, + element_size, ctor, dtor); +} /* __cxa_vec_cctor */ +#endif /* defined(__EDG_IA64_ABI) */ + + +#ifdef __EDG_IA64_ABI +EXTERN_C void ABI_NAMESPACE::__cxa_vec_ctor( + void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_constructor_ptr ctor, + a_destructor_ptr dtor) +/* +Construct number_of_elements objects of element_size in preallocated storage +at array_ptr using ctor. Use dtor to clean up if a constructor throws. +*/ +{ + (void)__edg_vec_ctor(array_ptr, number_of_elements, element_size, ctor, dtor, + /*zero_init=*/FALSE); +} /* __cxa_vec_ctor */ + + +EXTERN_C void ABI_NAMESPACE::__cxa_vec_dtor( + void *array_ptr, + size_t number_of_elements, + size_t element_size, + a_destructor_ptr dtor) +/* +Use dtor to destroy number_of_elements objects of element_size at array_ptr. +*/ +{ + __edg_vec_dtor(array_ptr, number_of_elements, element_size, dtor); +} /* __cxa_vec_dtor */ +#endif /* defined(__EDG_IA64_ABI) */ diff --git a/src/lower_init.c b/src/lower_init.c index c40153a0d7..645763e38b 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -2683,6 +2683,8 @@ NULL until then. STATIC_THREAD a_routine_ptr vec_new_routine, #if !IA64_ABI + vec_preallocated_ctor_routine, + vec_preallocated_dtor_routine, vec_new_eh_routine, vec_new_eh_zero_routine, array_new_routine, @@ -3378,7 +3380,8 @@ static an_expr_node_ptr make_vec_new_call(an_expr_node_ptr entity_node, a_routine_ptr dtor_routine, a_routine_ptr new_routine, a_routine_ptr delete_routine, - ARG_UNUSED a_boolean zero_storage) + ARG_UNUSED a_boolean zero_storage, + ARG_UNUSED a_boolean fixed_array) /* Make a call to a runtime routine (__vec_new or __array_new) that will allocate an array and call a constructor for each element of the @@ -3392,8 +3395,7 @@ called, or NULL if no constructor is to be called. dtor_routine is the destructor routine to be called -- this is non-NULL only if there is a destructor and if exceptions are enabled (in that case, it may be necessary to destroy array elements that were created if a -throw occurs halfway through the initialization of the array); -the runtime routine __vec_new_eh is called in that case. If +throw occurs halfway through the initialization of the array). If new_routine is non-NULL, it points to an "operator new[]" routine to be used to do the allocation; if it is null, the default routine is used. If delete_routine is non-NULL, it points to an "operator @@ -3404,7 +3406,8 @@ zeroed before the constructor is called, for value-initialization. The runtime routine __array_new is called for cases that require a special new or delete routine. This routine is not used for placement new cases. The routines called are different for the -IA-64 ABI; see comments below. +IA-64 ABI; see comments below. fixed_array is TRUE when the caller +supplies storage for an array that is not dynamically allocated. */ { an_expr_node_ptr call_node, arg_expr_list, size_elem_node; @@ -3497,7 +3500,24 @@ IA-64 ABI; see comments below. } /* if */ #endif /* IA64_ABI */ #if !IA64_ABI - if (zero_storage) { + if (fixed_array) { + num_elem_node->next = NULL; + num_elem_node = add_cast_if_necessary(num_elem_node, size_t_type); + entity_node->next = num_elem_node; + num_elem_node->next = size_elem_node; + dtor_addr_node = expr_for_pointer_to_destructor(dtor_routine); + ctor_addr_node->next = dtor_addr_node; + dtor_addr_node->next = node_for_integer_constant( + (long)zero_storage, ik_int); + call_node = make_prototyped_runtime_call_full( + "__edg_vec_ctor", + &vec_preallocated_ctor_routine, + void_star_type(), void_star_type(), + size_t_type, size_t_type, make_ctor_type(), + make_dtor_type(), + integer_type(ik_int), + NULL, arg_expr_list); + } else if (zero_storage) { /* __vec_new_eh_zero call, which zeroes storage before calling the constructor, for value-initialization. */ dtor_addr_node = expr_for_pointer_to_destructor(dtor_routine); @@ -3834,7 +3854,7 @@ IA-64 ABI, the routines called are different. call_node = make_vec_new_call(arg_entity_node, entity_type, num_elem_node, ctor_routine, dtor_routine, (a_routine_ptr)NULL, (a_routine_ptr)NULL, - zero_storage); + zero_storage, /*fixed_array=*/FALSE); if (prefix_size_node != NULL) { if (targ_use_var_arr_cookies) { assign_node = make_comma_node(assign_elem_size_node, assign_node); @@ -3890,8 +3910,8 @@ static an_expr_node_ptr make_vec_delete_call( a_routine_ptr delete_routine, a_boolean free_storage) /* -Make a call to a runtime routine (__vec_delete or __array_delete -for the Cfront-like ABI, __cxa_vec_dtor etc. for the IA-64 ABI) +Make a call to a runtime routine (__edg_vec_dtor, __vec_delete, or +__array_delete for the Cfront-like ABI, __cxa_vec_dtor etc. for the IA-64 ABI) that will call a destructor for each element of an array and then deallocate the array. entity_node gives the address of the array. num_elem_node is an expression giving the number of elements @@ -3910,6 +3930,7 @@ A pointer to the expression created is returned. a_type_ptr entity_type = entity_node->type; #if !IA64_ABI an_expr_node_ptr is_two_arg_node, free_storage_node; + a_boolean has_known_count = num_elem_node != NULL; #endif /* IA64_ABI */ an_expr_node_ptr prefix_size_node; an_expr_node_ptr orig_entity_node = entity_node; @@ -3926,7 +3947,18 @@ A pointer to the expression created is returned. /* Use the runtime array size from the "new[]". */ num_elem_node = var_arg_num_elem_node(); } /* if */ - if (delete_routine == NULL || aligned_delete) { + if (!free_storage && has_known_count && delete_routine == NULL) { + entity_node->next = add_cast_if_necessary(num_elem_node, size_t_type); + entity_node->next->next = size_elem_node; + size_elem_node->next = add_cast_if_necessary(dtor_addr_node, + make_dtor_type()); + call_node = make_prototyped_runtime_call_full( + "__edg_vec_dtor", + &vec_preallocated_dtor_routine, + void_type(), void_star_type(), size_t_type, + size_t_type, make_dtor_type(), NULL, NULL, + NULL, entity_node); + } else if (delete_routine == NULL || aligned_delete) { /* The call looks like __vec_delete(entity_node, num_elems, size_elem, dtor_addr_node, free_storage, 0) @@ -4218,8 +4250,8 @@ to the expression created is returned. /* The call looks like __vec_cctor (entity_node, num_elems, size_elem, cctor_routine, source_node) - __vec_cctor_eh(entity_node, num_elems, size_elem, cctor_routine, - source_node, dtor_routine) + __edg_vec_cctor(entity_node, num_elems, size_elem, cctor_routine, + source_node, dtor_routine) */ arg_expr_list = entity_node; entity_node->next = num_elem_node; @@ -4230,10 +4262,10 @@ to the expression created is returned. source_node = add_cast(source_node, void_star_type()); func_addr_node->next = source_node; if (exceptions_enabled && dtor_routine != NULL) { - /* __vec_cctor_eh call, with destructor. */ + /* Nonallocating copy construction with exception cleanup. */ dtor_addr_node = expr_for_pointer_to_destructor(dtor_routine); source_node->next = dtor_addr_node; - call_node = make_prototyped_runtime_call_full("__vec_cctor_eh", + call_node = make_prototyped_runtime_call_full("__edg_vec_cctor", &vec_cctor_eh_routine, void_type(), void_star_type(), @@ -5857,6 +5889,79 @@ primary routine has a definition. #endif /* IA64_ABI */ +static a_routine_ptr array_construction_loop(a_type_ptr element_type, + a_routine_ptr ctor_routine, + a_boolean zero_storage) +/* +Create and return a helper that calls ctor_routine for each element of +element_type, zeroing it first if zero_storage is TRUE. For array elements, +construct each subarray using a separate loop so pointer arithmetic stays +within the bounds of each array. +*/ +{ + a_routine_ptr helper_routine, nested_routine; + a_routine_type_supplement_ptr rtsp; + a_type_ptr pointer_type, count_type; + a_memory_region_number il_region; + a_scope_ptr scope; + a_variable_ptr element_ptr, count_var; + a_generated_routine_context context; + an_insert_location insert_location, loop_insert_location; + a_statement_ptr loop_stmt; + an_expr_node_ptr args, call, loop_expr, increment_expr; + + pointer_type = make_pointer_type(skip_typerefs(element_type)); + count_type = integer_type(targ_size_t_int_kind); + helper_routine = make_rout_entry((char *)NULL, sc_static, + void_type(), pointer_type); + rtsp = helper_routine->type->variant.routine.extra_info; + rtsp->param_type_list->next = alloc_param_type(count_type); + scope = make_routine_definition(helper_routine, /*make_return=*/TRUE, + curr_il_region_number, &il_region); + push_generated_routine_context(scope, il_region, &context); + element_ptr = make_lowered_param_variable(pointer_type); + count_var = make_lowered_param_variable(count_type); + scope->variant.routine.parameters = element_ptr; + element_ptr->next = count_var; + set_block_start_insert_location(scope->assoc_block, &insert_location); + set_insert_location_mark(&insert_location); + insert_pending_stmk_init_statements_at_mark(&insert_location); + loop_stmt = alloc_statement(stmk_while, /*compiler_generated=*/TRUE); + loop_expr = make_operator_node(eok_post_decr, count_type, + var_lvalue_expr(count_var)); + loop_stmt->expr = boolean_controlling_expr(loop_expr); + increment_expr = make_operator_node(eok_post_incr, pointer_type, + var_lvalue_expr(element_ptr)); + perform_post_pass_on_lowered_expression(increment_expr); + loop_stmt->variant.loop_statement = alloc_expr_statement(increment_expr); + insert_statement(loop_stmt, &insert_location); + set_expr_insert_location(increment_expr, &loop_insert_location); + args = var_rvalue_expr(element_ptr); + if (is_array_type(element_type)) { + a_type_ptr nested_type = array_element_type(element_type); + a_targ_size_t count = skip_typerefs(element_type)-> + variant.array.variant.number_of_elements; + nested_routine = array_construction_loop(nested_type, ctor_routine, + zero_storage); + args = add_cast_if_necessary(args, make_pointer_type(nested_type)); + args->next = node_for_host_large_integer((a_host_large_integer)count, + targ_size_t_int_kind); + call = make_call_node(nested_routine, args); + } else { + if (zero_storage) { + insert_call_to_zero_entity(element_type, /*have_complete_object=*/TRUE, + var_rvalue_expr(element_ptr), + (an_expr_node_ptr)NULL, (a_targ_size_t)0, + &loop_insert_location); + } /* if */ + call = make_call_node(ctor_routine, args); + } /* if */ + (void)insert_expr_statement(call, &loop_insert_location); + pop_generated_routine_context(scope, il_region, &context); + return helper_routine; +} /* array_construction_loop */ + + static void add_array_constructor_call( a_dynamic_init_ptr dip, an_expr_node_ptr entity_node, @@ -5877,6 +5982,7 @@ must NOT already be lowered (see comment in default_version_of_routine). a_routine_ptr ctor_routine, dtor_routine; an_expr_node_ptr call_node, num_elem_node; a_boolean zero_storage; + a_type_ptr element_type; #if CHECKING if (dip->kind != (a_dynamic_init_kind)dik_constructor) { @@ -5910,31 +6016,43 @@ must NOT already be lowered (see comment in default_version_of_routine). if (dip->init_expr_lifetime != NULL) { unbind_object_lifetime(dip->init_expr_lifetime); } /* if */ + element_type = type_from_init_pos_descr(ipdp); if (source_node != NULL) { /* Copy constructor case. */ check_assertion(!dip->variant.constructor.value_initialization); call_node = make_vec_cctor_call(entity_node, source_node, ipdp, ctor_routine, dtor_routine); - } else if (!zero_storage && - (!exceptions_enabled || dtor_routine == NULL)) { + } else if ((!exceptions_enabled || dtor_routine == NULL) && + ipdp->num_elem_node == NULL && + (ipdp->variable == NULL || !ipdp->variable->is_vla) && + ipdp->base_type != NULL && + !is_vla_type(ipdp->base_type) && + !(is_array_type(element_type) && + (is_vla_type(element_type) || + is_incomplete_array_type(element_type) || + num_array_elements(element_type) == 0))) { /* With exceptions disabled or no destructor to call, construct the elements directly without pulling in the allocating vector-new runtime. */ - an_insert_location helper_insert_location; - a_variable_ptr element_ptr; - a_type_ptr element_type = type_pointed_to(entity_node->type); a_routine_ptr helper_routine; - an_expr_node_ptr ctor_call; num_elem_node = num_elem_node_if_array(ipdp); check_assertion(num_elem_node != NULL); - helper_routine = helper_routine_to_loop_through_array_elements( - element_type, &element_ptr, - &helper_insert_location); - ctor_call = make_call_node(ctor_routine, var_rvalue_expr(element_ptr)); - (void)insert_expr_statement(ctor_call, &helper_insert_location); + helper_routine = array_construction_loop(element_type, ctor_routine, + zero_storage); entity_node = add_cast_if_necessary(entity_node, make_pointer_type(element_type)); + if (is_array_type(element_type)) { + num_elem_node = add_cast_if_necessary( + num_elem_node, + integer_type(targ_size_t_int_kind)); + num_elem_node->next = node_for_host_large_integer( + (a_host_large_integer)num_array_elements(element_type), + targ_size_t_int_kind); + num_elem_node = make_operator_node(eok_divide, + integer_type(targ_size_t_int_kind), + num_elem_node); + } /* if */ entity_node->next = add_cast_if_necessary( num_elem_node, integer_type(targ_size_t_int_kind)); @@ -5950,7 +6068,7 @@ must NOT already be lowered (see comment in default_version_of_routine). exceptions_enabled ? dtor_routine : (a_routine *)NULL, (a_routine *)NULL, (a_routine *)NULL, - zero_storage); + zero_storage, /*fixed_array=*/TRUE); } /* if */ /* Make a statement containing the call and insert it at the right location. */ @@ -6003,11 +6121,10 @@ dynamic init pointer because of the make_destruction_routine case. /* Generate code for the destructor call. */ if (num_elem_node != NULL) { #if !IA64_ABI - /* default_version_of_routine is not called on purpose; __vec_delete - knows about the implicit argument for destructors and generates - it automatically. */ + /* default_version_of_routine is not called on purpose; the array + destruction runtime supplies the implicit destructor argument. */ #endif /* !IA64_ABI */ - /* Generate the __vec_delete call. */ + /* Generate the array destruction call. */ /* Build an expression for the address of the destructor. */ dtor_addr_node = expr_for_pointer_to_destructor(dtor_routine); call_node = make_vec_delete_call(entity_node, num_elem_node, @@ -12571,7 +12688,7 @@ arrays with class elements. vec_new_node = make_vec_new_call(entity_node, ptr_elem_type, num_elem_node, ctor_routine, dtor_routine, new_routine, delete_routine, - zero_storage); + zero_storage, /*fixed_array=*/FALSE); #if ABI_CHANGES_FOR_PLACEMENT_DELETE } else { /* Placement new. Construct a call of __placement_array_new. */ @@ -20827,6 +20944,8 @@ Do one-time initialization of static variables declared in lower_init.c. STATIC_THREAD a_pch_saved_variable saved_vars[] = { pch_saved_var_array_elem(vec_new_routine), #if !IA64_ABI + pch_saved_var_array_elem(vec_preallocated_ctor_routine), + pch_saved_var_array_elem(vec_preallocated_dtor_routine), pch_saved_var_array_elem(vec_new_eh_routine), pch_saved_var_array_elem(vec_new_eh_zero_routine), pch_saved_var_array_elem(array_new_routine), @@ -20903,6 +21022,8 @@ Do one-time initialization of static variables declared in lower_init.c. between translation units. */ register_trans_unit_variable(vec_new_routine); #if !IA64_ABI + register_trans_unit_variable(vec_preallocated_ctor_routine); + register_trans_unit_variable(vec_preallocated_dtor_routine); register_trans_unit_variable(vec_new_eh_routine); register_trans_unit_variable(vec_new_eh_zero_routine); register_trans_unit_variable(array_new_routine); @@ -20986,6 +21107,8 @@ for each translation unit. { vec_new_routine = NULL; #if !IA64_ABI + vec_preallocated_ctor_routine = NULL; + vec_preallocated_dtor_routine = NULL; vec_new_eh_routine = NULL; vec_new_eh_zero_routine = NULL; array_new_routine = NULL; @@ -21076,4 +21199,3 @@ for each compilation. END_EDG_NAMESPACE #endif /* DO_IL_LOWERING */ - diff --git a/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.1.txt new file mode 100644 index 0000000000..4736ac0947 --- /dev/null +++ b/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +nonallocating array copy diff --git a/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.matches.txt new file mode 100644 index 0000000000..9d0ab7c500 --- /dev/null +++ b/tests/tests/regressions/.array-construction-copy-shape.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "nonallocating array copy": + <----------------------------------------------------------------------------- + nonallocating array copy + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-copy.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-copy.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-copy.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-copy.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-copy.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-copy.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-double-throw.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-double-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-double-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-double-throw.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-double-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-double-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.1.txt new file mode 100644 index 0000000000..20b71bf89d --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +nested array traversal diff --git a/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.matches.txt new file mode 100644 index 0000000000..1495930caa --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested-shape.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "nested array traversal": + <----------------------------------------------------------------------------- + nested array traversal + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.1.txt new file mode 100644 index 0000000000..a14a44dfce --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c +nested array traversal diff --git a/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.matches.txt b/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.matches.txt new file mode 100644 index 0000000000..1495930caa --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested-shape.rto/default.2.matches.txt @@ -0,0 +1,4 @@ +Matches for "nested array traversal": + <----------------------------------------------------------------------------- + nested array traversal + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-nested.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-nested.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-nested.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-nested.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-nested.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-nested.rto/default.2.1.txt new file mode 100644 index 0000000000..bfedef0468 --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-nested.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-nested.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-nested.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-runtime-cleanup.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-throw.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-throw.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-vla.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-vla.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-vla.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-vla.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.1.txt new file mode 100644 index 0000000000..a1b4f7e202 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.1.txt new file mode 100644 index 0000000000..bfedef0468 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero-runtime.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-zero.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-zero.rto/default.1.1.txt new file mode 100644 index 0000000000..9fde227c7e --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero.rto/default.1.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=1 --c++11 --exceptions Test_name.c +no allocation helper diff --git a/tests/tests/regressions/.array-construction-zero.rto/default.1.matches.txt b/tests/tests/regressions/.array-construction-zero.rto/default.1.matches.txt new file mode 100644 index 0000000000..a3cf367fc8 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero.rto/default.1.matches.txt @@ -0,0 +1,4 @@ +Matches for "no allocation helper": + <----------------------------------------------------------------------------- + no allocation helper + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/.array-construction-zero.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-zero.rto/default.2.1.txt new file mode 100644 index 0000000000..9d04910c83 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero.rto/default.2.1.txt @@ -0,0 +1,2 @@ +fe_only -DTEST_NUMBER=2 --c++11 --no_exceptions Test_name.c +no allocation helper diff --git a/tests/tests/regressions/.array-construction-zero.rto/default.2.matches.txt b/tests/tests/regressions/.array-construction-zero.rto/default.2.matches.txt new file mode 100644 index 0000000000..a3cf367fc8 --- /dev/null +++ b/tests/tests/regressions/.array-construction-zero.rto/default.2.matches.txt @@ -0,0 +1,4 @@ +Matches for "no allocation helper": + <----------------------------------------------------------------------------- + no allocation helper + -----------------------------------------------------------------------------> diff --git a/tests/tests/regressions/array-construction-cleanup.sft.cpp b/tests/tests/regressions/array-construction-cleanup.sft.cpp index fe7de15eb0..f58e4002d8 100644 --- a/tests/tests/regressions/array-construction-cleanup.sft.cpp +++ b/tests/tests/regressions/array-construction-cleanup.sft.cpp @@ -1,7 +1,7 @@ //type:fp //options_all:--c++11 --exceptions //require:BACK_END_IS_C_GEN_BE 1 -//filter:cat; grep -Eq '__vec_new_eh|__cxa_vec_ctor' Test_name.int.c && echo 'exception cleanup helper retained' +//filter:cat; grep -Eq '__edg_vec_ctor|__cxa_vec_ctor' Test_name.int.c && ! grep -Eq '__vec_new_eh|__cxa_vec_new' Test_name.int.c && echo 'exception cleanup helper retained' //match_regex:exception cleanup helper retained struct Element { diff --git a/tests/tests/regressions/array-construction-copy-shape.sft.cpp b/tests/tests/regressions/array-construction-copy-shape.sft.cpp new file mode 100644 index 0000000000..753c006191 --- /dev/null +++ b/tests/tests/regressions/array-construction-copy-shape.sft.cpp @@ -0,0 +1,17 @@ +//type:fp +//options_all:--c++11 --exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; grep -Eq '__edg_vec_cctor|__cxa_vec_cctor' Test_name.int.c && ! grep -Eq '__vec_cctor_eh|__cxa_vec_new' Test_name.int.c && echo 'nonallocating array copy' +//match_regex:nonallocating array copy + +struct Element { + Element(); + Element(const Element &); + ~Element(); +}; + +struct Holder { + Element elements[3]; +}; + +Holder copy(const Holder &source) { return source; } diff --git a/tests/tests/regressions/array-construction-copy.sft.cpp b/tests/tests/regressions/array-construction-copy.sft.cpp new file mode 100644 index 0000000000..8415c0ba21 --- /dev/null +++ b/tests/tests/regressions/array-construction-copy.sft.cpp @@ -0,0 +1,45 @@ +//type:rp +//options_all:--c++11 --exceptions + +int sequence; +int copied; +int throw_at; +int destroyed; +int order[8]; + +struct Element { + int value; + Element() : value(++sequence) {} + Element(const Element &source) : value(source.value) { + if (++copied == throw_at) throw 45; + } + ~Element() { order[destroyed++] = value; } +}; + +struct Holder { + Element elements[3]; +}; + +int main() { + Holder source; + throw_at = 3; + try { + Holder copy(source); + return 1; + } catch (int value) { + if (value != 45 || copied != 3 || destroyed != 2 || + order[0] != 2 || order[1] != 1) + return 2; + } + copied = 0; + throw_at = 0; + { + Holder copy(source); + if (copy.elements[0].value != 1 || copy.elements[1].value != 2 || + copy.elements[2].value != 3 || copied != 3) + return 3; + } + if (destroyed != 5 || order[2] != 3 || order[3] != 2 || order[4] != 1) + return 4; + return 0; +} diff --git a/tests/tests/regressions/array-construction-double-throw.sft.cpp b/tests/tests/regressions/array-construction-double-throw.sft.cpp new file mode 100644 index 0000000000..3315c627af --- /dev/null +++ b/tests/tests/regressions/array-construction-double-throw.sft.cpp @@ -0,0 +1,40 @@ +//type:rp +//options_all:--c++11 --exceptions + +#include + +extern "C" void _Exit(int); + +int constructed; +int destroyed; +int last_destroyed; + +void terminate_handler() { + _Exit(constructed == 3 && destroyed == 1 && last_destroyed == 2 ? 0 : 3); +} + +struct Element { + int id; + Element() : id(++constructed) { + if (id == 3) throw 1; + } + ~Element() noexcept(false) { + ++destroyed; + last_destroyed = id; + if (id == 2) throw 2; + } +}; + +struct Holder { + Element elements[4]; +}; + +int main() { + std::set_terminate(terminate_handler); + try { + Holder holder; + } catch (...) { + return 1; + } + return 2; +} diff --git a/tests/tests/regressions/array-construction-nested-shape.sft.cpp b/tests/tests/regressions/array-construction-nested-shape.sft.cpp new file mode 100644 index 0000000000..e6a17df01f --- /dev/null +++ b/tests/tests/regressions/array-construction-nested-shape.sft.cpp @@ -0,0 +1,15 @@ +//type:fp +//options_all:--c++11 +//options:--exceptions:--no_exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; grep -Eq 'struct Element \(\*\)\[3\]\[4\]' Test_name.int.c && grep -Eq 'struct Element \(\*\)\[4\]' Test_name.int.c && echo 'nested array traversal' +//match_regex:nested array traversal + +struct Element { + int value; + Element() : value(1) {} +}; + +void construct() { + Element elements[2][3][4]; +} diff --git a/tests/tests/regressions/array-construction-nested.sft.cpp b/tests/tests/regressions/array-construction-nested.sft.cpp new file mode 100644 index 0000000000..fa389a1807 --- /dev/null +++ b/tests/tests/regressions/array-construction-nested.sft.cpp @@ -0,0 +1,29 @@ +//type:rp +//options_all:--c++11 +//options:--exceptions:--no_exceptions + +int count; + +struct Element { + int value; + Element(int n = ++count) : value(n) {} +}; + +int main() { + Element elements[2][3][4]; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != 3; ++j) { + for (int k = 0; k != 4; ++k) { + if (elements[i][j][k].value != 1 + i * 12 + j * 4 + k) + return 1; + } + } + } + Element partial[2][3] = { { Element(90) }, { Element(91) } }; + if (partial[0][0].value != 90 || partial[0][1].value != 25 || + partial[0][2].value != 26 || partial[1][0].value != 91 || + partial[1][1].value != 27 || partial[1][2].value != 28 || + count != 28) + return 2; + return 0; +} diff --git a/tests/tests/regressions/array-construction-runtime-cleanup.sft.cpp b/tests/tests/regressions/array-construction-runtime-cleanup.sft.cpp new file mode 100644 index 0000000000..80ecc2d66f --- /dev/null +++ b/tests/tests/regressions/array-construction-runtime-cleanup.sft.cpp @@ -0,0 +1,108 @@ +//type:rp +//options_all:--c++11 --exceptions + +typedef decltype(sizeof(0)) Size; + +int constructed; +int destroyed; +int order[8]; +int deallocated; +alignas(long double) unsigned char storage[1024]; + +void *operator new[](Size) { return storage; } +void operator delete[](void *p) noexcept { + if (p == storage) ++deallocated; +} + +struct Dynamic { + int id; + static void *operator new[](Size) { return storage; } + static void operator delete[](void *p) { + if (p == storage) ++deallocated; + } + Dynamic() : id(++constructed) { + if (id == 3) throw 41; + } + ~Dynamic() { order[destroyed++] = id; } +}; + +struct ThrowingDestructor { + int id; + ThrowingDestructor() : id(++constructed) {} + ~ThrowingDestructor() noexcept(false) { + order[destroyed++] = id; + if (id == 3) throw 42; + } +}; + +struct DynamicDelete { + int id; + static void *operator new[](Size) { return storage; } + static void operator delete[](void *p) { + if (p == storage) ++deallocated; + } + DynamicDelete() : id(++constructed) {} + ~DynamicDelete() noexcept(false) { + order[destroyed++] = id; + if (id == 3) throw 43; + } +}; + +struct DefaultDynamicDelete { + int id; + DefaultDynamicDelete() : id(++constructed) {} + ~DefaultDynamicDelete() noexcept(false) { + order[destroyed++] = id; + if (id == 3) throw 44; + } +}; + +int main() { + try { + (void)new Dynamic[4]; + return 1; + } catch (int value) { + if (value != 41) return 21; + if (constructed != 3) return 22; + if (destroyed != 2) return 23; + if (order[0] != 2 || order[1] != 1) return 24; + if (deallocated != 1) return 25; + } + constructed = 0; + destroyed = 0; + try { + ThrowingDestructor elements[4]; + } catch (int value) { + if (value != 42 || constructed != 4 || destroyed != 4 || + order[0] != 4 || order[1] != 3 || order[2] != 2 || order[3] != 1) + return 3; + } + if (destroyed != 4) return 4; + constructed = 0; + destroyed = 0; + deallocated = 0; + try { + DynamicDelete *elements = new DynamicDelete[4]; + delete[] elements; + return 5; + } catch (int value) { + if (value != 43 || constructed != 4 || destroyed != 4 || + order[0] != 4 || order[1] != 3 || order[2] != 2 || order[3] != 1 || + deallocated != 1) + return 6; + } + constructed = 0; + destroyed = 0; + deallocated = 0; + try { + DefaultDynamicDelete *elements = new DefaultDynamicDelete[4]; + delete[] elements; + return 7; + } catch (int value) { + if (value != 44 || constructed != 4 || destroyed != 4 || + order[0] != 4 || order[1] != 3 || order[2] != 2 || order[3] != 1 || + deallocated != 1) + return 8; + } + return 0; +} diff --git a/tests/tests/regressions/array-construction-throw.sft.cpp b/tests/tests/regressions/array-construction-throw.sft.cpp new file mode 100644 index 0000000000..c19badb2b1 --- /dev/null +++ b/tests/tests/regressions/array-construction-throw.sft.cpp @@ -0,0 +1,83 @@ +//type:rp +//options_all:--c++11 --exceptions + +int calls; +int destroyed; +int order[4]; + +struct Guard { + ~Guard() { ++destroyed; } +}; + +struct TrivialDestructor { + TrivialDestructor() { + if (++calls == 3) + throw 42; + } +}; + +struct NontrivialDestructor { + int id; + NontrivialDestructor() : id(++calls) { + if (id == 3) + throw 43; + } + ~NontrivialDestructor() { order[destroyed++] = id; } +}; + +struct Holder { + NontrivialDestructor elements[4]; + Holder(); +}; + +Holder::Holder() {} + +struct ValueMember { + ValueMember() { + if (++calls == 3) + throw 44; + } +}; + +struct ValueElement { + int value; + ValueMember member; + ValueElement() = default; + ~ValueElement() { ++destroyed; } +}; + +struct ValueHolder { + ValueElement elements[4]; + ValueHolder() : elements{} {} +}; + +int main() { + try { + Guard guard; + TrivialDestructor elements[4]; + return 1; + } catch (int value) { + if (value != 42 || calls != 3 || destroyed != 1) + return 2; + } + calls = 0; + destroyed = 0; + try { + Holder holder; + return 3; + } catch (int value) { + if (value != 43 || calls != 3 || destroyed != 2 || + order[0] != 2 || order[1] != 1) + return 4; + } + calls = 0; + destroyed = 0; + try { + ValueHolder holder; + return 5; + } catch (int value) { + if (value != 44 || calls != 3 || destroyed != 2) + return 6; + } + return 0; +} diff --git a/tests/tests/regressions/array-construction-vla.sft.cpp b/tests/tests/regressions/array-construction-vla.sft.cpp new file mode 100644 index 0000000000..dc88f5811b --- /dev/null +++ b/tests/tests/regressions/array-construction-vla.sft.cpp @@ -0,0 +1,23 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +int count; + +struct Element { + int value; + Element() : value(++count) {} +}; + +int check(int rows) { + Element elements[rows][3]; + for (int i = 0; i != rows; ++i) { + for (int j = 0; j != 3; ++j) { + if (elements[i][j].value != i * 3 + j + 1) + return 1; + } + } + return count == rows * 3 ? 0 : 2; +} + +int main() { return check(2); } diff --git a/tests/tests/regressions/array-construction-zero-runtime.sft.cpp b/tests/tests/regressions/array-construction-zero-runtime.sft.cpp new file mode 100644 index 0000000000..c43f4b9609 --- /dev/null +++ b/tests/tests/regressions/array-construction-zero-runtime.sft.cpp @@ -0,0 +1,45 @@ +//type:rp +//options_all:--c++11 +//options:--exceptions:--no_exceptions + +typedef decltype(sizeof(0)) Size; + +void *operator new(Size, void *p) { return p; } +void operator delete(void *, void *) {} + +int destructed; + +struct Member { + int value; + Member() : value(42) {} +}; + +struct Element { + int value; + int Member::*member_pointer; + Member member; + Element() = default; + ~Element() { ++destructed; } +}; + +struct Container { + Element elements[2][3]; + Container() : elements{} {} +}; + +int main() { + alignas(Container) unsigned char storage[sizeof(Container)]; + for (Size i = 0; i != sizeof(storage); ++i) + storage[i] = 0x5a; + Container *container = new (storage) Container; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != 3; ++j) { + Element &element = container->elements[i][j]; + if (element.value != 0 || element.member_pointer != 0 || + element.member.value != 42) + return 1; + } + } + container->~Container(); + return destructed == 6 ? 0 : 2; +} diff --git a/tests/tests/regressions/array-construction-zero.sft.cpp b/tests/tests/regressions/array-construction-zero.sft.cpp new file mode 100644 index 0000000000..a9bfffc938 --- /dev/null +++ b/tests/tests/regressions/array-construction-zero.sft.cpp @@ -0,0 +1,24 @@ +//type:fp +//options_all:--c++11 +//options:--exceptions:--no_exceptions +//require:BACK_END_IS_C_GEN_BE 1 +//filter:cat; grep -Eq '__array_new|__vec_new|__cxa_vec_(new|ctor)' Test_name.int.c; [ $? = 1 ] && echo 'no allocation helper' +//match_regex:no allocation helper + +struct Member { + int value; + Member() : value(42) {} +}; + +struct Element { + int value; + Member member; + Element() = default; +}; + +struct Container { + Element elements[2]; + Container() : elements{} {} +}; + +Container make() { return Container(); } From fee5728617ba1a088ff4196825cb18c6e2487f2a Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:08 +0000 Subject: [PATCH 08/14] Document array lowering limitations [GH #561] Document the remaining traversal limitation. Clarify that exception handling can still allocate memory. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- doc/source/lower_il.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/doc/source/lower_il.rst b/doc/source/lower_il.rst index d385a6c626..bf11695e4f 100644 --- a/doc/source/lower_il.rst +++ b/doc/source/lower_il.rst @@ -2004,6 +2004,14 @@ allocation-capable vector runtime. Newly generated calls require a matching runtime library; the existing vector entry points remain available for previously generated code and dynamic allocation. +The runtime traverses preallocated arrays using byte offsets from the supplied +address. For nested arrays, strict ISO C pointer-bounds portability of this +traversal has not been established. + +Separating array construction from the allocation-capable vector runtime does +not make exception handling allocation-free: the exception runtime can still +allocate storage for thrown objects and its own bookkeeping. + For the IA-64 ABI, the standard runtime routines for these functions are: | ``__cxa_vec_ctor(``\ *array, num_elems, elem_size, ctor_routine,* From 83c1505eff38a09a4b557bae2c0e268c02f10788 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:10 +0000 Subject: [PATCH 09/14] Keep array cleanup in the allocating runtime [GH #561] Move __cleanup_vec_new_or_delete back to vec_newdel.c so it can call free_array directly, and drop the free_array function pointer. The exception runtime calls it, so programs that use exception handling link the allocating vector runtime; programs that are compiled without exception handling and do not use array new still do not. Document this. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- doc/source/lower_il.rst | 13 ++++++----- lib_src/throw.c | 9 ++++---- lib_src/vec_newdel.c | 44 +++++++++++++++++++++++++++++++++++--- lib_src/vec_newdel.h | 3 --- lib_src/vec_preallocated.c | 42 ------------------------------------ 5 files changed, 53 insertions(+), 58 deletions(-) diff --git a/doc/source/lower_il.rst b/doc/source/lower_il.rst index bf11695e4f..7150cabe46 100644 --- a/doc/source/lower_il.rst +++ b/doc/source/lower_il.rst @@ -1998,11 +1998,14 @@ nonzero. The loops zero each element first when value-initialization requires it. When construction needs exception cleanup, or for other construction cases handled by the runtime, the cfront-like ABI calls ``__edg_vec_ctor`` for preallocated storage. -Array destruction uses ``__edg_vec_dtor``. These entry points and the -exception unwinder's array cleanup are implemented separately from the -allocation-capable vector runtime. Newly generated calls require a matching -runtime library; the existing vector entry points remain available for -previously generated code and dynamic allocation. +Array destruction uses ``__edg_vec_dtor``. These entry points are +implemented separately from the allocation-capable vector runtime, so a +program that is compiled without exception handling and does not use array +new does not link that runtime. With exception handling, the unwinder's +array cleanup (``__cleanup_vec_new_or_delete``) is part of the allocating +vector runtime, which is then always linked. Newly generated calls require a +matching runtime library; the existing vector entry points remain available +for previously generated code and dynamic allocation. The runtime traverses preallocated arrays using byte offsets from the supplied address. For nested arrays, strict ISO C pointer-bounds portability of this diff --git a/lib_src/throw.c b/lib_src/throw.c index c4c8cd58df..fedf8c296f 100644 --- a/lib_src/throw.c +++ b/lib_src/throw.c @@ -831,13 +831,12 @@ requires cleanup. element_addr = (a_sizeof_t*)(obj_addr_array[(ehrdp + 1)->handle]); elements = (an_element_count)*element_addr; } /* if */ -#ifndef __EDG_IA64_ABI + /* Use the EDG-specific name in both ABIs: it cannot be resolved + by another runtime (e.g., libstdc++) and therefore links in + this library's preallocated-array routines whenever exception + handling is used. */ __edg_vec_dtor(obj_addr, (size_t)elements, ehasp->element_size, dtor_ptr); -#else /* ifdef __EDG_IA64_ABI */ - ABI_NAMESPACE::__cxa_vec_dtor(obj_addr, elements, - ehasp->element_size, dtor_ptr); -#endif /* ifdef __EDG_IA64_ABI */ } /* if */ } else if (has_vtbl_ptr) { /* A non-array object for which a special destructor must be called diff --git a/lib_src/vec_newdel.c b/lib_src/vec_newdel.c index bd465626c7..f1a94652e0 100644 --- a/lib_src/vec_newdel.c +++ b/lib_src/vec_newdel.c @@ -88,7 +88,6 @@ operation that is in process. aaehip->destructor = NULL; aaehip->delete_routine = NULL; aaehip->is_two_arg = FALSE; - aaehip->free_array = NULL; } /* add_vec_new_or_delete_eh_stack_entry */ #endif /* EXCEPTION_HANDLING */ @@ -480,7 +479,6 @@ use. if (create_eh_stack_entry) { add_vec_new_or_delete_eh_stack_entry(&ehse, &aaehi, /*is_vec_new=*/TRUE); aaehi.free_memory_on_cleanup = free_memory_on_cleanup; - aaehi.free_array = free_array; aaehi.number_of_elements = number_of_elements; aaehi.element_size = element_size; aaehi.prefix_size = prefix_size; @@ -812,7 +810,6 @@ elements will be destroyed and the exception will be rethrown. add_vec_new_or_delete_eh_stack_entry(&ehse, &aaehi, /*is_vec_new=*/FALSE); aaehi.free_memory_on_cleanup = delete_flag; - aaehi.free_array = free_array; aaehi.array_ptr = array_ptr; aaehi.number_of_elements = number_of_elements; aaehi.element_size = element_size; @@ -1001,6 +998,47 @@ some reason (i.e., overflow, less than zero, too small for initializer list). #endif /* defined(__EDG_IA64_ABI) */ +#if EXCEPTION_HANDLING +EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep) +/* +Use ehsep to clean up an interrupted array construction or destruction. +This is kept with the allocating vector routines so that it links +vec_newdel.o, and hence these definitions of the allocating vector entry +points, into every program that uses exception handling. +*/ +{ + an_array_alloc_eh_info_ptr info = ehsep->variant.array_alloc_eh_info; + size_t count; + size_t i; + +#ifdef __EDG_IA64_ABI + if (info->terminate_immediately) { + __call_terminate(); + } /* if */ +#endif /* defined(__EDG_IA64_ABI) */ + if (info->is_vec_new) { + count = info->elements_processed; + } else { + count = info->number_of_elements - info->elements_processed; + } /* if */ + if (info->destructor != NULL) { + for (i = count; i != 0; --i) { + void *element = (char *)info->array_ptr + (i - 1) * info->element_size; +#ifndef __EDG_IA64_ABI + (*info->destructor)(element, 0x2); +#else /* defined(__EDG_IA64_ABI) */ + (*info->destructor)(element); +#endif /* defined(__EDG_IA64_ABI) */ + } /* for */ + } /* if */ + if (info->free_memory_on_cleanup) { + free_array(info->array_ptr, + info->element_size * info->number_of_elements, + info->prefix_size, info->delete_routine, info->is_two_arg); + } /* if */ +} /* __cleanup_vec_new_or_delete */ +#endif /* EXCEPTION_HANDLING */ + #ifndef __EDG_IA64_ABI EXTERN_C void _array_pointer_not_from_vec_new() /* diff --git a/lib_src/vec_newdel.h b/lib_src/vec_newdel.h index ddad8fa5d6..163c89bd95 100644 --- a/lib_src/vec_newdel.h +++ b/lib_src/vec_newdel.h @@ -63,9 +63,6 @@ struct an_array_alloc_eh_info { a_delete_ptr delete_routine; /* Deallocation routine, or NULL for the default. */ a_boolean is_two_arg; /* TRUE for a two-argument deallocation routine. */ - void (*free_array)(void *, size_t, size_t, a_delete_ptr, int); - /* Array deallocator supplied by the allocating - runtime, or NULL for preallocated arrays. */ #ifdef __EDG_IA64_ABI a_boolean terminate_immediately; /* TRUE if a destructor throw requires termination. */ diff --git a/lib_src/vec_preallocated.c b/lib_src/vec_preallocated.c index 32b9945858..887761d7d5 100644 --- a/lib_src/vec_preallocated.c +++ b/lib_src/vec_preallocated.c @@ -41,52 +41,10 @@ than destruction. info->destructor = dtor; info->delete_routine = NULL; info->is_two_arg = FALSE; - info->free_array = NULL; #ifdef __EDG_IA64_ABI info->terminate_immediately = FALSE; #endif /* defined(__EDG_IA64_ABI) */ } /* add_preallocated_eh_entry */ - - -EXTERN_C void __cleanup_vec_new_or_delete(an_eh_stack_entry_ptr ehsep) -/* -Use ehsep to clean up an interrupted array construction or destruction. -Allocated arrays carry their deallocator in the stack entry; preallocated -arrays do not. -*/ -{ - an_array_alloc_eh_info_ptr info = ehsep->variant.array_alloc_eh_info; - size_t count; - size_t i; - -#ifdef __EDG_IA64_ABI - if (info->terminate_immediately) { - __call_terminate(); - } /* if */ -#endif /* defined(__EDG_IA64_ABI) */ - if (info->is_vec_new) { - count = info->elements_processed; - } else { - count = info->number_of_elements - info->elements_processed; - } /* if */ - if (info->destructor != NULL) { - for (i = count; i != 0; --i) { - void *element = (char *)info->array_ptr + (i - 1) * info->element_size; -#ifndef __EDG_IA64_ABI - (*info->destructor)(element, 0x2); -#else /* defined(__EDG_IA64_ABI) */ - (*info->destructor)(element); -#endif /* defined(__EDG_IA64_ABI) */ - } /* for */ - } /* if */ - if (info->free_memory_on_cleanup) { - check_assertion(info->free_array != NULL); - (*info->free_array)(info->array_ptr, - info->element_size * info->number_of_elements, - info->prefix_size, info->delete_routine, - info->is_two_arg); - } /* if */ -} /* __cleanup_vec_new_or_delete */ #endif /* EXCEPTION_HANDLING */ From c7ce41b4223ebf937b42595ce9114c650f402134 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:09:12 +0000 Subject: [PATCH 10/14] Fix cv-qualified and empty-class array construction [GH #561] Traverse array elements through unqualified pointers so constructors can be called on cv-qualified arrays. For multidimensional arrays of empty classes, traverse the innermost elements, because the generated C may give such rows a size of zero. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 645763e38b..35b71c6a4c 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -5910,7 +5910,11 @@ within the bounds of each array. a_statement_ptr loop_stmt; an_expr_node_ptr args, call, loop_expr, increment_expr; - pointer_type = make_pointer_type(skip_typerefs(element_type)); + /* Constructors are called on cv-qualified objects through an unqualified + "this" pointer, so traverse the elements with unqualified pointers. */ + element_type = make_unqualified_type(skip_typerefs(element_type), + /*unqualify_array_elements=*/TRUE); + pointer_type = make_pointer_type(element_type); count_type = integer_type(targ_size_t_int_kind); helper_routine = make_rout_entry((char *)NULL, sc_static, void_type(), pointer_type); @@ -6038,10 +6042,21 @@ must NOT already be lowered (see comment in default_version_of_routine). num_elem_node = num_elem_node_if_array(ipdp); check_assertion(num_elem_node != NULL); + if (is_array_type(element_type) && + skip_typerefs(underlying_array_element_type(element_type))->size <= + 1) { + /* The generated C may represent an empty class as a zero-sized + struct, which makes arithmetic on pointers to its subarrays + advance by zero bytes. Traverse the innermost elements + instead. */ + element_type = underlying_array_element_type(element_type); + } /* if */ helper_routine = array_construction_loop(element_type, ctor_routine, zero_storage); - entity_node = add_cast_if_necessary(entity_node, - make_pointer_type(element_type)); + entity_node = add_cast_if_necessary( + entity_node, + helper_routine->type->variant.routine.extra_info-> + param_type_list->type); if (is_array_type(element_type)) { num_elem_node = add_cast_if_necessary( num_elem_node, From 4a869f36a0a09f79e304cabeac033fea6f40ab28 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Thu, 19 Nov 2026 09:21:37 +0000 Subject: [PATCH 11/14] Construct VLA element sequences without allocation [GH #561] The fix for the initialization of VLAs (#557, #558, #559) constructs a sequence of array elements initialized with an aggregate by calling make_vec_new_call. The array construction changes add a fixed_array parameter to that routine; pass TRUE, as the storage is that of the VLA being initialized and is not dynamically allocated. --- src/lower_init.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lower_init.c b/src/lower_init.c index 35b71c6a4c..975e2a3a25 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -11304,7 +11304,8 @@ do_assignment:; num_elem_node, helper, dtor_routine, (a_routine_ptr)NULL, (a_routine_ptr)NULL, - /*zero_storage=*/FALSE), + /*zero_storage=*/FALSE, + /*fixed_array=*/TRUE), eff_insert_location); break; } /* if */ From 799b50c7ce3d19f37d576b6ba13c089ed631a8e8 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Thu, 19 Nov 2026 09:21:37 +0000 Subject: [PATCH 12/14] Update test expectations for the array construction changes [GH #561] The new array construction tests examine the generated C and require the C-generating back end; record them as REQUIREMENTS NOT MET for the edg_x86_64_cp and edg_x86_32_cp configurations. The preallocated array construction routines make the runtime library larger, which moves static data in the 32-bit executables. Update the edg_x86_32 and edg_x86_32_cp recordings of edg/rma/init33, which print absolute object addresses. --- .../edg_x86_32_cp/regressions.log | 10 ++ .../edg_x86_64_cp/regressions.log | 10 ++ .../edg/rma/.init33.rto/edg_x86_32.2.2.txt | 144 +++++++++--------- .../edg/rma/.init33.rto/edg_x86_32_cp.2.2.txt | 144 +++++++++--------- 4 files changed, 164 insertions(+), 144 deletions(-) diff --git a/tests/expectations/edg_x86_32_cp/regressions.log b/tests/expectations/edg_x86_32_cp/regressions.log index abd2f80e25..b68cb66412 100644 --- a/tests/expectations/edg_x86_32_cp/regressions.log +++ b/tests/expectations/edg_x86_32_cp/regressions.log @@ -1,3 +1,13 @@ +array-construction-cleanup.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: +array-construction-copy-shape.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: +array-construction-dynamic.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-dynamic.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-nested-shape.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-nested-shape.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-no-allocation.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-no-allocation.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-zero.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-zero.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: gh-183-redecl.sft.cpp:fp:-DTEST_NUMBER=1 --c++17:REQUIREMENTS NOT MET: gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit diff --git a/tests/expectations/edg_x86_64_cp/regressions.log b/tests/expectations/edg_x86_64_cp/regressions.log index abd2f80e25..b68cb66412 100644 --- a/tests/expectations/edg_x86_64_cp/regressions.log +++ b/tests/expectations/edg_x86_64_cp/regressions.log @@ -1,3 +1,13 @@ +array-construction-cleanup.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: +array-construction-copy-shape.sft.cpp:fp:-DTEST_NUMBER=1:REQUIREMENTS NOT MET: +array-construction-dynamic.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-dynamic.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-nested-shape.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-nested-shape.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-no-allocation.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-no-allocation.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: +array-construction-zero.sft.cpp:fp:-DTEST_NUMBER=1 --exceptions:REQUIREMENTS NOT MET: +array-construction-zero.sft.cpp:fp:-DTEST_NUMBER=2 --no_exceptions:REQUIREMENTS NOT MET: gh-183-redecl.sft.cpp:fp:-DTEST_NUMBER=1 --c++17:REQUIREMENTS NOT MET: gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=1 --g++:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit gh-309-diag.sft.cpp:fp:-DTEST_NUMBER=2 --g++ --no_exceptions:REQUIREMENTS NOT MET:Diagnostic position of implicit destructor calls at scope exit diff --git a/tests/tests/edg/rma/.init33.rto/edg_x86_32.2.2.txt b/tests/tests/edg/rma/.init33.rto/edg_x86_32.2.2.txt index a7c610d6e8..1d412febb2 100644 --- a/tests/tests/edg/rma/.init33.rto/edg_x86_32.2.2.txt +++ b/tests/tests/edg/rma/.init33.rto/edg_x86_32.2.2.txt @@ -1,73 +1,73 @@ ./a.out -S(134574788) -S(134574816) -S(134574836) -S(134574856) -S(134574876) -S(134574896) -S(134574944,134574816) -S(134574964,134574836) -S(134574984,134574856) -S(134575004,134574876) -S(134575024,134574896) -S(134575072) j=1 -S(134575092) j=2 -S(134575112) j=3 -S(134575132) j=4 -S(134575152) j=5 -S(134575172) -S(134575192) -S(134575212) -S(134575232) -S(134575252) -S(134575272) -S(134575292) -S(134575312) -S(134575332) -S(134575352) -S(134575372) -S(134575392) -S(134575412) -S(134575432) -S(134575452) -S(134575472) -S(134575492) -S(134575512) -S(134575532) -S(134575552) -~S(134575552) -~S(134575532) -~S(134575512) -~S(134575492) -~S(134575472) -~S(134575452) -~S(134575432) -~S(134575412) -~S(134575392) -~S(134575372) -~S(134575352) -~S(134575332) -~S(134575312) -~S(134575292) -~S(134575272) -~S(134575252) -~S(134575232) -~S(134575212) -~S(134575192) -~S(134575172) -~S(134575152) -~S(134575132) -~S(134575112) -~S(134575092) -~S(134575072) -~S(134575024) -~S(134575004) -~S(134574984) -~S(134574964) -~S(134574944) -~S(134574896) -~S(134574876) -~S(134574856) -~S(134574836) -~S(134574816) -~S(134574788) +S(134578884) +S(134578912) +S(134578932) +S(134578952) +S(134578972) +S(134578992) +S(134579040,134578912) +S(134579060,134578932) +S(134579080,134578952) +S(134579100,134578972) +S(134579120,134578992) +S(134579168) j=1 +S(134579188) j=2 +S(134579208) j=3 +S(134579228) j=4 +S(134579248) j=5 +S(134579268) +S(134579288) +S(134579308) +S(134579328) +S(134579348) +S(134579368) +S(134579388) +S(134579408) +S(134579428) +S(134579448) +S(134579468) +S(134579488) +S(134579508) +S(134579528) +S(134579548) +S(134579568) +S(134579588) +S(134579608) +S(134579628) +S(134579648) +~S(134579648) +~S(134579628) +~S(134579608) +~S(134579588) +~S(134579568) +~S(134579548) +~S(134579528) +~S(134579508) +~S(134579488) +~S(134579468) +~S(134579448) +~S(134579428) +~S(134579408) +~S(134579388) +~S(134579368) +~S(134579348) +~S(134579328) +~S(134579308) +~S(134579288) +~S(134579268) +~S(134579248) +~S(134579228) +~S(134579208) +~S(134579188) +~S(134579168) +~S(134579120) +~S(134579100) +~S(134579080) +~S(134579060) +~S(134579040) +~S(134578992) +~S(134578972) +~S(134578952) +~S(134578932) +~S(134578912) +~S(134578884) diff --git a/tests/tests/edg/rma/.init33.rto/edg_x86_32_cp.2.2.txt b/tests/tests/edg/rma/.init33.rto/edg_x86_32_cp.2.2.txt index a7c610d6e8..1d412febb2 100644 --- a/tests/tests/edg/rma/.init33.rto/edg_x86_32_cp.2.2.txt +++ b/tests/tests/edg/rma/.init33.rto/edg_x86_32_cp.2.2.txt @@ -1,73 +1,73 @@ ./a.out -S(134574788) -S(134574816) -S(134574836) -S(134574856) -S(134574876) -S(134574896) -S(134574944,134574816) -S(134574964,134574836) -S(134574984,134574856) -S(134575004,134574876) -S(134575024,134574896) -S(134575072) j=1 -S(134575092) j=2 -S(134575112) j=3 -S(134575132) j=4 -S(134575152) j=5 -S(134575172) -S(134575192) -S(134575212) -S(134575232) -S(134575252) -S(134575272) -S(134575292) -S(134575312) -S(134575332) -S(134575352) -S(134575372) -S(134575392) -S(134575412) -S(134575432) -S(134575452) -S(134575472) -S(134575492) -S(134575512) -S(134575532) -S(134575552) -~S(134575552) -~S(134575532) -~S(134575512) -~S(134575492) -~S(134575472) -~S(134575452) -~S(134575432) -~S(134575412) -~S(134575392) -~S(134575372) -~S(134575352) -~S(134575332) -~S(134575312) -~S(134575292) -~S(134575272) -~S(134575252) -~S(134575232) -~S(134575212) -~S(134575192) -~S(134575172) -~S(134575152) -~S(134575132) -~S(134575112) -~S(134575092) -~S(134575072) -~S(134575024) -~S(134575004) -~S(134574984) -~S(134574964) -~S(134574944) -~S(134574896) -~S(134574876) -~S(134574856) -~S(134574836) -~S(134574816) -~S(134574788) +S(134578884) +S(134578912) +S(134578932) +S(134578952) +S(134578972) +S(134578992) +S(134579040,134578912) +S(134579060,134578932) +S(134579080,134578952) +S(134579100,134578972) +S(134579120,134578992) +S(134579168) j=1 +S(134579188) j=2 +S(134579208) j=3 +S(134579228) j=4 +S(134579248) j=5 +S(134579268) +S(134579288) +S(134579308) +S(134579328) +S(134579348) +S(134579368) +S(134579388) +S(134579408) +S(134579428) +S(134579448) +S(134579468) +S(134579488) +S(134579508) +S(134579528) +S(134579548) +S(134579568) +S(134579588) +S(134579608) +S(134579628) +S(134579648) +~S(134579648) +~S(134579628) +~S(134579608) +~S(134579588) +~S(134579568) +~S(134579548) +~S(134579528) +~S(134579508) +~S(134579488) +~S(134579468) +~S(134579448) +~S(134579428) +~S(134579408) +~S(134579388) +~S(134579368) +~S(134579348) +~S(134579328) +~S(134579308) +~S(134579288) +~S(134579268) +~S(134579248) +~S(134579228) +~S(134579208) +~S(134579188) +~S(134579168) +~S(134579120) +~S(134579100) +~S(134579080) +~S(134579060) +~S(134579040) +~S(134578992) +~S(134578972) +~S(134578952) +~S(134578932) +~S(134578912) +~S(134578884) From 564e1584694a67549a30559f0f774078ce98b27e Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:44:28 +0000 Subject: [PATCH 13/14] Use kind-test macros instead of enumerator casts [GH #561] Apply the review cleanup of the VLA initialization fix (GH #557) to this branch: test dynamic initialization and constant kinds with dyn_init_is and constant_is, drop the remaining redundant enumerator casts, and rewrap the calls that now fit on fewer lines. No functional change. --- src/lower_init.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 975e2a3a25..af570f79d9 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -6702,8 +6702,7 @@ to lower_dynamic_init_aggregate_constant; see their description there. /* Add the return statement at the end of the routine to the return memo list, so that any exception handling epilogue is added before it. */ return_stmt = scope->assoc_block->variant.block.statements; - check_assertion(return_stmt != NULL && - return_stmt->kind == (a_statement_kind)stmk_return); + check_assertion(return_stmt != NULL && return_stmt->kind == stmk_return); add_to_return_memo_list(return_stmt); /* Create the parameters. */ entity_var = make_lowered_param_variable(rtsp->param_type_list->type); @@ -6789,7 +6788,7 @@ lower_dynamic_init_aggregate_constant; see their description there. a_boolean keep_constant = FALSE; a_boolean return_ptr; - check_assertion(dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate); + check_assertion(dyn_init_is(dip, dik_nonconstant_aggregate)); pointer_type = skip_typerefs(type); check_assertion(is_pointer_type(pointer_type)); #if IA64_ABI @@ -6809,8 +6808,7 @@ lower_dynamic_init_aggregate_constant; see their description there. /* Add the return statement at the end of the routine to the return memo list, so that any exception handling epilogue is added before it. */ return_stmt = scope->assoc_block->variant.block.statements; - check_assertion(return_stmt != NULL && - return_stmt->kind == (a_statement_kind)stmk_return); + check_assertion(return_stmt != NULL && return_stmt->kind == stmk_return); add_to_return_memo_list(return_stmt); entity_var = make_lowered_param_variable( rp->type->variant.routine.extra_info->param_type_list-> @@ -6882,13 +6880,11 @@ expression. used_node->next = node_for_host_large_integer( (a_host_large_integer)curr_elem, targ_size_t_int_kind); - used_node = make_operator_node((an_expr_operator_kind)eok_multiply, - count_type, used_node); + used_node = make_operator_node(eok_multiply, count_type, used_node); count_node = add_cast_if_necessary(ipdp->num_elem_node, count_type); count_node->next = used_node; - ipdp->num_elem_node = - make_operator_node((an_expr_operator_kind)eok_subtract, - count_type, count_node); + ipdp->num_elem_node = make_operator_node(eok_subtract, + count_type, count_node); } /* if */ ipdp->partial_initialization_starting_element = 0; } else { @@ -7260,13 +7256,12 @@ dealt with). ipmp->curr_elem, con_ptr, /*strip_nested_repeats=*/TRUE); if (exceptions_enabled && !dtor_case && - leaf_con->kind == (a_constant_repr_kind)ck_dynamic_init) { + constant_is(leaf_con, ck_dynamic_init)) { leaf_dip = leaf_con->variant.dynamic_init.ptr; init_leaf_sequence = leaf_dip->destructor != NULL && - (leaf_dip->kind == - (a_dynamic_init_kind)dik_nonconstant_aggregate || - leaf_dip->kind == (a_dynamic_init_kind)dik_constructor); + (dyn_init_is(leaf_dip, dik_nonconstant_aggregate) || + dyn_init_is(leaf_dip, dik_constructor)); } /* if */ (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, ipmp->curr_elem, con_ptr, @@ -10450,7 +10445,7 @@ individual element values at *insert_location. check_assertion(dip->is_partially_initialized && has_variable_length_rows(type_from_init_pos_descr(ipdp))); - if (con->kind == (a_constant_repr_kind)ck_aggregate && + if (constant_is(con, ck_aggregate) && con->variant.aggregate.first_constant != NULL) { ipd = *ipdp; ipd.assign_constants = TRUE; @@ -11280,8 +11275,7 @@ do_assignment:; if (exceptions_enabled && dip->destructor != NULL) { dtor_routine = dip->destructor; #if IA64_ABI - dtor_routine = alternate_entry_point(dtor_routine, - (a_ctor_or_dtor_kind)cdk_complete, + dtor_routine = alternate_entry_point(dtor_routine, cdk_complete, /*define_now=*/FALSE); #endif /* IA64_ABI */ } /* if */ From 6b5e8dfb53eaa74d3edd88d59c4b8f0c07ece54e Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 17:50:26 +0000 Subject: [PATCH 14/14] Destroy single-element array rows when a later element throws [GH #561] When a member array whose rows have a single element, such as P a[3][1], was value-initialized, the repeated initializer of the row was not stripped down to its element, so the elements already constructed were not destroyed when a later constructor threw. Treat a single-element array row like a nested repeat when finding the repeated leaf. Reported by CodeRabbit. The regression test now also requires that an exception is actually thrown and covers [3][1] and [3][1][2] members. The VLA rows test also covers a default member initializer that is evaluated for each element. --- src/lower_init.c | 32 +++++++++++++++---- .../value-init-array-member-throw.sft.cpp | 12 +++++-- .../regressions/vla-rows-brace-init.sft.cpp | 18 +++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index af570f79d9..d9a0bef557 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -6895,6 +6895,17 @@ expression. } /* set_partial_initialization_start */ +static a_boolean is_single_element_array_type(a_type_ptr type) +/* +Return TRUE if type is a (fixed-size) array type with a single element. +*/ +{ + type = skip_typerefs(type); + return is_array_type(type) && !type->variant.array.is_vla && + type->variant.array.variant.number_of_elements == 1; +} /* is_single_element_array_type */ + + static a_constant_ptr set_init_pos_for_init_repeat( an_init_pos_descr_ptr ipdp, an_init_pos_descr_ptr outer_ipdp, @@ -6919,7 +6930,9 @@ then covers only the constant dimensions and the number of innermost elements is likewise known only at run time. If strip_nested_repeats is TRUE and the repeated constant is an aggregate consisting of a single repeat (as for a multidimensional array), the nested -repeats are folded into the count. Return the constant to be repeated. +repeats are folded into the count; likewise, a row of a single element +(as for "A a[3][1]") is replaced by that element. Return the constant to be +repeated. */ { a_constant_ptr repeated_con = repeat_con->variant.init_repeat.constant; @@ -6970,15 +6983,22 @@ repeats are folded into the count. Return the constant to be repeated. multi-dimensional arrays). If so, strip any applicable ck_init_repeat constants and increase the count accordingly. */ while (repeated_con->kind == ck_aggregate && - repeated_con->variant.aggregate.first_constant->kind == - ck_init_repeat && + (repeated_con->variant.aggregate.first_constant->kind == + ck_init_repeat || + is_single_element_array_type(repeated_con->type)) && repeated_con->variant.aggregate.first_constant == repeated_con->variant.aggregate.last_constant) { a_constant_ptr rcon = repeated_con->variant.aggregate.first_constant; - check_assertion(rcon->variant.init_repeat.count != 0); - ipdp->array_element_count *= + if (constant_is(rcon, ck_init_repeat)) { + check_assertion(rcon->variant.init_repeat.count != 0); + ipdp->array_element_count *= (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; - repeated_con = rcon->variant.init_repeat.constant; + repeated_con = rcon->variant.init_repeat.constant; + } else { + /* A row of a single element (e.g., of "A a[3][1]"); the count + is unchanged. */ + repeated_con = rcon; + } /* if */ ipdp->array_element_type = repeated_con->type; } /* while */ } /* if */ diff --git a/tests/tests/regressions/value-init-array-member-throw.sft.cpp b/tests/tests/regressions/value-init-array-member-throw.sft.cpp index eea6080f71..fc052c3ce9 100644 --- a/tests/tests/regressions/value-init-array-member-throw.sft.cpp +++ b/tests/tests/regressions/value-init-array-member-throw.sft.cpp @@ -26,12 +26,15 @@ struct G2 { P a[2][3]; }; struct G3 { P a[2][2][2]; }; struct N { G g[2]; }; struct GV { int k = 5; Q a[2]; P b[2][2]; }; +struct G1 { P a[3][1]; }; +struct G12 { P a[3][1][2]; }; template static void check(int n, F f) { for (throw_at = 1; throw_at <= n; ++throw_at) { + bool threw = false; ctors = dtors = live = 0; - try { f(); } catch (int) {} - if (dtors != throw_at - 1 || live != 0) ++errors; + try { f(); } catch (int) { threw = true; } + if (!threw || dtors != throw_at - 1 || live != 0) ++errors; } } @@ -44,12 +47,15 @@ int main() { check(8, [] { N x{}; }); check(8, [] { GV x{}; }); check(12, [] { G2 x[2]{}; }); + check(3, [] { G1 x{}; }); + check(6, [] { G12 x{}; }); + check(3, [] { (void)G1{}; }); check(4, [] { static G x{}; }); check(4, [] { (void)G{}; }); check(4, [] { G *p = new G{}; delete p; }); /* Success path: everything constructed is destroyed. */ throw_at = 0; live = 0; - { G2 a{}; GV b{}; G c[2]{}; N d{}; } + { G2 a{}; GV b{}; G c[2]{}; N d{}; G1 e{}; G12 f{}; } if (live != 0) ++errors; return errors != 0; } diff --git a/tests/tests/regressions/vla-rows-brace-init.sft.cpp b/tests/tests/regressions/vla-rows-brace-init.sft.cpp index 6d8371c01e..df1ed00761 100644 --- a/tests/tests/regressions/vla-rows-brace-init.sft.cpp +++ b/tests/tests/regressions/vla-rows-brace-init.sft.cpp @@ -22,6 +22,9 @@ struct W { CD c; int i = 3; }; struct DO { int v; ~DO() {} }; struct S { int a, b; }; struct Q { int S::*p; int v; }; +static int counter; +static int next_value() { return ++counter; } +struct NM { int v = next_value(); }; static void empty_rows(int n) { int a[2][n] = {}; @@ -122,6 +125,20 @@ static void class_rows(int n) { } } +static void dynamic_member_init_rows(int n) { + // The default member initializer is evaluated for each element. + counter = 0; + NM a[2][n] = {}; + check(counter, 2 * n); + NM b[2][n][2] = {}; + check(counter, 6 * n); + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].v >= 1 && a[i][j].v <= 2 * n, 1); + check(b[i][j][1].v > 2 * n, 1); + } +} + static void ptr_to_member_rows(int n) { // A null pointer to data member is not all-bits-zero in the IA-64 ABI. Q a[2][n] = {{{&S::b, 5}}}; @@ -144,6 +161,7 @@ int main() { string_rows(n); struct_rows(n); class_rows(n); + dynamic_member_init_rows(n); ptr_to_member_rows(n); } return errors != 0;