rust-lang / compiler-team

A home for compiler team planning documents, meeting minutes, and other such things.
https://rust-lang.github.io/compiler-team/
Apache License 2.0
387 stars 69 forks source link

Rework rustc_serialize #329

Closed matthewjasper closed 4 years ago

matthewjasper commented 4 years ago

Proposal

rust-lang/rust#73851 reworks specialization in the following noticeable ways:

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:

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.

rustbot commented 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.

oli-obk commented 4 years ago

@rustbot second

extf33 commented 1 year ago

@matthewjasper

Hello, I came across this issue while looking for a way to serialize/deserialize the MIR Body. I have two questions.

  1. Where in the rustc code is the decode() method, which is associated with the MIR Body (as mentioned below), used?

  2. 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);
        }
    }
matthewjasper commented 1 year ago

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:

  1. There is one decode call here: https://github.com/rust-lang/rust/blob/af488be5f82b5e1010f7b52bc4fcda697793950d/compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs#L88 and another here: https://github.com/rust-lang/rust/blob/b402182d4e0b00254c1afd0620547e2ee42ae336/compiler/rustc_middle/src/query/on_disk_cache.rs#L532
  2. I don't think we have support for this. You'll need a decoder that implements 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