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

Ensure nested allocations in statics neither get deduplicated nor duplicated #121644

Merged
merged 16 commits into from
Mar 12, 2024

Conversation

oli-obk
Copy link
Contributor

@oli-obk oli-obk commented Feb 26, 2024

This PR generates new DefIds for nested allocations in static items and feeds all the right queries to make the compiler believe these are regular static items. I chose this design, because all other designs are fragile and make the compiler horribly complex for such a niche use case.

At present this wrecks incremental compilation performance in case nested allocations exist (because any query creating a DefId will be recomputed and never loaded from the cache). This will be resolved later in #115613 . All other statics are unaffected by this change and will not have performance regressions (heh, famous last words)

This PR contains various smaller refactorings that can be pulled out into separate PRs. It is best reviewed commit-by-commit. The last commit is where the actual magic happens.

r? @RalfJung on the const interner and engine changes

fixes #79738

@oli-obk
Copy link
Contributor Author

oli-obk commented Feb 26, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. labels Feb 26, 2024
@rustbot
Copy link
Collaborator

rustbot commented Feb 26, 2024

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

Some changes occurred in match checking

cc @Nadrieril

Some changes occurred in compiler/rustc_codegen_gcc

cc @antoyo, @GuillaumeGomez

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

Some changes occurred to the CTFE / Miri engine

cc @rust-lang/miri

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Feb 26, 2024
@bors
Copy link
Contributor

bors commented Feb 26, 2024

⌛ Trying commit 0aa7752 with merge 51f4acc...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 26, 2024
Ensure nested allocations in statics do not get deduplicated

This PR generates new `DefId`s for nested allocations in static items and feeds all the right queries to make the compiler believe these are regular `static` items. I chose this design, because all other designs are fragile and make the compiler horribly complex for such a niche use case.

At present this wrecks incremental compilation performance *in case nested allocations exist* (because any query creating a `DefId` will be recomputed and never loaded from the cache). This will be resolved later in rust-lang#115613 . All other statics are unaffected by this change and will not have performance implications (heh, famous last words)

This PR contains various smaller refactorings that can be pulled out into separate PRs. It is best reviewed commit-by-commit. The last commit is where the actual magic happens.

r? `@RalfJung` on the const interner and engine changes
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Feb 26, 2024

💔 Test failed - checks-actions

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 26, 2024
@oli-obk
Copy link
Contributor Author

oli-obk commented Feb 26, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Feb 26, 2024

⌛ Trying commit cc1ec8a with merge 2e8adb0...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 26, 2024
Ensure nested allocations in statics do not get deduplicated

This PR generates new `DefId`s for nested allocations in static items and feeds all the right queries to make the compiler believe these are regular `static` items. I chose this design, because all other designs are fragile and make the compiler horribly complex for such a niche use case.

At present this wrecks incremental compilation performance *in case nested allocations exist* (because any query creating a `DefId` will be recomputed and never loaded from the cache). This will be resolved later in rust-lang#115613 . All other statics are unaffected by this change and will not have performance implications (heh, famous last words)

This PR contains various smaller refactorings that can be pulled out into separate PRs. It is best reviewed commit-by-commit. The last commit is where the actual magic happens.

r? `@RalfJung` on the const interner and engine changes

fixes rust-lang#79738
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Feb 26, 2024

💔 Test failed - checks-actions

@oli-obk oli-obk force-pushed the unique_static_innards2 branch from cc1ec8a to e5836ab Compare February 26, 2024 23:33
@oli-obk
Copy link
Contributor Author

oli-obk commented Feb 26, 2024

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Feb 26, 2024

⌛ Trying commit e5836ab with merge ee17466...

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 26, 2024
Ensure nested allocations in statics do not get deduplicated

This PR generates new `DefId`s for nested allocations in static items and feeds all the right queries to make the compiler believe these are regular `static` items. I chose this design, because all other designs are fragile and make the compiler horribly complex for such a niche use case.

At present this wrecks incremental compilation performance *in case nested allocations exist* (because any query creating a `DefId` will be recomputed and never loaded from the cache). This will be resolved later in rust-lang#115613 . All other statics are unaffected by this change and will not have performance regressions (heh, famous last words)

This PR contains various smaller refactorings that can be pulled out into separate PRs. It is best reviewed commit-by-commit. The last commit is where the actual magic happens.

r? `@RalfJung` on the const interner and engine changes

fixes rust-lang#79738
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the unique_static_innards2 branch from e5836ab to 133d5bd Compare February 27, 2024 00:03
@rust-log-analyzer

This comment has been minimized.

@oli-obk oli-obk force-pushed the unique_static_innards2 branch from 5d506e7 to 8a763f6 Compare March 12, 2024 05:54
@oli-obk oli-obk force-pushed the unique_static_innards2 branch from 8a763f6 to e277373 Compare March 12, 2024 08:51
@oli-obk
Copy link
Contributor Author

oli-obk commented Mar 12, 2024

@bors r=RalfJung,nnethercote

@bors
Copy link
Contributor

bors commented Mar 12, 2024

📌 Commit e277373 has been approved by RalfJung,nnethercote

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 Mar 12, 2024
@bors
Copy link
Contributor

bors commented Mar 12, 2024

⌛ Testing commit e277373 with merge 3b85d2c...

@bors
Copy link
Contributor

bors commented Mar 12, 2024

☀️ Test successful - checks-actions
Approved by: RalfJung,nnethercote
Pushing 3b85d2c to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 12, 2024
@bors bors merged commit 3b85d2c into rust-lang:master Mar 12, 2024
12 checks passed
@rustbot rustbot added this to the 1.78.0 milestone Mar 12, 2024
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (3b85d2c): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.2% [-0.2%, -0.2%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
9.6% [9.6%, 9.6%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-5.1% [-5.1%, -5.1%] 1
All ❌✅ (primary) 9.6% [9.6%, 9.6%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.9% [3.8%, 4.1%] 2
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-3.0% [-3.0%, -3.0%] 1
All ❌✅ (primary) - - 0

Binary size

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
0.0% [0.0%, 0.0%] 4
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 0.0% [0.0%, 0.0%] 4

Bootstrap: 672.558s -> 674.911s (0.35%)
Artifact size: 309.99 MiB -> 310.31 MiB (0.10%)

Comment on lines +383 to +387

// Temporarily allow access to the static_root_ids for the purpose of validation.
let static_root_ids = ecx.machine.static_root_ids.take();
let res = const_validate_mplace(&ecx, &mplace, cid);
ecx.machine.static_root_ids = static_root_ids;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

in case anyone stumbles upon this in the future, this was a rebase screwup and is getting removed again in #122397 (comment)

pub fn main() {
unsafe {
assert_eq!(FOO as *const i32, BAR as *const i32);
assert_eq!(INNER_MOD_FOO as *const i32, INNER_MOD_BAR as *const i32);
Copy link
Member

Choose a reason for hiding this comment

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

Looks like this case still depends on deduplication of anonymous constants, causing the test to fail with cg_clif.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea it's more like a canary test than anything we want to necessarily guarantee. Though maybe we could start guaranteeing the deduplication by generating canonical static items whose id/name is their contents' stable hash?

Copy link
Member

Choose a reason for hiding this comment

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

yea it's more like a canary test than anything we want to necessarily guarantee.

Would it be possible to split the test into a guaranteed half and a canary half? That way the guaranteed half can be tested with cg_clif and the canary half can be ignored.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yea that sounds reasonable. Will open a PR shortly

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wait hold up, this is all guaranteed. I mistakenly thought there was deduplication due to some constants happening, but it's all static items. there is no deduplication. Instead these are defined as

pub mod inner {
    pub static INNER_MOD_FOO: &'static i32 = &43;
    pub static INNER_MOD_BAR: &'static i32 = INNER_MOD_FOO;
}

where we would expect them to be the same

Copy link
Member

@bjorn3 bjorn3 Mar 18, 2024

Choose a reason for hiding this comment

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

At the MIR level, INNER_MOD_FOO has 43 in promoted MIR, while INNER_MOD_BAR has it in an already evaluated anonymous allocation:

static INNER_MOD_FOO: &i32 = {
    let mut _0: &i32;
    let _1: &i32;
    let _2: i32;
    let mut _3: &i32;

    bb0: {
        StorageLive(_1);
        _3 = const INNER_MOD_FOO::promoted[0];
        _1 = &(*_3);
        _0 = &(*_1);
        StorageDead(_1);
        return;
    }
}

const INNER_MOD_FOO::promoted[0]: &i32 = {
    let mut _0: &i32;
    let mut _1: i32;

    bb0: {
        _1 = const 43_i32;
        _0 = &_1;
        return;
    }
}

static INNER_MOD_BAR: &i32 = {
    let mut _0: &i32;
    let mut _1: &&i32;

    bb0: {
        StorageLive(_1);
        _1 = const {alloc1: &&i32};
        _0 = (*_1);
        StorageDead(_1);
        return;
    }
}

alloc1 (static: INNER_MOD_FOO, size: 8, align: 8) {
    ╾─────alloc3<imm>─────╼                         │ ╾──────╼
}

alloc3 (size: 4, align: 4) {
    2b 00 00 00                                     │ +...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh right, this is using promotion 🤦 Does the test work for you if you change the body of INNER_MOD_FOO to

{
    let x = 43;
    &{x}
}

Copy link
Member

Choose a reason for hiding this comment

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

That will test that specific assertion. For the rest equivalent changes are necessary.

Copy link
Member

Choose a reason for hiding this comment

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

Some tests maybe also should be swapped to mutable references. The test is called "const-mut-refs" after all. ;)

bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 1, 2024
…n-attrs, r=oli-obk

Don't inherit codegen attrs from parent static

Putting this up partly for discussion and partly for review. Specifically, in rust-lang#121644, `@oli-obk` designed a system that creates new static items for representing nested allocations in statics. However, in that PR, oli made it so that these statics inherited the codegen attrs from the parent.

This causes problems such as colliding symbols with `#[export_name]` and ICEs with `#[no_mangle]` since these synthetic statics have no `tcx.item_name(..)`.

So the question is, is there any case where we *do* want to inherit codegen attrs from the parent? The only one that seems a bit suspicious is the thread-local attribute. And there may be some interesting interactions with the coverage attributes as well...

Fixes (after backport) rust-lang#123274. Fixes rust-lang#123243. cc rust-lang#121644.

r? `@oli-obk` cc `@nnethercote` `@RalfJung` (reviewers on that pr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. 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. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Anonymous allocations in statics get duplicated for multiple codegen units
10 participants