Repository navigation
Open
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-trait-systemArea: Trait systemArea: Trait systemC-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 suchT-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.
Description
When doing LTO, LLVM supports a devirtualization pass that converts some indirect calls to static, inlineable calls. This can unlock a lot of optimizations when dynamic dispatch is used frequently in a program.
I don't know how hard this would be to support in Rust. Clang has a flag -fwhole-program-vtables which we would need to emulate. From there it seems like it might "just work". I'm opening this issue to start collecting more information.
I also found #11915 which mentions this, but not sure if any of the information there is still accurate.
Reacted by Hudson Ayers, Alistair Francis, jhwgh1968, Esper Thomson, Squirrel, Daniel Liu, Petr Portnov | PROgrm_JARvis, Ming-Long Huang, Matt Johnston, Bryan Hitchcock and 11 more
Metadata
Metadata
Assignees
Labels
A-LLVMArea: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.A-trait-systemArea: Trait systemArea: Trait systemC-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 suchT-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.
Activity
sanxiyn commentedon Feb 3, 2020
In LLVM terms, this means emitting type metadata and using
llvm.type.test.flip1995 commentedon Jun 10, 2021
I'd be interested to look into this issue. Since I'm not that familiar with this part of the compiler (but want to get more familiar with it) I first wanted to ask what would be involved in fixing this.
AFAICT to fix this, a flag would have to be added to the compiler and if that flag is set, (some) types would have to be marked with the
!vcall_visibilitymetadata when lowering to LLVM IR. We should be able to query the visibility of items with theTyCtxt::visibilityquery which we would then use to convert to the corresponding!vcall_visibilitylike this:rustc_middle::ty::Visibility!vcall_visibility@tmandry could you give me some pointers where to start with this issue? Do you have an estimate how much work would be involved here?
bjorn3 commentedon Jun 10, 2021
This is not correct. A trait marked as invisible or restricted can still used by a foreign codegen unit or a foreign shared library. For example if it is used by a public generic or
#[inline]function. It can also be implemented for downstream types if there is a blanket implementation.flip1995 commentedon Jun 10, 2021
Ah right of course, so it has to depend on the visibility of the trait and its implementors. So if the trait is public, or if there are any generics involved or blanket impls exist, than it should be marked as
!vcall_visibility=0 (pub). But if the trait is not public and only specific implementations exist and the types of those specific impls are restricted,translation unitorlinkage unitcould be chosen.Aaron1011 commentedon Dec 14, 2021
It looks like we're already getting some form of this for free. The following code:
results in the following optimized LLVM IR for
use_it:bjorn3 commentedon Dec 15, 2021
This only happens when
use_itisn't public. Notice how the llvm ir foruse_itdoesn't have any arguments. I think LLVM propagates the argument intouse_itas it is constant, sees a load of a function pointer from an immutable vtable, replaces the load with a constant representing the actual function pointer and then turns the indirect call to the function pointer into a regular direct call.hudson-ayers commentedon Dec 15, 2021
Using the code you shared, I still see a vtable in the final binary: https://rust.godbolt.org/z/qzb6o9MKG . Were you using a different version of Rust, or different set of compilation flags to achieve this?
flip1995 commentedon Dec 15, 2021
FYI, I have a semi-working version here: https://github.com/flip1995/rust/tree/pk-vfe
It seems to still remove a bit too many functions or possibly does something wrong when converting the
type.checked.loadintrinsics back to normal loads. I'm currently investigating this.If the above example is compiled as a
crate-type=lib, then the my_method doesn't get removed. Godbolt compiles everything as a lib AFAIK. I guess as a binary, LLVM just get's rid of the whole vtable of the crate, since it can determine all the types and can get rid of dynamic dispatch and with that can remove the vtable of Foo entirely. (A short investigation revealed that this is most likely done by theGlobalOptPassin LLVM:Aaron1011 commentedon Dec 15, 2021
@hudson-ayers I didn't scroll down far enough in the playground output. While the function is no longer making a call through the vtable, the vtable itself is still present in the binary, as you noticed.
3 remaining items
flip1995 commentedon Jun 15, 2022
This shouldn't have been closed, because it isn't quite finished yet. IIRC (it has been some time since I actively worked on this) it only enables VFE, not WPD. Though WPD is enabled by default (for LTO at least) and VFE is only an extension of the WPD optimization.
flip1995 commentedon Jun 15, 2022
For what is missing, see https://github.com/flip1995/rust/blob/195f2082002c9db456e0fde8c1d5db79929ae293/src/doc/unstable-book/src/compiler-flags/virtual-function-elimination.md and soon the unstable book.
Ragarnoy commentedon May 2, 2024
Is this dead in the water then?
flip1995 commentedon May 2, 2024
I don't think anyone is working on this right now. But in theory it should be possible to enable this optimization in
rustc. What's missing is an analysis, that correctly determines the visibility of thevtablein the documented example. This is easier said then done though.Rollup merge of rust-lang#164290 - folkertdev:vfe-load-fix, r=saethlin
Rollup merge of rust-lang#164290 - folkertdev:vfe-load-fix, r=saethlin
Rollup merge of rust-lang#164290 - folkertdev:vfe-load-fix, r=saethlin
Unrolled build for #164290 in rollup 165243
Rollup merge of rust-lang#165570 - folkertdev:vfe-lto-error-message, …
Unrolled build for #165570 in rollup 165582
Rollup merge of rust-lang#165475 - folkertdev:vfe-upcast, r=WaffleLapkin
Rollup merge of rust-lang#165475 - folkertdev:vfe-upcast, r=WaffleLapkin
Rollup merge of rust-lang#165475 - folkertdev:vfe-upcast, r=WaffleLapkin