This is an archive of the discontinued LLVM Phabricator instance.

[LoongArch] Add codegen support for bswap
ClosedPublic

Authored by xen0n on Aug 7 2022, 7:08 AM.

Diff Detail

Event Timeline

xen0n created this revision.Aug 7 2022, 7:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2022, 7:08 AM
xen0n requested review of this revision.Aug 7 2022, 7:08 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2022, 7:08 AM
SixWeining added inline comments.Aug 7 2022, 6:59 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
72

As Table 1 described, revb.2h is available on LA32.
So for i16, we can use it.

llvm/test/CodeGen/LoongArch/bswap.ll
3

The standard way is to use two dashes.

llc --help | grep mtriple
  --mtriple=<string>                                                    - Override target triple for module

And --verify-machineinstrs:

llc --help-hidden | grep verify-machineinstrs
  --verify-machineinstrs                                                - Verify generated machine code

And --check-prefix:

FileCheck --help | grep check-prefix
  --check-prefix=<string>        - Prefix to use from check file (defaults to 'CHECK')
34–40

How about:

revb.2h $a0, $a0
rotri.w $a0, 16
xen0n added inline comments.Aug 7 2022, 7:04 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
72

I looked at the original document《龙芯架构 32 位精简版参考手册》 which doesn't contain this instruction, maybe it's because it's actually describing the LA32 Primary ISA? But apparently it described way more instructions than the tiny subset suitable for university courses.

I'll change anyway. Thanks for the fact checking.

llvm/test/CodeGen/LoongArch/bswap.ll
34–40

As revb.2h can be used in this case, I'll change this. Thanks.

xen0n added inline comments.Aug 7 2022, 7:06 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
72

Although it's really weird that REVB.W isn't being listed, even though it doesn't involve any operation wider than 32 bits... Maybe there are hardware implementation concerns but I'm not a HW designer so my intuition can fail me. Anyway rotri.w works.

xen0n added inline comments.Aug 7 2022, 7:09 PM
llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
72

Although it's really weird that REVB.W isn't being listed

My bad; there isn't REVB.W but only REVB.2W. LoongArch is not orthogonal... (many swear words omitted)

xen0n updated this revision to Diff 450687.Aug 7 2022, 7:55 PM

Enable revb.2h on LA32, use revb.2h + rotri.w to implement bswap.i32 on LA32.

xen0n updated this revision to Diff 450688.Aug 7 2022, 7:56 PM

fix typo

xen0n updated this revision to Diff 450689.Aug 7 2022, 7:58 PM

forgot to fix the FileCheck header

xen0n marked 2 inline comments as done.Aug 7 2022, 7:58 PM

Other parts LGTM.

llvm/lib/Target/LoongArch/LoongArchISelLowering.cpp
159

Another approach:

let Predicates = [IsLA32] in {
def : Pat<(bswap GPR:$rj), (ROTRI_W (REVB_2H GPR:$rj), 16)>;
}

I'm not sure which approach is better. But this one uses less code.

xen0n updated this revision to Diff 450691.Aug 7 2022, 8:20 PM

Simplify code by making more use of tblgen

xen0n marked 2 inline comments as done.Aug 7 2022, 8:22 PM

I submitted too early after realizing tblgen could be used for bswap.i32 on LA32, fixed anyway. Thanks for the suggestion.

xen0n updated this revision to Diff 450693.Aug 7 2022, 8:25 PM

apparently setOperationAction for Legal ops can be omitted

SixWeining accepted this revision.Aug 7 2022, 8:30 PM

LGTM. Thanks!

This revision is now accepted and ready to land.Aug 7 2022, 8:30 PM
This revision was landed with ongoing or failed builds.Aug 8 2022, 10:51 PM
This revision was automatically updated to reflect the committed changes.