Skip to content

Building a REPL on top of miri #511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
oli-obk opened this issue Nov 5, 2018 · 21 comments
Closed

Building a REPL on top of miri #511

oli-obk opened this issue Nov 5, 2018 · 21 comments
Labels
A-ux Area: This affects the general user experience C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Nov 5, 2018

@technetos so the steps are essentially

  1. create a loop around the main miri code in the miri binary
  2. read some text from the user and append it to a mutable string defined outside the loop
  3. format!("fn main() { let _: &'static *const () = &std::ptr::null(); {}}}", mut_string) to create the "main.rs" that you feed to the rust evaluator
  4. the miri-hook that currently starts the evaluation will now call
    let snapshot = GlobalId { promoted: Some(0), instance: main_instance };
    let _ = tcx.const_eval(ParamEnv::empty().and(snapshot));
  5. before that you do
    tcx.const_eval = |arg| {
        if arg.value == snapshot {
            // run original miri evaluator
        } else {
            // run `const_eval_provider`
        }
    };
    (well not quite, but have a look at where const_eval_provider is used and follow the call stack up until you see how to do the overriding thing in miri)
@RalfJung
Copy link
Member

RalfJung commented Nov 5, 2018

Why are these crazy hacks involving the promoted necessary and what do they even do?

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 5, 2018

Oh I forgot, running the miri evaluator should not pop the final function, but instead "just" create a special allocation that contains all (reachable?) allocations from Memory and all other state required like block index + statement index. Returning a pointer to that allocation will cause the promoted to have that value and incremental to store all that data in the cache.

Before starting the miri evaluator the next time, we make incremental give us the old value (more hacks, yay!) and restore the state from that.

@RalfJung
Copy link
Member

RalfJung commented Nov 5, 2018

Wtf... you definitely are crazy.^^

@RalfJung RalfJung added C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement A-ux Area: This affects the general user experience labels Nov 17, 2018
@theduke
Copy link

theduke commented Jun 6, 2019

I believe a moderately fast Rust REPL would do wonders for the Rust dev experience.

@oli-obk how useful and fast would a miri based repl be? Fast enough to provide utility vs eg re-running code in the playground?

Are your basic implementation suggestions from above still valid?
If so I'm interested in giving this a try.

Also, could miri theoretically be used to build a debugger with breakpoints and the ability to run code at those breakpoints? (printing values, modifying local variables before continuing, ...)

@oli-obk
Copy link
Contributor Author

oli-obk commented Jun 6, 2019

how [..] fast would a miri based repl be?

around 100000 times slower than direct execution. Depends on the kind of things that are happening though.

Also, could miri theoretically be used to build a debugger with breakpoints and the ability to run code at those breakpoints? (printing values, modifying local variables before continuing, ...)

I has been used to build such a thing practically: https://github.com/oli-obk/priroda

We need to update it to the latest master branch though :)

@nico-abram
Copy link
Contributor

How fast would it be compared to actually compiling the code, not just the direct execution?

@BatmanAoD
Copy link
Member

I'm not sure how close this is to what you were envisioning, @oli-obk, but here's an approach aimed at making Rust usable in Jupyter notebooks: https://github.com/google/evcxr

@bjorn3
Copy link
Member

bjorn3 commented Nov 29, 2019

And there is going to be rust-lang/compiler-team#213.

@BatmanAoD
Copy link
Member

@bjorn3 Very cool! I did not know about that, actually.

@zacps
Copy link

zacps commented Oct 15, 2020

What's the current status of this? Has it been superseded by the possibility of a cranelift backend?

@ibraheemdev
Copy link
Member

ibraheemdev commented Nov 12, 2020

Would a miri based REPL be faster than evcxr_repl? evcxr essentially writes user input to a crate and compiles that, saving previous results in a HashMap. Also, would a cranelift jit based REPL be more performant?

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 13, 2020

anything miri based will never be fast. But it would reduce duplication between the repl and miri since they can share lots of code.

@RalfJung
Copy link
Member

I don't see why a repl would have lots of code in common with Miri?

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 13, 2020

well, if you do the interpretation on the MIR level, then you will end up with some sort of mir interpreter similar to the miri-engine. It could be implemented on something different (e.g. LLVM-IR) or just JIT the code, but then all the OS stuff still needs to get handled somehow, and that will probably be similar to what miri is doing.

@RalfJung
Copy link
Member

Right but I don't see why a REPL would be based on interpretation, TBH.^^

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 13, 2020

True... So I guess we stop tracking this here and close this issue?

@bjorn3
Copy link
Member

bjorn3 commented Nov 13, 2020

The advantage of building a repl on top of miri is that it could run in the browser. Rustc can successfully be compiled to wasm32-wasi with a few small changes.

@RalfJung
Copy link
Member

The playground can also run in the browser though... and honestly compiling and running this on a server will likely still be faster than running Miri in Wasm.^^

@oli-obk
Copy link
Contributor Author

oli-obk commented Nov 13, 2020

I'm going to go ahead and close this. Even if a REPL will be miri based, it's not necessary to track this here.

@oli-obk oli-obk closed this as completed Nov 13, 2020
@toothbrushes
Copy link

@oli-obk is this being tracked anywhere else tho? I would love a Rust REPL, where y'all at? One idea is we could create a project, or zulip channel #project-repl

@bjorn3
Copy link
Member

bjorn3 commented Sep 23, 2021

Have you seen evcxr? It has both support for jupyter notebooks as well as a regular repl.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ux Area: This affects the general user experience C-enhancement Category: a PR with an enhancement or an issue tracking an accepted enhancement
Projects
None yet
Development

No branches or pull requests

9 participants