This is an archive of the discontinued LLVM Phabricator instance.

Implement support for AArch64ISD::MOVI in computeKnownBits
ClosedPublic

Authored by adriantong1024 on Oct 31 2022, 12:53 PM.

Details

Summary

This helps simplify a USHR+ORR into USRA on AArch64

Diff Detail

Event Timeline

adriantong1024 requested review of this revision.Oct 31 2022, 12:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptOct 31 2022, 12:53 PM

Sounds like a good use of isTargetCanonicalConstantNode.

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1977

cast<> will already assert.

1979

Does CN->getAPIntValue() work, or does it need to change the width of the APInt?

foad added inline comments.Nov 1 2022, 2:58 AM
llvm/test/CodeGen/AArch64/shift-accumulate.ll
124

Pre-commit this test and rebase the current patch on top of it?

tschuett added inline comments.
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1976

Could you you change it to dyn_cast with if to get the correct assert and error message?

Address comments from reviewers.

Thanks !

adriantong1024 added inline comments.Nov 1 2022, 7:20 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1979

I think its needs to be the same width as Known. Otherwise Known bitwidth will be changed and cause trouble later.

llvm/test/CodeGen/AArch64/shift-accumulate.ll
124

Hi Jay

Thank you for the comment. Do you mean I should change this test and commit it as a separate patch before the actual change (i.e. the implementation of the MOVI) ?

foad added inline comments.Nov 1 2022, 7:28 AM
llvm/test/CodeGen/AArch64/shift-accumulate.ll
124

Yes. Commit the test in a form that will pass on trunk, and then when you rebase this patch, it'll show the difference in the codegen caused by your patch. That makes it much easier for reviewers to see the effect of your patch.

(I'm not an AArch64 reviewer but this is pretty common practice for the backends I've worked on.)

adriantong1024 added inline comments.Nov 1 2022, 7:29 AM
llvm/test/CodeGen/AArch64/shift-accumulate.ll
124

Makes sense !. I will do so once this patch is accepted. Thanks for teaching me this.

tschuett added inline comments.Nov 1 2022, 7:35 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1976

I still believe that you need and if to check in Release mode whether it really is a constant node.

foad added inline comments.Nov 1 2022, 7:40 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1976

No, just use cast<> and remove the assert.

Address comments from reviewers. Thanks !

adriantong1024 added inline comments.Nov 1 2022, 7:45 AM
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1976

Thanks for the comment. I went back to check "The cast<> operator is a “checked cast” operation. It converts a pointer or reference from a base class to a derived class, causing an assertion failure if it is not really an instance of the right type." This seems to be what we want.

Let me know if there are other concerns!

There are other MOVI nodes created in ISel, have you considered adding support for those too?

MOVI,
MOVIshift,
MOVIedit,
MOVImsl,
FMOV,
MVNIshift,
MVNImsl,
llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1976

cast will do the assert for you. I would think it would be OK to always use case and drop the assert.

The operand of a MOVI will always be a ConstantSDNode. There is no need to check for it in release builds, it should be correct by construction.

llvm/test/CodeGen/AArch64/shift-accumulate.ll
124

Can you expand this to other types too. For example this, as well as i64 variants:

define <4 x i32> @usra_with_movi16(<8 x i16> %0, <8 x i16> %1) {
; CHECK-LABEL: usra_with_movi:
; CHECK:       // %bb.0:
; CHECK-NEXT:    movi v2.16b, #1
; CHECK-NEXT:    cmeq v0.16b, v0.16b, v1.16b
; CHECK-NEXT:    and v0.16b, v0.16b, v2.16b
; CHECK-NEXT:    usra v0.8h, v0.8h, #7
; CHECK-NEXT:    ret
  %3 = icmp eq <8 x i16> %0, %1
  %4 = zext <8 x i1> %3 to <8 x i16>
  %5 = bitcast <8 x i16> %4 to <4 x i32>
  %6 = lshr <4 x i32> %5, <i32 7, i32 7, i32 7, i32 7>
  %7 = or <4 x i32> %6, %5
  ret <4 x i32> %7
}

There are other MOVI nodes created in ISel, have you considered adding support for those too?

MOVI,
MOVIshift,
MOVIedit,
MOVImsl,
FMOV,
MVNIshift,
MVNImsl,

This patch comes out of a workload we have inside the company. Only MOVI needs to be fixed here. I will certainly implement support for others if there are use cases for them.

Address test case comment from dmgreen. Thanks !

It may be worth implementing them in separate patches to keep the changes small. If you can add the test cases for different sizes, this LGTM.

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
1979

Makes sense. The constant should always be an i32, even though the typesize is i8.

dmgreen accepted this revision.Nov 1 2022, 8:07 AM

Thanks. LGTM

This revision is now accepted and ready to land.Nov 1 2022, 8:07 AM
This revision was landed with ongoing or failed builds.Nov 1 2022, 8:57 AM
This revision was automatically updated to reflect the committed changes.