This is an archive of the discontinued LLVM Phabricator instance.

[CodeGen][AArch64] Generate Pseudo instructions for integer MLA/MAD/MLS/MSB
ClosedPublic

Authored by sushgokh on Mar 7 2023, 4:16 AM.

Diff Detail

Event Timeline

sushgokh created this revision.Mar 7 2023, 4:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2023, 4:16 AM
sushgokh requested review of this revision.Mar 7 2023, 4:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 7 2023, 4:16 AM
paulwalker-arm added inline comments.Mar 7 2023, 8:23 AM
llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
409–410

The add(a, select(... pattern has specific requirements for the result of inactive lanes that matches AArch64mla_m1. Did you mean to move it into AArch64mla_p?

411–414

As above I think the sub(a, select(... pattern should remain assigned to AArch64mls_m1.

494

Please delete redundant whitespace.

sushgokh added inline comments.Mar 8 2023, 2:54 AM
llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
409–410

The current semantics of add(a, select(...) ) is resulting in
mla za, p0/m, zn * zm

The patch, even after moving this pattern to AArch64mla_p, should generate the pseudo with same predicate semantics (i.e. p0/m), right? I assume that thats the meaning of FalseLanesUndef. Correct me if wrong.

If this understanding is incorrect,

  1. is there any other way to specify multiple patterns with different inactive lane requirements to a single pseudo?
  1. One option is define pattern that explicitly maps add(a, select(...) ) to specific pseudo.
  1. Other option is let this pattern remain with AArch64mla_m1 rather than AArch64mla_p
paulwalker-arm added inline comments.Mar 8 2023, 5:59 AM
llvm/lib/Target/AArch64/AArch64SVEInstrInfo.td
409–410

The current semantics of add(a, select(...) ) is resulting in
mla za, p0/m, zn * zm

The output is only valid because of how the operation has been register allocated. Remember the point of this patch is that MLA_ZPZZZ can emit an MLA or MAD based on what the register allocator chooses to do. You can see the bug the current implementation will introduce by running the following through llc:

`
define <vscale x 16 x i8> @good(<vscale x 16 x i8> %a, <vscale x 16 x i8> %b, <vscale x 16 x i8> %c, <vscale x 16 x i1> %mask) {
  %mul = mul nsw <vscale x 16 x i8> %b, %c
  %sel = select <vscale x 16 x i1> %mask, <vscale x 16 x i8> %mul, <vscale x 16 x i8> zeroinitializer
  %add = add <vscale x 16 x i8> %a, %sel
  ret <vscale x 16 x i8> %add
}

define <vscale x 16 x i8> @bad(<vscale x 16 x i8> %a, <vscale x 16 x i8> %b, <vscale x 16 x i8> %c, <vscale x 16 x i1> %mask) {
  %mul = mul nsw <vscale x 16 x i8> %a, %b
  %sel = select <vscale x 16 x i1> %mask, <vscale x 16 x i8> %mul, <vscale x 16 x i8> zeroinitializer
  %add = add <vscale x 16 x i8> %c, %sel
  ret <vscale x 16 x i8> %add
}

good:
	mla	z0.b, p0/m, z1.b, z2.b
	ret

bad:
	mad	z0.b, p0/m, z1.b, z2.b
	ret

Both functions expect the inactive lanes to return the original value of the addend. However the second example will incorrectly set the result of inactive lanes to the original value of the multiplicand. Essential both sets of IR are synonymous to the AArch64mla_m1 operation.

The patch, even after moving this pattern to AArch64mla_p, should generate the pseudo with same predicate semantics (i.e. p0/m), right? I assume that thats the meaning of FalseLanesUndef. Correct me if wrong.

FalseLanesUndef means the inactive lanes have no defined value but as explained above the add(a, select(...) ) pattern sets the inactive lanes to a defined value, namely added + 0.

If this understanding is incorrect,

  1. is there any other way to specify multiple patterns with different inactive lane requirements to a single pseudo?
  1. One option is define pattern that explicitly maps add(a, select(...) ) to specific pseudo.
  1. Other option is let this pattern remain with AArch64mla_m1 rather than AArch64mla_p

Based on the above I think option 3 is the only correct answer.

@paulwalker-arm

for e = 0 to elements-1
    if ElemP[mask, e, esize] == '1' then
        integer element1 = UInt(Elem[operand1, e, esize]);
        integer element2 = UInt(Elem[operand2, e, esize]);
        integer product = element1 * element2;
        if sub_op then
            Elem[result, e, esize] = Elem[operand3, e, esize] - product;
        else
            Elem[result, e, esize] = Elem[operand3, e, esize] + product;
    else
        Elem[result, e, esize] = Elem[**operand1**, e, esize];

Ah ! Didnt notice the semantics highlighted above. I was thinking that to be operand3. Thanks for pointing out. Will make appropriate changes and upload the patch

sushgokh updated this revision to Diff 503627.Mar 8 2023, 10:26 PM
paulwalker-arm accepted this revision.Mar 9 2023, 5:37 AM
This revision is now accepted and ready to land.Mar 9 2023, 5:37 AM