This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Select signed and unsigned bitfield extracts for XTHeadBb
ClosedPublic

Authored by philipp.tomsich on Feb 16 2023, 3:42 PM.

Details

Summary

The XTHeadBb extension hab both signed and unsigned bitfield
extraction instructions (TH.EXT and TH.EXTU, respectively) which have
previously only been supported for sign extension on byte, halfword,
and word-boundaries.

This adds the infrastructure to use TH.EXT and TH.EXTU for arbitrary
bitfield extraction.

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptFeb 16 2023, 3:42 PM
philipp.tomsich requested review of this revision.Feb 16 2023, 3:42 PM
craig.topper added inline comments.Feb 16 2023, 3:50 PM
llvm/test/CodeGen/RISCV/ctlz-cttz-ctpop.ll
1143 ↗(On Diff #498179)

This looks worse

llvm/test/CodeGen/RISCV/rv32xtheadbb.ll
404

This looks worse

  • fix (and X C1) cases for th.extu
    • don't use th.extu if andi can be used
    • ensure it is used for bitfield extractions starting at bit 0 (instead of falling back to (shr (shl X C1) C2))
  • now depends on D144278 (commutativity fix for THeadMac)
  • updated test cases (for this dependency)
craig.topper added inline comments.Feb 17 2023, 10:35 AM
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
698

What if LeftShAmt > RightShAmt?

1013

This can overflow is C2 is larger than the number of leading zeros in C1. That's what the if (C2 < Leading) at line 1014 is protecting. I think you want to be below that and probably below the SRLIW check at line 1016.

philipp.tomsich marked an inline comment as done.
  • reject recognition of bitfield-extract if (shr (shl X c1) c2) has c1 > c2, which would leave the extracted bitfield at a bitposition > 0 (and turn the LSB negative...)
  • add test-coverage for this case
philipp.tomsich marked 2 inline comments as done.Feb 17 2023, 11:18 AM
philipp.tomsich added inline comments.
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
698

Thanks! I added a test-case as well.

craig.topper added inline comments.Feb 17 2023, 11:26 AM
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
1014

Just to confirm, do you want th.extu preferred over this single SRLIW code?

philipp.tomsich marked an inline comment as done.Feb 17 2023, 11:30 AM
philipp.tomsich added inline comments.
llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp
1014

I was about to say "Yes.", but just changed my mind: the standard instruction should always be preferred, if no performance difference (giving the vendor an easier way to eventually deprecate a custom extension by expanding into multiple u-ops).

So I'll change this one more time.

  • prefer SRLIW over TH.EXTU
This revision is now accepted and ready to land.Feb 17 2023, 12:03 PM
This revision was landed with ongoing or failed builds.Feb 17 2023, 12:46 PM
This revision was automatically updated to reflect the committed changes.