Skip to content

[RISCV] correct the default VLEN to 128 #17853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jerryzj
Copy link

@jerryzj jerryzj commented Apr 17, 2025

  • From the RISCV ISA spec

The V vector extension depends upon the Zvl128b and Zve64d extensions.

Reference: https://github.com/riscv/riscv-isa-manual/blob/e5078e55ea977ef197066dcd3f7c8e09032cb348/src/v-st-ext.adoc?plain=1#L5094

- From the RISCV ISA spec

> The V vector extension depends upon the Zvl128b and Zve64d extensions.

Reference: https://github.com/riscv/riscv-isa-manual/blob/e5078e55ea977ef197066dcd3f7c8e09032cb348/src/v-st-ext.adoc?plain=1#L5094
@jerryzj jerryzj changed the title RISCV: correct the default VLEN to 128 [RISCV] correct the default VLEN to 128 Apr 17, 2025
@cbalint13 cbalint13 self-assigned this Apr 18, 2025
@cbalint13
Copy link
Contributor

Hi @jerryzj !

LLVM side (implicitly TVM) is aware of this already:

$ cat tvm-llvm-rvv.py 
#!/usr/bin/python3

import tvm

target = tvm.target.Target("llvm -device=riscv_cpu -mtriple=riscv32-linux-gnu -mcpu=generic-rv32 -mattr=+i,+m,+v")
print("LLVM [%s]" % target)
print("LLVM features: %s" % (tvm.target.codegen.llvm_get_cpu_features(target)))
$ ./tvm-llvm-rvv.py 
LLVM [llvm -keys=riscv_cpu,cpu -device=riscv_cpu -mattr=+i,+m,+v -mcpu=generic-rv32 -mtriple=riscv32-linux-gnu]
LLVM features: {'zve64x', 'zve32f', 'zve64f', 'zmmul', 'zve64d', 'f', 'zicsr', 'd', 'optimized-nf2-segment-load-store', 'm', 'zve32x', 'zvl32b', 'v', 'zvl128b', 'zvl64b', '32bit', 'i'}

Notice the presence of zvl128b, that was added due to +v presence, so this will be properly picked up anyway.


BUT, please note that in the case of no +v (absence) we still need a default TVM vector width, and this one has nothing to do with the ISA anymore, but yes let's settle it to 128 (down from current 256).

I am just working right now on refactoring RISCV target to also enable the SVE predication, will reference this PR.

@jerryzj
Copy link
Author

jerryzj commented Apr 18, 2025

Please refer to the ISA spec, the minimum vector length can be set via zvl<?>b

@cbalint13
Copy link
Contributor

Please refer to the ISA spec, the minimum vector length can be set via zvl<?>b

  • TVM is/was aware of this already.

Your code here touch the case for NO "v" extension presence, where TVM still needs a hint, but lets settle even this to 128.

@jerryzj
Copy link
Author

jerryzj commented Apr 19, 2025

Please refer to the ISA spec, the minimum vector length can be set via zvl<?>b

  • TVM is/was aware of this already.

Your code here touch the case for NO "v" extension presence, where TVM still needs a hint, but lets settle even this to 128.

If there is no v, zve* or other zvl specified, meaning that current CPU is not supporting vector operations hence no vector registers, why not set VLEN to 0?

@cbalint13
Copy link
Contributor

cbalint13 commented Apr 19, 2025

If there is no v, zve* or other zvl specified, meaning that current CPU is not supporting vector operations hence no vector registers, why not set VLEN to 0?

Currently vector_with != VLEN, it only becomes equivalent (and useful) with VLEN in case of SVE support (WiP).
There is a refactor effort of what is described here: #17859 , making things clear.


TVM is relaying on LLVM (well maintained) as root of trust , it extract "arch knowledge" from, not vice-versa:

Few outcomes for riscv case:

  1. If no v present, or whatever other arch it is, we still need a vector_with for TVM alignment hints (but doesn't really matter).
  2. If only v is present, but no extra attributes, then a zvl128b will be implicitly present, it will be added by LLVM library (due to ISA spec you mentioned)
  3. If v is present, and extra attributes like +zvl1024b as override, then largest one will be picked up (still from LLVM library).
  4. If no attributes are present but cpu is a known vendor one like -mcpu=sifive-x280 then zvl512b will be picked up from LLVM library side.

TVM only extracts zvl<XXX>b from LLVM library (riscv case), if they do something wrong TVM is also doing wrong.
See full scenarios that are checked as target tests: tests/python/target/test_riscv_features.py

One can use the sample code (posted here on top) to check what kind of features for various targets are yielded.

@cbalint13
Copy link
Contributor

cbalint13 commented Apr 19, 2025

@jerryzj

To further clarify where this vector_width ends up:

Up to this point we can sayvector_width != VLEN (no such relationship), but also can't set it to 0 (zero).
E.g. in case of x86 it is set to maximal 512 (for avx512) but in reality only few server class CPU have such vector feature.

Now, in the context of SVE (WiP) it will be really picked up and used to construct a list of vscale_range hints for arith provers in the SVE predication process. But SVE, in case of riscv, always implies that target have v extension, and also a minimum default zvl128b flag that will be picked up for sure from LLVM side. In this case vector_width == VLEN will be true here. This schema also will work for aarch64 SVE predication, the initial hint value of 128 will be used unaltered, this arch doesnt have so fancy flags with direct indication on the kind of vector machine.

In last instance if someone really want to enforce other initial hint value, (assuming really knowing what is doing) can override even this initial hint using -vector-width=xxx but this will get override in the case of exact inference from LLVM side (in case of riscv rvv for sure).


In the future, I would completely remove this confusing vector_width and realted codebits, and relay only on LLVM provided infos, but this will require some careful work spanning all involved targets, as a first step need to see how that alignment information could be extracted.


Let's set it to 128 as you proposed as a initial hint value, but this PR here failed to pass testcases otherwise it LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants