Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Allow JIT compilation for system emulation #521

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

vacantron
Copy link
Collaborator

@vacantron vacantron commented Dec 17, 2024

This patch makes rv32emu support the just-in-time (JIT) compilation in system simulation. To achieve this goal, a "satp (supervisor address translation and protection)" field has been introduced to the block structure in JIT mode to ensure the block cache is replaced correctly.

The file src/system.c has been updated for the easier reuse of functions, and the MOP fusion and T2C are disabled temporarily. To boot the Linux Kernel, try the following commands:

$ make ENABLE_SYSTEM=1 ENABLE_MOP_FUSION=0 ENABLE_JIT=1 ENABLE_T2C=0
$ ./build/rv32emu -k <image> -i <rootfs> -b <dtb>

@vacantron vacantron changed the title Support just-in-time (JIT) compilation in system simulation jit: Support just-in-time (JIT) compilation in system simulation Dec 17, 2024
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Benchmark suite Current: 09fc945 Previous: 73b1ba5 Ratio
Dhrystone 1343 Average DMIPS over 10 runs 1343 Average DMIPS over 10 runs 1
Coremark 975.239 Average iterations/sec over 10 runs 979.275 Average iterations/sec over 10 runs 1.00

This comment was automatically generated by workflow using github-action-benchmark.

@jserv jserv changed the title jit: Support just-in-time (JIT) compilation in system simulation Allow JIT compilation for system emulation Dec 17, 2024
.gitignore Show resolved Hide resolved
@jserv jserv added this to the release-2024.2 milestone Dec 17, 2024
This file is generated by "tools/gen-jit-template.py"

To bring up the Linux Kernel to just-in-time (JIT) compilation, we need
to update the memory-related operation with the memory management unit.
However, the current "src/rv32_jit.c" was generated by the template.
That template reduced the rework for the repeated statements, but also
reduced the flexibility and the intuitiveness for bring up the new
feature. In this commit, we deprecate that template and just use a
regular file for the flexibility.
This commit introduces "satp" field to the block structure in JIT mode
to ensure the block cache is replaced correctly.

The MOP fusion and T2C are disabled temporarily. Use the following
commands to boot Linux Kernel:

$ make ENABLE_SYSTEM=1 ENABLE_MOP_FUSION=0 ENABLE_JIT=1 ENABLE_T2C=0
$ ./build/rv32emu -k <image> -i <rootfs> -b <dtb>
Comment on lines +845 to +848
#if RV32_HAS(SYSTEM)
if (next_blk && next_blk->satp != rv->csr_satp)
next_blk = NULL;
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments for SATP.

Comment on lines +926 to +933
#if 0
/*
* This branch lookup updating is unused since we get the PC from it and
* use function "cache_get()" achieve the branch prediction of T1C.
* However, if the structure "branch_table_t" is going to reference the
* block directly, this updating is nacessary to avoid to use the freed
* blocks.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get it. When is it safe to release the resources?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As the comments above, the current workflow is safe. However, if the implementation of branch_table is changed (e.g. storing the possible blocks directly instead of the program counters), this updating cannot be ignored.

@@ -126,6 +129,18 @@ struct riscv_internal {
riscv_word_t X[N_RV_REGS];
riscv_word_t PC;

#if RV32_HAS(JIT) && RV32_HAS(SYSTEM)
/*
* Aarch encoder only accepts 9 bits signed offset. Do not put this
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aarch?

Comment on lines +138 to +139
uint32_t is_mmio; /* whether is MMIO or not */
uint32_t type; /* 0: read, 1: write */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you use bit-fields instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To simplify the emission of load/store instructions of both x86-64 and Aarch64 in src/rv32_jit.c, I would like to keep them as unsigned integer.

@@ -704,6 +706,7 @@ static inline void remove_next_nth_ir(const riscv_t *rv,
* Strategies are being devised to increase the number of instructions that
* match the pattern, including possible instruction reordering.
*/
#if RV32_HAS(MOP_FUSION)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrap around the comment as well.

@ChinYikMing
Copy link
Collaborator

Testing Report:

x86-64 machine:
Issue: kernel panic.

Kernel message Logs:

[    0.000000] Linux version 6.1.119 (mirv32@node11) (riscv32-buildroot-linux-gnu-gcc.br_real (Buildroot 2024.08.3) 14.2.0, GNU ld (GNU Binutils) 2.42) #1 Sat Dec 14 22:54:23 CST 2024
[    0.00000] Machine model: rv32emu
[   .00000] earlycon: ns16550 at MMIO 0xf4000000 (options '')
[   .00000] printk: bootcons1655] enabled
[    0.000000] Zone ranges:
[    0.00000]   Normal   [mem 0x0000000000000000-0x000000001fffffff]
[   .00000] Movable zone start for each node
[   .00000] Early memory node ranges
[   .00000]   node   0: [mem 0x000000000000000-0x000000001ffffff]
[   .00000] Initmem setup node 0 [mem 0x000000000000000-0x000000001ffffff]
[   .00000] SBI specification v0.3 detected
[   .00000] SBI implementation 99 Version
[   .00000] SBI TIME extension detected
[   .00000] SBI SRST extension detected
[    0.000000] riscv: base ISA extensions aim
[    0.00000] riscv: ELF capabilities aim
[   .00000] Bui zonelists, mobility grouping on.  Total13004
[   .00000] Kernel command line: earlycon console=ttyS0
[   .000000] Dentry cache hash table entries: 65536 (order: 6, 26214
[    0.00000] Inode-cache hash table en3276 (or13107 bytes, linear)
[   .00000] mem auto-init: stack:off, heap alloc:off, heap free:off
[   .00000] 2428K avail323K kernel c33K rwd83K rod16K i131742K reserveK cma-reserved)
[    0.000000] SLUB: HWalign=64, Order=0-3, MinObjects=0, CPUs=1, Nodes=1
[   .000000] NR_IRQS: 64, nr_irqs: 64, preallocated irqs: 0
[    0.00000] riscv-in3 local interrupts mapped
[    0.000000] plic: interrupt-controller@0: mapped 31 interrupts with 1 handlers for 1 contexts.
[    0.000000] riscv-timer: riscv_timer_init_dt: Registering clocksource cpuid [0] hartid [0]
[    0.00000] clocksource: riscv_clocksource: mask: 0xffffffffffffffff maxefdb196d, max_idle_ns: 440795204367 ns
[   .00000] sched_clo6 bits6MHz, resolut1n219902325555ns
[   .00068] Console: colour dummy device 80x25
[    0.00093] Calibrating delay loop (skipped), value calculated using timer frequency..
[   .00097] 00 BogoMI26000)
[   .00119] pid_max: de3276 mini30
[   .00203] Mount-cache hash table entries: 1024 (order: 0, 4096 bytes, linear)
[    0.00224] Mountpoint-cache hash table ent102 (orde409 bytes, linear)
[    0.00638] ASID allocator using 9 bits (512 entries)
[    0.008134] devtmpfs: initialized
[   .01275] clocksource: jiffies: mask: 0xffffffff max_fffffff, max_idle_ns: 7645041785100000 ns
[    0.013002] futex hash table entr25 (order: -1, 3072 bytes, l
[    0.015155] NET: Registered PF_NETLINK/PF_ROUTE protocol family
[    0.018778] platform soc@F0000000: Fixed dependency cycle(s) with /soc@F0000000/interrupt-controller@0
[    0.038375] clocksource: Switched to clocksource riscv_clocksource
[   .05459] sysfs: cannot create duplicate filename '/dev/char/4:'
[   .054745] CPU: 0 PID: 1 Comm: swapper Not tainted 6.1.119 #1
[    0.054941] Hardware name: rv32emu (DT)
[   .05504] Call Trace:
[   .05512] [<c0003ad>] dump_backtrace+03
[   .05531] [<c03228d>] show05
[   .05543] [<c032576>] dump_sta04
[   .05556] [<c032579>] dump02
[   .05568] [<c010e43>] sysfs_wa09
[   .05580] [<c010e76>] sysfs_do_create_l0c
[   .05594] [<c010e7a>] sysfs_creat04
[   .05606] [<c01fd13>] dev067
[   .05619] [<c01fd36>] device_re03
[   .05631] [<c01d117>] tty_register_devi021
[   .05645] [<c01d123>] tty_register_02
[   .05658] [<c01d13b>] tty_register01d
[   .05671] [<c033d8b>] v018
[   .05683] [<c033cd4>] t016
[   .05695] [<c033e6c>] chr_d010
[   .05708] [<c000110>] do_one_i026
[   .05720] [<c032c06>] kernel_init_f021
[   .05733] [<c0325a3>] kern011
[   .05746] [<c00023d>] ret_from_exc01
[    0.062338] Kernel panic - not syncing: Couldn't register console driver
[   .062494] CPU: 0 PID: 1 Comm: swapper Not tainted 6.1.119 #1
[    0.062688] Hardware name: rv32emu (DT)
[   .06279] Call Trace:
[   .06287] [<c0003ad>] dump_backtrace+03
[   .06306] [<c03228d>] show05
[   .06318] [<c032576>] dump_sta04
[   .06331] [<c032579>] dump02
[   .06343] [<c0322b8>] f
[   .06355] [<c033d7b>] v018
[   .06367] [<c033cd4>] t016
[   .06379] [<c033e6c>] chr_d010
[   .06391] [<c000110>] do_one_i026
[   .06404] [<c032c06>] kernel_init_f021
[   .06417] [<c0325a3>] kern011
[   .06429] [<c00023d>] ret_from_exc01
[   .06445] ---[ end Kernel panic - not syncing: Couldn't register console driver ]---

Aarch64 machine:
Issue: Coremark seems running forever, perhaps into a infinite loop. I have run about 30 mins but still no any output.

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

Successfully merging this pull request may close these issues.

3 participants