Skip to content

use try_mark_green for type-checking #45156

Closed
@nikomatsakis

Description

@nikomatsakis

Right now, the type-checker works by iterating over over every body in the crate and invoking typeck_tables_of on them; it then proceeds to discard the answer, since it doesn't actually care about the result, it just wants to ensure that the result has been computed (and in particular that any errors that may result have been reported).

In general, for cases where we are not interested in the result, I think that we should instead be using the try_mark_green operation, though we probably want a mildly cleaner interface to it first. The idea here is that try_mark_green will re-execute the query, but only if its inputs have changed; otherwise, it can mark the query as green without executing it. In particular, try_mark_green does not guarantee that the result of the query is "encached" in the tables when it completes.

This is useful for typeck in particular because I would like to at least have the option of skipping the typeck for particular items without saving the results onto disk. The current setup, where we actually do the query, means that we must have the typeck tables saved to disk, or else we will have to re-execute the typeck to regenerate them.

Now, even once we make this change, we will need to do more work to enable skipping typeck (particularly if we don't want to save the typeck tables to disk). We basically have to ensure that there are no other places in the code that will invoke typeck_tables_of unnecessarily (ideally: if the function or some dependent of it has not changed). In the shorter term, it may indeed be easier to just save the typeck results.

Still, this seems like a simple step we can take. Longer term, I think we will want to be able to minimize our disk and memory space footprint, while still achieving the best incremental results we can, and the ability to "lazilly" recompute things like typeck-tables will be an important factor in that.

I think I would break this issue into two parts:

Step 1. Package up the try_mark_green operation in some nicer way. I would do this by modifying the plumbing macros for the query infrastructure. I think we could add an accessor called try_mark_green, somewhat like force. It would look something like this:

/// Determines whether the query's result for `key` is up-to-date,
/// without necessarily executing the query. In particular, if all the inputs
/// to the query are green, then the query will not be re-executed. If however
/// some inputs have changed, then the query will be re-executed, and its
/// results cached (after being compared against the hash of the previous results).
///
/// This function is particularly useful when executing passes for their side-effects --
/// e.g., in order to report errors for erroneous programs.
///
/// Because the query will not execute unless it has changed, this function returns unit.
pub fn try_mark_green(
    tcx: TyCtxt<'a, $tcx, 'lcx>,
    key: $K)
{
    let dep_node = Self::to_dep_node(tcx, &key);
    tcx.dep_graph.try_mark_green(tcx, &dep_node);
}

Step 2. Convert code in librustc_typeck to use try_mark_green. Notably, change this line from self.tcx.typeck_tables_of(body_owner_def_id) to ty::maps::queries::typeck_tables_of::try_mark_green(self.tcx, body_owner_def_id).

cc @michaelwoerister, who may disagree forcefully with my premise here, I don't know

Metadata

Metadata

Assignees

No one assigned

    Labels

    E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-incr-compWorking group: Incremental compilation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions