Skip to content

GVN makes some post-monomorphisation errors optimisation-dependent #156836

Description

@narpfel

Code

I tried this code (https://rust.godbolt.org/z/EdW33qf15):

use std::marker::PhantomData;

pub fn f<T>() -> bool {
    struct Panic<T>(PhantomData<T>);

    impl<T> Panic<T> {
        const PANIC: bool = panic!();
    }

    Panic::<T>::PANIC
}

I expected to see this happen: I expect the panic to be a post-monomorphisation error, i. e. compilation should succeed because f is never monomorphised. This should be independent of the optimisation level.

Instead, this happened: This errors with -C opt-level=3, but not with -C opt-level=0.

This seems to be caused by a change in the GVN MIR opt between 1.74 and 1.75; the error can be reproduced by -Z mir-enable-passes=+GVN on 1.75 (but not 1.74).

I originally noticed this with an inline const block, which shows the same change between 1.74 and 1.75 with GVN enabled (https://rust.godbolt.org/z/xxGTjjvYn):

pub fn f<T>() -> bool {
    const { panic!() }
}

Version it worked on

It most recently worked on: Rust 1.76 (Rust 1.74 with -Z mir-enable-passes=+GVN)

Version with regression

It regressed in Rust 1.75 with -Z mir-enable-passes=+GVN.

GVN was enabled by default for 1.77 for opt-level != 0:
a03c972#diff-6202eda9e5f3c8b8a291cf165920ea8484c372002e485eafa920b319f7cb4dd3L112-R112

rustc --version --verbose:

rustc 1.77.0 (aedd173a2 2024-03-17)
binary: rustc
commit-hash: aedd173a2c086e558c2b66d3743b344f977621a7
commit-date: 2024-03-17
host: x86_64-unknown-linux-gnu
release: 1.77.0
LLVM version: 17.0.6

Backtrace

Backtrace

<backtrace>

Metadata

Metadata

Assignees

Labels

A-const-evalArea: Constant evaluation, covers all const contexts (static, const fn, ...)A-mir-optArea: MIR optimizationsA-mir-opt-GVNArea: MIR opt Global Value Numbering (GVN)C-bugCategory: This is a bug.I-prioritizeIssue needs a team member to assess the impact. Will be replaced by P-{low,medium,high,critical}T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-untriagedUntriaged performance or correctness regression.

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions