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

[RISCV] Division is costed with a reciprocal throughput of 1

Open bababuck opened this issue 10 months ago • 4 comments

Division is costed as having a reciprocal throughput of 1 on the RISCV backend. Would expect a higher cost for divisions.

./bin/opt -mtriple=riscv64 -mattr=+m,+v -passes=slp-vectorizer t2.ll -debug-only=SLP -S

define void @div_cost(ptr %pl, ptr %ps) {
  %gep_l0 = getelementptr inbounds i64, ptr %pl, i64 0
  %gep_l1 = getelementptr inbounds i64, ptr %pl, i64 1
  %gep_l2 = getelementptr inbounds i64, ptr %pl, i64 2
  %gep_l3 = getelementptr inbounds i64, ptr %pl, i64 3
  %gep_l4 = getelementptr inbounds i64, ptr %pl, i64 4
  %gep_l5 = getelementptr inbounds i64, ptr %pl, i64 5
  %gep_l6 = getelementptr inbounds i64, ptr %pl, i64 6
  %gep_l7 = getelementptr inbounds i64, ptr %pl, i64 7

  %load0  = load i64, ptr %gep_l0 , align 1
  %load1  = load i64, ptr %gep_l1 , align 1
  %load2  = load i64, ptr %gep_l2 , align 1
  %load3  = load i64, ptr %gep_l3 , align 1
  %load4  = load i64, ptr %gep_l4 , align 1
  %load5  = load i64, ptr %gep_l5 , align 1
  %load6  = load i64, ptr %gep_l6 , align 1
  %load7  = load i64, ptr %gep_l7 , align 1

  %div0 = udiv i64 %load0, %load4
  %div1 = udiv i64 %load1, %load5
  %div2 = udiv i64 %load2, %load6
  %div3 = udiv i64 %load3, %load7

  %gep_s0 = getelementptr inbounds i64, ptr %ps, i64 0
  %gep_s1 = getelementptr inbounds i64, ptr %ps, i64 1
  %gep_s2 = getelementptr inbounds i64, ptr %ps, i64 2
  %gep_s3 = getelementptr inbounds i64, ptr %ps, i64 3

  store i64 %div0, ptr %gep_s0, align 1
  store i64 %div1, ptr %gep_s1, align 1
  store i64 %div2, ptr %gep_s2, align 1
  store i64 %div3, ptr %gep_s3, align 1

  ret void
}
Scalars:
    %div0 = udiv i64 %load0, %load4
    %div1 = udiv i64 %load1, %load5
    %div2 = udiv i64 %load2, %load6
    %div3 = udiv i64 %load3, %load7
...
SLP:     VectorCost = 2
SLP:     ScalarCost = 4

bababuck avatar Jan 15 '26 18:01 bababuck

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

Author: Ryan Buchner (bababuck)

Division is costed as having a reciprocal throughput of `1` on the RISCV backend.

./bin/opt -mtriple=riscv64 -mattr=+m,+v -passes=slp-vectorizer t2.ll -debug-only=SLP -S

define void @<!-- -->div_cost(ptr %pl, ptr %ps) {
  %gep_l0 = getelementptr inbounds i64, ptr %pl, i64 0
  %gep_l1 = getelementptr inbounds i64, ptr %pl, i64 1
  %gep_l2 = getelementptr inbounds i64, ptr %pl, i64 2
  %gep_l3 = getelementptr inbounds i64, ptr %pl, i64 3
  %gep_l4 = getelementptr inbounds i64, ptr %pl, i64 4
  %gep_l5 = getelementptr inbounds i64, ptr %pl, i64 5
  %gep_l6 = getelementptr inbounds i64, ptr %pl, i64 6
  %gep_l7 = getelementptr inbounds i64, ptr %pl, i64 7

  %load0  = load i64, ptr %gep_l0 , align 1
  %load1  = load i64, ptr %gep_l1 , align 1
  %load2  = load i64, ptr %gep_l2 , align 1
  %load3  = load i64, ptr %gep_l3 , align 1
  %load4  = load i64, ptr %gep_l4 , align 1
  %load5  = load i64, ptr %gep_l5 , align 1
  %load6  = load i64, ptr %gep_l6 , align 1
  %load7  = load i64, ptr %gep_l7 , align 1

  %div0 = udiv i64 %load0, %load4
  %div1 = udiv i64 %load1, %load5
  %div2 = udiv i64 %load2, %load6
  %div3 = udiv i64 %load3, %load7

  %gep_s0 = getelementptr inbounds i64, ptr %ps, i64 0
  %gep_s1 = getelementptr inbounds i64, ptr %ps, i64 1
  %gep_s2 = getelementptr inbounds i64, ptr %ps, i64 2
  %gep_s3 = getelementptr inbounds i64, ptr %ps, i64 3

  store i64 %div0, ptr %gep_s0, align 1
  store i64 %div1, ptr %gep_s1, align 1
  store i64 %div2, ptr %gep_s2, align 1
  store i64 %div3, ptr %gep_s3, align 1

  ret void
}
Scalars:
    %div0 = udiv i64 %load0, %load4
    %div1 = udiv i64 %load1, %load5
    %div2 = udiv i64 %load2, %load6
    %div3 = udiv i64 %load3, %load7
...
SLP:     VectorCost = 2
SLP:     ScalarCost = 4

llvmbot avatar Jan 15 '26 18:01 llvmbot

Thanks for filing this, the tests in test/Analysis/CostModel/RISC-V/arith-int.ll confirm this too.

It looks like both the scalar and vector costs are costed as 1. Maybe the original thinking was that as long as the scalar and vector costs are the same relative to each other it's fine.

But the throughput costs not being relative to other instructions is going to cause problems. We should probably scale the cost by TCC_Expensive.

lukel97 avatar Jan 16 '26 06:01 lukel97

Yeah, honestly, this feels like an oversight. Most of our existing schedule models seem to indicate scalar div is quite expensive relatively. Going to something like TCC_Expensive for both by default would seem like a reasonable step. We'd have to evaluate to make sure it didn't do something surprising, but I suspect that's moderately low risk.

preames avatar Jan 16 '26 16:01 preames

Linking #170821

bababuck avatar Jan 16 '26 23:01 bababuck