Skip to content

Eagerly construct bodies of THIR #409

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
3 tasks done
LeSeulArtichaut opened this issue Feb 22, 2021 · 4 comments
Closed
3 tasks done

Eagerly construct bodies of THIR #409

LeSeulArtichaut opened this issue Feb 22, 2021 · 4 comments
Labels
major-change A proposal to make a major change to rustc major-change-accepted A major change proposal that was accepted T-compiler Add this label so rfcbot knows to poll the compiler team

Comments

@LeSeulArtichaut
Copy link
Contributor

LeSeulArtichaut commented Feb 22, 2021

Proposal

No longer construct the THIR lazily, layer by layer, and instead construct it eagerly.
This concretely means:

  • removing thir::ExprRef/thir::StmtRef and having a full THIR tree (not a "shallow" tree)
  • constructing a THIR body can be as simple as calling a build_thir function, without the need for visitors to construct it recursively themselves, simplifying code overall
  • we could avoid constructing patterns twice (for MIR building and exhaustiveness checking).

Also, this might allow to do more things on the THIR, like unsafety checking (#402).

(Suggested by @nikomatsakis on Zulip)

FAQ

  • Why did we do the streaming approach in the first place?
    • My memory is that I wanted to avoid increasing peak memory usage by constructing Yet Another IR. It was also a bit because I thought it was cool. 😁 --Niko
  • So... won't this increase peak memory usage?
    • Potentially, yes. We'll only keep the IR for one function at a time, it won't be stored persistently. This could be done either by "stealing" or by having all processing of THIR occur within one query. I lean mildly towards the second approach, but that may not work out well with exhaustiveness checking, I haven't looked closely at whether this query has additional outputs that couldn't be combined with MIR. --Niko
  • Can we make that more certain?
  • What alternatives have you considered?
    • We could do a "streaming approach".
    • We could also build the THIR multiple times, once for each user (this is what we do today, effectively).
  • A "streaming approach", what's that?
    • The idea would be to have "callbacks" of a sort. When a HIR expression is "mirrored" into THIR, we would also invoke a callback that would do unsafety checking at the same time.
    • The downside with this idea is that it relies on MIR construction to mirror the entire HIR. But that doesn't feel like a necessary requirement for MIR construction. You could imagine us doing optimizations where we avoid constructing the MIR for dead code, for example, or other things like that.
    • Another downside is that writing the 'unsafety check' in an "inverted" way, where it is getting callbacks for individual THIR nodes and not able to drive the mirroring process itself, may be difficult. I suspect the code would be very confusing. It may not even work, we haven't exhaustively explored this option.
    • Finally, exhaustiveness checking still requires us to build the THIR for all the patterns.
  • Why not build THIR multiple times? This is what we do today for exhaustiveness checking, after all, isn't it?
    • Yes, today we build the THIR more than once, but only for patterns. It just seems kind of slow and wasteful to build it multiple times. At the end of the day, execution time seems more important than peak memory usage. That said, it is totally viable for us to prototype the unsafety check using the mirroring API and then test the impact on compilation time, and decide whether to do this step afterwards. That might even be a good idea.

Mentors or Reviewers

@nikomatsakis

Process

The main points of the Major Change Process is as follows:

  • File an issue describing the proposal.
  • A compiler team member or contributor who is knowledgeable in the area can second by writing @rustbot second.
    • Finding a "second" suffices for internal changes. If however you are proposing a new public-facing feature, such as a -C flag, then full team check-off is required.
    • Compiler team members can initiate a check-off via @rfcbot fcp merge on either the MCP or the PR.
  • Once an MCP is seconded, the Final Comment Period begins. If no objections are raised after 10 days, the MCP is considered approved.

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.

@LeSeulArtichaut LeSeulArtichaut added T-compiler Add this label so rfcbot knows to poll the compiler team major-change A proposal to make a major change to rustc labels Feb 22, 2021
@rustbot
Copy link
Collaborator

rustbot commented Feb 22, 2021

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.

cc @rust-lang/compiler @rust-lang/compiler-contributors

@rustbot rustbot added the to-announce Announce this issue on triage meeting label Feb 22, 2021
@nikomatsakis
Copy link
Contributor

@rustbot second

@rustbot rustbot added the final-comment-period The FCP has started, most (if not all) team members are in agreement label Feb 24, 2021
@nikomatsakis
Copy link
Contributor

nikomatsakis commented Feb 24, 2021

Whoops, I probably shouldn't have seconded this, as there were some outstanding questions. Not sure how to cancel that, though?

EDIT: I just removed the label for now.

@nikomatsakis nikomatsakis removed the final-comment-period The FCP has started, most (if not all) team members are in agreement label Feb 24, 2021
@oli-obk
Copy link
Contributor

oli-obk commented Feb 24, 2021

@rustbot second

my concerns around peak memory usage have been resolved, we'll look into this during the actual impl, but I don't think anyone is very worried.

@rustbot rustbot added the final-comment-period The FCP has started, most (if not all) team members are in agreement label Feb 24, 2021
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Feb 25, 2021
@LeSeulArtichaut LeSeulArtichaut added major-change-accepted A major change proposal that was accepted and removed final-comment-period The FCP has started, most (if not all) team members are in agreement labels Mar 7, 2021
@rustbot rustbot added the to-announce Announce this issue on triage meeting label Mar 7, 2021
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 11, 2021
Eagerly construct bodies of THIR

With this PR:
 - the THIR is no longer constructed lazily, but is entirely built before being passed to the MIR Builder
 - the THIR is now allocated in arenas instead of `Box`es

However, this PR doesn't make any changes to the way patterns are constructed: they are still boxed, and exhaustiveness checking is unchanged.

Implements MCP rust-lang/compiler-team#409.
Closes rust-lang/project-thir-unsafeck#1.
r? `@ghost` cc `@nikomatsakis` `@oli-obk`
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Mar 11, 2021
@apiraino apiraino closed this as completed May 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
major-change A proposal to make a major change to rustc major-change-accepted A major change proposal that was accepted T-compiler Add this label so rfcbot knows to poll the compiler team
Projects
None yet
Development

No branches or pull requests

5 participants