Skip to content
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

Emit smir #117745

Merged
merged 7 commits into from Nov 18, 2023
Merged

Emit smir #117745

merged 7 commits into from Nov 18, 2023

Conversation

ouz-a
Copy link
Contributor

@ouz-a ouz-a commented Nov 9, 2023

This adds ability to -Zunpretty=smir and get smir output of a Rust file, this is obliviously pretty basic compared to mir output but I think we could iteratively improve it, and even at this state this is useful for us.

r? @celinval

@rustbot
Copy link
Collaborator

rustbot commented Nov 9, 2023

Failed to set assignee to celinval: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 9, 2023
@rustbot
Copy link
Collaborator

rustbot commented Nov 9, 2023

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

This PR changes Stable MIR

cc @oli-obk, @celinval, @spastorino, @ouz-a

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's awesome! I would like to suggest two things:

  1. To change the interface of all methods, except write_smir_pretty to use &Table instead of TyCtxt. This will make it much cleaner to invoke StableMIR APIs and to expose some of these methods in the stable_mir crate as well.
  2. To write to the given writer directly, instead of returning String in all methods. Users could still extract that information into a String if needed by passing a Vec<u8>.

compiler/rustc_session/src/config.rs Outdated Show resolved Hide resolved
compiler/stable_mir/src/mir/body.rs Outdated Show resolved Hide resolved
compiler/rustc_smir/src/rustc_internal/pretty.rs Outdated Show resolved Hide resolved
compiler/rustc_smir/src/rustc_internal/pretty.rs Outdated Show resolved Hide resolved
compiler/rustc_smir/src/rustc_internal/pretty.rs Outdated Show resolved Hide resolved
compiler/rustc_smir/src/rustc_internal/pretty.rs Outdated Show resolved Hide resolved
@celinval
Copy link
Contributor

r? @celinval

@ouz-a
Copy link
Contributor Author

ouz-a commented Nov 14, 2023

That's awesome! I would like to suggest two things:

1. To change the interface of all methods, except `write_smir_pretty` to use `&Table` instead of `TyCtxt`. This will make it much cleaner to invoke StableMIR APIs and to expose some of these methods in the `stable_mir` crate as well.

2. To write to the given writer directly, instead of returning `String` in all methods. Users could still extract that information into a `String` if needed by passing a `Vec<u8>`.

Thanks! Regarding your suggestions
1: I just tried this and result is less pleasant code, e.g

// from this to
                format!(
                    "[{}; {}]",
                    pretty_ty(ty.kind(), tcx),
                    internal(&len).try_to_scalar().unwrap()
                )
// this
                write!(w, "[");
                write!(w,"{}", pretty_ty(ty.kind(), tcx,w));
                write!(w, "; ");
                write!(w,"{}", internal(&len).try_to_scalar().unwrap());

2: I don't know how to make this work e.g this gives borrow errror

    run(tcx, || {
        with_tables(|tables| {
        let items = stable_mir::all_local_items();
        let _ = items.iter().map(|item| -> io::Result<()> {
            function_name(*item, tables)?;
 });

@celinval
Copy link
Contributor

Thanks! Regarding your suggestions 1: I just tried this and result is less pleasant code, e.g

Fair. Thanks for trying.

2: I don't know how to make this work e.g this gives borrow errror

You should remove tcx argument and stick to stable APIs as much as possible. Use the with_tables method only when you need tcx. Basically you should avoid invoking stable APIs inside the with* functions.

@rust-log-analyzer

This comment has been minimized.

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r=me after fixing the result value

compiler/stable_mir/src/mir/body.rs Outdated Show resolved Hide resolved
@ouz-a
Copy link
Contributor Author

ouz-a commented Nov 17, 2023

@bors r=celinval

@bors
Copy link
Contributor

bors commented Nov 17, 2023

📌 Commit 92657f1 has been approved by celinval

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Nov 17, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Nov 17, 2023
Emit smir

This adds ability to `-Zunpretty=smir` and get smir output of a Rust file, this is obliviously pretty basic compared to `mir` output but I think we could iteratively improve it, and even at this state this is useful for us.

r? `@celinval`
bors added a commit to rust-lang-ci/rust that referenced this pull request Nov 17, 2023
…iaskrgr

Rollup of 7 pull requests

Successful merges:

 - rust-lang#117338 (Remove asmjs)
 - rust-lang#117549 (Use `copied` instead of manual `map`)
 - rust-lang#117745 (Emit smir)
 - rust-lang#117964 (When using existing fn as module, don't claim it doesn't exist)
 - rust-lang#118006 (clarify `fn discriminant` guarantees: only free lifetimes may get erased)
 - rust-lang#118016 (Add stable mir members to triagebot config)
 - rust-lang#118022 (Miri subtree update)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 6227455 into rust-lang:master Nov 18, 2023
11 checks passed
@rustbot rustbot added this to the 1.76.0 milestone Nov 18, 2023
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Nov 18, 2023
Rollup merge of rust-lang#117745 - ouz-a:emit_smir, r=celinval

Emit smir

This adds ability to `-Zunpretty=smir` and get smir output of a Rust file, this is obliviously pretty basic compared to `mir` output but I think we could iteratively improve it, and even at this state this is useful for us.

r? ``@celinval``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants