Skip to content

Comments

feat!: Refactor initialisers#1552

Open
ghaith wants to merge 8 commits intomasterfrom
init_rework
Open

feat!: Refactor initialisers#1552
ghaith wants to merge 8 commits intomasterfrom
init_rework

Conversation

@ghaith
Copy link
Collaborator

@ghaith ghaith commented Nov 25, 2025

This PR refactors the initial value generation for PLC application

  • the init functions are removed and replaced with __ctor functions.
  • initial constructors (__ctors) are created for every type and POU created in the current application
  • external units are assumed to perform their own initialization.
  • If an external type is used in a PLC application, the constructor functions are called at initialization
  • A compilation flag was added to generate only the constructors (useful for external libraries written in C)
  • A compilation flag was added to generate constructors for functions marked as {external} (used in the standard lib).
  • Constructors are now part of the same compilation unit as the types they are constructing.

Review notes:
The bulk of this change is in the plc_lowering crate under the initializer.rs module.
Changes to the codegen and resolver were needed to fix issues where an initialzer code was not working in the body of a function.

After the main feature was done, i went through all open issues that mentioned init and tried to add a testcase for the specific issue and fix it. i tagged the issues fixed in this PR.
I split the commits into the main code change, the additional changes required to the pipeline and other componets, the new tests, documentation changes, some formatting changes and the insta tests.
I kept the changes in the snapshot isolated to one commit for easier review.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the initialization system within the compiler to move from explicit global initializer functions (__init__<name> and __user_init__<name>) to constructor-based initialization using LLVM's .init_array mechanism. The change consolidates init logic into a new plc_lowering::initializer module and introduces flags (--generate-external-constructors, --constructors-only) to control constructor generation for external libraries.

Changes:

  • Replaced manual initializer generation with constructor functions (<Type>__ctor)
  • Added linkage type Include to distinguish included files from external declarations
  • Removed legacy vtable and init globals from stdlib Rust implementations
  • Updated test snapshots to reflect filtered alignment values and removed init modules

Reviewed changes

Copilot reviewed 122 out of 618 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
compiler/plc_lowering/src/initializer.rs New init lowering module implementing constructor generation
compiler/plc_driver/src/pipelines/participant.rs Refactored InitParticipant to use new initializer
compiler/plc_ast/src/ast.rs Added LinkageType::Include and helper methods
compiler/plc_llvm/ New crate with C++ wrapper to set LLVM UseInitArray option
libs/stdlib/src/timers.rs, libs/stdlib/src/flanks.rs, etc. Removed manual vtable/init globals
libs/stdlib/tests/* Updated test helpers to use includes + compile_and_load
compiler/plc_driver/src/cli.rs Added --generate-external-constructors and --constructors-only flags
Snapshot files Updated for alignment filtering and removed __initializers modules
Comments suppressed due to low confidence (1)

compiler/plc_lowering/src/initializer.rs:1

  • The comment on line 4 in compiler/plc_lowering/src/tests/initializer_tests.rs contains a typo: 'aligment' should be 'alignment'.
//! The initializer lowering module is responsible for additing initialization logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

use plc_source::SourceCode;

#[test]
fn var_input_with_initializer_parses_correctly() {
Copy link

Copilot AI Feb 19, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Corrected spelling of 'initialisers' to 'initializers' in the PR title for consistency with US English spelling used throughout the codebase.

Copilot uses AI. Check for mistakes.
move the initializer into the lowering crate
change the initialization logic so that every pou or type has a _ctor
adjust the pipeline and the codegen to work with the new initializer
logic.
Fix some issue in the codegen/resolving logic
@ghaith ghaith requested a review from Copilot February 23, 2026 10:25
@ghaith ghaith marked this pull request as ready for review February 23, 2026 10:25
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review this pull request. There are 300 or more changed files, try reducing the number of files in this pull request and requesting a review from Copilot again.

@ghaith ghaith requested review from mhasel and volsa February 23, 2026 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant