Skip to content

black_box(&x) prevents emitting a constant return value, but it shouldn't #91010

Description

@fee1-dead

Another form of the same issue AFAICT: #86510

On latest nightly, black_box(&x) prevents codegenning a constant return value, but it shouldn't.

This is possibly because of over pessimism about interior mutability, but we should be able to tell whether a type is interior mutable or not.

Godbolt

#![feature(bench_black_box)]

use std::hint::black_box;

pub fn foo() -> i32 {
    let x = 1;
    black_box(&x);
    x
}

The return instruction moves from a location on the stack:

        mov     eax, dword ptr [rsp + 4]

Whereas for this:

#![feature(bench_black_box)]

use std::hint::black_box;

pub fn foo() -> i32 {
    let x = 1;
    let y = x;
    black_box(&y);
    x
}

It optimizes correctly.

        mov     eax, 1

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-codegenArea: Code generationC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-slowIssue: Problems and improvements with respect to performance of generated code.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