Skip to content
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

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

Open
theemathas opened this issue Mar 9, 2025 · 1 comment
Open

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

theemathas opened this issue Mar 9, 2025 · 1 comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. F-optimize_attribute `#![feature(optimize_attribute)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

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
@theemathas theemathas added the C-bug Category: This is a bug. label Mar 9, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Mar 9, 2025
@Noratrieb Noratrieb added A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. F-optimize_attribute `#![feature(optimize_attribute)]` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Mar 9, 2025
@hanna-kruppe
Copy link
Contributor

The LLVM optnone attribute is explicitly documented as:

This function attribute indicates that most optimization passes will skip this function, with the exception of interprocedural optimization passes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-LLVM Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues. C-bug Category: This is a bug. F-optimize_attribute `#![feature(optimize_attribute)]` T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants