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

SLP costs broadcast shuffles that are folded

Open lukel97 opened this issue 4 months ago • 10 comments

RISC-V has vfoo.vx instructions , which splat a scalar in the second operand. The SLP cost model doesn't account for this though and we end up overcosting by an insertelement + broadcast shuffle:

; RUN: opt -p slp-vectorizer -S -o - scratch/slp-splat-cost.ll -mtriple riscv64 -mattr=+v -debug 2<&1 | FileCheck %s

define void @fmul_splat(i32 %s, ptr %p, ptr %q) {
  %x0 = load i32, ptr %p
  %p1 = getelementptr inbounds nuw i32, ptr %p, i64 1
  %x1 = load i32, ptr %p1
  %p2 = getelementptr inbounds nuw i32, ptr %p, i64 2
  %x2 = load i32, ptr %p2
  %p3 = getelementptr inbounds nuw i32, ptr %p, i64 3
  %x3 = load i32, ptr %p3
  %m0 = add i32 %s, %x0
  %m1 = add i32 %s, %x1
  %m2 = add i32 %s, %x2
  %m3 = add i32 %s, %x3
  store i32 %m0, ptr %q
  %q1 = getelementptr i32, ptr %q, i64 1
  store i32 %m1, ptr %q1
  %q2 = getelementptr i32, ptr %q, i64 2
  store i32 %m2, ptr %q2
  %q3 = getelementptr i32, ptr %q, i64 3
  store i32 %m3, ptr %q3
  ret void
}
SLP: Calculated costs for Tree:
1.
Operand 0:
  i32 %s
  i32 %s
  i32 %s
  i32 %s
Operand 1:
    %x0 = load i32, ptr %p, align 4
    %x1 = load i32, ptr %p1, align 4
    %x2 = load i32, ptr %p2, align 4
    %x3 = load i32, ptr %p3, align 4
Scalars: 
    %m0 = add i32 %s, %x0
    %m1 = add i32 %s, %x1
    %m2 = add i32 %s, %x2
    %m3 = add i32 %s, %x3
State: Vectorize
MainOp:   %m0 = add i32 %s, %x0
AltOp:   %m0 = add i32 %s, %x0
VectorizedValue: NULL
ReuseShuffleIndices: Empty
ReorderIndices: 
UserTreeIndex: {User:0 EdgeIdx:0}
SLP: Costs:
SLP:     ReuseShuffleCost = 0
SLP:     VectorCost = 1
SLP:     ScalarCost = 4
SLP:     ReuseShuffleCost + VecCost - ScalarCost = -3
SLP: Adding cost -3 for bundle Idx: 1, n=4 [  %m0 = add i32 %s, %x0, ..].
SLP: Current total cost = -6
SLP: Adding cost 2 for bundle Idx: 2, n=4 [i32 %s, ..]. <------ # THIS SHOULD BE FREE
SLP: Current total cost = -4

I believe this overcosting is contributing to the performance regressions we're seeing with SLP whenever it's moved after unrolling, which causes it to stop vectorizing, see https://github.com/llvm/llvm-project/pull/135318#issuecomment-5099702573

I'm not sure what the correct TTI API should look like. We have canSplatOperand(unsigned Opcode, int Operand) in RISCVTTIImpl but it's not a generic TTI hook.

cc @bababuck @alexey-bataev

lukel97 avatar Jul 28 '26 06:07 lukel97

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

Author: Luke Lau (lukel97)

RISC-V has `vfoo.vx` instructions , which splat a scalar in the second operand. The SLP cost model doesn't account for this though and we end up overcosting by an insertelement + broadcast shuffle:
; RUN: opt -p slp-vectorizer -S -o - scratch/slp-splat-cost.ll -mtriple riscv64 -mattr=+v -debug 2&lt;&amp;1 | FileCheck %s

define void @<!-- -->fmul_splat(i32 %s, ptr %p, ptr %q) {
  %x0 = load i32, ptr %p
  %p1 = getelementptr inbounds nuw i32, ptr %p, i64 1
  %x1 = load i32, ptr %p1
  %p2 = getelementptr inbounds nuw i32, ptr %p, i64 2
  %x2 = load i32, ptr %p2
  %p3 = getelementptr inbounds nuw i32, ptr %p, i64 3
  %x3 = load i32, ptr %p3
  %m0 = add i32 %s, %x0
  %m1 = add i32 %s, %x1
  %m2 = add i32 %s, %x2
  %m3 = add i32 %s, %x3
  store i32 %m0, ptr %q
  %q1 = getelementptr i32, ptr %q, i64 1
  store i32 %m1, ptr %q1
  %q2 = getelementptr i32, ptr %q, i64 2
  store i32 %m2, ptr %q2
  %q3 = getelementptr i32, ptr %q, i64 3
  store i32 %m3, ptr %q3
  ret void
}
SLP: Calculated costs for Tree:
1.
Operand 0:
  i32 %s
  i32 %s
  i32 %s
  i32 %s
Operand 1:
    %x0 = load i32, ptr %p, align 4
    %x1 = load i32, ptr %p1, align 4
    %x2 = load i32, ptr %p2, align 4
    %x3 = load i32, ptr %p3, align 4
Scalars: 
    %m0 = add i32 %s, %x0
    %m1 = add i32 %s, %x1
    %m2 = add i32 %s, %x2
    %m3 = add i32 %s, %x3
State: Vectorize
MainOp:   %m0 = add i32 %s, %x0
AltOp:   %m0 = add i32 %s, %x0
VectorizedValue: NULL
ReuseShuffleIndices: Empty
ReorderIndices: 
UserTreeIndex: {User:0 EdgeIdx:0}
SLP: Costs:
SLP:     ReuseShuffleCost = 0
SLP:     VectorCost = 1
SLP:     ScalarCost = 4
SLP:     ReuseShuffleCost + VecCost - ScalarCost = -3
SLP: Adding cost -3 for bundle Idx: 1, n=4 [  %m0 = add i32 %s, %x0, ..].
SLP: Current total cost = -6
SLP: Adding cost 2 for bundle Idx: 2, n=4 [i32 %s, ..]. &lt;------ # THIS SHOULD BE FREE
SLP: Current total cost = -4

I believe this overcosting is contributing to the performance regressions we're seeing with SLP whenever it's moved after unrolling, which causes it to stop vectorizing, see https://github.com/llvm/llvm-project/pull/135318#issuecomment-5099702573

I'm not sure what the correct TTI API should look like. We have canSplatOperand(unsigned Opcode, int Operand) in RISCVTTIImpl but it's not a generic TTI hook.

cc @bababuck @alexey-bataev

The SLP cost model doesn't account for this though and we end up overcosting by an insertelement + broadcast shuffle:

If we're costing this as an insertelement + broadcast shuffle that would still be overcosting for cases when we can't splat the operand since we would still just use a vmv.v.x instruction.

bababuck avatar Jul 28 '26 07:07 bababuck

I'm not sure what the correct TTI API should look like. We have canSplatOperand(unsigned Opcode, int Operand) in RISCVTTIImpl but it's not a generic TTI hook.

I would think something along the lines of canSplatOperand would suffice. When we go to cost a splat tree entry, we could check it's user TreeEntry to get the opcode for calling canSplatOperand. Though this doesn't quite fit cleanly into the SLP costing since that works on a TreeEntry at a time.

bababuck avatar Jul 28 '26 07:07 bababuck

I'm not sure what the correct TTI API should look like. We have canSplatOperand(unsigned Opcode, int Operand) in RISCVTTIImpl but it's not a generic TTI hook.

I would think something along the lines of canSplatOperand would suffice. When we go to cost a splat tree entry, we could check it's user TreeEntry to get the opcode for calling canSplatOperand. Though this doesn't quite fit cleanly into the SLP costing since that works on a TreeEntry at a time.

I guess you could have a canSplatOperand that you would call on the users of the splat, and then if all users are splattable then you could plumb through a TTI::VectorInstrContext hint to getShuffleCost and getVectorInstrCost that says all the users are splattable. The RISC-V hooks would then return TCC_Free.

lukel97 avatar Jul 28 '26 07:07 lukel97

I'm not sure what the correct TTI API should look like. We have canSplatOperand(unsigned Opcode, int Operand) in RISCVTTIImpl but it's not a generic TTI hook.

I would think something along the lines of canSplatOperand would suffice. When we go to cost a splat tree entry, we could check it's user TreeEntry to get the opcode for calling canSplatOperand. Though this doesn't quite fit cleanly into the SLP costing since that works on a TreeEntry at a time.

I guess you could have a canSplatOperand that you would call on the users of the splat, and then if all users are splattable then you could plumb through a TTI::VectorInstrContext hint to getShuffleCost and getVectorInstrCost that says all the users are splattable. The RISC-V hooks would then return TCC_Free.

Extending VectorInstrContext and adding it to getShuffleCost/getVectorInstCost looks like the right approach

alexey-bataev avatar Jul 28 '26 12:07 alexey-bataev

@lukel97 if you don't have the bandwidth at the moment I can knock this out just lmk,

bababuck avatar Jul 28 '26 15:07 bababuck

@lukel97 if you don't have the bandwidth at the moment I can knock this out just lmk,

Not only do I not have the bandwidth, I also don't have the SLP-know-how unlike yourself, so if you want to take it on that would be great!

lukel97 avatar Jul 28 '26 15:07 lukel97

Are you sure about this? We have existing operand OperandValueKind information which does include the splat/broadcast on various operands. I believe we use that in cost callbacks.

Edit for better phrasing - I'm not saying your wrong about the problem, just that this may be a simple oversight in SLP not passing the right OperandValueKind to an existing callback. The SLP code is sufficiently scattered we could easily have missed a call site or two.

preames avatar Aug 18 '26 03:08 preames

Are you sure about this? We have existing operand OperandValueKind information which does include the splat/broadcast on various operands. I believe we use that in cost callbacks.

Edit for better phrasing - I'm not saying your wrong about the problem, just that this may be a simple oversight in SLP not passing the right OperandValueKind to an existing callback. The SLP code is sufficiently scattered we could easily have missed a call site or two.

I think the issue with in the SLP is that each node is costed individually, so when we go to cost the gather (splat) node no information is currently considered about which instruction(s) might use it. If I'm understanding correctly, we're overcosting the splat operation, not the binary instruction with the splat operand, which is why using OperandValueKind would not work.

The additional challenge is that a single SLP gather node might be used as an operand to multiple different nodes, only some of which may have .vx variants.

bababuck avatar Aug 18 '26 19:08 bababuck

If I'm understanding correctly, we're overcosting the splat operation, not the binary instruction with the splat operand, which is why using OperandValueKind would not work.

I think that's also my understanding too. But it's a bit confusing because sometimes OperandValueKind is used to compute the cost of its operands, but from my undersstanding:

  • OperandValueKind in getArithmeticInstrCost is used to compute the cost of materializing a constant vector
  • getArithmeticInstrCost does NOT include the cost of splatting a non-constant uniform vector
  • A non-constant uniform vector operand needs a dedicated shufflevector + insertelement so it's costed via getShuffleInstrCost + getVectorInstrCost.

This issue serves to fill in the gap that the shufflevector + insertelement is folded away on RISC-V with vadd.vx. vadd.vi doesn't have this problem because we can use OperandValueKind, this is the relevant code from RISC-V's TTI:

 auto getConstantMatCost =
      [&](unsigned Operand, TTI::OperandValueInfo OpInfo) -> InstructionCost {
    if (OpInfo.isUniform() && canSplatOperand(Opcode, Operand))
      // Two sub-cases:
      // * Has a 5 bit immediate operand which can be splatted.
      // * Has a larger immediate which must be materialized in scalar register
      // We return 0 for both as we currently ignore the cost of materializing
      // scalar constants in GPRs.
      return 0;

    return getConstantPoolLoadCost(Ty, CostKind);
  };

lukel97 avatar Aug 27 '26 12:08 lukel97