--- tags: Research --- # rv32emu 加速規劃 參考 [ria-jit](https://github.com/ria-jit/ria-jit/blob/master/src/gen/instr/patterns.c) `IR` 格式 ``` { instruction name, rs1, rs2, rd, h1, h2, imm} * mnem : mnem * rs1, rs2, rd : * rs1_h1 means, that rs1 of the instruction * has to be the same as in the instruction at * the position stored in h1. (block_cache[pattern_start + h1]). * rs2, rd work the same. all are also available with h2. * * Example: * pattern_element jsjfbefjb[] = { * {.....}, * {.....}, * {ADDI, rd_h1, DONT_CARE, rs1_h2, 1, 0, 1, 32} * }; * * In this example, rs1 of the 3rd instruction has to be * the same register as rd of the second instruction, * and rd has to be the same register as rs1 of the first instruction. * The immediate has to be 32. * * NOTE: not all options are implemented for every field yet * (to avoid unnecessary comparisons in matcher). * Do so in the switch-case under the corresponding comments (e.g. "rs1 match") in optimize.c * * h1, h2 : helper variables * * imm : immediate * ``` Parse elf file 並將 elf file 中的指令轉成上述 IR 格式,一個 IR block 存有一串指令轉換後的 IR,IR block 的邊界為 branch 指令、jump 指令﹑label 或程式結束。這些 IR block 會存在 block cache 中,key 值為 block 中第一個指令的 `pc`。 ## 更改部分 1. 在 function `elf_load` 中將指令轉為 IR,原本會將指令和資料寫入 memory,改動後只寫入資料。 2. 原本 JIT 流程如果找不到 block 會進行 `fetch_and_record` 指令,現在改成直接到 block cache 找對應的 IR block。 3. 將 instruction to C code 改寫成 IR to C code ![](https://i.imgur.com/yNwIcvZ.png) >圖為原始 JIT 實作與加入 IR 後的比較 ## 預期加速部分 * `fetch_and_record` 指令改成直接到 block cache 找對應的 IR block。 * IR 轉成 C code 時可以 package 多個指令,因為這個 IR 格式可以檢查暫存器的相依性 1. AUIPC + ADDI -> 指令合成一個 funciton 2. AUIPC + LW -> 指令合成一個 funciton 3. LUI + ALU 指令 -> 指令合成一個 funciton 4. ALU 指令 + branch 指令 -> 指令合成一個 funciton 5. LW + ALU 指令 + branch 指令 -> 指令合成一個 funciton 6. SLLI + SRLI -> 指令合成一個 funciton(改成用 and mask) ...待補