llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[RISCV] computeKnownBits - add support for RISCVISD::VECREDUCE_ADD_VL

Open sabonaoabdi opened this issue 4 months ago • 4 comments

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.

sabonaoabdi avatar Jul 13 '26 07:07 sabonaoabdi

@llvm/issue-subscribers-backend-risc-v

Author: Sabona Abdi (sabonaoabdi)

`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.

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!

skadewdl3 avatar Jul 13 '26 08:07 skadewdl3

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.

sabonaoabdi avatar Jul 13 '26 08:07 sabonaoabdi

#210493

sabonaoabdi avatar Jul 18 '26 07:07 sabonaoabdi