This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][GlobalISel] Legalize bitshift instructions for narrow types
ClosedPublic

Authored by nitinjohnraj on Jul 19 2023, 7:44 PM.

Details

Summary

Legalize G_SHL, G_ASHR and G_LSHR for types narrower and upto (and including) XLen: (i7, i8,
i16 and i32) for rv32 and (i8, i15, i16, i32 and i64) for rv64. This requires
adding some rules to handle G_ANYEXT, G_ZEXT and G_SEXT.

Diff Detail

Event Timeline

nitinjohnraj created this revision.Jul 19 2023, 7:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2023, 7:44 PM
nitinjohnraj requested review of this revision.Jul 19 2023, 7:44 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 19 2023, 7:44 PM
arsenm added inline comments.Jul 20 2023, 4:04 PM
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
38

Do you actually need to permit anything besides XLenVT? I would hope if all of these were illegal the artifact combiner would eliminate all the casts in your examples

46

Don't use references with LLT

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-ashr.mir
4

Need some < 8 bit and odd intermediate sizes too

craig.topper added inline comments.Jul 20 2023, 10:06 PM
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
38

I had tried to help Nitin with this. I think it wasn't working with just .clampScalar(0, XLenLLT, XLenLLT);. The code here was taken from AArch64 and modified to XLen.

59

This should be 2*XLen. The AArch64 code says 128 here.

tschuett added inline comments.
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
34

You claim: " types narrower and upto XLen". But you clamp to XLenLLT?

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-ashr.mir
22

Input and output are %4?

tschuett added inline comments.Jul 20 2023, 10:52 PM
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
59

The builder provides various methods to help you to legalize operations.

nitinjohnraj marked 2 inline comments as done.

Address comments

nitinjohnraj marked 5 inline comments as done.Jul 27 2023, 12:36 PM
nitinjohnraj added inline comments.
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
34

XLenLLT is the only legal type on RISCV.

llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-ashr.mir
4

This doesn't work with odd-sized types right now, I'll update the description and fix that in a future patch.

I added one test for i1; but in order to pass it, I needed to change the legalization logic for G_{ANY/Z/S}EXT. Specifically, I allowed the SrcSize and DstSize to be as small as 1 for legal instructions. But neither X86 nor AArch64 allows this, so I'm not sure we should be allowing this.

22

Thank you for catching this.

nitinjohnraj retitled this revision from [RISCV][GlobalISel] Legalize bitshift instructions for narrow types to [RISCV][GlobalISel] Legalize bitshift instructions for narrow types that are a power of 2.
nitinjohnraj marked an inline comment as done.Jul 27 2023, 12:57 PM
nitinjohnraj added inline comments.
llvm/test/CodeGen/RISCV/GlobalISel/legalizer/rv32/legalize-ashr.mir
4

To clarify, I meant it didn't work will odd types less than 8. I'll add a test for an odd type.

nitinjohnraj marked an inline comment as not done.Jul 27 2023, 1:01 PM
nitinjohnraj added inline comments.
llvm/lib/Target/RISCV/GISel/RISCVLegalizerInfo.cpp
59

This needs to be handled in parent revision.

Added rules for G_SEXT_INREG. Now this patch works for non-pow 2 types.

nitinjohnraj retitled this revision from [RISCV][GlobalISel] Legalize bitshift instructions for narrow types that are a power of 2 to [RISCV][GlobalISel] Legalize bitshift instructions for narrow types.Aug 1 2023, 12:11 PM
nitinjohnraj edited the summary of this revision. (Show Details)
nitinjohnraj marked an inline comment as done.

Ping! Is anyone available to review this?

This revision is now accepted and ready to land.Aug 1 2023, 3:57 PM
This revision was landed with ongoing or failed builds.Aug 7 2023, 3:13 PM
This revision was automatically updated to reflect the committed changes.