[llvm] [AArch64] Implement latency costs for loads (PR #201567)

John Brawn via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 19 07:16:59 PDT 2026


================
@@ -5115,7 +5117,9 @@ InstructionCost AArch64TTIImpl::getMemoryOpCost(unsigned Opcode, Type *Ty,
     const MCSchedModel &Sched = ST->getSchedModel();
     const TargetInstrInfo *TII = ST->getInstrInfo();
     unsigned SchedClass = TII->get(Inst).getSchedClass();
-    return (LT.first - 1) + Sched.computeInstrLatency(*ST, SchedClass);
+    const MCSchedClassDesc *SCD = Sched.getSchedClassDesc(SchedClass);
+    return (LT.first - 1) * Sched.getReciprocalThroughput(*ST, *SCD) +
+           Sched.computeInstrLatency(*ST, *SCD);
----------------
john-brawn-arm wrote:

I don't think so? The later loads don't depend on the earlier loads, so they don't have to wait for them to complete before being issued.

e.g. looking at
```
define <4 x i32> @fn2(ptr %ptr, <4 x i32> %arg) {
  %val = load <16 x i32>, ptr %ptr
  %val_0 = shufflevector <16 x i32> %val, <16 x i32> poison, <4 x i32> <i32 0, i32 1, i32 2, i32 3>
  %val_1 = shufflevector <16 x i32> %val, <16 x i32> poison, <4 x i32> <i32 4, i32 5, i32 6, i32 7>
  %val_2 = shufflevector <16 x i32> %val, <16 x i32> poison, <4 x i32> <i32 8, i32 9, i32 10, i32 11>
  %val_3 = shufflevector <16 x i32> %val, <16 x i32> poison, <4 x i32> <i32 12, i32 13, i32 14, i32 15>
  %add1 = add <4 x i32> %val_3, %arg
  %add2 = add <4 x i32> %val_2, %add1
  %add3 = add <4 x i32> %val_1, %add2
  %add4 = add <4 x i32> %val_0, %add3
  ret <4 x i32> %add4
}
```
with ``llc -mtriple=aarch64-none-elf -O0`` (using O0 to prevent ldp from being generated which complicates things) the code generated is
```
        ldr     q0, [x0]
        ldr     q1, [x0, #16]
        ldr     q2, [x0, #32]
        ldr     q3, [x0, #48]
        add     v3.4s, v3.4s, v4.4s
        add     v2.4s, v2.4s, v3.4s
        add     v1.4s, v1.4s, v2.4s
        add     v0.4s, v0.4s, v1.4s
```
looking at this with ``llvm-mca -mtriple=aarch64-none-elf -mcpu=neoverse-v2 --timeline`` it reports the ldr as having latency 6 and reciprocal throughput 1/3, and looking at the timeline view it has
```
[0,0]     DeeeeeeER .    .    .    .   .   ldr  q0, [x0]
[0,1]     DeeeeeeER .    .    .    .   .   ldr  q1, [x0, #16]
[0,2]     DeeeeeeER .    .    .    .   .   ldr  q2, [x0, #32]
[0,3]     D=eeeeeeER.    .    .    .   .   ldr  q3, [x0, #48]
[0,4]     D=======eeER   .    .    .   .   add  v3.4s, v3.4s, v4.4s
[0,5]     D=========eeER .    .    .   .   add  v2.4s, v2.4s, v3.4s
[0,6]     .D==========eeER    .    .   .   add  v1.4s, v1.4s, v2.4s
[0,7]     .D============eeER  .    .   .   add  v0.4s, v0.4s, v1.4s
```
the number of cycles that the first add stalls for (the number of "=" entries) is 7, meaning the latency of the whole 16xi32 load is 7.

Though actually here the costmodel says the latency is 6, due to type conversion meaning the reciprocal throughput of 1/3 is converted to InstructionCost (i.e. integer) value of 0. I'll fix that.

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


More information about the llvm-commits mailing list