Skip to content
New issue

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

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

Already on GitHub? # to your account

RISCVアセンブリ生成が失敗してしまう #1

Closed
dum-601 opened this issue Jul 1, 2022 · 4 comments
Closed

RISCVアセンブリ生成が失敗してしまう #1

dum-601 opened this issue Jul 1, 2022 · 4 comments

Comments

@dum-601
Copy link

dum-601 commented Jul 1, 2022

現在、書籍の内容に従ってLLVMのビルドを終え、RISCV向けのアセンブリを生成するコマンド(P102~103)を試しているところです。しかし、

${BUILD}/bin/llc -march=riscv64 -filetype=asm riscv_test.bc

を実行すると、以下のようなエラーが出てしまいます。

'x86-64' is not a recognized processor for this target (ignoring processor)
'generic' is not a recognized processor for this target (ignoring processor)
'+cx8' is not a recognized feature for this target (ignoring feature)
'+fxsr' is not a recognized feature for this target (ignoring feature)
'+mmx' is not a recognized feature for this target (ignoring feature)
'+sse' is not a recognized feature for this target (ignoring feature)
'+sse2' is not a recognized feature for this target (ignoring feature)
'+x87' is not a recognized feature for this target (ignoring feature)
'generic' is not a recognized processor for this target (ignoring processor)
'x86-64' is not a recognized processor for this target (ignoring processor)
'generic' is not a recognized processor for this target (ignoring processor)
'+cx8' is not a recognized feature for this target (ignoring feature)
'+fxsr' is not a recognized feature for this target (ignoring feature)
'+mmx' is not a recognized feature for this target (ignoring feature)
'+sse' is not a recognized feature for this target (ignoring feature)
'+sse2' is not a recognized feature for this target (ignoring feature)
'+x87' is not a recognized feature for this target (ignoring feature)
'generic' is not a recognized processor for this target (ignoring processor)
LLVM ERROR: Cannot select: t17: ch = store<(store 4 into %ir.i), trunc to i32> t16, Constant:i64<1>, FrameIndex:i64<2>, undef:i64
  t13: i64 = Constant<1>
  t9: i64 = FrameIndex<2>
  t4: i64 = undef
In function: main
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/kokoro601/llvm-myriscvx120/build/bin/llc -march=riscv64 -filetype=asm riscv_test.bc
1.      Running pass 'Function Pass Manager' on module 'riscv_test.bc'.
2.      Running pass 'RISCV DAG->DAG Pattern Instruction Selection' on function '@main'
 #0 0x00005620038d0e7a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/Unix/Signals.inc:565:22
 #1 0x00005620038d0f35 PrintStackTraceSignalHandler(void*) /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/Unix/Signals.inc:632:1
 #2 0x00005620038cebab llvm::sys::RunSignalHandlers() /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/Signals.cpp:71:20
 #3 0x00005620038d07c6 SignalHandler(int) /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x00007faa13207420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #5 0x00007faa12cd400b raise /build/glibc-SzIz7B/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007faa12cb3859 abort /build/glibc-SzIz7B/glibc-2.31/stdlib/abort.c:81:7
 #7 0x0000562003810b73 llvm::install_bad_alloc_error_handler(void (*)(void*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool), void*) /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/ErrorHandling.cpp:130:61
 #8 0x000056200381096c llvm::report_fatal_error(llvm::StringRef, bool) /home/kokoro601/llvm-myriscvx120/llvm/lib/Support/ErrorHandling.cpp:90:68
 #9 0x00005620036cd2e1 /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:3763:21
#10 0x00005620036cca09 llvm::SelectionDAGISel::SelectCodeCommon(llvm::SDNode*, unsigned char const*, unsigned int) /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:3666:9
#11 0x000056200213de92 llvm::RISCVDAGToDAGISel::SelectCode(llvm::SDNode*) /home/kokoro601/llvm-myriscvx120/build/lib/Target/RISCV/RISCVGenDAGISel.inc:459585:0
#12 0x000056200213c810 llvm::RISCVDAGToDAGISel::Select(llvm::SDNode*) /home/kokoro601/llvm-myriscvx120/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp:779:0
#13 0x00005620036bef89 llvm::SelectionDAGISel::DoInstructionSelection() /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1118:32
#14 0x00005620036be262 llvm::SelectionDAGISel::CodeGenAndEmitDAG() /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:957:61
#15 0x00005620036bc939 llvm::SelectionDAGISel::SelectBasicBlock(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Instruction, true, false, void>, false, true>, bool&) /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:711:1
#16 0x00005620036c163f llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:1611:33
#17 0x00005620036bb543 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/SelectionDAG/SelectionDAGISel.cpp:506:7
#18 0x000056200213de60 llvm::RISCVDAGToDAGISel::runOnMachineFunction(llvm::MachineFunction&) /home/kokoro601/llvm-myriscvx120/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h:37:3
#19 0x0000562002878c95 llvm::MachineFunctionPass::runOnFunction(llvm::Function&) /home/kokoro601/llvm-myriscvx120/llvm/lib/CodeGen/MachineFunctionPass.cpp:72:33
#20 0x0000562002ee0c3a llvm::FPPassManager::runOnFunction(llvm::Function&) /home/kokoro601/llvm-myriscvx120/llvm/lib/IR/LegacyPassManager.cpp:1435:20
#21 0x0000562002ee0f03 llvm::FPPassManager::runOnModule(llvm::Module&) /home/kokoro601/llvm-myriscvx120/llvm/lib/IR/LegacyPassManager.cpp:1481:13
#22 0x0000562002ee1375 (anonymous namespace)::MPPassManager::runOnModule(llvm::Module&) /home/kokoro601/llvm-myriscvx120/llvm/lib/IR/LegacyPassManager.cpp:1550:20
#23 0x0000562002edc470 llvm::legacy::PassManagerImpl::run(llvm::Module&) /home/kokoro601/llvm-myriscvx120/llvm/lib/IR/LegacyPassManager.cpp:541:13
#24 0x0000562002ee1c4b llvm::legacy::PassManager::run(llvm::Module&) /home/kokoro601/llvm-myriscvx120/llvm/lib/IR/LegacyPassManager.cpp:1678:1
#25 0x0000562001c21970 compileModule(char**, llvm::LLVMContext&) /home/kokoro601/llvm-myriscvx120/llvm/tools/llc/llc.cpp:687:66
#26 0x0000562001c1f64c main /home/kokoro601/llvm-myriscvx120/llvm/tools/llc/llc.cpp:385:35
#27 0x00007faa12cb5083 __libc_start_main /build/glibc-SzIz7B/glibc-2.31/csu/../csu/libc-start.c:342:3
#28 0x0000562001c1e63e _start (/home/kokoro601/llvm-myriscvx120/build/bin/llc+0x131263e)
Aborted

上のようなエラーにより、RISCV向けのアセンブリを生成することができずにいます。(P101に記載されている、x86向けアセンブリの生成には成功しています。) 解決策などありましたら、ご教授いただけると幸いです。よろしくお願いします。なお、環境は Ubuntu 20.04 (WSL) です。

@msyksphinz-self
Copy link
Owner

本件、出版前に確認できておらず申し訳ありません。clangおよびllcのオプションが不足していました。

  1. Clangのオプションを以下のように変更してください (--target=riscv64-unknown-elf を追加して、RISC-V向けのLLVM IRを出力する必要があります)
$ ${BUILD}/bin/clang bin/clang-12 riscv_test.c -emit-llvm -c --target=riscv64-unknown-elf

もしこの際に以下のようなstdio.hが読み込めないというエラーが発生した場合、お手数ですがLLVMのビルドオプションを変更して再ビルドする必要があります。

${BUILD}/bin/clang-12 riscv_test.c -emit-llvm -c --target=riscv64-unknown-elf -march=rv64imafdc
riscv_test.c:1:10: fatal error: 'stdio.h' file not found
#include <stdio.h>
         ^~~~~~~~~
1 error generated

上記のエラーが発生する場合、LLVMのビルドオプションに以下を追加して再ビルドしてください。-DDEFAULT_SYSROOT=により、RISC-VのGNUツールチェインが入っている場所を指定します。具体的には、P.70にてツールチェインのインストール場所として使用している ${HOME}/riscv64_githubを適宜読み替えてください。

cmake -G Ninja \
	-DDEFAULT_SYSROOT="${HOME}/riscv64_github/riscv64-unknown-elf" \
	-DCMAKE_BUILD_TYPE="Debug" -DLLVM_TARGETS_TO_BUILD="X86;RISCV" \
  -DLLVM_ENABLE_PROJECTS="clang;libcxx;libcxxabi" ../llvm
  1. llcのオプションを以下のように変更してください。-mattr="+d,+f"によりABIを指定する必要があります。
$ ${BUILD}/bin/llc riscv_test.bc -march=riscv64 -mattr="+d,+f" -filetype=asm

riscv_test.sが生成されれば成功となります。

  1. また、こちらの確認に関しまして以下のリポジトリに実行環境を用意しています。合わせてご確認ください。

https://github.com/msyksphinz-self/llvm-myriscvx-tests/tree/master/program/chapter03/riscv_test

$ make

${BUILD}/bin/clang-12 riscv_test.c -emit-llvm -c --target=riscv64-unknown-elf
${BUILD/build/bin/llc riscv_test.bc -march=riscv64 --float-abi=hard -mattr="+d,+f" -filetype=asm
riscv64-unknown-elf-gcc riscv_test.s -march=rv64gc -lc -o riscv_test

Spikeにより以下のように動作すれば問題ありません。

$ spike pk riscv_test
bbl loader
Total = 55

@dum-601
Copy link
Author

dum-601 commented Jul 2, 2022

1に従ってLLVMをビルドし直し、2,3のコマンドを実行すると、問題なく動作しました。
ご対応ありがとうございます。

@dum-601 dum-601 closed this as completed Jul 2, 2022
@ohno418
Copy link

ohno418 commented Jul 18, 2022

WSLでないLinux上で本を進めています。このissueと同じ問題が発生し、私の場合は上記のコメントの方法を使っても解決しませんでした。解決方法が見つかったのでここに残しておきます。

$ ${BUILD}/bin/clang -emit-llvm -c riscv_test.c --target=riscv64-unknown-linux-gnu --sysroot=${HOME}/riscv64_github/sysroot
$ ${BUILD}/bin/llc riscv_test.bc -march=riscv64 -mattr="+d,+f" -filetype=asm
$ riscv64-unknown-linux-gnu-gcc riscv_test.s -lc -static -o riscv_test
$ spike pk riscv_test

(LLVMを再ビルドしなくても--sysrootの指定でおそらく大丈夫そうです。)

@msyksphinz-self
Copy link
Owner

ご指摘ありがとうございます。こちらの件、正誤表に追記しました。

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

No branches or pull requests

3 participants