Skip to content

Make untracked incr. comp. information inaccessible #90317

Open

Description

@cjgillot

When iterating over a collection, be it a Vec, a HashMap or an IndexMap, the order of items influences the value of the resulting hash: [a, b] and [b, a] have different hashes.

Meanwhile, there is some information we do not want to track. This is the case of the value of LocalDefId. Inserting a function in a file will change other functions LocalDefId, but it will not change their DefPathHash.

My concern is about controlling this information flow. In order to do that ToStableHashKey trait replaces the iteration order of the collection (which for hash maps is based on the key and the allocated memory capacity and should be irrelevant to the compilation), by the order of a stable hash key (the DefPathHash when the key is LocalDefId). By sorting the vectors by stable key, we manage the information flow.

Using IndexMap, the iteration order is the insertion order. Normally, this insertion order should only depend on tracked information obtained by depending on another query. For instance, a HIR visitor will create a query dependency on hir_owner_nodes, which hashes the in-code declaration order of functions. However, and this is my concern, the order of LocalDefId is freely available without using a query and is purposely untracked.

In order to make IndexMaps safe for incr. comp., we need to ensure untracked information is inaccessible.

Known issues:

  • Stop implementing PartialOrd and Ord on DefId;
  • Stop implementing PartialOrd, Ord and Idx for LocalDefId;
  • Stop implementing PartialOrd, Ord and Idx for LocalExpnId;
  • Stop implementing PartialOrd and Ord for SyntaxContext;
  • Enforce that UNTRACKED options are not accessed within a query.

Originally posted by @cjgillot in #90253 (comment)

Activity

  1. added
    A-incr-compArea: Incremental compilation
    E-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.
    E-help-wantedCall for participation: Help is requested to fix this issue.
    on Oct 26, 2021
  2. pierwill commented on Oct 29, 2021

    @pierwill
    Contributor
    • Stop implementing PartialOrd and Ord on DefId...

    Can these items be done one at a time?

  3. cjgillot commented on Oct 29, 2021

    @cjgillot
    ContributorAuthor

    Of course!

  4. pierwill commented on Oct 29, 2021

    @pierwill
    Contributor

    Will give this a go and see what happens. @rustbot claim

  5. pierwill commented on Oct 30, 2021

    @pierwill
    Contributor

    I started experimenting with LocalDefId. I wonder if the DefId work might end up being slightly less complex. 🤔

  6. pierwill commented on Nov 2, 2021

    @pierwill
    Contributor

    I wonder if the DefId work might end up being slightly less complex. 🤔

    Update: No. 😄

  7. pierwill commented on Nov 3, 2021

    @pierwill
    Contributor

    Looks like removing Ord, PartialOrd for ExpnId and LocalExpnId might require changes to the macro rustc_index::newtype_index because of this line:

    #[derive(Copy, PartialEq, Eq, Hash, PartialOrd, Ord, $($derives),*)]

  8. bjorn3 commented on Nov 5, 2021

    @bjorn3
    Member

    Track accesses to command-line options;
    Track accesses to debugging command-line options.

    Most of them are already "tracked" by discarding the entire incr comp cache if they change. The ones that aren't tracked are explicitly not tracked. See the UNTRACKED mentions in https://github.com/rust-lang/rust/blob/d22dd65835190278f315e06442614142653ec98f/compiler/rustc_session/src/options.rs A couple of them should be marked as TRACKED though I think.

  9. pierwill commented on Nov 8, 2021

    @pierwill
  10. pierwill commented on Nov 11, 2021

    @pierwill
    Contributor

    @cjgillot It looks like rustc_span::hygiene::ExpnId does not implement Ord or PartialOrd. rustc_span::hygiene::LocalExpnId does implement ordering.

    (So does rustc_middle::thir::ExprId. Is that related to this issue?)

  11. 66 remaining items

  12. added a commit that references this issue on Mar 22, 2024
  13. added a commit that references this issue on Mar 22, 2024
  14. added a commit that references this issue on Mar 28, 2024
  15. added a commit that references this issue on Jun 21, 2024
  16. added a commit that references this issue on Apr 2, 2025
  17. added a commit that references this issue on Apr 3, 2025
  18. added a commit that references this issue on Apr 3, 2025
  19. added a commit that references this issue on Apr 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-incr-compArea: Incremental compilationE-hardCall for participation: Hard difficulty. Experience needed to fix: A lot.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions