FPGA開発日記

カテゴリ別記事インデックス https://msyksphinz.github.io/github_pages , English Version https://fpgadevdiary.hatenadiary.com/

"Creating an LLVM Backend for the Cpu0 Architecture"をやってみる(3. BackEndの追加)

f:id:msyksphinz:20181123225150p:plain

LLVMにCpu0アーキテクチャを追加するチュートリアル。次はBackEndの追加を行う。参考にしたのは以下。

  • Tutorial: Creating an LLVM Backend for the Cpu0 Architecture : Backend structure

Backend structure — Tutorial: Creating an LLVM Backend for the Cpu0 Architecture

チュートリアルLLVMのバージョンはどうも古いようで、きちんとパッチが当たらない。 いろいろ改造したので、GitHubに7.0.0に対応したものを構築した。

github.com

まずは簡単にバックエンドを追加。ビルドすると以下のような結果になった。Chapter-3_0の部分を追加した。 AssertionがFailとなる。

$ ./bin/llc -march=cpu0 -mcpu=help
Available CPUs for this target:

  cpu032I  - Select the cpu032I processor.
  cpu032II - Select the cpu032II processor.

Available features for this target:

  ch10_1   - Enable Chapter instructions..
  ch11_1   - Enable Chapter instructions..
  ch11_2   - Enable Chapter instructions..
  ch12_1   - Enable Chapter instructions..
  ch3_1    - Enable Chapter instructions..
...
llc: /home/msyksphinz/work/riscv/llvm-msyksphinz/lib/CodeGen/LLVMTargetMachine.cpp:60: void llvm::LLVMTargetMachine::initAsmInfo(): Assertion `TmpAsmInfo && "MCAsmInfo not initialized. " "Make sure you include the correct TargetSelect.h" "and that InitializeAllTargetMCs() is being invoked!"' failed.
...

次にAssertion Failを直す。Chapter3_2を追加する。

github.com

一点、CPURegSizeの実装が古かったので、いろいろ調べて以下のように変更した。

  • 変更前
  unsigned CPURegSize = Cpu0::CPURegsRegClass.getSize();
  • 変更後
  unsigned CPURegSize = TRI->getRegSizeInBits(Cpu0::CPURegsRegClass) / 8;

変更を実装後にビルドを行うと、Assertion Failは出なくなった。

$ ./bin/llc -march=cpu0 -mcpu=help
Available CPUs for this target:

  cpu032I  - Select the cpu032I processor.
  cpu032II - Select the cpu032II processor.

Available features for this target:

  ch10_1   - Enable Chapter instructions..
  ch11_1   - Enable Chapter instructions..
  ch11_2   - Enable Chapter instructions..
  ch12_1   - Enable Chapter instructions..
  ch3_1    - Enable Chapter instructions..
  ch3_2    - Enable Chapter instructions..
  ch3_3    - Enable Chapter instructions..
  ch3_4    - Enable Chapter instructions..
  ch3_5    - Enable Chapter instructions..
  ch4_1    - Enable Chapter instructions..
  ch4_2    - Enable Chapter instructions..
  ch5_1    - Enable Chapter instructions..
  ch6_1    - Enable Chapter instructions..
  ch7_1    - Enable Chapter instructions..
  ch8_1    - Enable Chapter instructions..
  ch8_2    - Enable Chapter instructions..
  ch9_1    - Enable Chapter instructions..
  ch9_2    - Enable Chapter instructions..
  ch9_3    - Enable Chapter instructions..
  chall    - Enable Chapter instructions..
  cmp      - Enable 'cmp' instructions..
  cpu032I  - Cpu032I ISA Support.
  cpu032II - Cpu032II ISA Support (slt).
  slt      - Enable 'slt' instructions..