Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle mapping of record members to external C code types and layout. #9399

Merged
merged 5 commits into from
Sep 20, 2022

Conversation

mahge
Copy link
Contributor

@mahge mahge commented Sep 20, 2022

Cleanup external function handling.

  • This is done in preparation for implementing of conversion types and
    function for sending and receiving record types to external C functions
    while making sure that types are interpreted correctly
    modelica_integer (a.k.a long) vs int right now.'

Handle mapping of recrods to external function arguments.

  • Each Modelica record now gets two struct definitions. The first,
    already existing, version uses OpenModelica types for the record members.

    The new additional type uses types specified in Modelica Standard for
    mapping of external arguments, i.e., uses double, int, int, '
    const char*, and int for Real, Integer, Boolean, String, and Enumeration
    repsectively, regardless of our internal representations.

  • Each Modelica record also gets two addition functions for converting
    to/from one representation of the record to the other.

  • This is a lot of additional code generation. However, it can be mitigated
    by identifying the only records that need it and avoiding it for the others.

  • Records that contain arrays or other records are not yet supported in
    this mode.

Add mapping support for record record members.

  • Extend the record conversion functions to handle nested records.
  • We do not support arrays yet.

typedef external versions for alias records too.

Add marker for records needing conversion to external versions.

  • This marker is set to false for all record types at the moment. The
    additional code is generated for all records regardless of the value.

    Once we implement the necessary analysis, we can change the value for
    records that need it and disable the generation being done for all
    records right now.

Improves #8591.

  - This is done in preparation for implementing of conversion types and
    function for sending and receiving record types to external C functions
    while making sure that types are interpreted correctly
      modelica_integer (a.k.a long) vs int right now.'
  - Each Modelica record now gets two struct definitions. The first,
    already existing, version uses OpenModelica types for the record members.

    The new additional type uses types specified in Modelica Standard for
    mapping of external arguments, i.e., uses `double`, `int`, `int`, '
    `const char*`, and `int` for Real, Integer, Boolean, String, and Enumeration
    repsectively, regardless of our internal representations.

  - Each Modelica record also gets two addition functions for converting
    to/from one representation of the record to the other.

  - This is a lot of additional code generation. However, it can be mitigated
    by identifying the only records that need it and avoiding it for the others.

  - Records that contain arrays or other records are not yet supported in
    this mode.
  - Extend the record conversion functions to handle nested records.
  - We do not support arrays yet.
  - This marker is set to false for all record types at the moment. The
    additional code is generated for all records regardless of the value.

    Once we implement the necessary analysis we can change the value for
    records that need it and disable the generation being done for all
    records right now.
@mahge mahge added the CI/CMake/Disable/All Make Jenkins disable CMake builds on all platforms. label Sep 20, 2022
@mahge mahge self-assigned this Sep 20, 2022
@mahge
Copy link
Contributor Author

mahge commented Sep 20, 2022

@casella This should provide support for mapping of record members to external C structs. It fixes the original issue in #8591 and the MWE posted in there.

@perost is working on identifying and marking the records that need this additional handling. That should considerably reduce the amount of additional code we generate. We will do a follow up fix once that is ready.

@casella
Copy link
Contributor

casella commented Sep 20, 2022

OK, I see this passed the core testsuite, so I'd merge it and see what it does to the library testsuite, and also test it with ExternalMedia myself, since it is not yet in the library testsuite.

Then we can decide if we keep it for 1.20.0 and roll it back aftwards, or if it really doesn't work well, so we roll it back now.

@casella casella merged commit 1cd773f into OpenModelica:master Sep 20, 2022
@mahge mahge deleted the ticket_8591 branch September 21, 2022 07:46
perost added a commit to perost/OpenModelica that referenced this pull request Sep 21, 2022
- Implement marking of externally used records as needed by OpenModelica#9399.
perost added a commit that referenced this pull request Sep 21, 2022
- Implement marking of externally used records as needed by #9399.
mahge added a commit that referenced this pull request Sep 25, 2022
* Use the externally used records marker.

* Set marker to always be true for the OF.

* Use a map/dictionary instead of keeping separate lists.

  - Use a map (string, record Declaration) to keep a track of records in
    the SimCode. This allows for a simpler and quicker check. In addition
    we can update entries easily.

* Add MetaModelica records to the map as well.

* Debug help

* Fix traversal order to avoid possible(?) infinite recursion.

* Disable the old creation of record declarations.

  - See what fails in the testsuite.
  - It was actually affecting some tests because nested records were cycling
    back and messing with the order. Not exactly sure how but is not
    relevant anymore.

* Simplify processing of record declrations.

  - Remove returned lists from functions:
     elaborateNestedRecordDeclarations
     elaborateRecordDeclarationsFromTypes
     elaborateRecordDeclarationsForRecord
     elaborateNestedRecordDeclarations

  - Remove input lists from functions:
    - elaborateRecordDeclarationsFromTypes

* Make sure we do not overwrite true values to false.

  - If an entry already exists in the map and we always update, then there
    is a chance we might overwrite a 'true' value with a 'false' value
    for external conversion marker.

    Check if the entry exists and if it is marked false while then new
    incoming entry is marked true, then update it. Otherwise do nothing.

* Remove input lists from functions

  - Remove input lists from functions:
    - elaborateNestedRecordDeclarations
    - elaborateRecordDeclarationsForRecord

* Remove input and output lists from more functions.

  - Removed from
    - elaborateRecordDeclarationsForMetarecords

* Remove input and output lists from elaborateRecordDeclarations.

* Remove input and output lists from more functions.

  - Remove input and output lists from functions:
    - elaborateFunctions2
    - elaborateFunction

* Convert recursive functions to loops.

  - Recursive functions converted to loops:
    - elaborateNestedRecordDeclarations
    - elaborateRecordDeclarationsForMetarecords
    - elaborateRecordDeclarationsFromTypes

* Rename some functions to be more descriptive.

  - elaborateRecordDeclarationsForMetarecords -> collectRecDeclsFromMetaRecordCallExps
  - elaborateNestedRecordDeclarations -> collectRecDeclsFromTypesVars
  - elaborateRecordDeclarationsFromTypes -> collectRecDeclsFromTypes
  - elaborateRecordDeclarationsForRecord -> collectRecDeclsFromType

* Change how records are collected from metarecordcalls

  - Instead of:
    - traversing all expressions, collecting all meta record calls to a list,
      and then traversing this list to collect record declarations

    - collect record declarations while traversing all expressions (without
      collecting metarecordcalls into a whole new list.)

* Rename functions to be more descriptive.

  - elaborateRecordDeclarations -> collectRecDeclsFromElems

* Convert recursive functions to loops.

  - Convert recursive functions to loops:
    - collectRecDeclsFromElems (used to be `elaborateRecordDeclarations`)

* Some minor cleanup and renaming.

  - declMap -> recDeclsMap
  - needsExternalConversion -> usedExternally
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/CMake/Disable/All Make Jenkins disable CMake builds on all platforms.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants