Skip to content

Conversation

@Timmmm
Copy link
Collaborator

@Timmmm Timmmm commented Feb 5, 2025

This adds basic support for Sail unit tests. These have some pros and cons over C tests:

Pros:

  • They don't require a RISC-V C compiler.
  • They can test internal code (hence why I've called them unit tests).
  • They can "cheat", e.g. to change privilege mode you can just magically cur_privilege = Supervisor. With C you have to implement a syscall, etc.

Cons:

  • They are tied to internal code which will make refactoring more tedious because it means you are likely to need to update the tests. We can minimise that by using "high level" functions like execute(), read_CSR(), etc.
  • They only run on the Sail model, so we can't e.g. verify them against SPIKE.

Given the sorry state of testing in this repo, and the fact that we don't have a way of writing C tests at all yet, I think this is probably a good medium term approach.

Currently the unit test executable reuses a load of code from the emulator, which is not ideal. When we have wrapped the model in a nice C++ library we can use that instead.

@jordancarlin
Copy link
Collaborator

Feels like these have the potential to be a lower barrier of entry for writing certain kinds of tests,(especially for tricky to reach edge cases of the system), so seems like a good idea to me. Once we get the C/assembly test situation sorted out we can think about what makes sense to run as a C test versus a unit test, but having both options seems like a net positive.

@jordancarlin
Copy link
Collaborator

jordancarlin commented Apr 3, 2025

@Timmmm Now that Sail 0.19 is released with unit test support, this is something that can be finished up if we still want it. Not sure that there are a whole lot of tests that should exist here instead of as first party assembly tests, but seems reasonable to include support for them.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 39c5491 to 6598087 Compare May 13, 2025 09:52
@Timmmm Timmmm marked this pull request as ready for review May 13, 2025 09:53
@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 6598087 to e73b81d Compare May 13, 2025 10:31
@github-actions
Copy link

github-actions bot commented May 13, 2025

Test Results

401 tests  +1   401 ✅ +1   1m 32s ⏱️ +3s
  1 suites ±0     0 💤 ±0 
  1 files   ±0     0 ❌ ±0 

Results for commit 359c327. ± Comparison against base commit d8c3006.

♻️ This comment has been updated with latest results.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch 3 times, most recently from 8a55801 to a1d9603 Compare May 23, 2025 10:00
Copy link
Collaborator

@jordancarlin jordancarlin left a comment

Choose a reason for hiding this comment

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

Might be worth adding a README to the test/unit_tests directory explaining that the tests themselves are in the model/unit_tests directory or directly inline with the model. Otherwise, LGTM. Great to get more testing in!

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from a1d9603 to d4e8acd Compare June 13, 2025 12:08
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 13, 2025

Rebased & README added.

@Timmmm Timmmm added will be merged Scheduled to be merged soon if nobody objects and removed will be merged Scheduled to be merged soon if nobody objects labels Jun 13, 2025
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 13, 2025

I'll see if I can sort out the Lean errors.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from d4e8acd to 272ea4a Compare June 16, 2025 14:01
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jun 16, 2025

Rebased but it's broken now and awkward to fix due to using the Sail generated header. Will probably have to wait until runtime XLEN is merged.

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 272ea4a to bea6229 Compare July 1, 2025 13:15
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jul 1, 2025

Rebased & tweaked a little to handle the changes since. I'll give this a couple of days before merging in case anyone wants to take a look.

@Timmmm Timmmm added the will be merged Scheduled to be merged soon if nobody objects label Jul 1, 2025
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jul 1, 2025

Ah yeah good call - also since the XLEN stuff was merged I can move the generated C into the library. I renamed it to riscv_model since it's not just the support code now.

@Timmmm Timmmm removed the will be merged Scheduled to be merged soon if nobody objects label Jul 1, 2025
@jordancarlin
Copy link
Collaborator

Seems to have broken Lean:

error: build failed
✖ [45/46] Building LeanRV64D
trace: .> LEAN_PATH=/home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/.lake/build/lib/lean /home/runner/.elan/toolchains/leanprover--lean4-nightly---nightly-2025-06-21/bin/lean --tstack=400000 /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/LeanRV64D.lean -R /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D -o /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/.lake/build/lib/lean/LeanRV64D.olean -i /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/.lake/build/lib/lean/LeanRV64D.ilean -c /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/.lake/build/ir/LeanRV64D.c --setup /home/runner/work/sail-riscv/sail-riscv/build/model/Lean_RV64D/.lake/build/ir/LeanRV64D.setup.json --json
error: LeanRV64D.lean:171:32: unknown identifier 'undefined_RVFI_DII_Instruction_Packet'
error: LeanRV64D.lean:172:30: unknown identifier 'undefined_RVFI_DII_Execution_Packet_InstMetaData'
error: LeanRV64D.lean:173:28: unknown identifier 'undefined_RVFI_DII_Execution_Packet_PC'
error: LeanRV64D.lean:174:29: unknown identifier 'undefined_RVFI_DII_Execution_Packet_Ext_Integer'
error: LeanRV64D.lean:176:29: unknown identifier 'undefined_RVFI_DII_Execution_Packet_Ext_MemAccess'
error: LeanRV64D.lean:355:91: unknown identifier 'sail_main'
error: Lean exited with code 1

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 1a02bb3 to 80ec84e Compare July 2, 2025 08:00
This adds basic support for Sail unit tests. These have some pros and cons over C tests:

Pros:

* They don't require a RISC-V C compiler.
* They can test internal code (hence why I've called them unit tests).
* They can "cheat", e.g. to change privilege mode you can just magically `cur_privilege = Supervisor`. With C you have to implement a syscall, etc.

Cons:

* They are tied to internal code which will make refactoring more tedious because it means you are likely to need to update the tests. We can minimise that by using "high level" functions like `execute()`, `read_CSR()`, etc.
* They only run on the Sail model, so we can't e.g. verify them against SPIKE.

Currently the unit test executable reuses a load of code from the emulator, which is not ideal. When we have wrapped the model in a nice C++ library we can use that instead.
@Alasdair
Copy link
Collaborator

Alasdair commented Jul 2, 2025

That's the same issue I've been having with my modules PR, so maybe the issue is unrelated if it's here too

@Timmmm Timmmm force-pushed the user/timh/unit_tests branch from 80ec84e to 359c327 Compare July 3, 2025 11:17
@Timmmm
Copy link
Collaborator Author

Timmmm commented Jul 3, 2025

Ok I had a look into this and the issue is that Lean is treating the very last function in the code as special. On master the LeanRV64D.lean file starts with

import LeanRV64D.Main

but on this branch it starts with

import LeanRV64D.TestMstatus

So the problem is I added a file after main.sail. I guess your PR does the same thing?

I'll reorder the files in this PR since it doesn't make much difference but that seems like something that should be fixed.

@Timmmm Timmmm requested a review from jordancarlin July 7, 2025 12:07

sail_config_set_string(DEFAULT_JSON);
sail_set_abstract_xlen();
sail_set_abstract_ext_d_supported();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might eventually be worth factoring these abstract type initializations out so they don't get out of sync with the main simulator as we add new abstract types (vlen, etc.). Doesn't need to happen in this PR though.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Same with the config_* and trace_log.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I still don't quite understand why they aren't called automatically in model_init().

Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe they could be? If we ensured calling them is idempotent moving them into model_init shouldn't be a breaking change. I may have been overthinking some ordering issue between setting up abstract types and initialising letbindings and registers.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Slightly different topic: is there a reason for model_init() not being included in the generated C header?

Copy link
Collaborator

@pmundkur pmundkur left a comment

Choose a reason for hiding this comment

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

It would be great to finally get this in!


sail_config_set_string(DEFAULT_JSON);
sail_set_abstract_xlen();
sail_set_abstract_ext_d_supported();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same with the config_* and trace_log.

@pmundkur pmundkur added the will be merged Scheduled to be merged soon if nobody objects label Jul 8, 2025
@Timmmm Timmmm added this pull request to the merge queue Jul 9, 2025
Merged via the queue into riscv:master with commit 576ce07 Jul 9, 2025
7 checks passed
@Timmmm Timmmm deleted the user/timh/unit_tests branch July 14, 2025 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

will be merged Scheduled to be merged soon if nobody objects

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants