Skip to content

LLVM IPSCCP doesn't respect optimize(none) #138272

Open
@theemathas

Description

@theemathas

I compiled this code with -Copt-level=3:

#![feature(optimize_attribute)]

#[unsafe(no_mangle)]
fn foo() -> i32 {
    square(3)
}

#[optimize(none)]
fn square(x: i32) -> i32 {
    x * x
}

I expected foo() to call square(3), and use its return value. Intuitively, since square is marked as optimize(none), the optimizer shouldn't be allowed to look at its contents or optimize it.

Instead, I got this assembly (which doesn't make a ton of sense to me, especially since square seems to also be optimized):

foo:
        push    rax
        call    example::square::h6431aa0b845b3bf9
        mov     eax, 9
        pop     rcx
        ret

example::square::h6431aa0b845b3bf9:
        mov     dword ptr [rsp - 4], 3
        ret

It appears that IPSCCPPass in LLVM is doing this optimization. Godbolt

I'm unsure if this is a bug, since optimize() seems to only be a hint.

CC tracking issue of optimize_attribute: #54882

Meta

Reproduced on Godbolt with full compiler version:

rustc 1.87.0-nightly (efea9896f 2025-03-08)
binary: rustc
commit-hash: efea9896f506baa08f40444e07774e827646d57a
commit-date: 2025-03-08
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0
Internal compiler ID: nightly

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.C-bugCategory: This is a bug.F-optimize_attribute`#![feature(optimize_attribute)]`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