Webcentive RISC-V Reference

vsetvli

Set vector length immediate

Extension
V
XLEN
RV32 and RV64
Assembly (riscv-unified-db)
vsetvli xd, xs1, vtypei
Length
32-bit

Encoding

3130201915141211760
0zimm11rs1111rd1010111

Match 0x00007057, mask 0x8000707f. Source: vsetvli 31=0 zimm11 rs1 14..12=0x7 rd 6..0=0x57 in riscv-opcodes/extensions/rv_v.

FieldBitsriscv-opcodes name
zimm1130:20zimm11
rs119:15rs1
rd11:7rd

Description

Sets the vector length register vl and vector type register vtype based on immediate values. Configures SEW, LMUL, and other vector parameters for subsequent vector operations.

Description from riscv-unified-db, reformatted.

Operation

# Save old vector state for ratio comparison (needed for xs1==0 && xd==0 case)
VectorState old_state = vector_state();

XReg new_vtype = vtypei;
if ((new_vtype[xlen() - 1] == 1'b1) # software is setting the illegal bit
    || ((new_vtype & 8'd0) != 0)    # reserved bits
    || (new_vtype[5] == 1)          # reserved vsew encoding
    || (new_vtype[2:0] == 3'b100)   # reserved vlmul encoding
    # || (xlen() == 32 && new_vtype[2:0] == 3'b101) # reserved LMUL in RV32
    )
{
  CSR[vtype].VILL = 1;
  CSR[vtype].VMA = 0;
  CSR[vtype].VTA = 0;
  CSR[vtype].VSEW = 0;
  CSR[vtype].VLMUL = 0;
  CSR[vl].VALUE = 0;
} else {
  # valid, do the write
  CSR[vtype].VILL = 0;
  CSR[vtype].VMA = new_vtype[7];
  CSR[vtype].VTA = new_vtype[6];
  CSR[vtype].VSEW = new_vtype[5:3];
  CSR[vtype].VLMUL = new_vtype[2:0];

  VectorState state = vector_state();
  XReg vlen = VLEN;
  XReg vlmax = (vlen << state.log2_lmul) >> state.log2_sew;
  XReg avl = X[xs1];
  XReg ceil_avl_over_two = (avl + 1) / 2;

  # Determine vl based on xs1 (AVL source) and xd (destination)
  if (xs1 == 0 && xd != 0) {
    # rs1 == x0, rd != x0: set vl to VLMAX
    CSR[vl].VALUE = vlmax;
  } else if (xs1 == 0) {
    # rs1 == x0, rd == x0: keep existing vl, but check LMUL/SEW ratio
    XReg old_ratio = old_state.log2_sew - old_state.log2_lmul;
    XReg new_ratio = state.log2_sew - state.log2_lmul;
    if (new_ratio != old_ratio) {
      # Ratio changed - set vill and clear vl
      CSR[vtype].VILL = 1;
      CSR[vtype].VMA = 0;
      CSR[vtype].VTA = 0;
      CSR[vtype].VSEW = 0;
      CSR[vtype].VLMUL = 0;
      CSR[vl].VALUE = 0;
    }
    # else: keep vl unchanged
  } else if (avl <= vlmax) {
    # Normal stripmining: AVL <= VLMAX
    CSR[vl].VALUE = avl;
  } else if (avl < 2 * vlmax) {
    # VLMAX < AVL < 2*VLMAX: implementation-defined behavior
    if (RVV_VL_WHEN_AVL_LT_DOUBLE_VLMAX == "ceil(AVL/2)") {
      CSR[vl].VALUE = ceil_avl_over_two;
    } else if (RVV_VL_WHEN_AVL_LT_DOUBLE_VLMAX == "VLMAX") {
      CSR[vl].VALUE = vlmax;
    } else {
      unpredictable("Implementations may choose a custom value for vl in the case AVL < (2*VLMAX), so long as ceil(AVL/2) <= vl <= VLMAX");
    }
  } else {
    # AVL >= 2*VLMAX: set vl to VLMAX
    CSR[vl].VALUE = vlmax;
  }
}
X[xd] = CSR[vl].VALUE;
CSR[vstart].VALUE = 0;

Pseudocode in riscv-unified-db’s IDL, quoted unchanged.

Specification and related pages