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

generic `@llvm.ssub.sat` optimizes less well than target-specific `@llvm.aarch64.neon.sqsub`

Open folkertdev opened this issue 2 years ago • 11 comments

The generic @llvm.ssub.sat.v2i64 intrinsic optimizes less well than the target-specific @llvm.aarch64.neon.sqsub.v2i64 intrinsic.

This godbolt shows the issue https://godbolt.org/z/4qEe3xM9v

We see two functions that use these two saturating subtractions, but are otherwise the same. they generate very similar initial LLVM IR, except that the generic function is called in a slightly different way (requiring some extra allocas).

define void @specific(ptr dead_on_unwind noalias nocapture noundef writable sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %a, ptr noalias nocapture noundef align 16 dereferenceable(16) %b, ptr noalias nocapture noundef align 16 dereferenceable(16) %c) unnamed_addr {
start:
  %0 = alloca [16 x i8], align 16
  %1 = alloca [16 x i8], align 16
  %2 = alloca [16 x i8], align 16
  %3 = alloca [16 x i8], align 16
  %4 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr %4)
  %5 = load <4 x i32>, ptr %b, align 16
  store <4 x i32> %5, ptr %3, align 16
  %6 = load <4 x i32>, ptr %c, align 16
  store <4 x i32> %6, ptr %2, align 16
  call void @core::core_arch::aarch64::neon::generated::vqdmull_high_laneq_s32::h66aa645ca0aefe90(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %4, ptr noalias nocapture noundef align 16 dereferenceable(16) %3, ptr noalias nocapture noundef align 16 dereferenceable(16) %2)
  %_4 = load <2 x i64>, ptr %4, align 16
  call void @llvm.lifetime.end.p0(i64 16, ptr %4)
  %7 = load <2 x i64>, ptr %a, align 16
  store <2 x i64> %7, ptr %1, align 16
  store <2 x i64> %_4, ptr %0, align 16
  ; after InlinerPass this call becomes a call to `@llvm.aarch64.neon.sqsub.v2i64` 
  call void @core::core_arch::arm_shared::neon::generated::vqsubq_s64::h1887dd6c0650937c(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %1, ptr noalias nocapture noundef align 16 dereferenceable(16) %0)
  ret void
}

define void @generic(ptr dead_on_unwind noalias nocapture noundef writable sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %a, ptr noalias nocapture noundef align 16 dereferenceable(16) %b, ptr noalias nocapture noundef align 16 dereferenceable(16) %c) unnamed_addr {
start:
  %0 = alloca [16 x i8], align 16
  %1 = alloca [16 x i8], align 16
  %2 = alloca [16 x i8], align 16
  call void @llvm.lifetime.start.p0(i64 16, ptr %2)
  %3 = load <4 x i32>, ptr %b, align 16
  store <4 x i32> %3, ptr %1, align 16
  %4 = load <4 x i32>, ptr %c, align 16
  store <4 x i32> %4, ptr %0, align 16
  call void @core::core_arch::aarch64::neon::generated::vqdmull_high_laneq_s32::h66aa645ca0aefe90(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %2, ptr noalias nocapture noundef align 16 dereferenceable(16) %1, ptr noalias nocapture noundef align 16 dereferenceable(16) %0)
  %_4 = load <2 x i64>, ptr %2, align 16
  call void @llvm.lifetime.end.p0(i64 16, ptr %2)
  %5 = load <2 x i64>, ptr %a, align 16
  %6 = call <2 x i64> @llvm.ssub.sat.v2i64(<2 x i64> %5, <2 x i64> %_4)
  store <2 x i64> %6, ptr %_0, align 16
  ret void
}

As a user, I expect the generic variant to eventually be lowered to the specific one.

When the intrinsics are used on their own, this is in fact the case: https://godbolt.org/z/ErjETo3bh. Both functions emit the sqsub instruction. This logic appears to be implemented here.

But in my example, there is an optimization that @llvm.aarch64.neon.sqsub.v2i64 participates in, but the generic @llvm.ssub.sat.v2i64 does not.

specific:
        ldr     q0, [x1]
        ldr     q1, [x2]
        ldr     q2, [x0]
        sqdmlsl2        v2.2d, v0.4s, v1.s[1]
        str     q2, [x8]
        ret

generic:
        ldr     q0, [x1]
        ldr     q1, [x2]
        sqdmull2        v0.2d, v0.4s, v1.s[1]
        ldr     q1, [x0]
        sqsub   v0.2d, v1.2d, v0.2d
        str     q0, [x8]
        ret

This is unexpected, and seems to imply that many optimizations are missed when using the generic SIMD intrinsics (at least for Neon). My intuition is that the lowering of the generic to the specific instruction occurs too late. It should occur earlier so that it can participate in backend-specific optimizations.

folkertdev avatar Jun 05 '24 12:06 folkertdev

@llvm/issue-subscribers-backend-aarch64

Author: Folkert de Vries (folkertdev)

The generic `@llvm.ssub.sat.v2i64` intrinsic optimizes less well than the target-specific `@llvm.aarch64.neon.sqsub.v2i64` intrinsic.

This godbolt shows the issue https://godbolt.org/z/4qEe3xM9v

We see two functions that use these two saturating subtractions, but are otherwise the same. they generate very similar initial LLVM IR, except that the generic function is called in a slightly different way (requiring some extra allocas).

define void @<!-- -->specific(ptr dead_on_unwind noalias nocapture noundef writable sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %a, ptr noalias nocapture noundef align 16 dereferenceable(16) %b, ptr noalias nocapture noundef align 16 dereferenceable(16) %c) unnamed_addr {
start:
  %0 = alloca [16 x i8], align 16
  %1 = alloca [16 x i8], align 16
  %2 = alloca [16 x i8], align 16
  %3 = alloca [16 x i8], align 16
  %4 = alloca [16 x i8], align 16
  call void @<!-- -->llvm.lifetime.start.p0(i64 16, ptr %4)
  %5 = load &lt;4 x i32&gt;, ptr %b, align 16
  store &lt;4 x i32&gt; %5, ptr %3, align 16
  %6 = load &lt;4 x i32&gt;, ptr %c, align 16
  store &lt;4 x i32&gt; %6, ptr %2, align 16
  call void @<!-- -->core::core_arch::aarch64::neon::generated::vqdmull_high_laneq_s32::h66aa645ca0aefe90(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %4, ptr noalias nocapture noundef align 16 dereferenceable(16) %3, ptr noalias nocapture noundef align 16 dereferenceable(16) %2)
  %_4 = load &lt;2 x i64&gt;, ptr %4, align 16
  call void @<!-- -->llvm.lifetime.end.p0(i64 16, ptr %4)
  %7 = load &lt;2 x i64&gt;, ptr %a, align 16
  store &lt;2 x i64&gt; %7, ptr %1, align 16
  store &lt;2 x i64&gt; %_4, ptr %0, align 16
  ; after InlinerPass this call becomes a call to `@<!-- -->llvm.aarch64.neon.sqsub.v2i64` 
  call void @<!-- -->core::core_arch::arm_shared::neon::generated::vqsubq_s64::h1887dd6c0650937c(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %1, ptr noalias nocapture noundef align 16 dereferenceable(16) %0)
  ret void
}

define void @<!-- -->generic(ptr dead_on_unwind noalias nocapture noundef writable sret([16 x i8]) align 16 dereferenceable(16) %_0, ptr noalias nocapture noundef align 16 dereferenceable(16) %a, ptr noalias nocapture noundef align 16 dereferenceable(16) %b, ptr noalias nocapture noundef align 16 dereferenceable(16) %c) unnamed_addr {
start:
  %0 = alloca [16 x i8], align 16
  %1 = alloca [16 x i8], align 16
  %2 = alloca [16 x i8], align 16
  call void @<!-- -->llvm.lifetime.start.p0(i64 16, ptr %2)
  %3 = load &lt;4 x i32&gt;, ptr %b, align 16
  store &lt;4 x i32&gt; %3, ptr %1, align 16
  %4 = load &lt;4 x i32&gt;, ptr %c, align 16
  store &lt;4 x i32&gt; %4, ptr %0, align 16
  call void @<!-- -->core::core_arch::aarch64::neon::generated::vqdmull_high_laneq_s32::h66aa645ca0aefe90(ptr noalias nocapture noundef sret([16 x i8]) align 16 dereferenceable(16) %2, ptr noalias nocapture noundef align 16 dereferenceable(16) %1, ptr noalias nocapture noundef align 16 dereferenceable(16) %0)
  %_4 = load &lt;2 x i64&gt;, ptr %2, align 16
  call void @<!-- -->llvm.lifetime.end.p0(i64 16, ptr %2)
  %5 = load &lt;2 x i64&gt;, ptr %a, align 16
  %6 = call &lt;2 x i64&gt; @<!-- -->llvm.ssub.sat.v2i64(&lt;2 x i64&gt; %5, &lt;2 x i64&gt; %_4)
  store &lt;2 x i64&gt; %6, ptr %_0, align 16
  ret void
}

As a user, I expect the generic variant to eventually be lowered to the specific one.

When the intrinsics are used on their own, this is in fact the case: https://godbolt.org/z/ErjETo3bh. Both functions emit the sqsub instruction. This logic appears to be implemented here.

But in my example, there is an optimization that @<!-- -->llvm.aarch64.neon.sqsub.v2i64 participates in, but the generic @<!-- -->llvm.ssub.sat.v2i64 does not.

specific:
        ldr     q0, [x1]
        ldr     q1, [x2]
        ldr     q2, [x0]
        sqdmlsl2        v2.2d, v0.4s, v1.s[1]
        str     q2, [x8]
        ret

generic:
        ldr     q0, [x1]
        ldr     q1, [x2]
        sqdmull2        v0.2d, v0.4s, v1.s[1]
        ldr     q1, [x0]
        sqsub   v0.2d, v1.2d, v0.2d
        str     q0, [x8]
        ret

This is unexpected, and seems to imply that many optimizations are missed when using the generic SIMD intrinsics (at least for Neon). My intuition is that the lowering of the generic to the specific instruction occurs too late. It should occur earlier so that it can participate in backend-specific optimizations.

llvmbot avatar Jun 05 '24 16:06 llvmbot

this is still a problem, here is a pure LLVM reproduction https://godbolt.org/z/c8eshf1a1

I believe the issue is here

https://github.com/llvm/llvm-project/blob/d781ac1cf0d561b79f6ff834d786c6a999429901/llvm/lib/Target/AArch64/AArch64InstrInfo.td#L6202-L6214

which does not account for the platform-independent intrinsics. However, the actual fix might be to lower to the platform-specific intrinsic earlier?

folkertdev avatar Mar 15 '25 15:03 folkertdev

The interesting patterns here are part of SIMDIndexedLongSQDMLXSDTied (notably the use of Accum).

We could lower aarch64_neon_{u,s}q{add,sub} to their equivalent ISD::{U,S}{ADD,SUB}SAT target independent nodes, this might be worthwhile in it's own right (any thoughts @davemgreen ?).

That said, AArch64 only supports vector types with these target independent nodes [1], so we can't remove the intrinsics entirely since they are used to support scalar arguments. This also wouldn't be GISel friendly.

A simpler approach would be to PatFrag the intrinsic and target independent nodes together, in a similar way to AArch64uabd, though the former suggestion would be a more valuable change.

[1] https://reviews.llvm.org/D69374

SpencerAbson avatar Mar 30 '25 13:03 SpencerAbson

Yeah, we should be able to convert the vector types over:

case Intrinsic::aarch64_neon_sqadd:
  if (Op.getValueType().isVector())
    return DAG.getNode(ISD::SADDSAT, dl, Op.getValueType(), Op.getOperand(1),
                       Op.getOperand(2));
  return SDValue();

davemgreen avatar Mar 30 '25 16:03 davemgreen

@davemgreen that lowering already happens I believe. It is specifically the fusing with the llvm.aarch64.neon.sqdmull that does not happen when using the generic saturating add/sub.

The snippet you posted actually breaks that fusing for @llvm.aarch64.neon.sqadd.*.

folkertdev avatar Apr 30 '25 14:04 folkertdev

I also tried adding this additional rule

def : Pat<(i64 (int_sadd_sat (i64 FPR64:$Rd),
                   (i64 (int_aarch64_neon_sqdmulls_scalar (i32 FPR32:$Rn),
                                                        (i32 FPR32:$Rm))))),
          (SQDMLALi32 FPR64:$Rd, FPR32:$Rn, FPR32:$Rm)>;

but that does not actually trigger from what I can see by running this test

define <2 x i64> @test_vqdmlal_lane_s32_foo(<2 x i64> %a, <2 x i32> %b, <2 x i32> %v) {
; CHECK-LABEL: test_vqdmlal_lane_s32_foo:
; CHECK:       // %bb.0: // %entry
; CHECK-NEXT:    // kill: def $d2 killed $d2 def $q2
; CHECK-NEXT:    sqdmlal v0.2d, v1.2s, v2.s[1]
; CHECK-NEXT:    ret
entry:
  %shuffle = shufflevector <2 x i32> %v, <2 x i32> undef, <2 x i32> <i32 1, i32 1>
  %vqdmlal2.i = tail call <2 x i64> @llvm.aarch64.neon.sqdmull.v2i64(<2 x i32> %b, <2 x i32> %shuffle)
  %vqdmlal4.i = tail call <2 x i64> @llvm.sadd.sat.v2i64(<2 x i64> %a, <2 x i64> %vqdmlal2.i)
  ret <2 x i64> %vqdmlal4.i
}

this still does not fuse

          1290:  .p2align 2 
          1291:  .type test_vqdmlal_lane_s32_foo,@function 
          1292: test_vqdmlal_lane_s32_foo: // @test_vqdmlal_lane_s32_foo 
          1293:  .cfi_startproc 
          1294: // %bb.0: // %entry 
          1295:  // kill: def $d2 killed $d2 def $q2 
next:1688'0                                         X error: no match found
          1296:  sqdmull v1.2d, v1.2s, v2.s[1] 
next:1688'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
next:1688'1      ?                              possible intended match
          1297:  sqadd v0.2d, v0.2d, v1.2d 
next:1688'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~

folkertdev avatar Apr 30 '25 16:04 folkertdev

@davemgreen that lowering already happens I believe. It is specifically the fusing with the llvm.aarch64.neon.sqdmull that does not happen when using the generic saturating add/sub.

The snippet you posted actually breaks that fusing for @llvm.aarch64.neon.sqadd.*.

It would need to update the tablegen patterns too, but those should now all reliably be sadd.sat intrinsics, so there would no longer need to be patterns for both. The idea is to make sure that when we get to selection, llvm.sadd.sat and aarch64.neon.sqadd look the same so we only need one set of patterns (some of the duplicates can be removed, and the existing patterns for sqadd(sqmull) will apply to everything). Doing it for vector should be simple enough, doing it for scalar looks more difficult.

I would probably recommend getting the vector code working first, then the scalar patterns we can try and handle later if needed. They are more difficult if the scalar types are not legal or the operation should happen on a fpr register.

davemgreen avatar Apr 30 '25 16:04 davemgreen

The idea is to make sure that when we get to selection, llvm.sadd.sat and aarch64.neon.sqadd look the same so we only need one set of patterns

Right, I misunderstood that at first, but specifically you want to make aarch64.neon.sqadd look like llvm.sadd.sat, right? (is there a reason to prefer that to the opposite?).

When I try that, the existing pattern no longer matches, and I'm not sure what pattern to use to make it match again. Also just generally, I have no idea what I'm doing here (I would just like this to work so rust's stdarch can be a little simpler), so if this is a quick fix for you, please make it :)

folkertdev avatar Apr 30 '25 17:04 folkertdev

Going sqadd->sadd.sat has the benefit that it might lead to some optimizations (like constant folding) that would not be performed otherwise on the target specific intrinsics. (Ideally they could be removed earlier in the front-end, but there is a consideration (that we don't support at the moment) with saturating intrinsics setting qc flags, so it is more contentious than the rest).

Can you take a look at https://github.com/SpencerAbson/llvm-project/commit/330fcff25bb5a895f830dde791b6bfe749f9aecc and update it, and hopefully add some basic GISel support so they don't fall back? It sounds like you are already on the same path, but that will hopefully get you further along it and seems to have some useful tests in it.

davemgreen avatar May 01 '25 06:05 davemgreen

Going sqadd->sadd.sat has the benefit that it might lead to some optimizations (like constant folding) that would not be performed otherwise on the target specific intrinsics.

Neat!

Can you take a look at SpencerAbson@330fcff and update it

Okay yes that is very helpful.

add some basic GISel support so they don't fall back?

I'm not sure exactly what you mean here. I think you want to prevent llvm.sadd.sat from being rewritten to aarch64.neon.sqadd?

However, the string sqadd does not occur in llvm/lib/Target/AArch64/GISel/ at all, so I'm not sure what that would look like.

SADDSAT occurs just once, here:

https://github.com/llvm/llvm-project/blob/1f56afa9b4d1839b8c8703ff9c7a4784546a6451/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp#L380-L389

folkertdev avatar May 01 '25 22:05 folkertdev

Hi - something like this code in GISel: https://github.com/llvm/llvm-project/blob/4504e775509483ec20912bc1d805717eecb311ca/llvm/lib/Target/AArch64/GISel/AArch64LegalizerInfo.cpp#L1771

It is the equivalent of the code for this but for GISel, so might need to handle vectors in the same way. The scalar intrinsics might not be handled yet in gisel.

  case Intrinsic::aarch64_neon_sqadd:
    if (Op.getValueType().isVector())
      return DAG.getNode(ISD::SADDSAT, dl, Op.getValueType(), Op.getOperand(1),
                         Op.getOperand(2));
    return SDValue();

davemgreen avatar May 18 '25 12:05 davemgreen