Skip to content

replace the implementation of inline functions #14527

Open

Description

Rust's implementation of #[inline] functions is far from ideal. It's a major contributor to both slow compile times and bloated binaries.

There are an enormous number of #[inline] functions in the standard libraries, and most have far more than one layer of inner #[inline] function calls. The work to convert these functions to LLVM IR, optimize and translate to machine code is duplicated for every single crate. The inline pass is not used at --opt-level=0 and --opt-level=1, so the result is wasted time and duplicated code without any benefits.

It is possible to implement #[inline] functions without duplicating the work of converting to LLVM IR and optimizing. The compiler can also entirely avoid any duplicated function bodies when the optimization level is not high enough for inline, the function is used as a function pointer or it is above the threshold.

Before compiling all of the functions in a library crate, Rust should create an LLVM module with all of the externally reachable inline functions in the crate. It will run the optimization passes on this LLVM module before continuing to compile, and it end up stored as metadata in the rlib or dynamic library in the bytecode format.

The compiler will then continue on with the compilation of the other functions in the crate. The work to generate optimized LLVM IR from the externally reachable #[inline] is already complete and can be reused. These functions will not be marked internal, because other crates will be able to call through to these.

Now, when Rust is compiling another crate, it can start by fetching the LLVM bytecode for the required inline functions. These functions will be marked available_externally and use the original symbol from the source library, so that if inlining does not occur there will be no duplicate code. At --opt-level=0 and --opt-level=1, it can simply generate an external call immediately and ignore the bytecode blob.

It would also be possible to leverage this for instantiations of generic functions, by making the instantiations already done by the library available externally as LLVM bytecode blobs in the metadata.

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

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationArea: Code generationC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchCategory: An issue highlighting optimization opportunities or PRs implementing suchI-compiletimeIssue: Problems and improvements with respect to compile times.Issue: Problems and improvements with respect to compile times.I-slowIssue: Problems and improvements with respect to performance of generated code.Issue: Problems and improvements with respect to performance of generated code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions