Skip to content

Commit

Permalink
Rename ContinuationObject and ContinuationReference (bytecodealliance…
Browse files Browse the repository at this point in the history
…#144)

This PR renames `ContinuationObject` to `VMContRef` and
`ContinuationReference` to `VMContObj`.

This was mostly just a search-replace job, but also involved renaming
lots of variables named things like `contref` or `contobj` (and various
variations thereof), as well as replacing mentions of "continuation
object" in text.

The baseline implementation remains mostly unchanged since it was
already using that nomenclature, only some minor changes at the
boundaries/when interacting with functions defined in both
implementations (e.g.,
`typed_continuations_load_continuation_reference`) were necessary.

This resolves bytecodealliance#104.
  • Loading branch information
frank-emrich authored Mar 28, 2024
1 parent 9277a59 commit 750146b
Show file tree
Hide file tree
Showing 15 changed files with 238 additions and 251 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -388,9 +388,9 @@ debug-builtins = ["wasmtime/debug-builtins"]
# Enables compatibility shims with Wasmtime 13 and prior's CLI.
old-cli = []

# Until we implement proper reference counting for `ContinuationReference` objects,
# we may use this flag to bypass the creation of ContinuationReference objects,
# directly using the corresponding ContinuationObject instead.
# Until we implement proper reference counting for `VMContObj` objects,
# we may use this flag to bypass the creation of VMContObj objects,
# directly using the corresponding VMContRef instead.
# This is to allow running benchmarks that may create a lot of such objects and
# may otherwise run out of memory.
# Note that enabling this is highly unsafe, as it makes it impossible to detect
Expand Down
26 changes: 13 additions & 13 deletions cranelift/wasm/src/code_translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,10 +2535,10 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let arg_types = environ.continuation_arguments(*type_index).to_vec();
let result_types = environ.continuation_returns(*type_index).to_vec();
let r = state.pop1();
let contobj =
let contref =
environ.translate_cont_new(builder, state, r, &arg_types, &result_types)?;
let contref = environ.typed_continuations_new_cont_ref(builder, contobj);
state.push1(contref);
let contobj = environ.typed_continuations_new_cont_obj(builder, contref);
state.push1(contobj);
}
Operator::Resume {
type_index,
Expand All @@ -2560,10 +2560,10 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
.collect::<Vec<(u32, ir::Block)>>();

let arity = environ.continuation_arguments(*type_index).len();
let (contref, call_args) = state.peekn(arity + 1).split_last().unwrap();
let (contobj, call_args) = state.peekn(arity + 1).split_last().unwrap();

let cont_return_vals =
environ.translate_resume(builder, *type_index, *contref, call_args, &resumetable);
environ.translate_resume(builder, *type_index, *contobj, call_args, &resumetable);

state.popn(arity + 1); // arguments + continuation
state.pushn(&cont_return_vals);
Expand All @@ -2580,11 +2580,11 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let tag_index_val = builder.ins().iconst(I32, *tag_index as i64);
environ.translate_suspend(builder, tag_index_val);

let contobj = environ.typed_continuations_load_continuation_object(builder);
let contref = environ.typed_continuations_load_continuation_reference(builder);

let return_types = environ.tag_returns(*tag_index).to_vec();
let return_values =
environ.typed_continuations_load_tag_return_values(builder, contobj, &return_types);
environ.typed_continuations_load_tag_return_values(builder, contref, &return_types);

state.pushn(&return_values);
}
Expand All @@ -2596,17 +2596,17 @@ pub fn translate_operator<FE: FuncEnvironment + ?Sized>(
let dst_arity = environ.continuation_arguments(*dst_index).len();
let arg_count = src_arity - dst_arity;

let (original_contref, args) = state.peekn(arg_count + 1).split_last().unwrap();
let contobj =
environ.typed_continuations_cont_ref_get_cont_obj(builder, *original_contref);
let (original_contobj, args) = state.peekn(arg_count + 1).split_last().unwrap();
let contref =
environ.typed_continuations_cont_obj_get_cont_ref(builder, *original_contobj);

let src_arity_value = builder.ins().iconst(I32, src_arity as i64);
environ.typed_continuations_store_resume_args(builder, args, src_arity_value, contobj);
environ.typed_continuations_store_resume_args(builder, args, src_arity_value, contref);

let new_contref = environ.typed_continuations_new_cont_ref(builder, contobj);
let new_contobj = environ.typed_continuations_new_cont_obj(builder, contref);

state.popn(arg_count + 1);
state.push1(new_contref);
state.push1(new_contobj);
}
Operator::ResumeThrow {
type_index: _,
Expand Down
18 changes: 9 additions & 9 deletions cranelift/wasm/src/environ/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ pub trait FuncEnvironment: TargetEnvironment {
&mut self,
builder: &mut FunctionBuilder,
type_index: u32,
contref: ir::Value,
contobj: ir::Value,
resume_args: &[ir::Value],
resumetable: &[(u32, ir::Block)],
) -> Vec<ir::Value>;
Expand Down Expand Up @@ -655,7 +655,7 @@ pub trait FuncEnvironment: TargetEnvironment {
fn typed_continuations_load_tag_return_values(
&mut self,
builder: &mut FunctionBuilder,
contobj: ir::Value,
contref: ir::Value,
valtypes: &[wasmtime_types::WasmValType],
) -> std::vec::Vec<ir::Value>;

Expand All @@ -673,7 +673,7 @@ pub trait FuncEnvironment: TargetEnvironment {
builder: &mut FunctionBuilder,
values: &[ir::Value],
remaining_arg_count: ir::Value,
contobj: ir::Value,
contref: ir::Value,
);

/// TODO
Expand All @@ -682,25 +682,25 @@ pub trait FuncEnvironment: TargetEnvironment {
/// TODO
fn tag_returns(&self, tag_index: u32) -> &[wasmtime_types::WasmValType];

/// Returns a pointer to the currently running continuation object.
/// Returns a pointer to the currently running continuation reference.
/// Traps if not currently running inside a continuation.
fn typed_continuations_load_continuation_object(
fn typed_continuations_load_continuation_reference(
&mut self,
builder: &mut FunctionBuilder,
) -> ir::Value;

/// TODO
fn typed_continuations_new_cont_ref(
fn typed_continuations_new_cont_obj(
&mut self,
builder: &mut FunctionBuilder,
contobj_addr: ir::Value,
contref_addr: ir::Value,
) -> ir::Value;

/// TODO
fn typed_continuations_cont_ref_get_cont_obj(
fn typed_continuations_cont_obj_get_cont_ref(
&mut self,
builder: &mut FunctionBuilder,
contref: ir::Value,
contobj: ir::Value,
) -> ir::Value;

/// Returns whether the CLIF `x86_blendv` instruction should be used for the
Expand Down
12 changes: 6 additions & 6 deletions crates/continuations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,20 @@ pub const STACK_CHAIN_MAIN_STACK_DISCRIMINANT: usize = 1;
/// `wasmtime_runtime::continuation::StackChain`.
pub const STACK_CHAIN_CONTINUATION_DISCRIMINANT: usize = 2;

/// Encodes the life cycle of a `ContinuationObject`.
/// Encodes the life cycle of a `VMContRef`.
#[derive(PartialEq)]
#[repr(i32)]
pub enum State {
/// The `ContinuationObject` has been created, but `resume` has never been
/// The `VMContRef` has been created, but `resume` has never been
/// called on it. During this stage, we may add arguments using `cont.bind`.
Allocated,
/// `resume` has been invoked at least once on the `ContinuationObject`,
/// `resume` has been invoked at least once on the `VMContRef`,
/// meaning that the function passed to `cont.new` has started executing.
/// Note that this state does not indicate whether the execution of this
/// function is currently suspended or not.
Invoked,
/// The function originally passed to `cont.new` has returned normally.
/// Note that there is no guarantee that a ContinuationObject will ever
/// Note that there is no guarantee that a VMContRef will ever
/// reach this status, as it may stay suspended until being dropped.
Returned,
}
Expand Down Expand Up @@ -278,9 +278,9 @@ pub mod offsets {
pub const LENGTH: usize = offset_of!(Payloads, length);
}

/// Offsets of fields in `wasmtime_runtime::continuation::ContinuationObject`.
/// Offsets of fields in `wasmtime_runtime::continuation::VMContRef`.
/// We uses tests there to ensure these values are correct.
pub mod continuation_object {
pub mod vm_cont_ref {
use crate::Payloads;

/// Offset of `limits` field
Expand Down
28 changes: 14 additions & 14 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2808,11 +2808,11 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
&mut self,
builder: &mut FunctionBuilder,
type_index: u32,
contref: ir::Value,
contobj: ir::Value,
resume_args: &[ir::Value],
resumetable: &[(u32, ir::Block)],
) -> Vec<ir::Value> {
wasmfx_impl::translate_resume(self, builder, type_index, contref, resume_args, resumetable)
wasmfx_impl::translate_resume(self, builder, type_index, contobj, resume_args, resumetable)
}

fn translate_resume_throw(
Expand Down Expand Up @@ -2856,19 +2856,19 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
fn typed_continuations_load_tag_return_values(
&mut self,
builder: &mut FunctionBuilder,
contobj: ir::Value,
contref: ir::Value,
valtypes: &[WasmValType],
) -> Vec<ir::Value> {
wasmfx_impl::typed_continuations_load_tag_return_values(self, builder, contobj, valtypes)
wasmfx_impl::typed_continuations_load_tag_return_values(self, builder, contref, valtypes)
}

/// TODO
fn typed_continuations_cont_ref_get_cont_obj(
fn typed_continuations_cont_obj_get_cont_ref(
&mut self,
builder: &mut FunctionBuilder,
contref: ir::Value,
contobj: ir::Value,
) -> ir::Value {
wasmfx_impl::typed_continuations_cont_ref_get_cont_obj(self, builder, contref)
wasmfx_impl::typed_continuations_cont_obj_get_cont_ref(self, builder, contobj)
}

/// TODO
Expand All @@ -2877,14 +2877,14 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
builder: &mut FunctionBuilder,
values: &[ir::Value],
remaining_arg_count: ir::Value,
contobj: ir::Value,
contref: ir::Value,
) {
wasmfx_impl::typed_continuations_store_resume_args(
self,
builder,
values,
remaining_arg_count,
contobj,
contref,
)
}

Expand All @@ -2898,19 +2898,19 @@ impl<'module_environment> cranelift_wasm::FuncEnvironment for FuncEnvironment<'m
wasmfx_impl::typed_continuations_store_payloads(self, builder, valtypes, values)
}

fn typed_continuations_load_continuation_object(
fn typed_continuations_load_continuation_reference(
&mut self,
builder: &mut FunctionBuilder,
) -> ir::Value {
wasmfx_impl::typed_continuations_load_continuation_object(self, builder)
wasmfx_impl::typed_continuations_load_continuation_reference(self, builder)
}

fn typed_continuations_new_cont_ref(
fn typed_continuations_new_cont_obj(
&mut self,
builder: &mut FunctionBuilder,
contobj_addr: ir::Value,
contref_addr: ir::Value,
) -> ir::Value {
wasmfx_impl::typed_continuations_new_cont_ref(self, builder, contobj_addr)
wasmfx_impl::typed_continuations_new_cont_obj(self, builder, contref_addr)
}

fn use_x86_blendv_for_relaxed_laneselect(&self, ty: Type) -> bool {
Expand Down
26 changes: 13 additions & 13 deletions crates/cranelift/src/wasmfx/baseline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use cranelift_wasm::{FuncTranslationState, WasmResult, WasmValType};
use wasmtime_environ::PtrSize;

#[allow(unused_imports)]
pub(crate) use shared::typed_continuations_cont_ref_get_cont_obj;
pub(crate) use shared::typed_continuations_cont_obj_get_cont_ref;
#[allow(unused_imports)]
pub(crate) use shared::typed_continuations_new_cont_ref;
pub(crate) use shared::typed_continuations_new_cont_obj;

fn typed_continuations_load_payloads<'a>(
env: &mut crate::func_environ::FuncEnvironment<'a>,
Expand Down Expand Up @@ -43,7 +43,7 @@ fn typed_continuations_load_payloads<'a>(
pub(crate) fn typed_continuations_load_tag_return_values<'a>(
env: &mut crate::func_environ::FuncEnvironment<'a>,
builder: &mut FunctionBuilder,
contobj: ir::Value,
contref: ir::Value,
valtypes: &[WasmValType],
) -> Vec<ir::Value> {
let _memflags = ir::MemFlags::trusted();
Expand All @@ -57,7 +57,7 @@ pub(crate) fn typed_continuations_load_tag_return_values<'a>(
env,
let args_ptr =
tc_baseline_continuation_arguments_ptr(
contobj,
contref,
nargs
)
);
Expand All @@ -79,7 +79,7 @@ pub(crate) fn typed_continuations_load_tag_return_values<'a>(
debug_assert!(valtypes.len() == args.len());

// Clear the arguments buffer
call_builtin!(builder, env, tc_baseline_clear_arguments(contobj));
call_builtin!(builder, env, tc_baseline_clear_arguments(contref));

return args;
}
Expand All @@ -93,7 +93,7 @@ pub(crate) fn typed_continuations_store_resume_args<'a>(
builder: &mut FunctionBuilder,
values: &[ir::Value],
_remaining_arg_count: ir::Value,
contobj: ir::Value,
contref: ir::Value,
) {
if values.len() > 0 {
// Retrieve the pointer to the arguments buffer.
Expand All @@ -103,7 +103,7 @@ pub(crate) fn typed_continuations_store_resume_args<'a>(
env,
let args_ptr =
tc_baseline_continuation_arguments_ptr(
contobj,
contref,
nargs
)
);
Expand Down Expand Up @@ -140,7 +140,7 @@ pub(crate) fn typed_continuations_store_payloads<'a>(
}
}

pub(crate) fn typed_continuations_load_continuation_object<'a>(
pub(crate) fn typed_continuations_load_continuation_reference<'a>(
env: &mut crate::func_environ::FuncEnvironment<'a>,
builder: &mut FunctionBuilder,
) -> ir::Value {
Expand All @@ -152,7 +152,7 @@ pub(crate) fn translate_resume<'a>(
env: &mut crate::func_environ::FuncEnvironment<'a>,
builder: &mut FunctionBuilder,
type_index: u32,
resumee_ref: ir::Value,
resumee_obj: ir::Value,
resume_args: &[ir::Value],
resumetable: &[(u32, ir::Block)],
) -> Vec<ir::Value> {
Expand Down Expand Up @@ -201,7 +201,7 @@ pub(crate) fn translate_resume<'a>(
// Prelude: Push the continuation arguments.
{
let resumee_fiber =
shared::typed_continuations_cont_ref_get_cont_obj(env, builder, resumee_ref);
shared::typed_continuations_cont_obj_get_cont_ref(env, builder, resumee_obj);
if resume_args.len() > 0 {
let nargs = builder.ins().iconst(I64, resume_args.len() as i64);

Expand Down Expand Up @@ -302,9 +302,9 @@ pub(crate) fn translate_resume<'a>(
// entirely.
let mut args = typed_continuations_load_payloads(env, builder, &param_types);

// Create and push the continuation reference.
let resumee_ref = shared::typed_continuations_new_cont_ref(env, builder, resumee_fiber);
args.push(resumee_ref);
// Create and push the continuation object.
let resumee_obj = shared::typed_continuations_new_cont_obj(env, builder, resumee_fiber);
args.push(resumee_obj);

// Finally, emit the jump to `label`.
builder.ins().jump(label, &args);
Expand Down
Loading

0 comments on commit 750146b

Please sign in to comment.