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

Generic inline functions are not always inline if generics are shared #103499

Closed
jachris opened this issue Oct 24, 2022 · 2 comments
Closed

Generic inline functions are not always inline if generics are shared #103499

jachris opened this issue Oct 24, 2022 · 2 comments

Comments

@jachris
Copy link
Contributor

jachris commented Oct 24, 2022

Consider the following example.

// crate A
#[inline]
pub fn identity_i32(x: i32) -> i32 {
    x
}

#[inline]
pub fn identity<T>(x: T) -> T {
    x
}

pub fn use_from_a() {
    identity(42);
}
// crate B
fn main() {
    let _ = a::identity(42);
    let _ = a::identity_i32(42);
}

Current behavior

identity::<i32> is codegened only once, in crate A. The LLVM IR of crate B does not have the definition, only a declaration (because it was already instantiated for A). On the other hand, identity_i32 is codegened twice and thus available for LLVM inlining.

Expected behavior

I would have expected that crate A gets its own copy of identity::<i32>. This would match the behavior of non-generic inline functions, and it would also enable more optimizations.


Related: rust-lang/rust-clippy#2253 (the proposed change would make it meaningful to annotate generic functions with #[inline])

I would try to create a PR for this if we can agree that the expected behavior is more reasonable.

@jachris
Copy link
Contributor Author

jachris commented Oct 24, 2022

Actually, this is currently only observable for -O0, -O1, -Os and -Oz, or -Zshare_generics=on with any optimization level. Unless -Zshare_generics=on becomes the default in the future, we therefore don't have to be too concerned with missed optimizations. However, with the given optimization levels, there still is a mismatch between inline non-generic and inline generic functions.

@jachris jachris changed the title Generic inline functions are not always inline Generic inline functions are not always inline if generics are shared Oct 25, 2022
@jachris
Copy link
Contributor Author

jachris commented Oct 25, 2022

I'm closing this, since it is more or less #102539 (which I somehow didn't see before).

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

No branches or pull requests

1 participant