Unnecessary local variable and memcpy not elided during struct initialization #77955
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.
Here is a case where a local variable isn't optimized away, which introduces an unnecessary copy:
item
is created on the stack, and then copied into theS
temporary. Butitem
is otherwise unused, so the compiler could just populate the temporary directly.Assembly at
-C opt-level=3
:The problem occurs if the array initializer is
1u64
, but not if it's0u64
. With1u64
, the generated IR uses a loop to populate the array, whereas with0u64
, it doesn't. The issue is likely that LLVM is unable to see through the relative complexity of the loop to notice thatitem
can be eliminated.Here is the LLVM-IR before any optimization passes:
If you replace the array with a struct with 17 u64 members, the problem also goes away. Again, this causes the IR to not use a loop to initialize
item
.This was found while investigating #77613. I think the cause is slightly different, though I would guess it's the same general idea--LLVM can't see how to make some optimization through multiple basic blocks.
#45663 is also related, but may or may not have the same root cause.
Reproduces from 1.0.
The text was updated successfully, but these errors were encountered: