riscv-collab / riscv-gnu-toolchain

GNU toolchain for RISC-V, including GCC
Other
3.42k stars 1.13k forks source link

Adding custom instruction to vector compiler #1055

Closed Mousavikia closed 1 year ago

Mousavikia commented 2 years ago

Hi I have installed vector version of gcc using following instructions:

git clone https://github.com/riscv/riscv-gnu-toolchain -b rvv-intrinsic
cd riscv-gnu-toolchain
git submodule update --init --recursive
mkdir build && cd build
../configure --prefix=/desired/installation/path
make

I know that you (@jim-wilson ) said this branch is not reliable and llvm is better for vector extension, but consider I want to use this (since I don't know how to do the following in llvm), How can I add custom vector instructions to this version? I have done it for normal compiler (Main branch) is this totally same or should I do some thing else?

zhongjuzhe commented 2 years ago

We have a new version of GCC with full implementation for RVV including intrinsic and auto-vectorization. And I am actively maintaining recently and cleaning up the codes, it will be pushed upstream GCC in the future. I would suggest you use the branch riscv-gcc-rvv-next in riscv-gcc branch and riscv-binutils-2.38 in riscv-binutils to build up the whole toolchain. The intrinsic supports are all listed in here:https://github.com/riscv-non-isa/rvv-intrinsic-doc/tree/new-policy-proposal/overloaded_intrinsic_funcs To support auto-vectorization: -mrvv -mriscv-vector-bits. Any PRs or questions are all welcome.

BHbean commented 2 years ago

Excuse me, could you(@zhongjuzhe) please tell me if there exists any docs for building? I want to build a riscv-toolchain that contains rvv intrinsic using riscv-gcc-rvv-next branch in riscv-gcc repo. However, I did not see any guidance in the repo. Besides, as I have no admin permission on the server, I could not use texi2html command to generate an HTML doc to read. So should I just follow the steps on https://gcc.gnu.org/install/? Are there any differences when building this riscv-gcc-rvv-next branch? Thanks in advance!

zhongjuzhe commented 2 years ago

@BHbean To build the whole toolchain, I suggest you use this repo: https://github.com/riscv-collab/riscv-gnu-toolchain which already assemble binutils, gcc, binutils,....,etc. Follow the guideline of README file, you can build up the whole toolchain. To support RVV feature, you just need to checkout riscv-gcc-rvv-next in riscv-gcc and riscv-binutils-2.38 in riscv-binutils. There is no difference when building the toolchain with riscv-gcc-rvv-next branch in riscv-gcc.

BHbean commented 2 years ago

@zhongjuzhe I think I got u. I will explain my understanding as followed. Firstly, I just get the toolchain sources using

git clone https://github.com/riscv/riscv-gnu-toolchain -b rvv-intrinsic

Then I just need to change the .gitmodules file for the following modules

[submodule "riscv-binutils"]
    path = riscv-binutils
    url = https://github.com/riscv-collab/riscv-binutils-gdb.git
    branch = riscv-binutils-2.38
[submodule "riscv-gcc"]
    path = riscv-gcc
    url = https://github.com/riscv-collab/riscv-gcc.git
    branch = riscv-gcc-rvv-next

and leave other modules the same. Finally, I just need to follow the guideline of README file to finish my build. Did I make it right?

zhongjuzhe commented 2 years ago

@BHbean Yes, I think you are right. Don't forget to git pull after you change the .gitmodules file and then make sure the repo is what you want using git log. Besides, to enable RVV feature, you should bring v into --with-arch compile option. For example --with-arch=rv64gcv.

BHbean commented 2 years ago

@zhongjuzhe Thanks a lot! Your suggestions are really useful! I will try it out as soon as possible!

MahmouodMagdi commented 2 years ago

@zhongjuzhe can you help me to build the toolchain with vector support please ? I just need the right commands to write it on the terminal

zhongjuzhe commented 2 years ago

@zhongjuzhe can you help me to build the toolchain with vector support please ? I just need the right commands to write it on the terminal

./configure --prefix=/opt/riscv --with-arch=rv64gcv_zfh --with-abi=lp64d make

MahmouodMagdi commented 2 years ago

@zhongjuzhe can you help me to build the toolchain with vector support please ? I just need the right commands to write it on the terminal

./configure --prefix=/opt/riscv --with-arch=rv64gcv_zfh --with-abi=lp64d

make

Thanks for your help ♥️

LiQi19970428 commented 2 years ago

@zhongjuzhe Hi I have installed vector version of gcc,but there is something wrong with auto-vectorization: -mrvv。 And I have also changed the .gitmodules file。Modules are as followed: $ git submodule -9826b03b747b841f5fc6de2054bf1ef3f5c4bdf3 glibc -85e0e3519655220688e757b9d5bfd314923548bd musl 415fdd4279b85eeec9d54775ce13c5c412451e08 newlib (newlib-4.1.0) -553032db17440f8de011390e5a1cfddd13751b0b qemu 20756b0fbe065a84710aa38f2457563b57546440 riscv-binutils (heads/riscv-binutils-2.38) -4ea498a8e1fafeb568530d84db1880066478c86b riscv-dejagnu +05f2bee1fab961c3dedd46af317dc3320886a731 riscv-gcc (heads/riscv-gcc-rvv-next) 5da071ef0965b8054310d8dde9975037b0467311 riscv-gdb (remotes/origin/fsf-gdb-10.1-with-sim)

But it went wrong as followed: riscv64-unknown-elf-gcc: error: unrecognized command line option '-mrvv'

What should I do? Thanks.

zhongjuzhe commented 2 years ago

I removed -mrvv compile option to enable auto-vectorization. auto-vectorization is enable by -free-vectorize which is common GCC compile option. If you use - O3,the autovectorization is enabled by default.---- Replied Message @.>Date06/03/2022 19:14 @.> @.**@.>SubjectRe: [riscv-collab/riscv-gnu-toolchain] Adding custom instruction to vector compiler (Issue #1055)

@zhongjuzhe

Hi I have installed vector version of gcc,but there is something wrong with auto-vectorization: -mrvv。

And I have also changed the .gitmodules file。Modules are as followed:

$ git submodule

-9826b03b747b841f5fc6de2054bf1ef3f5c4bdf3 glibc

-85e0e3519655220688e757b9d5bfd314923548bd musl

415fdd4279b85eeec9d54775ce13c5c412451e08 newlib (newlib-4.1.0)

-553032db17440f8de011390e5a1cfddd13751b0b qemu

20756b0fbe065a84710aa38f2457563b57546440 riscv-binutils (heads/riscv-binutils-2.38)

-4ea498a8e1fafeb568530d84db1880066478c86b riscv-dejagnu

+05f2bee1fab961c3dedd46af317dc3320886a731 riscv-gcc (heads/riscv-gcc-rvv-next)

5da071ef0965b8054310d8dde9975037b0467311 riscv-gdb (remotes/origin/fsf-gdb-10.1-with-sim)

But it went wrong as followed:

riscv64-unknown-elf-gcc: error: unrecognized command line option '-mrvv'

What should I do?

Thanks.

—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you were mentioned.Message ID: @.***>

[

{

@.***": "http://schema.org",

@.***": "EmailMessage",

"potentialAction": {

@.***": "ViewAction",

"target": "https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1055#issuecomment-1145858821",

"url": "https://github.com/riscv-collab/riscv-gnu-toolchain/issues/1055#issuecomment-1145858821",

"name": "View Issue"

},

"description": "View this Issue on GitHub",

"publisher": {

@.***": "Organization",

"name": "GitHub",

"url": "https://github.com"

}

}

]

LiQi19970428 commented 2 years ago

I used the examples you provided, but the results obtained did not seem to support RVV.The command is riscv64-unknown-elf-gcc -ftree-vectorize -O3 -S auto.c and the result is foo: beqz a3,.L1 slli a3,a3,1 add a6,a2,a3 .L3: lb a5,0(a0) lb a3,0(a1) lhu a4,0(a2) addi a2,a2,2 mulw a5,a5,a3 addi a0,a0,1 addi a1,a1,1 addw a5,a5,a4 sh a5,-2(a2) bne a6,a2,.L3 .L1: ret

zhongjuzhe commented 2 years ago

riscv-gcc (heads/riscv-gcc-rvv-next) 5da071ef0965b8054310d8dde9975037b0467311 riscv-gdb (remotes/origin/fsf-gdb-10.1-with-sim)

It's weird. Can you use riscv64-unknown-elf-gcc -v and show me the message in your terminal?

LiQi19970428 commented 2 years ago

Thanks.The message is as followed Using built-in specs. COLLECT_GCC=riscv64-unknown-elf-gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/riscv64-unknown-elf/9.3.0/lto-wrapper Target: riscv64-unknown-elf Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr --includedir='/usr/include' --mandir='/usr/share/man' --infodir='/usr/share/info' --sysconfdir=/etc --localstatedir=/var --disable-silent-rules --libdir='/usr/lib/x86_64-linux-gnu' --libexecdir='/usr/lib/x86_64-linux-gnu' --disable-maintainer-mode --disable-dependency-tracking --target=riscv64-unknown-elf --prefix=/usr --infodir=/usr/share/doc/gcc-riscv64-unknown-elf/info --mandir=/usr/share/man --htmldir=/usr/share/doc/gcc-riscv64-unknown-elf/html --pdfdir=/usr/share/doc/gcc-riscv64-unknown-elf/pdf --bindir=/usr/bin --libexecdir=/usr/lib --libdir=/usr/lib --with-pkgversion= --disable-shared --disable-threads --enable-languages=c,c++ --enable-tls --with-newlib --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --with-system-zlib --enable-checking=yes --enable-multilib --with-abi=lp64d --disable-libstdcxx-pch --disable-libstdcxx --disable-fixinc --with-arch=rv64imafdc --with-gnu-as --with-gnu-ld --with-as=/usr/lib/riscv64-unknown-elf/bin/as --with-ld=/usr/lib/riscv64-unknown-elf/bin/ld AR_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/ar AS_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/as NM_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/nm LD_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/ld OBJDUMP_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/objdump RANLIB_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/ranlib READELF_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/readelf STRIP_FOR_TARGET=/usr/lib/riscv64-unknown-elf/bin/strip CFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' CPPFLAGS='-Wdate-time -D_FORTIFY_SOURCE=2' CXXFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' FCFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' FFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' GCJFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' LDFLAGS='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now' OBJCFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' OBJCXXFLAGS='-g -O2 -fdebug-prefix-map=/build/gcc-riscv64-unknown-elf-3seJsn/gcc-riscv64-unknown-elf-9.3.0=. -fstack-protector-strong' 'CFLAGS_FOR_TARGET=-Os -mcmodel=medany' 'CXXFLAGS_FOR_TARGET=-Os -mcmodel=medany' Thread model: single gcc version 9.3.0 ()

zhongjuzhe commented 2 years ago

You can see the last sentence: gcc version 9.3.0 (). You are using the wrong GCC. The RVV GCC should be gcc version 12. You can use git log in the riscv-gcc directory to check whether the last commit is me.

LiQi19970428 commented 2 years ago

Thanks. I used git log and the last commit is you.

zhongjuzhe commented 2 years ago

Does it work now? If it is not working, I suggest you git pull + recompile the toolchain again until you get the gcc version 12. From your message, it is obvious that you are using an old GCC which is GCC 9.3.0.

@.***

From: Late Date: 2022-06-07 14:53 To: riscv-collab/riscv-gnu-toolchain CC: zhongjuzhe; Mention Subject: Re: [riscv-collab/riscv-gnu-toolchain] Adding custom instruction to vector compiler (Issue #1055) Thanks. I used git log and the last commit is you. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

LiQi19970428 commented 2 years ago

Thanks.But it is not working. And the instructions are as followed:

git clone https://github.com/riscv/riscv-gnu-toolchain cd riscv-gnu-toolchain git clone --recursive https://git.qemu.org/git/qemu.git git clone --recursive https://github.com/riscv/riscv-newlib.git git clone -b riscv-binutils-2.38 --recursive https://github.com/riscv/riscv-binutils-gdb.git git clone --recursive https://github.com/riscv/riscv-dejagnu.git git clone --recursive https://github.com/riscv/riscv-glibc.git git clone -b riscv-gcc-rvv-next --recursive https://github.com/riscv/riscv-gcc.git ./configure --prefix=/home/liqi/riscv --with-arch=rv64gcv --with-abi=lp64d sudo make

Is there something wrong?

zhongjuzhe commented 2 years ago

Can you which riscv64-unknown-elf-gcc to check the path of the toolchain you install?

@.***

From: Late Date: 2022-06-07 17:02 To: riscv-collab/riscv-gnu-toolchain CC: zhongjuzhe; Mention Subject: Re: [riscv-collab/riscv-gnu-toolchain] Adding custom instruction to vector compiler (Issue #1055) Thanks.But it is not working. And the instructions are as followed: git clone https://github.com/riscv/riscv-gnu-toolchain cd riscv-gnu-toolchain git clone --recursive https://git.qemu.org/git/qemu.git git clone --recursive https://github.com/riscv/riscv-newlib.git git clone -b riscv-binutils-2.38 --recursive https://github.com/riscv/riscv-binutils-gdb.git git clone --recursive https://github.com/riscv/riscv-dejagnu.git git clone --recursive https://github.com/riscv/riscv-glibc.git git clone -b riscv-gcc-rvv-next --recursive https://github.com/riscv/riscv-gcc.git ./configure --prefix=/home/liqi/riscv --with-arch=rv64gcv --with-abi=lp64d sudo make Is there something wrong? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

LiQi19970428 commented 2 years ago

It's on /usr/bin/riscv64-unknown-elf-gcc,and according to the date of the file, it is indeed an old version. So is compilation failed?

zhongjuzhe commented 2 years ago

Use /home/liqi/riscv/bin/riscv64-unknown-elf-gcc -O3 -S auto.c.

@.***

From: Late Date: 2022-06-07 17:32 To: riscv-collab/riscv-gnu-toolchain CC: zhongjuzhe; Mention Subject: Re: [riscv-collab/riscv-gnu-toolchain] Adding custom instruction to vector compiler (Issue #1055) It's on /usr/bin/riscv64-unknown-elf-gcc,and according to the date of the file, it is indeed an old version. So is compilation failed? — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

LiQi19970428 commented 2 years ago

Thank you for your help. Sorry to take up your precious time.

zhongjuzhe commented 2 years ago

Does it work now?

@.***

From: Late Date: 2022-06-07 21:12 To: riscv-collab/riscv-gnu-toolchain CC: zhongjuzhe; Mention Subject: Re: [riscv-collab/riscv-gnu-toolchain] Adding custom instruction to vector compiler (Issue #1055) Thank you for your help. Sorry to take up your precious time. — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you were mentioned.Message ID: @.***>

LiQi19970428 commented 2 years ago

Yes,thanks。

TommyMurphyTM1234 commented 1 year ago

This is a list of all issues logged that I can find that relate to adding new instructions to the toolchain:

At this stage in the evolution of RISC-V,the majority of instruction addition will be done under the auspices of the relevant groups working on standardising, prototyping, and implementing/enhancing RISC-V extensions and their corresponding instructions. As such, there should be less of a demand for others to do so.

However, where somebody still wants to implement a non stadard custom extension/instruction here are some useful resources: