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

#[optimize(none)] should imply #[inline(never)] #136329

Open
theemathas opened this issue Jan 31, 2025 · 2 comments · May be fixed by #136358
Open

#[optimize(none)] should imply #[inline(never)] #136329

theemathas opened this issue Jan 31, 2025 · 2 comments · May be fixed by #136358
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@theemathas
Copy link
Contributor

theemathas commented Jan 31, 2025

I compiled this code with -C opt-level=3:

#![feature(optimize_attribute)]

#[optimize(none)]
pub fn foo() {
    let _x = 123;
}

#[no_mangle]
pub fn bar() {
    foo();
}

I expected the assembly to either contain the number 123 somewhere, or produce a warning or error. Instead, I got this assembly:

bar:
        ret

Godbolt link

Adding #[inline(never)] to foo() gives the expected behavior. Godbolt link

Assembly after adding `#[inline(never)]`
example::foo::h70a9b33d354cf334:
        mov     dword ptr [rsp - 4], 123
        ret

bar:
        jmp     qword ptr [rip + example::foo::h70a9b33d354cf334@GOTPCREL]

The #[optimize(none)] attribute was recently added in #128657. And according to the discussion in the tracking issue at #54882, it seems like the main motivation of #[optimize(none)] is for debugging.

In this case, it seems like the compiler is inlining foo() into bar() and then proceeding to optimize bar(). This seems counterproductive for debugging. Therefore, I think that #[optimize(none)] should imply #[inline(never)]. Or if that is "too magic", then the compiler should at least produce a warning or error.

Meta

Reproduces on godbolt with rustc 1.86.0-nightly (ae5de6c75 2025-01-29)

@theemathas theemathas added the C-bug Category: This is a bug. label Jan 31, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 31, 2025
@clubby789
Copy link
Contributor

cc #128657

Looks like it's being inlined in MIR

@clubby789 clubby789 linked a pull request Jan 31, 2025 that will close this issue
@hanna-kruppe
Copy link
Contributor

hanna-kruppe commented Jan 31, 2025

This is surprisingly subtle. On the one hand, in LLVM IR every function marked optnone must be marked noinline. So it might make sense for the MIR inliner to behave the same. On the other hand, LLVM's LangRef describes optnone like this:

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

And IPO passes can lead to inlining-equivalent effects, as discussed (for example) in #73739. So I don't know if the proposed fix is actually a reliable way to achieve "call to optimize(none) function is never eliminated" or something like that. Such calls might still get eliminated on the basis of IPO even if this specific call currently isn't eliminated with noinline. @nikic mentioned a hypothetical LLVM IR-level noipa attribute in #73739 but as far as I know this still doesn't exist.

workingjubilee added a commit to workingjubilee/rustc that referenced this issue Jan 31, 2025
…thlin

`#[optimize(none)]` implies `#[inline(never)]`

Fixes rust-lang#136329
@saethlin saethlin added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 1, 2025
jhpratt added a commit to jhpratt/rust that referenced this issue Feb 1, 2025
…thlin

`#[optimize(none)]` implies `#[inline(never)]`

Fixes rust-lang#136329
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 1, 2025
…thlin

`#[optimize(none)]` implies `#[inline(never)]`

Fixes rust-lang#136329
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 1, 2025
…thlin

`#[optimize(none)]` implies `#[inline(never)]`

Fixes rust-lang#136329
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 1, 2025
…thlin

`#[optimize(none)]` implies `#[inline(never)]`

Fixes rust-lang#136329
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants