Wasmoon-specific JIT integration and native runtime glue
Dependencies
///|
test "plan a small MilkIR function for x64 JIT integration" {
let signature = @milkir.Signature::Signature([I64, I64], [I64])
let milk = @milkir.Function::with_signature("add64", signature)
let lhs = milk.param(0).unwrap()
let rhs = milk.param(1).unwrap()
let sum = milk.new_value(I64)
let entry = milk.new_block([])
entry.append_inst(milk.new_inst(Scalar(IntBinary(Add)), [lhs, rhs], [sum]))
entry.set_terminator(Return([sum]))
let plan = plan_milkir_integration_for_target(milk, X64)
inspect(plan.entry_symbol, content="add64")
debug_inspect(plan.target, content="X64")
inspect(plan.object.get_bytes().length() > 0, content="true")
}pub suberror ArtifactIncompatible {
TargetMismatch(expected~ : TargetSpec, actual~ : TargetSpec)
MissingCpuFeature(name~ : String)
DuplicateCpuFeature(name~ : String)
JitAbiVersionMismatch(expected~ : Int, actual~ : Int)
CodegenRevisionMismatch(expected~ : String, actual~ : String)
ModuleIdentityMismatch(expected~ : ModuleIdentity, actual~ : ModuleIdentity)
CompilationPolicyMismatch(expected~ : CompilationPolicy, actual~ : CompilationPolicy)
DuplicateFunctionIndex(index~ : Int)
DuplicateSymbol(name~ : String)
UnknownSymbol(kind~ : String, name~ : String)
} derive(Eq, Debug)pub suberror ArtifactLoadError {
DecodeFailed(cause~ : ArtifactDecodeError)
Incompatible(cause~ : ArtifactIncompatible)
CodeObjectInvalid(cause~ : CodeObjectVerifyError)
} derive(Eq, Debug)pub(all) suberror CHeapError {
OutOfBoundsArrayAccess
NullReference
OutOfMemory
}pub(all) suberror GCSetupError {
MissingJITContext
InvalidFuncCount(num_funcs~ : Int)
FuncTypeIndicesLengthMismatch(func_type_indices_len~ : Int, num_funcs~ : Int)
MissingFunctionTableContext(num_funcs~ : Int)
} derive(Debug)impl Show for GCSetupErrorpub suberror JitInstallError {
ResolutionFailed(kind~ : String, name~ : String)
AllocationFailed(function_index~ : Int)
RelocationFailed(function_index~ : Int, message~ : String)
ProtectionFailed(function_index~ : Int)
RegistrationFailed(function_index~ : Int, message~ : String)
PublicationFailed(message~ : String)
TrampolineAllocationFailed(code_size~ : Int)
} derive(Eq, Debug)pub suberror JitPipelineError {
SemanticLoweringFailed(cause~ : SemanticLowerError)
RuntimeAbiInputInvalid(cause~ : MachVVerifyError)
RuntimeAbiOutputInvalid(cause~ : MachVVerifyError)
RuntimeAbiContractViolation(message~ : String)
AArch64AbiInvalid(cause~ : InternalAbiError)
X64AbiInvalid(cause~ : InternalAbiError)
MissingEnvironmentField(target~ : NativeTarget, field~ : String)
AArch64LoweringFailed(cause~ : AArch64LowerError)
X64LoweringFailed(cause~ : X64LowerError)
AArch64CompilationFailed(cause~ : AArch64CompileError)
X64CompilationFailed(cause~ : X64CompileError)
AArch64AllocationFailed(cause~ : AArch64AllocationError)
X64AllocationFailed(cause~ : X64AllocationError)
AArch64FramePlanningFailed(cause~ : AArch64FrameError)
X64FramePlanningFailed(cause~ : X64FrameError)
AArch64EmissionFailed(cause~ : AArch64EmitError)
X64EmissionFailed(cause~ : X64EmitError)
AArch64LinkPreparationFailed(cause~ : AArch64LinkError)
X64LinkPreparationFailed(cause~ : X64LinkError)
InvalidJitRelocation(target~ : NativeTarget, index~ : Int, message~ : String)
UnsupportedEntrySignature(message~ : String)
UnsupportedHostArchitecture(tag~ : Int)
UnsupportedHostOsAbi(tag~ : Int)
InvalidFunctionIndex(index~ : Int)
} derive(Debug)impl Show for JitPipelineErrorimpl Show for NativeExecutionStateErrorpub suberror NativeFiberError {
StackAllocationFailed(Int64)
WrongThread
InvalidTransition(NativeFiberPhase)
InvalidState(Int)
UnexpectedYield(Int64)
NativeFailure(Int)
} derive(Eq, Debug)impl Show for NativeFiberErrorpub(all) struct ArtifactCompatibility {
target : TargetSpec
available_cpu_features : Array[CpuFeature]
jit_abi_version : Int
codegen_revision : String
module_identity : ModuleIdentity
policy : CompilationPolicy
runtime_symbols : Array[ExternalSymbol]
data_symbols : Array[DataSymbol]
} derive(Eq, Debug)pub struct BacktraceFrame {
pc : Int64
name : String
offset : Int64
func_idx : Int
}fn CHeap::alloc_array(self : CHeap, type_idx : Int, len : Int, init_value : Value) -> Int raise CHeapErrorfn CHeap::alloc_array_from_values(self : CHeap, type_idx : Int, elements : Array[Value]) -> Int raise CHeapErrorfn CHeap::array_copy(self : CHeap, dst_idx : Int, dst_offset : Int, src_idx : Int, src_offset : Int, count : Int) -> Unit raise CHeapErrorfn CHeap::array_fill(self : CHeap, array_idx : Int, offset : Int, value : Value, count : Int) -> Unit raise CHeapErrorfn CHeap::array_get(self : CHeap, array_idx : Int, elem_idx : Int, elem_type : ValueType) -> Value raise CHeapErrorfn CHeap::array_set(self : CHeap, array_idx : Int, elem_idx : Int, value : Value) -> Unit raise CHeapErrorpub(all) enum CompilationMode {
Interpret
Baseline
Optimized
}impl Show for CompilationModefn CompilationRequest::CompilationRequest(func_idx : Int, mode : CompilationMode) -> CompilationRequestfn CompilationRequest::with_priority(func_idx : Int, mode : CompilationMode, priority : Int) -> CompilationRequestpub(all) struct CompilationStrategy {
profiler : Profiler
queue : CompilationQueue
config : TieredConfig
decisions_interpret : Int
decisions_compile : Int
decisions_compiled : Int
}impl Show for CompilationStrategyfn CompilationStrategy::schedule_compilation(self : CompilationStrategy, func_idx : Int, mode : CompilationMode) -> Boolpub struct DWARFBuilder {
// private fields
}fn DWARFBuilder::add_function(self : DWARFBuilder, name : String, addr : Int64, size : Int, func_idx : Int) -> Unitpub struct FunctionInfo {
name : String
func_idx : Int
offset : Int64
}pub(all) enum FunctionTemperature {
Cold
Warm
Hot
}impl Show for FunctionTemperaturefn GcSafepoint::GcSafepoint(code_offset : Int, live_refs : Array[Int], metadata : Int64) -> GcSafepointpub(all) struct HotThreshold {
warm_threshold : Int
hot_threshold : Int
}pub struct InstalledCode {
// private fields
}pub struct InstalledFunction {
// private fields
}pub struct InstalledImport {
// private fields
}pub struct InstalledTrampoline {
// private fields
}pub struct JITFunctionDebug {
ir : String
machv_before_regalloc : String
machv_after_regalloc : String
machine_code : String
}fn JITFunctionDebug::JITFunctionDebug(ir : String, machv_before_regalloc : String, machv_after_regalloc : String, machine_code : String) -> JITFunctionDebugpub struct JitCodeInstaller {
// private fields
}fn JitCodeInstaller::install(self : JitCodeInstaller, loaded : LoadedArtifact) -> InstalledCode raise JitInstallErrorfn JitCodeInstaller::install_trampoline(self : JitCodeInstaller, code : Array[Int]) -> InstalledTrampoline raise JitInstallErrorpub(all) struct JitIntegrationPlan {
entry_symbol : String
target : NativeTarget
object : JitCodeObject
}pub struct JitPipelineDiagnostics {
semantic_machv : String
target_vcode : String
allocated_vcode : String
code_object : String
machine_code : String
} derive(Debug)pub(all) struct MemoryDescriptorLayout {
base_offset : Int
current_length_offset : Int
}pub struct MemoryInfo {
ptr : Int64
size : Int64
max_pages : Int?
}type NativeExecutionStateimpl Debug for NativeExecutionStatefn NativeExecutionState::begin(self : NativeExecutionState, release? : () -> Unit) -> NativeInvocationHandle raise NativeExecutionStateErrortype NativeFiberimpl Debug for NativeFiberfn NativeFiber::continue_with(self : NativeFiber, value? : Int64) -> NativeFiberEvent raise NativeFiberErrortype NativeInvocationHandleimpl Debug for NativeInvocationHandlefn NativeInvocationHandle::cancel(self : NativeInvocationHandle) -> Unit raise NativeExecutionStateErrorfn NativeInvocationHandle::complete(self : NativeInvocationHandle) -> Unit raise NativeExecutionStateErrorfn NativeInvocationHandle::park(self : NativeInvocationHandle) -> Unit raise NativeExecutionStateErrorfn NativeInvocationHandle::phase(self : NativeInvocationHandle) -> NativeInvocationPhase raise NativeExecutionStateErrorfn NativeInvocationHandle::reactivate(self : NativeInvocationHandle) -> Unit raise NativeExecutionStateErrorfn NativeInvocationHandle::resource_id(self : NativeInvocationHandle) -> Int64 raise NativeExecutionStateErrortype NativeJITContextfn NativeJITContext::alloc_guarded_memory(self : NativeJITContext, initial_pages : Int, max_pages : Int?) -> Int64fn NativeJITContext::call_trampoline(self : NativeJITContext, trampoline_ptr : Int64, func_ptr : Int64, values_vec : FixedArray[Int64], values_len : Int, use_stack_switch : Bool) -> Intfn NativeJITContext::init_wasi(self : NativeJITContext, args : Array[String], env : Array[String], preopens : Array[(String, String)], quiet? : Bool) -> Unitfn NativeJITContext::register_safepoints(self : NativeJITContext, func_idx : Int, safepoints : Array[SafepointSite]) -> Boolfn NativeJITContext::set_cancellation_callback(self : NativeJITContext, callback : () -> Bool) -> Unitfn NativeJITContext::set_hostcall_callback(self : NativeJITContext, callback : () -> NativeHostcallOutcome) -> Unitfn NativeJITContext::set_indirect(self : NativeJITContext, table_idx : Int, func_idx : Int, type_idx : Int) -> Unitfn NativeJITContext::set_memory_pointers(self : NativeJITContext, memories : Array[MemoryInfo]) -> Unitfn NativeJITContext::set_table_pointers(self : NativeJITContext, jit_tables : Array[JITTable?]) -> Unitfn NativeJITContext::set_wasi_stdin_callback(self : NativeJITContext, callback : () -> Bytes) -> Unitfn NativeJITContext::setup_gc(self : NativeJITContext, heap : CHeap, types : Array[SubType], canonical_indices : Array[Int], func_type_indices : Array[Int]) -> Unit raise GCSetupErrorfn NativeJITContext::setup_gc_with_func_table(self : NativeJITContext, heap : CHeap, types : Array[SubType], canonical_indices : Array[Int], func_type_indices : Array[Int], func_table_ptr : Int64, num_funcs : Int) -> Unit raise GCSetupErrorfn NativeJITContext::setup_segments(self : NativeJITContext, datas : Array[Data], elem_segments : Array[Array[Int64]], data_dropped? : Array[Bool], elem_dropped? : Array[Bool]) -> Unitfn NativeJITContext::start_continuation(self : NativeJITContext, trampoline_ptr : Int64, func_ptr : Int64, values : FixedArray[Int64], values_len : Int, stack_size? : Int64) -> NativeJITContinuation raise NativeFiberErrorfn NativeJITContext::throw_exception_values(self : NativeJITContext, tag_addr : Int, values : Array[Int64]) -> Unittype NativeJITContinuationfn NativeJITContinuation::continue_after_hostcall(self : NativeJITContinuation, retry~ : Bool) -> NativeJITContinuationEvent raise NativeFiberErrorfn NativeJITContinuation::continue_with(self : NativeJITContinuation, value? : Int64) -> NativeJITContinuationEvent raise NativeFiberErrorpub(all) struct Profiler {
counter : CallCounter
threshold : HotThreshold
hot_functions : HashSet[Int]
pending_compilation : HashSet[Int]
compiled_functions : HashSet[Int]
}fn RuntimeSymbolResolver::add_symbol(self : RuntimeSymbolResolver, name : String, address : Int64) -> Unitfn RuntimeSymbolResolver::resolve_symbol(self : RuntimeSymbolResolver, name : String) -> RuntimeSymbol?pub(all) struct TieredConfig {
baseline_threshold : Int
optimize_threshold : Int
synchronous : Bool
max_concurrent : Int
}pub struct TrapDetails {
signal : Int
pc : Int64
lr : Int64
fp : Int64
frame_lr : Int64
fault_addr : Int64
brk_imm : Int
func_idx : Int
x0 : Int64
x1 : Int64
x2 : Int64
x3 : Int64
x6 : Int64
x7 : Int64
x8 : Int64
x9 : Int64
x10 : Int64
x11 : Int64
x15 : Int64
}pub(all) struct TrapReport {
trap_kind : String
message : String
signal : Int
signal_name : String
pc : Int64
lr : Int64
fp : Int64
frame_lr : Int64
fault_addr : Int64
brk_imm : Int
wasm_func_idx : Int?
wasm_func_name : String?
wasm_offset : Int64?
wasm_frames : Array[WasmTrapFrame]
host_frames : Array[String]
dump_path : String?
}pub(all) struct VMContextLayout {
memory0_offset : Int
memory0_base_offset : Int
memory0_size_offset : Int
func_table_offset : Int
table0_base_offset : Int
table0_elements_offset : Int
globals_offset : Int
tables_offset : Int
table_count_offset : Int
func_count_offset : Int
table_sizes_offset : Int
table_max_sizes_offset : Int
memories_offset : Int
memory_count_offset : Int
debug_current_func_idx_offset : Int
gc_heap_ptr_offset : Int
gc_heap_limit_offset : Int
gc_heap_offset : Int
pointer_stride : Int
global_value_stride : Int
table_entry_value_offset : Int
table_entry_stride : Int
func_table_entry_stride : Int
}pub(all) struct WasmTrapFrame {
pc : Int64
func_idx : Int
func_name : String
wasm_offset : Int64
}let WASMOON_CODEGEN_REVISION : Stringlet WASMOON_JIT_ABI_VERSION : Intfn alloc_guarded_memory_desc(initial_pages : Int, max_pages : Int?) -> Int64fn alloc_memory(size : Int64) -> Int64fn alloc_memory_desc(size_bytes : Int64, max_pages : Int?, is_memory64? : Bool, page_size_log2? : Int, is_shared? : Bool) -> Int64fn artifact_compatibility(manifest : CompatibilityManifest, available_cpu_features~ : Array[CpuFeature], runtime_symbols~ : Array[ExternalSymbol], data_symbols~ : Array[DataSymbol]) -> ArtifactCompatibilityfn artifact_module_identity(name : String, source_module : Bytes, semantic_features : Bytes) -> ModuleIdentityfn build_artifact_manifest(target : NativeTarget, operating_system_abi : OperatingSystemAbi, module_identity~ : ModuleIdentity, policy~ : CompilationPolicy, required_cpu_features? : Array[CpuFeature]) -> CompatibilityManifestfn build_entry_trampoline_for_target(param_types : Array[ValueType], result_types : Array[ValueType], target : NativeTarget) -> Array[Int] raise HostcallImportTrampolineErrorfn build_host_artifact_manifest(module_identity~ : ModuleIdentity, policy~ : CompilationPolicy, required_cpu_features? : Array[CpuFeature]) -> CompatibilityManifest raise JitPipelineErrorfn build_hostcall_import_trampoline_for_target(param_types : Array[ValueType], result_types : Array[ValueType], host_func_addr : Int, target : NativeTarget) -> HostcallImportTrampolineCode raise HostcallImportTrampolineErrorfn compile_wasm_body_artifact_function(function : Function, validation_context : WasmValidationContext, target : NativeTarget, function_index~ : Int, signature~ : Signature, use_subtype_indirect_check? : Bool, canonical_type_indices? : Array[Int]) -> FunctionCode raise JitPipelineErrorfn compile_wasm_body_diagnostics_for_target(function : Function, validation_context : WasmValidationContext, target : NativeTarget, use_subtype_indirect_check? : Bool, canonical_type_indices? : Array[Int]) -> JitPipelineDiagnostics raise JitPipelineErrorfn decode_heap_ref(encoded : Int64) -> Intfn encode_heap_ref(heap_idx : Int) -> Int64fn encode_i31(value : Int) -> Int64fn gc_debug_set_fail_alloc(fail_at : Int, fail_every? : Int) -> Unitfn get_hostcall_func_addr() -> Intfn get_hostcall_num_arg_slots() -> Intfn get_hostcall_num_result_slots() -> Intfn get_import_trampoline(module_name : String, field_name : String) -> Int64?fn is_jit_supported_module(module_name : String) -> Boolfn load_artifact(bytes : Bytes, compatibility : ArtifactCompatibility, limits? : DecodeLimits) -> LoadedArtifact raise ArtifactLoadErrorfn lower_wasm_body_semantic_machv(function : Function, validation_context : WasmValidationContext, use_subtype_indirect_check? : Bool, canonical_type_indices? : Array[Int]) -> Function raise JitPipelineErrorfn memory_descriptor_fill(mem_desc_ptr : Int64, destination : Int64, value : Int, size : Int) -> Intfn memory_descriptor_grow(mem_desc_ptr : Int64, delta_pages : Int, max_pages : Int) -> Intfn memory_descriptor_move(mem_desc_ptr : Int64, destination : Int64, source : Int64, size : Int) -> Intfn memory_descriptor_read(mem_desc_ptr : Int64, offset : Int64, out : FixedArray[Byte], size : Int) -> Intfn memory_descriptor_write(mem_desc_ptr : Int64, offset : Int64, data : FixedArray[Byte], size : Int) -> Intfn memory_descriptor_write_bytes(mem_desc_ptr : Int64, offset : Int64, data : Bytes, size : Int) -> Intfn memory_init(mem_ptr : Int64, offset : Int64, data : Bytes) -> Boolfn memory_read(mem_ptr : Int64, offset : Int64, size : Int) -> Bytesfn native_value_slot_read(base : Int64, slot : Int) -> Int64fn native_value_slot_write(base : Int64, slot : Int, value : Int64) -> Unitfn plan_entry_trampoline_for_target(param_types : Array[ValueType], result_types : Array[ValueType], target : NativeTarget) -> JitIntegrationPlan raise JitPipelineErrorfn plan_hostcall_import_trampoline_layout(param_types : Array[ValueType], result_types : Array[ValueType], max_int_regs : Int, max_float_regs : Int) -> HostcallImportTrampolineLayoutfn plan_milkir_integration_for_target(func : Function, target : NativeTarget) -> JitIntegrationPlan raise JitPipelineErrorfn plan_wasm_body_aarch64_code_object(function : Function, validation_context : WasmValidationContext, use_subtype_indirect_check? : Bool, canonical_type_indices? : Array[Int]) -> UnlinkedCodeObject raise JitPipelineErrorfn plan_wasm_body_milkir_integration_for_target(func : Function, validation_context : WasmValidationContext, target : NativeTarget) -> JitIntegrationPlan raise JitPipelineErrorfn plan_wasm_body_x64_code_object(function : Function, validation_context : WasmValidationContext, use_subtype_indirect_check? : Bool, canonical_type_indices? : Array[Int]) -> UnlinkedCodeObject raise JitPipelineErrorfn resolve_import_function(external_imports : Map[String, Map[String, Int64]], module_name : String, field_name : String) -> ImportFunctionResolutionfn verify_artifact(decoded : Artifact, compatibility : ArtifactCompatibility) -> LoadedArtifact raise ArtifactLoadErrorfn wasm_import_identity(function_index : Int, module_name : String, function_name : String, function_type : FuncType) -> ImportIdentityfn wasm_trap_payload(reason : String) -> IntWasmoon-specific JIT integration and native runtime glue
Dependencies