FPGA開発日記

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

オリジナルLLVM Backendを追加しよう (17. フレーム処理)

LLVMにはすでにRISC-Vのバックエンドサポートが追加されている。しかし、勉強のために独自のRISC-V実装をLLVMに追加している。

f:id:msyksphinz:20190117013715p:plain

関数呼び出しの際のフレームを生成する。これはCpu0 BackendのChapter3_5に相当するところだ。 これがうまくいくと、main()関数を呼び出すという処理が行えるようになる。

最初に実装したところだと、なぜか途中でコード生成が止まってしまいどうなっているのか分からなかった。

% ./bin/llc -march=myriscvx32 -relocation-model=pic -filetype=asm ch3.bc -o -
        .text
        .section .mdebug.abiO32
        .previous
        .file   "ch3.cpp"
'mips32r2' is not a recognized processor for this target (ignoring processor)
'+mips32r2' is not a recognized feature for this target (ignoring feature)
'-noabicalls' is not a recognized feature for this target (ignoring feature)
'mips32r2' is not a recognized processor for this target (ignoring processor)
'+mips32r2' is not a recognized feature for this target (ignoring feature)
'-noabicalls' is not a recognized feature for this target (ignoring feature)
Selecting: t7: ch = MYRISCVXISD::Ret t6, Register:i32 $a0, t6:1

Selecting: t6: ch,glue = CopyToReg t4, Register:i32 $a0, Constant:i32<0>

Selecting: t4: ch = store<(store 4 into %ir.retval)> t0, Constant:i32<0>, FrameIndex:i32<0>, undef:i32

Selecting: t5: i32 = Register $a0

Selecting: t1: i32 = Constant<0>

Selecting: t0: ch = EntryToken
(ここで無限ループに入る)

色々調べていると、emilinateFrameIndexを実装しないと途中でハングしてしまうらしい。 ハングじゃなくてちゃんと警告を出してほしいなあ。。。

eliminateFrameIndex()をちゃんと実装すると、コードが生成されるようになった。

github.com

% ./bin/llc -march=myriscvx32 -relocation-model=pic -filetype=asm ch3.bc -o -
        .text
        .section .mdebug.abiO32
        .previous
        .file   "ch3.cpp"
        .globl  main                    # -- Begin function main
        .p2align        2
        .type   main,@function
        .ent    main                    # @main
main:
        .frame  $x8,8,$x1
        .mask   0x00000000,0
        .set    noreorder
        .set    nomacro
# %bb.0:                                # %entry
        addi    x2, x2, -8
        addi    x10, x0, 0
        sw      x10, 4(x2)
        addi    x2, x2, 8
        ret
        .set    macro
        .set    reorder
        .end    main
$func_end0:
        .size   main, ($func_end0)-main
                                        # -- End function

どうにか生成できたぞ。