Skip to content

WIP: Construct preallocated arrays without the allocating vector runtime [GH #561] - #571

Draft
lexx42xxel wants to merge 13 commits into
edgcpp:mainfrom
lexx42xxel:feature/static-array-construction
Draft

lexx42xxel wants to merge 13 commits into
edgcpp:mainfrom
lexx42xxel:feature/static-array-construction

Conversation

@lexx42xxel

Copy link
Copy Markdown

Proposed in discussion #561; there is no feature issue yet.

Important

This branch is based on the VLA initialization fix PR #569 (#557, #558, #559,
#554), so its first 4 commits belong to that PR and drop out once it is
merged. The feature changes make_vec_new_call, which the VLA fix also
calls; commit "Construct VLA element sequences without allocation" adapts
that call. The last commit updates test expectations: the new tests need
the C-generating back end (REQUIREMENTS NOT MET on the _cp
configurations), and the larger runtime library moves the absolute
addresses printed by edg/rma/init33 on the 32-bit configurations.

Note

Work in progress. The implementation is complete and tested, but it is
opened as WIP pending agreement on the design in #561. Feedback sought on:

  • whether the approach suits the project (inline loops when no cleanup is
    needed, otherwise a new non-allocating runtime file);
  • how the new runtime entry points should be versioned or packaged,
    because generated code now needs the matching runtime library;
  • whether [GH #561] (the discussion) is acceptable as the commit tag or
    should be replaced by a feature issue number.

Problem

The target is embedded real-time code, where building without exception
handling and without dynamic memory allocation is standard practice (coding
guidelines for such code commonly prohibit both).

TASKING uses the EDG front end in its compiler toolchains for safety-critical
embedded systems, and would like to contribute this and related fixes to the
EDG project on GitHub.

In such programs, construction and destruction of arrays with static,
automatic or otherwise preallocated storage are still lowered to calls into
the vector runtime (vec_newdel.c), which also implements the allocating
new[]/delete[] support. A program that never allocates therefore still
links the allocation code.

Design

  • src/lower_init.c: when no exception cleanup is needed, the array is not
    variably sized and the nested bounds are known and nonzero, generate nested
    element construction loops, zeroing each element first if
    value-initialization requires it. Otherwise call __edg_vec_ctor.
  • lib_src/vec_preallocated.c (new): the non-allocating entry points
    (__edg_vec_ctor, __edg_vec_dtor, __edg_vec_cctor) for preallocated
    storage. vec_newdel.c keeps the allocating entry points and the
    unwinder's array cleanup __cleanup_vec_new_or_delete (so that it can
    call free_array directly). A program compiled without exception
    handling that does not use array new no longer links vec_newdel.o. With
    exception handling it still does, because the unwinder's array cleanup
    lives there; that is acceptable, since the targets that care about this
    are built without exceptions.
  • Elements are traversed through unqualified pointers (constructors on
    cv-qualified arrays). For multidimensional arrays of empty classes the
    innermost elements are traversed, because the generated C may give such
    rows size zero.
  • doc/source/lower_il.rst documents the behaviour and remaining limits:
    • exception handling itself still allocates;
    • newly generated calls need the matching runtime library (existing entry
      points remain for previously generated code).

Alternatives considered

  • Only split vec_newdel.c into separate object files. Exception
    cleanup would still reference the allocating code, so the dependency would
    remain.
  • Always generate loops, including exception cleanup. Larger generated
    code for every array with a nontrivial destructor; the runtime path is kept
    for those cases.

Compatibility and performance

  • Generated code for arrays without cleanup now uses inline loops (no runtime
    call).
  • The front end's compile time is not affected measurably; see the bench job.
  • Code generated by the new front end needs the new runtime library.

Related

Tests

tests/tests/regressions/array-construction-*.sft.cpp (14 tests:
semantics, nested/copy shapes, cleanup and double throw, zero-length, VLA,
runtime cleanup, no allocation helper referenced in the generated C).

Checklist

  • Open issue: not yet; proposed in discussion Construct static and preallocated arrays without the allocating vector runtime #561. Commits are tagged [GH #561].
  • Design and alternatives described above.
  • EDG style; comments and doc/source/lower_il.rst updated.
  • Changes entries: src/pending_changes/gh-561-static-array-construction.txt,
    lib_src/pending_changes/gh-561-static-array-construction.txt.
  • Tests fail before and pass after.
  • edg-docker-test --posture=extensive on all four configurations: no regressions; recordings updated with -W.

… [GH edgcpp#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>
…dgcpp#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>
…cpp#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>
edgcpp#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>
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>
Simplify regression filters and clarify the lowering condition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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>
Document the remaining traversal limitation.

Clarify that exception handling can still allocate memory.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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>
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>
The fix for the initialization of VLAs (edgcpp#557, edgcpp#558, edgcpp#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.
…p#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.
@coderabbitai

coderabbitai Bot commented Nov 20, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 799b50c7ce3d:

@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 799b50c7ce3d:

CI success.

Apply the review cleanup of the VLA initialization fix (GH edgcpp#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.
@edgcpp-bot

Copy link
Copy Markdown
Collaborator

AcknowlEDG review results updated for 564e1584694a:

CI success.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants