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

Post-monomorphization MIR optimizations #66969

Open
Aaron1011 opened this issue Dec 2, 2019 · 4 comments
Open

Post-monomorphization MIR optimizations #66969

Aaron1011 opened this issue Dec 2, 2019 · 4 comments
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

See #44285

Currently, all MIR optimizations are run before we perform monomorphization. This causes us to miss a number of optimization opportunities:

@hanna-kruppe
Copy link
Contributor

As far as I know, all MIR optimizations today and those alluded to here can be performed just as well or better by LLVM and the main reason MIR optimizations are worthwhile nonetheless is because they can be quicker due to not redoing work for every monomorphization.

Doing changes to monomorphized MIR would not just give up that competitive advantage, it also comes with other costs. Currently there's no such thing as monomorphized MIR, codegen just walks the generic MIR and applies substitutions only as needed to generate monomorphic LLVM IR. If a specialized copy of the MIR is created for each monomorphization (which seems necessary to run passes that modify each monomorphization differently), that's extra allocations and copies that may not pay off.

@jonas-schievink jonas-schievink added A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 2, 2019
@JohnTitor JohnTitor added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jan 12, 2020
@bluss
Copy link
Member

bluss commented May 22, 2020

Would it be beneficial to use monomorphized constants to eliminate dead branches and dead code? For example, in the following, skip emitting everything that's used by other_method::<T>.

fn foo<T>() {
    if some_const_boolean_property::<T>() {
        /* do nothing */
    } else {
        other_method::<T>();
    }
}

@bjorn3
Copy link
Member

bjorn3 commented May 22, 2020

@rustbot modify labels: +A-mir-opt

@rustbot rustbot added the A-mir-opt Area: MIR optimizations label May 22, 2020
@tmiasko
Copy link
Contributor

tmiasko commented May 27, 2020

This is already happening when using MIR inliner (mir-opt-level >= 2).

@workingjubilee workingjubilee added the C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such label Oct 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-MIR Area: Mid-level IR (MIR) - https://blog.rust-lang.org/2016/04/19/MIR.html A-mir-opt Area: MIR optimizations C-enhancement Category: An issue proposing an enhancement or a PR with one. C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

9 participants