Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions tests/c/goto_loop.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Run-time:
// env-var: YKD_SERIALISE_COMPILATION=1
// stdout: 1000
// stderr:

Expand Down
1 change: 1 addition & 0 deletions tests/c/indirect_branch.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Run-time:
// env-var: YKD_LOG_IR=jit-pre-opt
// env-var: YKD_LOG=3
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// FIXME: match some IR/output
// ...
Expand Down
1 change: 1 addition & 0 deletions tests/c/jit_disabled.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Run-time:
// env-var: YK_JITC=none
// env-var: YKD_LOG=4
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// 4
// 3
Expand Down
1 change: 1 addition & 0 deletions tests/c/phi1.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// env-var: YKD_LOG_IR=aot,jit-pre-opt
// env-var: YKD_LOG=4
// env-var: YKD_LOG_STATS=/dev/null
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// yk-tracing: start-tracing
// i=4, val=1
Expand Down
1 change: 1 addition & 0 deletions tests/c/phi3.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// env-var: YKD_LOG_IR=aot,jit-pre-opt
// env-var: YKD_LOG=4
// env-var: YKD_LOG_STATS=/dev/null
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// yk-tracing: start-tracing
// i=4, val=3
Expand Down
1 change: 1 addition & 0 deletions tests/c/ptradd.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// env-var: YKD_LOG_IR=aot,jit-pre-opt
// env-var: YKD_LOG=4
// env-var: YKD_LOG_STATS=/dev/null
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// yk-tracing: start-tracing
// i=4, val=3, p=4
Expand Down
1 change: 1 addition & 0 deletions tests/c/simple_interp_loop1.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Run-time:
// env-var: YKD_LOG=4
// env-var: YKD_SERIALISE_COMPILATION=1
// env-var: YKD_LOG_STATS=/dev/null
// stderr:
// yk-tracing: start-tracing
Expand Down
1 change: 1 addition & 0 deletions tests/c/simple_interp_loop2.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Run-time:
// env-var: YKD_LOG=4
// env-var: YKD_LOG_STATS=/dev/null
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// yk-tracing: start-tracing
// pc=0, mem=4
Expand Down
1 change: 1 addition & 0 deletions tests/c/stats4.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Run-time:
// env-var: YKD_LOG_STATS=-
// env-var: YKD_SERIALISE_COMPILATION=1
// stderr:
// {
// ...
Expand Down
77 changes: 77 additions & 0 deletions tests/lua/unrolling.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
for i = 0, 5 do
if i > 2 then
for j = 0, 2 do
io.stderr:write("j ", tostring(j), "\n")
end
end
io.stderr:write("i ", tostring(i), "\n")
end
io.stderr:write("exit\n")

-- Run-time:
-- env-var: YK_HOT_THRESHOLD=3
-- env-var: YK_SIDETRACE_THRESHOLD=2
-- env-var: YKD_LOG=4
-- env-var: YKD_LOG_IR=debugstrs
-- env-var: YKD_SERIALISE_COMPILATION=1
-- stderr:
-- i 0
-- i 1
-- i 2
-- yk-tracing: start-tracing: unrolling.lua:2: GTI
-- j 0
-- yk-warning: tracing-aborted: tracing unrolled a loop: unrolling.lua:4: GETTABUP
-- j 1
-- j 2
-- i 3
-- j 0
-- j 1
-- yk-tracing: start-tracing: unrolling.lua:4: GETTABUP
-- j 2
-- i 4
-- yk-tracing: stop-tracing: unrolling.lua:4: GETTABUP
-- --- Begin debugstrs: unrolling.lua:4: GETTABUP ---
-- ; {
-- ; "trid": "1",
-- ; "start": {
-- ; "kind": "ControlPoint"
-- ; },
-- ; "end": {
-- ; "kind": "Loop"
-- ; }
-- ; }
-- unrolling.lua:4: GETTABUP
-- unrolling.lua:4: GETFIELD
-- unrolling.lua:4: SELF
-- unrolling.lua:4: LOADK
-- unrolling.lua:4: GETTABUP
-- unrolling.lua:4: MOVE
-- unrolling.lua:4: CALL
-- unrolling.lua:4: LOADK
-- unrolling.lua:4: CALL
-- unrolling.lua:3: FORLOOP
-- unrolling.lua:7: GETTABUP
-- unrolling.lua:7: GETFIELD
-- unrolling.lua:7: SELF
-- unrolling.lua:7: LOADK
-- unrolling.lua:7: GETTABUP
-- unrolling.lua:7: MOVE
-- unrolling.lua:7: CALL
-- unrolling.lua:7: LOADK
-- unrolling.lua:7: CALL
-- unrolling.lua:1: FORLOOP
-- unrolling.lua:2: GTI
-- unrolling.lua:3: LOADI
-- unrolling.lua:3: LOADI
-- unrolling.lua:3: LOADI
-- unrolling.lua:3: FORPREP
-- --- End debugstrs ---
-- j 0
-- yk-execution: enter-jit-code: unrolling.lua:4: GETTABUP
-- j 1
-- yk-execution: deoptimise ...
-- yk-execution: enter-jit-code: unrolling.lua:4: GETTABUP
-- j 2
-- i 5
-- yk-execution: deoptimise ...
-- exit
97 changes: 27 additions & 70 deletions ykrt/src/compile/j2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ mod x64;

use crate::{
compile::{
CompilationError, CompiledTrace, Compiler, GuardId, j2::codebuf::CodeBufInProgress,
jitc_yk::AOT_MOD,
CompilationError, CompiledTrace, Compiler, Trace, TraceEnd, TraceStart,
j2::codebuf::CodeBufInProgress, jitc_yk::AOT_MOD,
},
location::HotLocation,
log::{IRPhase, should_log_ir},
mt::{MT, TraceId},
trace::AOTTraceIterator,
mt::MT,
};
use libc::{MAP_ANON, MAP_FAILED, MAP_PRIVATE, PROT_READ, PROT_WRITE, mmap, munmap};
use parking_lot::Mutex;
Expand Down Expand Up @@ -225,19 +223,28 @@ impl J2 {
}

impl Compiler for J2 {
fn root_compile(
fn compile(
self: Arc<Self>,
mt: Arc<MT>,
ta_iter: Box<dyn AOTTraceIterator>,
trid: TraceId,
hl: Arc<Mutex<HotLocation>>,
promotions: Box<[u8]>,
debug_strs: Vec<String>,
coupler: Option<std::sync::Arc<dyn CompiledTrace>>,
trace: Trace,
) -> Result<Arc<dyn CompiledTrace>, CompilationError> {
let kind = match coupler {
Some(tgt_ctr) => aot_to_hir::BuildKind::Coupler { tgt_ctr },
None => aot_to_hir::BuildKind::Loop,
let (hl, bkind) = match (trace.trace_start, &trace.trace_end) {
(TraceStart::ControlPoint { hl }, TraceEnd::Loop) => (hl, aot_to_hir::BuildKind::Loop),
(TraceStart::ControlPoint { hl }, TraceEnd::Coupler(coupler_tid)) => (
hl,
aot_to_hir::BuildKind::Coupler {
tgt_ctr: mt.compiled_trace(*coupler_tid),
},
),
(TraceStart::Guard { .. }, TraceEnd::Loop) => unreachable!(),
(TraceStart::Guard { parent_ctr, gid }, TraceEnd::Coupler(coupler_tid)) => (
parent_ctr.hl().upgrade().unwrap(),
aot_to_hir::BuildKind::Side {
src_ctr: parent_ctr,
src_gid: gid,
tgt_ctr: mt.compiled_trace(*coupler_tid),
},
),
};

#[cfg(target_arch = "x86_64")]
Expand All @@ -248,61 +255,11 @@ impl Compiler for J2 {
&self,
&AOT_MOD,
Arc::clone(&hl),
ta_iter,
trid,
kind,
promotions,
debug_strs,
)
.build()?;

let log = should_log_ir(IRPhase::Asm);

#[cfg(target_arch = "x86_64")]
let minlen = x64::x64hir_to_asm::X64HirToAsm::codebuf_minlen(&hm);
let buf = self.mmap_codebufinprogress(minlen);
#[cfg(target_arch = "x86_64")]
let be = x64::x64hir_to_asm::X64HirToAsm::new(&hm, buf, log);

let ct = hir_to_asm::HirToAsm::new(&hm, hl, be, log).build(mt.clone())?;

// Register JITted code (if required).
mt.trace_profiler().register_ctr(&ct).map_err(|e| {
CompilationError::General(format!("failed to register jitted code with profiler: {e}"))
})?;

Ok(ct)
}

fn sidetrace_compile(
self: Arc<Self>,
mt: Arc<MT>,
ta_iter: Box<dyn AOTTraceIterator>,
trid: TraceId,
src_ctr: Arc<dyn CompiledTrace>,
src_gid: GuardId,
tgt_ctr: Arc<dyn CompiledTrace>,
hl: Arc<Mutex<HotLocation>>,
promotions: Box<[u8]>,
debug_strs: Vec<String>,
) -> Result<Arc<dyn CompiledTrace>, CompilationError> {
#[cfg(target_arch = "x86_64")]
type AotToHir = aot_to_hir::AotToHir<x64::Reg>;

let hm = AotToHir::new(
&mt,
&self,
&AOT_MOD,
Arc::clone(&hl),
ta_iter,
trid,
aot_to_hir::BuildKind::Side {
src_ctr,
src_gid,
tgt_ctr,
},
promotions,
debug_strs,
trace.ta_iter,
trace.ctrid,
bkind,
trace.promotions,
trace.debug_strs,
)
.build()?;

Expand Down
59 changes: 37 additions & 22 deletions ykrt/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,30 +43,11 @@ pub(crate) enum CompilationError {

/// The trait that every JIT compiler backend must implement.
pub(crate) trait Compiler: Send + Sync {
/// Compile a mapped root trace into machine code.
fn root_compile(
/// Compile the [Trace] `trace`.
fn compile(
self: Arc<Self>,
mt: Arc<MT>,
aottrace_iter: Box<dyn AOTTraceIterator>,
ctrid: TraceId,
hl: Arc<Mutex<HotLocation>>,
promotions: Box<[u8]>,
debug_strs: Vec<String>,
connector_ctr: Option<Arc<dyn CompiledTrace>>,
) -> Result<Arc<dyn CompiledTrace>, CompilationError>;

/// Compile a guard trace into machine code.
fn sidetrace_compile(
self: Arc<Self>,
mt: Arc<MT>,
aottrace_iter: Box<dyn AOTTraceIterator>,
ctrid: TraceId,
parent_ctr: Arc<dyn CompiledTrace>,
gid: GuardId,
target_ctr: Arc<dyn CompiledTrace>,
hl: Arc<Mutex<HotLocation>>,
promotions: Box<[u8]>,
debug_strs: Vec<String>,
trace: Trace,
) -> Result<Arc<dyn CompiledTrace>, CompilationError>;
}

Expand All @@ -79,6 +60,40 @@ pub(crate) fn default_compiler() -> Result<Arc<dyn Compiler>, Box<dyn Error>> {
}
}

/// A recorded (but not yet compiled) trace.
pub(crate) struct Trace {
pub(crate) trace_start: TraceStart,
pub(crate) trace_end: TraceEnd,
/// The [TraceId] this trace should have when compiled.
pub(crate) ctrid: TraceId,
pub(crate) ta_iter: Box<dyn AOTTraceIterator>,
pub(crate) promotions: Box<[u8]>,
pub(crate) debug_strs: Vec<String>,
}

/// How a recorded trace started.
#[derive(Clone)]
pub(crate) enum TraceStart {
/// This trace started at a control point.
ControlPoint { hl: Arc<Mutex<HotLocation>> },
/// This trace started at guard [TraceStart::Guard::gid] in [TraceStart::Guard::parent_ctr].
Guard {
parent_ctr: Arc<dyn CompiledTrace>,
gid: GuardId,
},
}

/// How a recorded trace ended.
#[derive(Clone)]
pub(crate) enum TraceEnd {
/// A [TraceStart::ControlPoint] trace looped. Note: by definition a [TraceStart::Guard] can
/// not end in [TraceEnd::Loop].
Loop,
/// A trace ended at another (possibly compiled, possibly tracing, possibly compiling) trace
/// that has/will have the tid [TraceId].
Coupler(TraceId),
}

pub(crate) trait CompiledTrace: fmt::Debug + Send + Sync {
/// Return this trace's [TraceId].
fn ctrid(&self) -> TraceId;
Expand Down
Loading