kendryte / k230_sdk

Kendryte K230 SDK
BSD 2-Clause "Simplified" License
165 stars 35 forks source link

[help]: Illegal Instruction Error When Running RVV Code on CanMV-K230-V1.0 #32

Closed zhanghb97 closed 8 months ago

zhanghb97 commented 10 months ago

Help

Hi,

I am attempting to run RVV code on the CanMV-K230-V1.0 board and have encountered an illegal instruction error.

I am using llvm/clang and risc-v gnu toolchain to generate the executable. The executable runs successfully on the QEMU emulator, but when I attempt to run the same one on the CanMV-K230-V1.0 board, I'm facing an "illegal instruction" error. The error message is as follows:

[ 6308.323798] log.elf[267]: unhandled signal 4 code 0x1 at 0x0000000000010662 in log.elf[10000+5f000]
[ 6308.332984] CPU: 0 PID: 267 Comm: log.elf Tainted: G           O      5.10.4 #2
[ 6308.340347] epc: 0000000000010662 ra : 00000000000108ac sp : 0000003ffff3dba0
[ 6308.347536]  gp : 00000000000720c8 tp : 00000000000747a0 t0 : 000000006ffffdff
[ 6308.354788]  t1 : 2f2f2f2f2f2f2f2f t2 : 000000000000006a s0 : 0000003ffff3dbd0
[ 6308.362044]  s1 : 0000000000010c04 a0 : 0000000000000046 a1 : 0000003ffff3dd38
[ 6308.369294]  a2 : 0000003ffff3dd48 a3 : 0000000000000000 a4 : 0000003ffff3dbf8
[ 6308.376540]  a5 : 0000000000010644 a6 : 0000000000072768 a7 : 127d6a7c7a2f4943
[ 6308.383790]  s2 : 00000000000103e8 s3 : 0000000000000000 s4 : 00000000000103e8
[ 6308.391039]  s5 : 0000000000000000 s6 : 0000000000156748 s7 : 0000000000000000
[ 6308.398291]  s8 : 0000000000000000 s9 : 0000000000159fc0 s10: 0000000000156770
[ 6308.405539]  s11: 0000000000000001 t3 : ffffffffffffffff t4 : 00000000000009d4
[ 6308.412785]  t5 : 0000000000000032 t6 : 0000000000000198
[ 6308.418122] status: 0000000200004020 badaddr: 0000000005157557 cause: 0000000000000002
Illegal instruction (core dumped)

The source code is:

#include <riscv_vector.h>
#include <stdio.h>

int main() {
  int avl = 70;
  int vl = vsetvl_e32m2(avl);
  printf("vl: %d\n", vl);

  return 0;
}

I also provide the assembly code here:

    .text
    .attribute  4, 16
    .attribute  5, "rv64i2p0_m2p0_a2p0_f2p0_d2p0_c2p0_v1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0"
    .file   "rvv-c-setvl.c"
    .globl  main                            # -- Begin function main
    .p2align    1
    .type   main,@function
main:                                   # @main
# %bb.0:
    addi    sp, sp, -48
    sd  ra, 40(sp)                      # 8-byte Folded Spill
    sd  s0, 32(sp)                      # 8-byte Folded Spill
    addi    s0, sp, 48
    li  a0, 0
    sd  a0, -40(s0)                     # 8-byte Folded Spill
    sw  a0, -20(s0)
    li  a0, 70
    sw  a0, -24(s0)
    lw  a0, -24(s0)
    vsetvli a0, a0, e32, m2, ta, mu
    sw  a0, -28(s0)
    lw  a1, -28(s0)
.Lpcrel_hi0:
    auipc   a0, %pcrel_hi(.L.str)
    addi    a0, a0, %pcrel_lo(.Lpcrel_hi0)
    call    printf@plt
                                        # kill: def $x11 killed $x10
    ld  a0, -40(s0)                     # 8-byte Folded Reload
    addi    sp, s0, -48
    ld  ra, 40(sp)                      # 8-byte Folded Reload
    ld  s0, 32(sp)                      # 8-byte Folded Reload
    addi    sp, sp, 48
    ret
.Lfunc_end0:
    .size   main, .Lfunc_end0-main
                                        # -- End function
    .type   .L.str,@object                  # @.str
    .section    .rodata.str1.1,"aMS",@progbits,1
.L.str:
    .asciz  "vl: %d\n"
    .size   .L.str, 8

    .ident  "clang version 16.0.0 (https://github.com/llvm/llvm-project.git e31d27e46048ccc3294d6b215dc778b3390e7834)"
    .section    ".note.GNU-stack","",@progbits
    .addrsig
    .addrsig_sym printf

Given that the vsetvli is seemingly the only RVV instruction in my code, I suspect it leads to the illegal instruction issue. What are the possible causes of this problem? Is there a good way to locate specific illegal commands?

Thanks in advance!

camel-cdr commented 10 months ago

AFAIK only the second core (ID=1) supports rvv, and you ran it on the first core (ID=0).

You either need to work with the src/rt-smart/userapps/ (see the k230_docs example), or configure linux to run on the second core, see this reddit discussion.

zhangxiaojingCAN commented 8 months ago

close the issue. You can reopen it if any further question.