Open
Description
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