Skip to content

Commit

Permalink
Merge pull request #1461 from ptersilie/fix_set_running_trace
Browse files Browse the repository at this point in the history
Don't require updating the running trace during side-tracing.
  • Loading branch information
ltratt authored Nov 11, 2024
2 parents 82b2e2d + 4750184 commit 52ab921
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 110 deletions.
6 changes: 0 additions & 6 deletions tests/c/nested_sidetrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,10 @@
// yk-jit-event: enter-jit-code
// yk-jit-event: execute-side-trace
// 24
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 26
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 28
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 30
// ...
Expand All @@ -72,15 +69,12 @@
// yk-jit-event: execute-side-trace
// yk-jit-event: execute-side-trace
// 51
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// yk-jit-event: execute-side-trace
// 54
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// yk-jit-event: execute-side-trace
// 57
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// yk-jit-event: execute-side-trace
// 60
Expand Down
3 changes: 0 additions & 3 deletions tests/c/side-trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@
// yk-jit-event: enter-jit-code
// yk-jit-event: execute-side-trace
// 24
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 26
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 28
// yk-jit-event: re-enter-jit-code
// yk-jit-event: execute-side-trace
// 30
// yk-jit-event: deoptimise
Expand Down
8 changes: 7 additions & 1 deletion ykrt/src/compile/jitc_yk/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#![allow(dead_code)]

use super::CompilationError;
use crate::{compile::jitc_yk::jit_ir::Module, compile::CompiledTrace, location::HotLocation, MT};
use crate::{
compile::{jitc_yk::jit_ir::Module, CompiledTrace, GuardIdx},
location::HotLocation,
MT,
};
use parking_lot::Mutex;
use std::{error::Error, sync::Arc};

Expand All @@ -26,13 +30,15 @@ pub(crate) trait CodeGen: Send + Sync {
/// defined in [super::YkSideTraceInfo::sp_offset].
/// * `root_offset` - Stack pointer offset of the root trace as defined in
/// [super::YkSideTraceInfo::sp_offset].
/// * `prevguards` - List of [GuardIdx]'s of previous guards failures leading up to this trace.
fn codegen(
&self,
m: Module,
mt: Arc<MT>,
hl: Arc<Mutex<HotLocation>>,
sp_offset: Option<usize>,
root_offset: Option<usize>,
prevguards: Option<Vec<GuardIdx>>,
) -> Result<Arc<dyn CompiledTrace>, CompilationError>;
}

Expand Down
81 changes: 39 additions & 42 deletions ykrt/src/compile/jitc_yk/codegen/x64/deopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
};
use dynasmrt::Register as _;
use libc::c_void;
use std::{ptr, sync::Arc};
use std::{ptr, slice, sync::Arc};
use yksmp::Location as SMLocation;

use super::{X64CompiledTrace, RBP_DWARF_NUM, REG64_BYTESIZE};
Expand All @@ -31,54 +31,51 @@ const REGISTER_NUM: usize = RECOVER_REG.len() + 2;
///
/// # Arguments
///
/// * gidx - The [GuardIdx] of the failing guard.
/// * gidx - The [GuardIdx] of the current failing guard.
/// * gptr - Pointer to a list of previous [GuardIdx]'s leading up to the current guard failure.
/// * glen - Length for list in `gptr`.
#[no_mangle]
pub(crate) extern "C" fn __yk_guardcheck(gidx: u64) -> *const libc::c_void {
let gidx = GuardIdx::from(usize::try_from(gidx).unwrap());
// The currently executed trace.
let (ctr, root) = MTThread::with(|mtt| mtt.running_trace());
let ctr = ctr
.unwrap()
.as_any()
.downcast::<X64CompiledTrace>()
.unwrap();
let info = &ctr.deoptinfo[&usize::from(gidx)];
pub(crate) extern "C" fn __yk_guardcheck(
gidx: usize,
gptr: *const usize,
glen: usize,
) -> *const libc::c_void {
let v = unsafe { slice::from_raw_parts(gptr, glen) };
let ctr = running_trace(v);
let info = &ctr.deoptinfo[&gidx];
if let Some(st) = info.guard.ctr() {
let staddr = st.entry();
let mt = Arc::clone(&ctr.mt);
mt.log.log(Verbosity::JITEvent, "execute-side-trace");

// If `root` is Some, then this is the side-trace of a side-trace, so we need to pass in
// the root instead of the immediate parent.
if root.is_some() {
MTThread::with(|mtt| {
mtt.set_running_trace(Some(st), root);
});
} else {
MTThread::with(|mtt| {
mtt.set_running_trace(Some(st), Some(ctr));
});
};

return staddr;
}
std::ptr::null()
}

/// Informs the meta-tracer that we have looped back from a side-trace into the root trace.
pub(crate) extern "C" fn __yk_reenter_jit() {
// Get the root trace and set it as the new running trace.
let (_, root) = MTThread::with(|mtt| mtt.running_trace());
let root = root
/// Get the actual running trace by walking down the guards from the root trace using the
/// [GuardIdx]'s passed in via `gidxs`.
///
/// # Arguments
///
/// * gidxs - List of [GuardIdx]'s for previous guard failures.
fn running_trace(gidxs: &[usize]) -> Arc<X64CompiledTrace> {
let (ctr, _) = MTThread::with(|mtt| mtt.running_trace());
let mut ctr = ctr
.clone()
.unwrap()
.as_any()
.downcast::<X64CompiledTrace>()
.unwrap();
let mt = Arc::clone(&root.mt);
mt.log.log(Verbosity::JITEvent, "re-enter-jit-code");
MTThread::with(|mtt| {
mtt.set_running_trace(Some(root), None);
});
for gidx in gidxs {
ctr = ctr.deoptinfo[gidx]
.guard
.ctr()
.unwrap()
.as_any()
.downcast::<X64CompiledTrace>()
.unwrap();
}
ctr
}

/// Deoptimise back to the interpreter. This function is called from a failing guard (see
Expand All @@ -88,23 +85,23 @@ pub(crate) extern "C" fn __yk_reenter_jit() {
///
/// * `frameaddr` - the RBP value for main interpreter loop (and also the JIT since the trace
/// executes on the same frame)
/// * `gidx` - the [GuardIdx] of the failing guard
/// * `gidx` - the [GuardIdx] of the current failing guard
/// * `gp_regs` - a pointer to the saved values of the 16 general purpose registers in the same
/// order as [crate::compile::jitc_yk::codegen::x64::lsregalloc::GP_REGS]
/// * gptr - Pointer to a list of previous [GuardIdx]'s leading up to the current guard failure.
/// * glen - Length for list in `gptr`.
#[no_mangle]
pub(crate) extern "C" fn __yk_deopt(
frameaddr: *const c_void,
gidx: u64,
gp_regs: &[u64; 16],
fp_regs: &[u64; 16],
gptr: *const usize,
glen: usize,
) -> *const libc::c_void {
let v = unsafe { slice::from_raw_parts(gptr, glen) };
let ctr = running_trace(v);
let gidx = GuardIdx::from(usize::try_from(gidx).unwrap());
let (ctr, _) = MTThread::with(|mtt| mtt.running_trace());
let ctr = ctr
.unwrap()
.as_any()
.downcast::<X64CompiledTrace>()
.unwrap();
let aot_smaps = AOT_STACKMAPS.as_ref().unwrap();
let info = &ctr.deoptinfo[&usize::from(gidx)];
let mt = Arc::clone(&ctr.mt);
Expand Down
Loading

0 comments on commit 52ab921

Please sign in to comment.