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

Fix build failures and expand test coverage #535

Merged
merged 38 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
75d3725
Treat warnings as errors by adding -Werror
eleanorLYJ Jan 5, 2025
6c8728d
Fix compiler error when enabling JIT
eleanorLYJ Jan 4, 2025
9747560
CI: Add compiler matrix for gcc and clang testing
eleanorLYJ Jan 1, 2025
6fa2dd4
Fix compiler error with JIT when Zba is disabled
eleanorLYJ Jan 4, 2025
30eca0e
CI: Add test case for disabling Zba with JIT enabled
eleanorLYJ Jan 5, 2025
8de334b
Fix compiler errors with JIT when Zbb is disabled
eleanorLYJ Jan 4, 2025
3aa33ec
CI: Add test case for disabling Zbb with JIT enabled
eleanorLYJ Jan 5, 2025
36c8123
Fix compiler errors with JIT when Zbc is disabled
eleanorLYJ Jan 4, 2025
c5838e3
CI: Add test case for disabling Zbc with JIT enabled
eleanorLYJ Jan 5, 2025
027b035
Fix compiler errors with JIT when Zbs is disabled
eleanorLYJ Jan 5, 2025
7409812
CI: Add test case for disabling Zbs with JIT enabled
eleanorLYJ Jan 5, 2025
1b6d1c1
Fix compiler error with when Zicsr is disabled
eleanorLYJ Jan 4, 2025
5d6795b
Fix compiler error with JIT when Zicsr is disabled
eleanorLYJ Jan 4, 2025
b95f752
CI: Add test case for disabling Zicsr
eleanorLYJ Jan 5, 2025
0e5dbdd
Fix compiler error with JIT when EXT_M is disabled
eleanorLYJ Jan 4, 2025
872dd60
CI: Add test case for disabling EXT_M with JIT enabled
eleanorLYJ Jan 5, 2025
bd81cc4
Fix compiler error when MOP_FUSION is disabled
eleanorLYJ Jan 5, 2025
d8f5cff
CI: Add test case for disabling MOP FUSION
eleanorLYJ Jan 5, 2025
d320440
CI: Add test case for disabling BLOCK CHAINGING
eleanorLYJ Jan 5, 2025
9a0ce5a
Fix Clang error when using 'make tests'
eleanorLYJ Jan 5, 2025
9b10a79
CI: Add test case for disabling Zba
eleanorLYJ Jan 5, 2025
dced99b
CI: Add test case for disabling Zbb
eleanorLYJ Jan 5, 2025
6d2ca02
CI: Add test case for disabling Zbc
eleanorLYJ Jan 5, 2025
7384d92
CI: Add test case for disabling Zbs
eleanorLYJ Jan 5, 2025
2ca5dcc
Fix Clang error when disabling Zicsr
eleanorLYJ Jan 5, 2025
f71f435
Fix Clang error when disabling MOP FUSION
eleanorLYJ Jan 5, 2025
01f2328
Fix Clang error when disabling BLOCK_CHAINING
eleanorLYJ Jan 5, 2025
3e0dd5a
Fix Clang error with JIT when EXT_C is disabled
eleanorLYJ Jan 5, 2025
25b9ac0
Fix Clang error with JIT when EXT_M is disabled
eleanorLYJ Jan 6, 2025
2c16b2c
Fix undefined behavior caused by mismatched parameter types
eleanorLYJ Jan 6, 2025
ad7f3b9
Fix unused variable warning when JIT and SYSTEM are enabled
eleanorLYJ Jan 10, 2025
740b601
Fix undeclared identifier 'attr' error
eleanorLYJ Jan 10, 2025
1bb3cdf
CI: Add test case for disabling Zifencei
eleanorLYJ Jan 13, 2025
eb9ea7b
CI: Add test case for disabling Zicsr with JIT enabled
eleanorLYJ Jan 13, 2025
61176c0
CI: Add test case for disabling Zifencei with JIT enabled
eleanorLYJ Jan 13, 2025
004180a
CI: Add test case for disabling MOP_FUSION with JIT enabled
eleanorLYJ Jan 13, 2025
be10551
CI: Add test case for disabling BLOCK_CHAINING with JIT enabled
eleanorLYJ Jan 13, 2025
99c52b9
CI: Add 'if: always()' for steps
eleanorLYJ Jan 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ jobs:
host-x64:
needs: [detect-code-related-file-changes]
if: needs.detect-code-related-file-changes.outputs.has_code_related_changes == 'true'
strategy:
fail-fast: false
matrix:
compiler: [gcc, clang]
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
Expand All @@ -49,47 +53,95 @@ jobs:
sudo chmod +x ./llvm.sh
sudo ./llvm.sh 18
shell: bash
- name: Install compiler
id: install_cc
uses: rlalik/setup-cpp-compiler@master
with:
compiler: ${{ matrix.compiler }}
- name: default build
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: make -j$(nproc)
if: ${{ always() }}
- name: check + tests
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make clean
make check -j$(nproc)
make tests -j$(nproc)
make misalign -j$(nproc)
make tool -j$(nproc)
if: ${{ always() }}
- name: diverse configurations
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make distclean && make ENABLE_EXT_M=0 check -j$(nproc)
make distclean && make ENABLE_EXT_A=0 check -j$(nproc)
make distclean && make ENABLE_EXT_F=0 check -j$(nproc)
make distclean && make ENABLE_EXT_C=0 check -j$(nproc)
make distclean && make ENABLE_SDL=0 check -j$(nproc)
make distclean && make ENABLE_Zicsr=0 check -j$(nproc)
make distclean && make ENABLE_MOP_FUSION=0 check -j$(nproc)
make distclean && make ENABLE_BLOCK_CHAINING=0 check -j$(nproc)
make distclean && make ENABLE_Zba=0 check -j$(nproc)
make distclean && make ENABLE_Zbb=0 check -j$(nproc)
make distclean && make ENABLE_Zbc=0 check -j$(nproc)
make distclean && make ENABLE_Zbs=0 check -j$(nproc)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe also add:
make distclean && make ENABLE_Zifencei=0 check -j$(nproc)

make distclean && make ENABLE_Zifencei=0 check -j$(nproc)
if: ${{ always() }}
- name: misalignment test in block emulation
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make -C tests/system/alignment/
make distclean && make ENABLE_ELF_LOADER=1 ENABLE_EXT_C=0 ENABLE_SYSTEM=1 misalign-in-blk-emu -j$(nproc)
if: ${{ always() }}
- name: MMU test
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make -C tests/system/mmu/
make distclean && make ENABLE_ELF_LOADER=1 ENABLE_SYSTEM=1 mmu-test -j$(nproc)
if: ${{ always() }}
- name: gdbstub test
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make distclean && make ENABLE_GDBSTUB=1 gdbstub-test -j$(nproc)
if: ${{ always() }}
- name: JIT test
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make ENABLE_JIT=1 clean && make ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_EXT_A=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_EXT_F=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_EXT_C=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_EXT_M=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_Zba=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_Zbb=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_Zbc=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_Zbs=0 ENABLE_JIT=1 check -j$(nproc)
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about repeating all diverse configuration tests with JIT enabled?

make ENABLE_JIT=1 clean && make ENABLE_Zicsr=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_Zifencei=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_MOP_FUSION=0 ENABLE_JIT=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_BLOCK_CHAINING=0 ENABLE_JIT=1 check -j$(nproc)
if: ${{ always() }}
- name: undefined behavior test
run: |
make clean && make ENABLE_UBSAN=1 check -j$(nproc)
make ENABLE_JIT=1 clean && make ENABLE_JIT=1 ENABLE_UBSAN=1 check -j$(nproc)
if: ${{ always() }}
- name: boot Linux kernel test
env:
CC: ${{ steps.install_cc.outputs.cc }}
run: |
make clean && make ENABLE_SYSTEM=1 && make ENABLE_SYSTEM=1 artifact -j$(nproc)
.ci/boot-linux.sh
make ENABLE_SYSTEM=1 clean
if: ${{ always() }}

host-arm64:
needs: [detect-code-related-file-changes]
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ BIN := $(OUT)/rv32emu
CONFIG_FILE := $(OUT)/.config
-include $(CONFIG_FILE)

CFLAGS = -std=gnu99 -O2 -Wall -Wextra
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since we are modifying this, do we also want to add -Wshadow to catch more problems :) ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this depends on whether our coding style aims to forbid local variables from shadowing other variables?

CFLAGS = -std=gnu99 -O2 -Wall -Wextra -Werror
CFLAGS += -Wno-unused-label
CFLAGS += -include src/common.h -Isrc/

Expand Down
6 changes: 3 additions & 3 deletions mk/tests.mk
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ $(CACHE_TEST_OUT): $(CACHE_TEST_TARGET)

$(CACHE_TEST_TARGET): $(CACHE_TEST_OBJS)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) $^ -o $@
$(Q)$(CC) $^ -o $@ $(LDFLAGS)

$(CACHE_TEST_OUTDIR)/%.o: $(CACHE_TEST_SRCDIR)/%.c
$(VECHO) " CC\t$@\n"
Expand All @@ -95,7 +95,7 @@ $(MAP_TEST_OUT): $(MAP_TEST_TARGET)

$(MAP_TEST_TARGET): $(MAP_TEST_OBJS)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) $^ -o $@
$(Q)$(CC) $^ -o $@ $(LDFLAGS)

$(MAP_TEST_OUTDIR)/%.o: $(MAP_TEST_SRCDIR)/%.c
$(VECHO) " CC\t$@\n"
Expand All @@ -107,7 +107,7 @@ $(PATH_TEST_OUT): $(PATH_TEST_TARGET)

$(PATH_TEST_TARGET): $(PATH_TEST_OBJS)
$(VECHO) " CC\t$@\n"
$(Q)$(CC) $^ -o $@
$(Q)$(CC) $^ -o $@ $(LDFLAGS)

$(PATH_TEST_OUTDIR)/%.o: $(PATH_TEST_SRCDIR)/%.c
$(VECHO) " CC\t$@\n"
Expand Down
88 changes: 54 additions & 34 deletions src/emulate.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ extern struct target_ops gdbstub_ops;
#define IF_imm(i, v) (i->imm == v)

#if RV32_HAS(SYSTEM)
#if !RV32_HAS(JIT)
static bool need_clear_block_map = false;
#endif
static uint32_t reloc_enable_mmu_jalr_addr;
static bool reloc_enable_mmu = false;
bool need_retranslate = false;
Expand Down Expand Up @@ -79,14 +81,14 @@ static void __trap_handler(riscv_t *rv);
}

/* FIXME: use more precise methods for updating time, e.g., RTC */
#if RV32_HAS(Zicsr)
static uint64_t ctr = 0;
static inline void update_time(riscv_t *rv)
{
rv->csr_time[0] = ctr & 0xFFFFFFFF;
rv->csr_time[1] = ctr >> 32;
}

#if RV32_HAS(Zicsr)
/* get a pointer to a CSR */
static uint32_t *csr_get_ptr(riscv_t *rv, uint32_t csr)
{
Expand Down Expand Up @@ -374,42 +376,45 @@ static uint32_t peripheral_update_ctr = 64;
#endif

/* Interpreter-based execution path */
#define RVOP(inst, code, asm) \
static bool do_##inst(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, \
uint32_t PC) \
{ \
IIF(RV32_HAS(SYSTEM))(ctr++;, ) cycle++; \
code; \
IIF(RV32_HAS(SYSTEM)) \
( \
if (need_handle_signal) { \
need_handle_signal = false; \
return true; \
}, ) nextop : PC += __rv_insn_##inst##_len; \
IIF(RV32_HAS(SYSTEM)) \
(IIF(RV32_HAS(JIT))( \
, if (unlikely(need_clear_block_map)) { \
block_map_clear(rv); \
need_clear_block_map = false; \
rv->csr_cycle = cycle; \
rv->PC = PC; \
return false; \
}), ); \
if (unlikely(RVOP_NO_NEXT(ir))) \
goto end_op; \
const rv_insn_t *next = ir->next; \
MUST_TAIL return next->impl(rv, next, cycle, PC); \
end_op: \
rv->csr_cycle = cycle; \
rv->PC = PC; \
return true; \
#define RVOP(inst, code, asm) \
static bool do_##inst(riscv_t *rv, const rv_insn_t *ir, uint64_t cycle, \
uint32_t PC) \
{ \
IIF(RV32_HAS(SYSTEM))(ctr++;, ) cycle++; \
code; \
IIF(RV32_HAS(SYSTEM)) \
( \
if (need_handle_signal) { \
need_handle_signal = false; \
return true; \
}, ) nextop : PC += __rv_insn_##inst##_len; \
IIF(RV32_HAS(SYSTEM)) \
(IIF(RV32_HAS(JIT))( \
, if (unlikely(need_clear_block_map)) { \
block_map_clear(rv); \
need_clear_block_map = false; \
rv->csr_cycle = cycle; \
rv->PC = PC; \
return false; \
}), ); \
if (unlikely(RVOP_NO_NEXT(ir))) \
goto end_op; \
const rv_insn_t *next = ir->next; \
MUST_TAIL return next->impl(rv, next, cycle, PC); \
end_op: \
rv->csr_cycle = cycle; \
rv->PC = PC; \
return true; \
}

#include "rv32_template.c"
#undef RVOP

/* multiple LUI */
static bool do_fuse1(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
static bool do_fuse1(riscv_t *rv,
const rv_insn_t *ir,
uint64_t cycle,
uint32_t PC)
{
cycle += ir->imm2;
opcode_fuse_t *fuse = ir->fuse;
Expand All @@ -426,7 +431,10 @@ static bool do_fuse1(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
}

/* LUI + ADD */
static bool do_fuse2(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
static bool do_fuse2(riscv_t *rv,
const rv_insn_t *ir,
uint64_t cycle,
uint32_t PC)
{
cycle += 2;
rv->X[ir->rd] = ir->imm;
Expand All @@ -442,7 +450,10 @@ static bool do_fuse2(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
}

/* multiple SW */
static bool do_fuse3(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
static bool do_fuse3(riscv_t *rv,
const rv_insn_t *ir,
uint64_t cycle,
uint32_t PC)
{
cycle += ir->imm2;
opcode_fuse_t *fuse = ir->fuse;
Expand All @@ -466,7 +477,10 @@ static bool do_fuse3(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
}

/* multiple LW */
static bool do_fuse4(riscv_t *rv, rv_insn_t *ir, uint64_t cycle, uint32_t PC)
static bool do_fuse4(riscv_t *rv,
const rv_insn_t *ir,
uint64_t cycle,
uint32_t PC)
{
cycle += ir->imm2;
opcode_fuse_t *fuse = ir->fuse;
Expand Down Expand Up @@ -550,6 +564,7 @@ FORCE_INLINE bool insn_is_translatable(uint8_t opcode)
}
#endif

#if RV32_HAS(BLOCK_CHAINING)
FORCE_INLINE bool insn_is_unconditional_branch(uint8_t opcode)
{
switch (opcode) {
Expand All @@ -558,7 +573,9 @@ FORCE_INLINE bool insn_is_unconditional_branch(uint8_t opcode)
case rv_insn_jal:
case rv_insn_jalr:
case rv_insn_mret:
#if RV32_HAS(Zicsr)
case rv_insn_csrrw:
#endif
#if RV32_HAS(SYSTEM)
case rv_insn_sret:
#endif
Expand Down Expand Up @@ -588,6 +605,7 @@ FORCE_INLINE bool insn_is_direct_branch(uint8_t opcode)
return false;
}
}
#endif

FORCE_INLINE bool insn_is_indirect_branch(uint8_t opcode)
{
Expand Down Expand Up @@ -664,6 +682,7 @@ static void block_translate(riscv_t *rv, block_t *block)
block->ir_tail->next = NULL;
}

#if RV32_HAS(MOP_FUSION)
#define COMBINE_MEM_OPS(RW) \
next_ir = ir->next; \
count = 1; \
Expand Down Expand Up @@ -800,6 +819,7 @@ static void match_pattern(riscv_t *rv, block_t *block)
}
}
}
#endif

typedef struct {
bool is_constant[N_RV_REGS];
Expand Down
4 changes: 4 additions & 0 deletions src/jit.c
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ static inline void emit_alu64(struct jit_state *state, int op, int src, int dst)
#endif
}

#if RV32_HAS(EXT_M)
static inline void emit_alu64_imm8(struct jit_state *state,
int op,
int src UNUSED,
Expand All @@ -742,6 +743,7 @@ static inline void emit_alu64_imm8(struct jit_state *state,
}
#endif
}
#endif

/* Register to register mov */
static inline void emit_mov(struct jit_state *state, int src, int dst)
Expand Down Expand Up @@ -1690,6 +1692,7 @@ static void ra_load2(struct jit_state *state, int vm_reg_idx1, int vm_reg_idx2)
offsetof(riscv_t, X) + 4 * vm_reg_idx2);
}

#if RV32_HAS(EXT_M)
static void ra_load2_sext(struct jit_state *state,
int vm_reg_idx1,
int vm_reg_idx2,
Expand Down Expand Up @@ -1733,6 +1736,7 @@ static void ra_load2_sext(struct jit_state *state,
offsetof(riscv_t, X) + 4 * vm_reg_idx2);
}
}
#endif

void parse_branch_history_table(struct jit_state *state, rv_insn_t *ir)
{
Expand Down
4 changes: 3 additions & 1 deletion src/riscv.c
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,10 @@ bool rv_has_halted(riscv_t *rv)
void rv_delete(riscv_t *rv)
{
assert(rv);
#if !RV32_HAS(JIT)
#if !RV32_HAS(JIT) || (RV32_HAS(SYSTEM) && !RV32_HAS(ELF_LOADER))
vm_attr_t *attr = PRIV(rv);
#endif
#if !RV32_HAS(JIT)
map_delete(attr->fd_map);
memory_delete(attr->mem);
block_map_destroy(rv);
Expand Down
2 changes: 2 additions & 0 deletions src/t2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ FORCE_INLINE LLVMBasicBlockRef t2c_block_map_search(struct LLVM_block_map *map,
T2C_LLVM_GEN_ADDR(rs1, X, ir->rs1);
T2C_LLVM_GEN_ADDR(rs2, X, ir->rs2);
T2C_LLVM_GEN_ADDR(rd, X, ir->rd);
#if RV32_HAS(EXT_C)
T2C_LLVM_GEN_ADDR(ra, X, rv_reg_ra);
T2C_LLVM_GEN_ADDR(sp, X, rv_reg_sp);
#endif
T2C_LLVM_GEN_ADDR(PC, PC, 0);

#define T2C_LLVM_GEN_STORE_IMM32(builder, val, addr) \
Expand Down
Loading
Loading