ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.24k stars 2.57k forks source link

RISC-V Vector - undefined identifier `__builtin_rvv_vsetvlimax` #12106

Open kassane opened 2 years ago

kassane commented 2 years ago

Zig Version

0.10.0-dev.2981+7090f0471

Steps to Reproduce

I am trying to do some tests with the zig language using riscv vectorization, however both stage1 and stage2 are not supported. Then while using the c-code to perform these tests, but an error occurred!

References RISC-V Vector Extension Intrinsic Document RISC-V Vector extension Spec

Expected Behavior

Works

Actual Behavior

Fail

./o/c623eb095a0ac102857c1f0657e7a2b2/cimport.zig:22632:29: error: unable to translate macro: undefined identifier `__builtin_rvv_vsetvlimax`
pub const vsetvlmax_e32m1 = @compileError("unable to translate macro: undefined identifier `__builtin_rvv_vsetvlimax`"); // /opt/compiler-explorer/zig-master/lib/include/riscv_vector.h:72:9
kassane commented 2 years ago

I don't known what would be the relevance of this to the project #4456 itself. But it would help in testing in real and simulated environments of this architecture.

I use a low-cost device based on the T-Head/Alibaba model this CPU is known as D1 (Xuantie C906) and unfortunately does not have mainstream llvm support. This device supports some extensions beyond baseline_rv64 including 128bit vectorization. But here comes another internal issue of riscv-v (rvv), because currently devices released before rvv1.0 ratification, have partial support for some drafts (D1 - rvv 0.7.1 support).

topolarity commented 2 years ago

@cImport doesn't support intrinsics afaik (it translates to Zig under the hood)

More importantly, I don't believe that Zig has any way of exposing these operations (at least, not in their full generality), since its vectors are fixed-length. ARM SVE intrinsics have the same problem. This is subject to change as the language evolves, of course.

For more discussion, see: https://github.com/ziglang/zig/issues/7702

Your best bet in the meantime is to try using inline ASM instead.

kassane commented 2 years ago

Your best bet in the meantime is to try using inline ASM instead.

Yes, it is! I was recently looking at but possible solution applied to rustacean and maybe I can replicate this experiment with zig and asm. https://github.com/cryptape/rvv-encoder

--- edit

Another reference

kassane commented 1 year ago

zig cc master version no build riscv_vector.h, v0.10.x works.

see: https://godbolt.org/z/6Yacqc1qn

0.11.0-dev.3386+99fe2a23c - Error ```bash In file included from :17: /opt/compiler-explorer/zig-master/lib/include/riscv_vector.h:18:2: error: "Vector intrinsics require the vector extension." #error "Vector intrinsics require the vector extension." ^ :120:18: error: call to undeclared function 'vsetvlmax_e8m8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] size_t vlmax = vsetvlmax_e8m8(); ^ :124:25: error: call to undeclared function 'vle8ff_v_i8m8'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] vint8m8_t vec_src = vle8ff_v_i8m8(src, &vl, vlmax); ^ :124:15: error: initializing 'vint8m8_t' (aka '__rvv_int8m8_t') with an expression of incompatible type 'int' vint8m8_t vec_src = vle8ff_v_i8m8(src, &vl, vlmax); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :126:33: error: call to undeclared function 'vmseq_vx_i8m8_b1'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] vbool1_t string_terminate = vmseq_vx_i8m8_b1(vec_src, 0, vl); ^ :126:14: error: initializing 'vbool1_t' (aka '__rvv_bool1_t') with an expression of incompatible type 'int' vbool1_t string_terminate = vmseq_vx_i8m8_b1(vec_src, 0, vl); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :127:21: error: call to undeclared function 'vmsif_m_b1'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] vbool1_t mask = vmsif_m_b1(string_terminate, vl); ^ :127:14: error: initializing 'vbool1_t' (aka '__rvv_bool1_t') with an expression of incompatible type 'int' vbool1_t mask = vmsif_m_b1(string_terminate, vl); ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :129:5: error: call to undeclared function 'vse8_v_i8m8_m'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] vse8_v_i8m8_m(mask, dst, vec_src, vl); ^ :134:21: error: call to undeclared function 'vfirst_m_b1'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration] first_set_bit = vfirst_m_b1(string_terminate, vl); ^ :139:9: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes] int main() { ^ void 1 warning and 10 errors generated. Compiler returned: 1 ```
alexrp commented 2 months ago

To be clear, is there still a bug here, or should this be closed?

Rexicon226 commented 2 months ago

To be clear, is there still a bug here, or should this be closed?

The original issue that translate-c is not seeing the __builtin_rvv_vsetvlimax builtin function remains. The zig cc issue of the functions not being found is not a bug, the functions were renamed to prepend with __riscv_, so __riscv_vsetvlmax_e8m8. More context: https://github.com/llvm/llvm-project/commit/54b40a1785e9e7602fd69cd274cb46fc5746e029