Skip to content

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

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

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in 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
4 participants