dyn closures shouldn't lose range analysis information of the environment #55145
Labels
A-LLVM
Area: Code generation parts specific to LLVM. Both correctness bugs and optimization-related issues.
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
I-slow
Issue: Problems and improvements with respect to performance of generated code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Minimal reproducible example:
currently compiles into:
But given that the
b
is guaranteed to betrue
, I'd expect it to be equivalent to inlining a constant.The only way to propagate such invariants currently seems to be by using the LLVM
assume
intrinsic which is available only on nightly. E.g. for example above:compiles to
which is much closer to the expected result - function is using a constant as it should. However, this still allocates data for a variable even though it's no longer necessary - it has only one value and isn't used in the optimised function body anyway.
Note that the range analysis issue applies specifically to dynamically dispatched closures (which are still necessary sometimes and are used for generic callbacks), however the second part of the issue (unnecessary allocation) applies to statically dispatched generic closures too. For example:
compiles to:
I understand these examples might seem superficial and can be easily optimised by hand, but I hope they do showcase a more generic issue, which also applies to e.g. captures of
enum
inmatch
branches where captured enum is guaranteed to be within the already matched variants, but this information is not properly propagated and lost to the closure, preventing further optimisations.The text was updated successfully, but these errors were encountered: