stan-dev / stanc3

The Stan transpiler (from Stan to C++ and beyond).
BSD 3-Clause "New" or "Revised" License
138 stars 44 forks source link

Update generated `transform_inits_impl` to directly accept a var_context. #1305

Closed WardBrian closed 1 year ago

WardBrian commented 1 year ago

Submission Checklist

Release notes

Closes #1304.

This PR does a couple things:

  1. Re-using the code gen we already have for the constructor, transform_inits_impl now takes in a var_context directly. This is what fixes #1304, and also will help out with #1100. The previous behavior of flattening it down to a flat vector was hacky, and for complex numbers or tuples, wrong.

  2. Adds a new function called unconstrain_array which looks much more like the former transform_inits. This is the twin of write_array and can be eventually exposed/used in place of a lot of the places we're creating a array_var_context. The idea for a signature like this dates back to when the deserializer was added: https://github.com/stan-dev/stan/pull/3013#issuecomment-795906563. I added this here to keep the existing code for transform_inits_impl around rather than deleting it and re-coding it later, but it will currently be unused.

The second item was actually my original goal, but while working on it I uncovered #1304. The first nicely kills two birds with one stone, fixing that issue and helping out with something I had been stuck on in the tuples PR.

This was able to be done mostly by re-using code already in the compiler. Unfortunately, since the reading code is all generated in Transform_mir rather than lower down, the simplest solution was to add another (empty until the backend) item to the MIR.

Copyright and Licensing

By submitting this pull request, the copyright holder is agreeing to license the submitted work under the BSD 3-clause license (https://opensource.org/licenses/BSD-3-Clause)