This is an archive of the discontinued LLVM Phabricator instance.

[RISCV] Add patterns for vector narrowing integer right shift instructions
ClosedPublic

Authored by eopXD on Jan 16 2022, 11:42 PM.

Diff Detail

Event Timeline

eopXD created this revision.Jan 16 2022, 11:42 PM
eopXD requested review of this revision.Jan 16 2022, 11:42 PM
eopXD updated this revision to Diff 400446.Jan 16 2022, 11:43 PM

Update code.

jacquesguan added inline comments.Jan 17 2022, 12:57 AM
llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td
611

Because only the low lg2(2*SEW) bits of the shift-amount value are used, I think we could also add zero extend here.

eopXD updated this revision to Diff 400469.Jan 17 2022, 1:36 AM
eopXD marked an inline comment as done.

Address @jacquesguan 's comment.

llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td
611

Good idea, thanks!

jacquesguan added inline comments.Jan 17 2022, 4:57 AM
llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td
609

Because here we always have truncate from SEW*2 to SEW, could we directly use trunc here and move these classes and patterns to RISCVInstrInfoVSDPatterns.td? In this way, maybe no need to use (riscv_vmset_vl (XLenVT -1)) for mask and (XLenVT -1) for vl.

eopXD marked an inline comment as done.Jan 17 2022, 8:48 AM
eopXD added inline comments.
llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td
609

I have tried to use trunc and found out we can't do this. The reason is that the RISCV backend only support one stage of truncate. So the compiler will first legalize it to RISCVISD::TRUNCATE_VECTOR_VL and split it into step(s) (e.g. if doing i64 -> i16, it will turnt into 2 instruction)

Here are the llc debug message to show you the "legalization" from trunc to RISCVISD::TRUNCATE_VECTOR_VL:

Type-legalized selection DAG: %bb.0 'vnsrl_wv_nxv1i32:'
SelectionDAG has 11 nodes:
  t0: ch = EntryToken
        t2: nxv1i64,ch = CopyFromReg t0, Register:nxv1i64 %0
          t4: nxv1i32,ch = CopyFromReg t0, Register:nxv1i32 %1
        t6: nxv1i64 = sign_extend t4
      t7: nxv1i64 = srl t2, t6
    t8: nxv1i32 = truncate t7
  t11: ch,glue = CopyToReg t0, Register:nxv1i32 $v8, t8
  t12: ch = RISCVISD::RET_FLAG t11, Register:nxv1i32 $v8, t11:1



Legalizing vector op: t6: nxv1i64 = sign_extend t4
Trying custom legalization

Legalizing vector op: t7: nxv1i64 = srl t2, t6
Legal node: nothing to do

Legalizing vector op: t8: nxv1i32 = truncate t7
Trying custom legalization
Creating constant: t13: i32 = TargetConstant<-1>
Creating new node: t14: nxv1i1 = RISCVISD::VMSET_VL TargetConstant:i32<-1>
Creating new node: t15: nxv1i32 = RISCVISD::TRUNCATE_VECTOR_VL t7, t14, TargetConstant:i32<-1>
Vector-legalized selection DAG: %bb.0 'vnsrl_wv_nxv1i32:'
SelectionDAG has 13 nodes:
  t0: ch = EntryToken
        t2: nxv1i64,ch = CopyFromReg t0, Register:nxv1i64 %0
          t4: nxv1i32,ch = CopyFromReg t0, Register:nxv1i32 %1
        t6: nxv1i64 = sign_extend t4
      t7: nxv1i64 = srl t2, t6
      t14: nxv1i1 = RISCVISD::VMSET_VL TargetConstant:i32<-1>
    t15: nxv1i32 = RISCVISD::TRUNCATE_VECTOR_VL t7, t14, TargetConstant:i32<-1>
  t11: ch,glue = CopyToReg t0, Register:nxv1i32 $v8, t15
  t12: ch = RISCVISD::RET_FLAG t11, Register:nxv1i32 $v8, t11:1
craig.topper added inline comments.Jan 17 2022, 9:22 AM
llvm/lib/Target/RISCV/RISCVInstrInfoVVLPatterns.td
612

(XlenVT -1) -> VLMax

613

(XlenVT -1) -> VLMax

627

Same here

eopXD updated this revision to Diff 400591.Jan 17 2022, 9:40 AM
eopXD marked 3 inline comments as done.

Address @craig.topper 's comment.

eopXD marked an inline comment as done.Jan 17 2022, 9:41 AM
eopXD updated this revision to Diff 400600.Jan 17 2022, 10:04 AM

Update code: rewrite to VLMax

This revision is now accepted and ready to land.Jan 18 2022, 3:36 PM
This revision was landed with ongoing or failed builds.Jan 18 2022, 10:30 PM
This revision was automatically updated to reflect the committed changes.