Skip to content

Conversation

@ibvqeibob
Copy link

@ibvqeibob ibvqeibob commented Nov 14, 2025

Add pre/post-step callbacks. These are currently unused but will be used in future by log/trace outputs and maybe RVFI.

  • Extend callbacks_if with {pre,post}_step_callback(bool is_waiting) default no-op virtual methods.
  • Add call_{pre,post}_step_callbacks helpers in riscv_callbacks.{h,cpp} to fan out the new callbacks to all registered handlers.
  • Invoke {pre,post}_step_callback around each ztry_step in run_sail(), passing the current is_waiting state.
  • Implement {pre,post}_step_callback in log_callbacks as no-op placeholders with TODOs for future per-step logging/statistics.

Related to: #1353

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.

I'm curious what use case you had in mind for these callbacks. Is it worth generalizing the pre_step callback to include rvfi->pre_step?

@ibvqeibob
Copy link
Author

I'm curious what use case you had in mind for these callbacks. Is it worth generalizing the pre_step callback to include rvfi->pre_step?

The main use cases I had in mind for these per-step callbacks are:
1.lightweight per-step logging (e.g. PC / instruction traces),
2.collecting simple statistics (instruction counts, non-waiting steps, etc.),
3.and providing a single hook point where we can later plug in things like RVFI,debugging or breakpoint logic without touching run_sail() again.
Right now this PR only introduces the generic pre_step/post_step interface and wires it into the main loop, so that existing components like log_callbacks can start using it.

@Timmmm Timmmm changed the title Expose per-step pre_step/post_step callbacks in Sail RISC-V C model Expose per-step pre_step/post_step callbacks Nov 27, 2025
Copy link
Collaborator

@Timmmm Timmmm left a comment

Choose a reason for hiding this comment

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

Sorry for the slow review - this looks great. Just a few minor renamings to match the existing name style.

@Timmmm
Copy link
Collaborator

Timmmm commented Nov 27, 2025

Is it worth generalizing the pre_step callback to include rvfi->pre_step?

I thought that too, but RVFI has special flow control stuff that's a bit awkward. I think we should probably just special-case it for now and think of something better in a future PR.

As for use cases, I need this exact thing for binary output support. You can collect information for a step in the callbacks then commit it in post_step_callback().

@ibvqeibob let me know if you would be offended if I make the renaming changes - I suddenly have an immediate need for JSON output and it will be easier with this PR.

ibvqeibob and others added 5 commits November 28, 2025 09:13
Co-authored-by: Tim Hutt <[email protected]>
Signed-off-by: uestc_yd <[email protected]>
Co-authored-by: Tim Hutt <[email protected]>
Signed-off-by: uestc_yd <[email protected]>
Co-authored-by: Tim Hutt <[email protected]>
Signed-off-by: uestc_yd <[email protected]>
Co-authored-by: Tim Hutt <[email protected]>
Signed-off-by: uestc_yd <[email protected]>
Co-authored-by: Tim Hutt <[email protected]>
Signed-off-by: uestc_yd <[email protected]>
@ibvqeibob
Copy link
Author

Is it worth generalizing the pre_step callback to include rvfi->pre_step?

I thought that too, but RVFI has special flow control stuff that's a bit awkward. I think we should probably just special-case it for now and think of something better in a future PR.

As for use cases, I need this exact thing for binary output support. You can collect information for a step in the callbacks then commit it in post_step_callback().

@ibvqeibob let me know if you would be offended if I make the renaming changes - I suddenly have an immediate need for JSON output and it will be easier with this PR.

Thanks a lot for the review and for pushing the renaming changes – they look great to me.
Keeping RVFI’s pre_step special-cased for now also makes sense. I’m happy that this per-step callback can help with your JSON/binary output work as well.
Please let me know if there’s anything else you’d like me to adjust in this PR.

@Timmmm
Copy link
Collaborator

Timmmm commented Nov 28, 2025

Thanks, there were a few more renamings to do and some unused variable warnings which I've fixed. LGTM now.

Comment on lines 437 to 457
if (!is_waiting) {
if (config_print_step) {
fprintf(trace_log, "\n");
}
step_no++;
insn_cnt++;
total_insns++;
}

if (do_show_times && (total_insns & 0xfffff) == 0) {
uint64_t start_us = 1000000 * ((uint64_t)interval_start.tv_sec)
+ ((uint64_t)interval_start.tv_usec);
if (gettimeofday(&interval_start, nullptr) < 0) {
fprintf(stderr, "Cannot gettimeofday: %s\n", strerror(errno));
exit(EXIT_FAILURE);
}
uint64_t end_us = 1000000 * ((uint64_t)interval_start.tv_sec)
+ ((uint64_t)interval_start.tv_usec);
fprintf(stdout, "kips: %" PRIu64 "\n",
((uint64_t)1000) * 0x100000 / (end_us - start_us));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

After this is merged I think we can move almost all of this stuff into the log callback handler.

@github-actions
Copy link

Test Results

2 115 tests  ±0   2 115 ✅ ±0   20m 35s ⏱️ -24s
    1 suites ±0       0 💤 ±0 
    1 files   ±0       0 ❌ ±0 

Results for commit 25021da. ± Comparison against base commit 785ad0f.

@Timmmm Timmmm added the will be merged Scheduled to be merged soon if nobody objects label Nov 28, 2025
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.

One minor nit, but LGTM.

Comment on lines +105 to +115
void log_callbacks::pre_step_callback(bool is_waiting)
{
// TODO: per-step logging or statistics
(void)is_waiting;
}

void log_callbacks::post_step_callback(bool is_waiting)
{
// TODO: post-step logging or statistics
(void)is_waiting;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

No need to include the empty implementations here since we added the [[maybe_unused]] annotations.

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.

5 participants