This is an archive of the discontinued LLVM Phabricator instance.

[RISCV][Docs] Document code generation for vector extension
Needs ReviewPublic

Authored by luke on Jan 23 2023, 3:55 AM.

Details

Summary

Over the past few weeks I've been documenting my understanding of how code is generated for the vector extension.
I thought it would be useful to solidify this knowledge somewhere, so I have written a document that is largely based off of the original RFC, but updated for the current state of lib/Target/RISCV.

Specifically, it gives a walkthrough of how code is generated for

  • Scalable vectors
  • Fixed-length vectors
  • Vector predication instructions

It may be the case that this documentation is too implementation specific and will get outdated quickly, so let me know if there is a better place to share this knowledge.
And likewise, there may be parts that I'm misunderstanding, so please feel free to correct me!

Diff Detail

Event Timeline

luke created this revision.Jan 23 2023, 3:55 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 23 2023, 3:55 AM
luke requested review of this revision.Jan 23 2023, 3:55 AM
luke edited the summary of this revision. (Show Details)Jan 23 2023, 4:05 AM
luke added inline comments.Jan 23 2023, 4:07 AM
llvm/docs/RISCV/RISCVVectorExtension.rst
317

Am I correct in understanding that the main reason for the VL nodes is that it defers having to select an LMUL? Or is there another reason as to why they are used vs just selecting a pseudo instruction directly and using a constant for its VL operand

luke added a comment.Jan 23 2023, 4:08 AM

Rendered PDF

Thanks for kicking this off. Unfortunately I don't have the time right now to give an in-depth review.

I only did a quick review.

llvm/docs/RISCV/RISCVVectorExtension.rst
163

VLEN is not limited to 128 bits. It can be 32 or 64.

186

This mapping also prevents the value of vscale from being examined if ELEN and VLEN are both 32.

294

This file is also used VP intrinsics for scalable vectors.

Matt added a subscriber: Matt.Feb 6 2023, 9:14 AM
luke added inline comments.Feb 6 2023, 3:02 PM
llvm/docs/RISCV/RISCVVectorExtension.rst
186

I'm not sure if I understand, could you clarified by what you mean by examined? Is it related to https://reviews.llvm.org/D128286

294

This is mentioned in the vector predication section below

luke updated this revision to Diff 495284.EditedFeb 6 2023, 3:14 PM

Add section on standard vector extensions

craig.topper added inline comments.Feb 6 2023, 3:19 PM
llvm/docs/RISCV/RISCVVectorExtension.rst
186

We can't use llvm.vscale or ISD:VSCALE if ELEN is 32. See this code in RISCVISelLowering.cpp. Specifically the fatal_error.

case ISD::VSCALE: {
  MVT VT = Op.getSimpleValueType();
  SDLoc DL(Op);
  SDValue VLENB = DAG.getNode(RISCVISD::READ_VLENB, DL, VT);
  // We define our scalable vector types for lmul=1 to use a 64 bit known
  // minimum size. e.g. <vscale x 2 x i32>. VLENB is in bytes so we calculate
  // vscale as VLENB / 8.
  static_assert(RISCV::RVVBitsPerBlock == 64, "Unexpected bits per block!");
  if (Subtarget.getRealMinVLen() < RISCV::RVVBitsPerBlock)
    report_fatal_error("Support for VLEN==32 is incomplete.");
craig.topper added inline comments.Feb 6 2023, 3:25 PM
llvm/docs/RISCV/RISCVVectorExtension.rst
317

Selecting a pseudoinstruction from lowering would be make it difficult to do other optimizations without checking different combinations of pseudoinstruction opcodes.

We can't use the fixed vector types in the isel patterns because the mapping from fixed vector type to LMUL isn't static and isel patterns require the types to be explicitly mentioned in the patterns.

luke added inline comments.Feb 6 2023, 3:39 PM
llvm/docs/RISCV/RISCVVectorExtension.rst
186

Thanks, that makes sense.

luke updated this revision to Diff 495299.Feb 6 2023, 3:42 PM

Add note about mapping of types when VLEN=32

LWenH added a subscriber: LWenH.Jul 1 2023, 9:56 PM
LWenH added inline comments.Jul 1 2023, 9:59 PM
llvm/docs/RISCV/RISCVVectorExtension.rst
212

Hi, as a doc reader I'm quite confuse why here for the i128 type would be the 1/2 under the LMUL=1. In order to represent the i128 type, shouldn't the vscale be extended to 2 (LMUL=1) to be more direct?

It would be great if there was a section describing how vector registers are spilled / restored.
Or, more generally, how the stack space is allocated for registers that don't have length know at compile time.

llvm/docs/RISCV/RISCVVectorExtension.rst
20

"is also a power of two" repeats the previous sentence.

35
40–42

Should the group names be the same as in "Register classes" seciton, v0m4 etc.?

128
evandro removed a subscriber: evandro.Jul 2 2023, 4:03 PM
luke added inline comments.Jul 4 2023, 3:45 AM
llvm/docs/RISCV/RISCVVectorExtension.rst
212

At LMUL=1, the minimum vector size should be 64 bits, but you can't specify the minimum vector length as anything less than 1 e.g. <vscale x ½ x i128>, if that makes sense. I guess it could represent i128 vectors at LMUL=2 with <vscale x 1 x i128> though