Skip to content

gvn: bail out unavoidable non-ssa locals in repeat #141252

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
May 28, 2025

Conversation

dianqk
Copy link
Member

@dianqk dianqk commented May 19, 2025

Fixes #141251.

We cannot transform *elem to array[idx1] in the following code, as idx1 has already been modified.

    mir! {
        let array;
        let elem;
        {
            array = [*val; 5];
            elem = &array[idx1];
            idx1 = idx2;
            RET = *elem;
            Return()
        }
    }

Perhaps I could transform it to array[0], but I prefer the conservative approach.

r? mir-opt

We cannot transform `*elem` to `array[idx1]` in the following code,
as `idx1` has already been modified.

```rust
    mir! {
        let array;
        let elem;
        {
            array = [*val; 5];
            elem = &array[idx1];
            idx1 = idx2;
            RET = *elem;
            Return()
        }
    }
```
@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 May 19, 2025
_4 = [copy (*_3); 5];
_5 = &_4[_1];
_1 = copy _2;
_0 = copy (*_5);
Copy link
Member Author

Choose a reason for hiding this comment

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

@saethlin
Copy link
Member

Your change does work, but I feel like we have a systematic problem that this patch doesn't fix. Isn't GVN supposed to notice that idx1 is not an SSA local then avoid doing optimizations involving it?

@@ -682,6 +683,7 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
ProjectionElem::Index(idx) => {
if let Value::Repeat(inner, _) = self.get(value) {
*from_non_ssa_index |= self.locals[idx].is_none();
return Some(*inner);
}
let idx = self.locals[idx]?;
Copy link
Member Author

Choose a reason for hiding this comment

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

@saethlin Yes, right here. This is an exception for the repeat array, because whichever value we access, it's all the same. However, we have overlooked the out-of-bounds case here.

@saethlin
Copy link
Member

Sorry it took me a while to convince myself this is a good strategy for fixing this. I now agree.

@bors r+

@bors
Copy link
Collaborator

bors commented May 27, 2025

📌 Commit be5d6c5 has been approved by saethlin

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 May 27, 2025
bors added a commit that referenced this pull request May 28, 2025
Rollup of 8 pull requests

Successful merges:

 - #140367 (add `asm_cfg`: `#[cfg(...)]` within `asm!`)
 - #140894 (Make check-cfg diagnostics work in `#[doc(cfg(..))]`)
 - #141252 (gvn: bail out unavoidable non-ssa locals in repeat)
 - #141517 (rustdoc: use descriptive tooltip if doctest is conditionally ignored)
 - #141551 (Make two transmute-related MIR lints into HIR lint)
 - #141591 (ci: fix llvm test coverage)
 - #141647 (Bump master `stage0` compiler)
 - #141659 (Add `Result::map_or_default` and `Option::map_or_default`)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit ee4efa1 into rust-lang:master May 28, 2025
6 checks passed
@rustbot rustbot added this to the 1.89.0 milestone May 28, 2025
rust-timer added a commit that referenced this pull request May 28, 2025
Rollup merge of #141252 - dianqk:gvn-repeat-index, r=saethlin

gvn: bail out unavoidable non-ssa locals in repeat

Fixes #141251.

We cannot transform `*elem` to `array[idx1]` in the following code, as `idx1` has already been modified.

```rust
    mir! {
        let array;
        let elem;
        {
            array = [*val; 5];
            elem = &array[idx1];
            idx1 = idx2;
            RET = *elem;
            Return()
        }
    }
```

Perhaps I could transform it to `array[0]`, but I prefer the conservative approach.

r? mir-opt
@dianqk dianqk deleted the gvn-repeat-index branch May 28, 2025 04:56
github-actions bot pushed a commit to rust-lang/miri that referenced this pull request May 28, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang/rust#140367 (add `asm_cfg`: `#[cfg(...)]` within `asm!`)
 - rust-lang/rust#140894 (Make check-cfg diagnostics work in `#[doc(cfg(..))]`)
 - rust-lang/rust#141252 (gvn: bail out unavoidable non-ssa locals in repeat)
 - rust-lang/rust#141517 (rustdoc: use descriptive tooltip if doctest is conditionally ignored)
 - rust-lang/rust#141551 (Make two transmute-related MIR lints into HIR lint)
 - rust-lang/rust#141591 (ci: fix llvm test coverage)
 - rust-lang/rust#141647 (Bump master `stage0` compiler)
 - rust-lang/rust#141659 (Add `Result::map_or_default` and `Option::map_or_default`)

r? `@ghost`
`@rustbot` modify labels: rollup
github-actions bot pushed a commit to model-checking/verify-rust-std that referenced this pull request May 30, 2025
Rollup of 8 pull requests

Successful merges:

 - rust-lang#140367 (add `asm_cfg`: `#[cfg(...)]` within `asm!`)
 - rust-lang#140894 (Make check-cfg diagnostics work in `#[doc(cfg(..))]`)
 - rust-lang#141252 (gvn: bail out unavoidable non-ssa locals in repeat)
 - rust-lang#141517 (rustdoc: use descriptive tooltip if doctest is conditionally ignored)
 - rust-lang#141551 (Make two transmute-related MIR lints into HIR lint)
 - rust-lang#141591 (ci: fix llvm test coverage)
 - rust-lang#141647 (Bump master `stage0` compiler)
 - rust-lang#141659 (Add `Result::map_or_default` and `Option::map_or_default`)

r? `@ghost`
`@rustbot` modify labels: rollup
@Mark-Simulacrum
Copy link
Member

@rust-timer build 11adb2b

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (11adb2b): comparison URL.

Overall result: ❌ regressions - no action needed

Benchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf.

@bors rollup=never
@rustbot label: -S-waiting-on-perf -perf-regression

Instruction count

This is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.

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

Max RSS (memory usage)

Results (primary 6.1%, secondary -0.5%)

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)
6.1% [6.1%, 6.1%] 1
Regressions ❌
(secondary)
3.7% [3.7%, 3.7%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-4.7% [-4.7%, -4.7%] 1
All ❌✅ (primary) 6.1% [6.1%, 6.1%] 1

Cycles

This benchmark run did not return any relevant results for this metric.

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 779.377s -> 780.041s (0.09%)
Artifact size: 366.47 MiB -> 366.35 MiB (-0.03%)

# for free to join this conversation on GitHub. Already have an account? # 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.

GVN makes an incorrect index access
6 participants