Add basic Serde serialization capabilities to Stable MIR by sskeirik · Pull Request #126963 · rust-lang/rust (original) (raw)
This PR adds basic Serde serialization capabilities to Stable MIR. It is intentionally minimal (just wrapping all stable MIR types with a Serde derive
), so that any important design decisions can be discussed before going further. A simple test is included with this PR to validate that JSON can actually be emitted.
Notes
When I wrapped the Stable MIR error types in compiler/stable_mir/src/error.rs
, it caused test failures (though I'm not sure why) so I backed those out.
Future Work
So, this PR will support serializing basic stable MIR, but it does not support serializing interned values beneath Ty
s and AllocId
s, etc... My current thinking about how to handle this is as follows:
- Add new
visited_X
fields to theTables
struct for each interned category of interest. - As serialization is occuring, serialize interned values as usual and also record the interned value we referenced in
visited_X
.
(Possibly) In addition, if an interned value recursively references other interned values, record those interned values as well. - Teach the stable MIR
Context
how to access thevisited_X
values and expose them with wrappers instable_mir/src/lib.rs
to users (e.g. to serialize and/or further analyze them).
Pros
This approach does not commit to any specific serialization format regarding interned values or other more complex cases, which avoids us locking into any behaviors that may not be desired long-term.
Cons
The user will need to manually handle serializing interned values.
Alternatives
- We can directly provide access to the underlying
Tables
maps for interned values; the disadvantage of this approach is that it either requires extra processing for users to filter out to only use the values that they need or users may serialize extra values that they don't need. The advantage is that the implementation is even simpler. The other pros/cons are similar to the above. - We can directly serialize interned values by expanding them in-place. The pro is that this may make some basic inputs easier to consume. However, the cons are that there will need to be special provisions for dealing with cyclical values on both the producer and consumer and global values will possibly need to be de-duplicated on the consumer side.