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

Rust beta/nightly optimises everything away #5782

Closed
mattgodbolt opened this issue Nov 21, 2023 · 9 comments
Closed

Rust beta/nightly optimises everything away #5782

mattgodbolt opened this issue Nov 21, 2023 · 9 comments

Comments

@mattgodbolt
Copy link
Member

Not sure why but:
https://godbolt.org/z/71erEzGT6 optimizes to nothing, but removing the -O generates code as expected.

Not sure if this is a new thing where unused code is deleted that we don't understand?

@mattgodbolt
Copy link
Member Author

Our flags are:

-C debuginfo=1 -o /tmp/compiler-explorer-compiler20231021-7139-1r6ir3a.t9ox/output.s --emit asm -Cllvm-args=--x86-asm-syntax=intel --crate-type rlib --color=always --edition 2021 -O /tmp/compiler-explorer-compiler20231021-7139-1r6ir3a.t9ox/example.rs

and with the filters off we only see:

        .text
        .intel_syntax noprefix
        .file   "example.e701e141d12e9087-cgu.0"
        .section        .debug_aranges,"",@progbits
        .ident  "rustc version 1.75.0-beta.1 (782883f60 2023-11-12)"
        .section        ".note.GNU-stack","",@progbits

so...not sure what's going on here!

@ohsix
Copy link

ohsix commented Nov 21, 2023

if i add -C link-dead-code=yes it stays

setting inline-threshold=0 didn't do it (it's not an inlining thing)

@Nilstrieb
Copy link

As of recently, rustc started to automatically add #[inline] to small functions. The attribute makes it so that functions are only codegened in codegen units that actually need them. rust-lang/rust#116505
I'm not sure what the best solution is here (there is an unstable flag to turn it off, and adding #[no_mangle] to functions is also a simple fix (not really something CE can do though)) @saethlin I forgot what you recommended to the playground and can't find it right now, so asking you :)

@saethlin
Copy link

saethlin commented Nov 21, 2023

Your can pass -Zcross-crate-inline-threshold=never to explicitly turn off the effect of that PR.

But there's no real fix, because rustc internals don't permit one currently. The problem is that rustc computes symbol visibility before doing its own pre-LLVM optimizations, so the visibility needs to be conservative enough so the MIR inliner doesn't make symbols public that weren't before. So "cross-crate-inlinable" means that the function is both lazily lowered (which means the assembly disappears) and the function becomes possible for the MIR inliner to inline.

Adding #[no_mangle] or #[inline(never)] does cause the function to be lowered in the current crate, but causes the same knock-on effect, where calls to that function stop being inlined.

In general my advice is to set -Zcross-crate-inline-threshold=0 or =never. never is a more recent option and prevents cost=0 functions from getting the cross-crate codegen strategy. And then wait for us to fix the inliner visibility issue, it is a bug and we should get to it eventually.

@mattgodbolt mattgodbolt changed the title Rust beta/inghtly optimises everything away Rust beta/nightly optimises everything away Nov 22, 2023
@mattgodbolt
Copy link
Member Author

Thanks everyone!

@mattgodbolt
Copy link
Member Author

I think i'm going to close this as "working as intended" ... it's unfortunate but we can't go putting too many byzantine surprising compiler options into the compiler to work around things the compiler's trying hard to do for us...

@saethlin
Copy link

FWIW, this property has been surprising to a lot of people at this point; it wouldn't hurt to have a note (in the UI? I swear I saw the guidance to add pub on the site long ago) that in addition to making functions you want to see pub that you may need to add #[inline(never)] to them.

@partouf
Copy link
Contributor

partouf commented Nov 22, 2023

Could change the default example https://github.com/compiler-explorer/compiler-explorer/blob/main/examples/rust/default.rs
but it will only show up for new users/new domain/new browser/etc

@Kimundi
Copy link

Kimundi commented Dec 23, 2023

I wonder if it would make sense for CE to provide a small helper macro for Rust code, eg

#[codegen]
fn foo() {}

that expands to just

#[inline(never)]
pub foo() {}

Probably not worth the effort though, as it hard to predict what else might break in the future, and if fixing those case can be done with a attribute

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants