Closed matthewjasper closed 4 years ago
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.
@rustbot second
@matthewjasper
Hello, I came across this issue while looking for a way to serialize/deserialize the MIR Body. I have two questions.
Where in the rustc code is the decode()
method, which is associated with the MIR Body (as mentioned below), used?
Is it possible to serialize the MIR Body to a file (during a MIR pass) and then load it back by deserializing it? If so, how should the code be written? I thought of a logic like the one below.
use crate::MirPass;
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_serialize::opaque::{FileEncoder, MemDecoder};
use rustc_serialize::{Decodable, Encodable};
use std::fs;
pub struct MirSerDe;
impl<'tcx> MirPass<'tcx> for MirSerDe {
fn run_pass(&self, _tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let tmpfile = tempfile::NamedTempFile::new().unwrap();
let tmpfile = tmpfile.path();
// Serialize
let mut encoder = FileEncoder::new(&tmpfile).unwrap();
Encodable::encode(body, &mut encoder);
encoder.finish().unwrap();
// Deserialize
let data = fs::read(&tmpfile).unwrap();
let mut decoder = MemDecoder::new(&data[..], 0);
let decoded = Decodable::decode(&mut decoder);
assert_eq!(body, decoded);
}
}
Zulip (or a new issue if you have a suitable repository for it) might be a better place to discuss this than this Github issue, but to answer your questions:
TyDecoder
and encoder that implements TyEncoder
and neither of the current types used for this would be suitable. You could try implementing your own. What do you actually want to do here? Why can't you keep the MIR in memory
Proposal
rust-lang/rust#73851 reworks specialization in the following noticeable ways:
rustc_macros
rather than the built-inRustcEncodable
andRustcDecodable
derives.Encodable
andDecodable
now directly use specialization.Further documentation of serialization in rustc after the PR has landed can be found at rust-lang/rustc-guide#785.
Mentors or Reviewers
PR has already been reviewed by @oli-obk
Process
The main points of the Major Change Process is as follows:
@rustbot second
.-C flag
, then full team check-off is required.@rfcbot fcp merge
on either the MCP or the PR.You can read more about Major Change Proposals on forge.
Comments
This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.