Open michalt opened 1 year ago
@llvm/issue-subscribers-backend-risc-v
Author: Michal Terepeta (michalt)
Simpler repro, also happening on armv8: https://godbolt.org/z/5aGa5zYj4 (and, with larger constants, on x86_64 too - https://godbolt.org/z/eva138MGn, which gets especially weird as it actually factors out the adding of 10000000000
but then still spills & reloads the small integer additions to it)
I put up a patch https://github.com/llvm/llvm-project/pull/69983 to make the constant expansion rematerializable to avoid the spills. It's not enabled by default in the patch, but it can enable via command line for testing.
@topperc Cool, thanks! This does get rid of the spills/reloads, but AFAICS the generated code is still not nearly as good as when we avoid the compile-time constant.
add a0, a0, a2
(we just continue to increment the pointer by the offset stored in a2
), so we only need a single instruction.li
& add
combo for each (and each li
is really lui
+ addi
). So the # of instructions is not actually improved much, I think. Would this be something that would be taken care by some subsequent pass? Or is your second suggestion about something similar to StraightLineStrengthReduce
our best bet for approaching the codegen quality when using a runtime value?
We recently ran into an issue with poor scalar RV64 codegen: https://godbolt.org/z/fGjGn7rr1 Interestingly, the issue is not there when the offsets are not compile-time constants.
EDIT: See https://github.com/llvm/llvm-project/issues/69586#issuecomment-1773773510 for a better repro.
The repro is using SiFive's VCIX instructions, because that's how we noticed the issue, but I don't think the VCIX instructions are actually particularly important here. (Although they might just act as "barriers" to other optimizations/transformations, which might contribute to surfacing the actual program?)
@topperc had an initial look at this: