[llvm] [RISCV] Use slideup to lower build_vector when all operand are (extract_element X, 0) (PR #154450)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 2 09:31:40 PDT 2025


================
@@ -4513,41 +4513,104 @@ static SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG,
 
   const unsigned Policy = RISCVVType::TAIL_AGNOSTIC | RISCVVType::MASK_AGNOSTIC;
 
+  // General case: splat the first operand and slide other operands down one
+  // by one to form a vector. Alternatively, if every operand is an
+  // extraction from element 0 of a vector, we use that vector from the last
+  // extraction as the start value and slide up instead of slide down. Such that
+  // (1) we can avoid the initial splat (2) we can turn those vslide1up into
+  // vslideup of 1 later and eliminate the vector to scalar movement, which is
+  // something we cannot do with vslide1down/vslidedown.
+  // Of course, using vslide1up/vslideup might increase the register pressure,
+  // and that's why we conservatively limit to cases where every operands is an
+  // extraction from first element.
+  SmallVector<SDValue> Operands(Op->op_begin(), Op->op_end());
----------------
mshockwave wrote:

Good question and I guess it's related to your `llvm::reverse` comment earlier: we can keep everything iterator (ranges) if we can write the following code:
```
for (SDValue V : (IsSlideUp ? llvm::reverse(Op->op_values()) : Op->op_values()))
```
but unfortunately I don't think that would be possible without some iterator type adaption, given `llvm::reverse` having a different type than `op_values()`.

https://github.com/llvm/llvm-project/pull/154450


More information about the llvm-commits mailing list