[RISCV] computeKnownBits - add support for RISCVISD::VECREDUCE_ADD_VL
RISCVTargetLowering::computeKnownBitsForTargetNode doesn't handle
RISCVISD::VECREDUCE_ADD_VL, so known bits are lost once an
ISD::VECREDUCE_ADD is lowered to the RISC-V vector-length (VL) form.
Context: #207631 added generic computeKnownBits support for
ISD::VECREDUCE_ADD. For fixed-length RVV reductions (e.g. the
sum-of-absolute-differences pattern), the reduction lowers to
RISCVISD::VECREDUCE_ADD_VL, which has no known-bits handling — so the
generic bound doesn't survive on that path. A mask that could otherwise
fold away (RISC-V's vmv.x.s already sign-extends the element to XLEN)
is instead materialized as an slli/srli pair. See the diffs in
llvm/test/CodeGen/RISCV/rvv/fixed-vectors-sad.ll.
Adding a computeKnownBitsForTargetNode case for
RISCVISD::VECREDUCE_ADD_VL (and likely the sibling *_VL reduction
nodes) would recover the bound.
Complication: unlike the generic node,
VECREDUCE_ADD_VL operates on scalable vectors and takes the vector
length as a runtime operand, so there's no compile-time element
count to feed KnownBits::reduceAdd(NumElts). Bounding the reduction
without a static element count is the non-trivial part.
Extends #206289.
@llvm/issue-subscribers-backend-risc-v
Author: Sabona Abdi (sabonaoabdi)
Context: #207631 added generic computeKnownBits support for
ISD::VECREDUCE_ADD. For fixed-length RVV reductions (e.g. the
sum-of-absolute-differences pattern), the reduction lowers to
RISCVISD::VECREDUCE_ADD_VL, which has no known-bits handling — so the
generic bound doesn't survive on that path. A mask that could otherwise
fold away (RISC-V's vmv.x.s already sign-extends the element to XLEN)
is instead materialized as an slli/srli pair. See the diffs in
llvm/test/CodeGen/RISCV/rvv/fixed-vectors-sad.ll.
Adding a computeKnownBitsForTargetNode case for
RISCVISD::VECREDUCE_ADD_VL (and likely the sibling *_VL reduction
nodes) would recover the bound.
Complication: unlike the generic node,
VECREDUCE_ADD_VL operates on scalable vectors and takes the vector
length as a runtime operand, so there's no compile-time element
count to feed KnownBits::reduceAdd(NumElts). Bounding the reduction
without a static element count is the non-trivial part.
Extends #206289.
Hi @sabonaoabdi, I'd like to take a shot at this issue, if it works as a good first issue for someone newer to this area. I have some prior experience with LLVM, specifically OpenMP - though I'm completely new to LLVM backend development.
Happy to get up to speed on the relevant components as I go. Could this be assigned to me? Thanks!
Hi @Andarwinux @skadewdl3, apologies for the confusion here. I filed this thinking it was a standalone task, but it’s actually a prerequisite for my in-flight PR (#207631): the known-bits tightening there triggers a regression that needs this VECREDUCE_ADD_VL handling to resolve. Since it’s blocking my own work and the two are tightly coupled, it’d be a lot cleaner for me to take this one rather than coordinate the handoff across PRs.
So sorry for the mixup.
#210493