Skip to content

serialize std::variant#1344

Merged
feldergast merged 2 commits intosstsimulator:develfrom
leekillough:serialize_variant
May 27, 2025
Merged

serialize std::variant#1344
feldergast merged 2 commits intosstsimulator:develfrom
leekillough:serialize_variant

Conversation

@leekillough
Copy link
Contributor

This adds serialization of std::variant.

@github-actions github-actions bot added AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) AT: CMAKE-FORMAT PASS and removed AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) labels May 25, 2025
@github-actions
Copy link

CMAKE-FORMAT TEST - PASSED

@github-actions github-actions bot added AT: CLANG-FORMAT FAIL AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) labels May 25, 2025
@github-actions
Copy link

CLANG-FORMAT TEST - FAILED (on last commit):
Run > ./scripts/clang-format-test.sh using clang-format v20 to check formatting

@github-actions github-actions bot added AT: CLANG-FORMAT PASS and removed AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) labels May 25, 2025
@github-actions
Copy link

CLANG-FORMAT TEST - PASSED

@github-actions
Copy link

CMAKE-FORMAT TEST - PASSED

@sst-autotester
Copy link
Contributor

Status Flag 'Pre-Test Inspection' - - This Pull Request Requires Inspection... The code must be inspected by a member of the Team before Testing/Merging
NO INSPECTION HAS BEEN PERFORMED ON THIS PULL REQUEST! - This PR must be inspected by setting label 'AT: PRE-TEST INSPECTED'.

@leekillough
Copy link
Contributor Author

template <typename... Types>
template <size_t... INDEX>
constexpr void (*serialize_impl<std::variant<Types...>>::set_index<std::index_sequence<INDEX...>>[])(
    std::variant<Types...>&) { [](std::variant<Types...>& obj) { obj.template emplace<INDEX>(); }... };

In case the code is too hard to understand, here's a dissection:

template <typename... Types>

The outer template parameter list.

template <size_t... INDEX>

The inner template parameter list.

constexpr

The constexpr applies to the set_index variable template being defined here, which also means that, as a constexpr static member variable of serialize_impl<std::variant<Types...>>, set_index is inline, and so it can be defined in a header file without causing ODR violations (multiply-defined symbols).

 void (*                                                                                [])( std::variant<Types...>&)
        serialize_impl<std::variant<Types...>>::set_index<std::index_sequence<INDEX...>> 

A partial specialization of the set_index member variable template of serialize_impl<std::variant<Types...> which matches std::index_sequence<INDEX...>, which is a list of 0, 1, 2, ..., sizeof...(Types)-1.

This specialization is declared as an unspecified-size array of pointers to functions taking std::variant<Types...>& and returning void.

 { 
   [](std::variant<Types...>& obj) { obj.template emplace<INDEX>(); }
   ... 
 };

An initialization list for the unspecified-size array of function pointers, which gets expanded for each element of the INDEX list of subscripts.

Each element of the list is a lambda expression with no captures which takes a std::variant<Types...>& and (implicitly auto) returns void. Because there are no captures, the lambda expression is implicitly convertible to the function pointer type of the array elements.

The lambda function calls the emplace() method on the obj argument with the template argument INDEX, which is the index corresponding to the current function pointer array element in the initialization list.

Passing the emplace() method a template argument of INDEX causes the Types... type corresponding to INDEX to become the variant's new type.

The emplace() argument list is empty, so it default-initializes the new variant type.

Copy link
Contributor

@feldergast feldergast left a comment

Choose a reason for hiding this comment

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

Thanks for the comments. I mostly understand what's going on and am willing to take the rest on faith. Can you add those comments to the appropriate places in the code so that we have it in the future when someone needs to understand the code.

@github-actions github-actions bot added AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) and removed AT: WIP Mark PR as a Work in Progress (No Autotesting Performed) labels May 27, 2025
@github-actions
Copy link

CLANG-FORMAT TEST - PASSED

@github-actions
Copy link

CMAKE-FORMAT TEST - PASSED

@leekillough
Copy link
Contributor Author

I have added a long comment at the end of the file explaining what the table set_index[] does and the motivation for it, e.g.

Why not just change the active variant by setting it to an index which changes at runtime?

Because changing the active variant requires that the new variant index be known at compile time, so a table of each possible new variant is needed, and this table can be dispatched with a runtime new value of index.

I have also reformatted the code in a // clang-format off ... // clang-format on region so that the syntactic elements are clearer.

@sst-autotester
Copy link
Contributor

Status Flag 'Pre-Test Inspection' - SUCCESS: The last commit to this Pull Request has been INSPECTED by label AT: PRE-TEST INSPECTED! Autotester is Removing Label; this inspection will remain valid until a new commit to source branch is performed.

@sst-autotester
Copy link
Contributor

Status Flag 'Pull Request AutoTester' - Testing Jenkins Projects:

Pull Request Auto Testing STARTING (click to expand)

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements

  • Build Num: 2068
  • Status: STARTED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements_MR-2

  • Build Num: 2024
  • Status: STARTED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements_MT-2

  • Build Num: 2023
  • Status: STARTED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-core_Make-Dist

  • Build Num: 801
  • Status: STARTED

Build Information

Test Name: SST__AutotestGen2_NewFW_OSX-14-XC15-ARM2_OMPI-4.1.6_PY3.10_sst-elements

  • Build Num: 579
  • Status: STARTED

Using Repos:

Repo: CORE (leekillough/sst-core)
  • Branch: serialize_variant
  • SHA: f668eeb
  • Mode: TEST_REPO
Repo: SQE (sstsimulator/sst-sqe)
  • Branch: devel
  • SHA: bb4b04da3cbca0dbf36fff9d79593f5f1db96034
  • Mode: SUPPORT_REPO
Repo: ELEMENTS (sstsimulator/sst-elements)
  • Branch: devel
  • SHA: 9213397d54ddaad188280f5f62430b45b30665e7
  • Mode: SUPPORT_REPO
Repo: MACRO (sstsimulator/sst-macro)
  • Branch: devel
  • SHA: 31e2c16aa4d07b502bcd9c97b1872a0329ed0b38
  • Mode: SUPPORT_REPO

Pull Request Author: leekillough

@sst-autotester
Copy link
Contributor

Status Flag 'Pull Request AutoTester' - Jenkins Testing: all Jobs PASSED

Pull Request Auto Testing has PASSED (click to expand)

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements

  • Build Num: 2068
  • Status: PASSED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements_MR-2

  • Build Num: 2024
  • Status: PASSED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-elements_MT-2

  • Build Num: 2023
  • Status: PASSED

Build Information

Test Name: SST__AutotestGen2_NewFW_sst-test_OMPI-4.1.4_PY3.6_sst-core_Make-Dist

  • Build Num: 801
  • Status: PASSED

Build Information

Test Name: SST__AutotestGen2_NewFW_OSX-14-XC15-ARM2_OMPI-4.1.6_PY3.10_sst-elements

  • Build Num: 579
  • Status: PASSED

@sst-autotester
Copy link
Contributor

Status Flag 'Pre-Merge Inspection' - SUCCESS: The last commit to this Pull Request has been INSPECTED AND APPROVED by [ feldergast ]!

@sst-autotester
Copy link
Contributor

Status Flag 'Pull Request AutoTester' - Pull Request MUST BE MERGED MANUALLY BY Project Team - This Repo does not support Automerge

@feldergast feldergast merged commit a273250 into sstsimulator:devel May 27, 2025
7 checks passed
@leekillough leekillough deleted the serialize_variant branch October 5, 2025 17:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants