Repository navigation
cranelift-wasm: call out_of_gas/new_epoch without clobbering registers
#15507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -91,6 +91,8 @@ pub(crate) struct BuiltinFunctions { | |
|
|
||
| builtins: [Option<ir::FuncRef>; BuiltinFunctionIndex::len() as usize], | ||
| breakpoint_trampoline: Option<ir::FuncRef>, | ||
| out_of_gas_preserve_all: Option<ir::FuncRef>, | ||
| new_epoch_preserve_all: Option<ir::FuncRef>, | ||
| } | ||
|
|
||
| impl BuiltinFunctions { | ||
|
|
@@ -99,6 +101,8 @@ impl BuiltinFunctions { | |
| types: BuiltinFunctionSignatures::new(compiler), | ||
| builtins: [None; BuiltinFunctionIndex::len() as usize], | ||
| breakpoint_trampoline: None, | ||
| out_of_gas_preserve_all: None, | ||
| new_epoch_preserve_all: None, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -127,24 +131,63 @@ impl BuiltinFunctions { | |
| f | ||
| } | ||
|
|
||
| /// A call to the `out_of_gas` builtin through its `PreserveAll` | ||
| /// trampoline. | ||
| /// | ||
| /// Fuel checks sit at function entries and in every loop header. A call | ||
| /// with the normal Wasm ABI there clobbers all caller-saved registers on a | ||
| /// path that is almost never taken, which forces every value that is live | ||
| /// across the check into a callee-saved register or a stack slot. The | ||
| /// `PreserveAll` trampoline clobbers nothing, so values stay in whatever | ||
| /// registers they are in. The trampoline returns nothing and raises the | ||
| /// builtin's trap itself. | ||
| pub(crate) fn out_of_gas_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cfallin question for you, did you have a particular reason for doing this style of getting the breakpoint intrisnic vs an alternative approach along the lines of https://github.com/bytecodealliance/wasmtime/pull/14975/changes#diff-f4e65f6eab2ed0d2568b98f7090476dab5cf37bbf7e7d5ef10a6a2ca9e97c33e where internally libcalls are special-cased? Personally I'm partial to the latter approach as it makes so all usage of the libcall is guaranteed to have a single ABI as opposed to accidentally using the wrong helper to get the wrong ABI. Even more ideally I'd love like a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason that we add new libcalls with |
||
| let ptr = self.types.pointer_type; | ||
| *self.out_of_gas_preserve_all.get_or_insert_with(|| { | ||
| Self::preserve_all(func, ptr, BuiltinFunctionIndex::out_of_gas(), false) | ||
| }) | ||
| } | ||
|
|
||
| /// Like `out_of_gas_preserve_all`, for the `new_epoch` builtin. The new | ||
| /// deadline is not returned and has to be reloaded from the store. | ||
| pub(crate) fn new_epoch_preserve_all(&mut self, func: &mut Function) -> ir::FuncRef { | ||
| let ptr = self.types.pointer_type; | ||
| *self.new_epoch_preserve_all.get_or_insert_with(|| { | ||
| Self::preserve_all(func, ptr, BuiltinFunctionIndex::new_epoch(), false) | ||
| }) | ||
| } | ||
|
|
||
| /// Imports `builtin` through its `PreserveAll` trampoline, which takes | ||
| /// only the vmctx, returns nothing and raises the builtin's trap itself. | ||
| fn preserve_all( | ||
| func: &mut Function, | ||
| pointer_type: ir::Type, | ||
| builtin: BuiltinFunctionIndex, | ||
| patchable: bool, | ||
| ) -> ir::FuncRef { | ||
| let mut signature = ir::Signature::new(CallConv::PreserveAll); | ||
| signature.params.push(ir::AbiParam::special( | ||
| pointer_type, | ||
| ir::ArgumentPurpose::VMContext, | ||
| )); | ||
| let signature = func.import_signature(signature); | ||
| let key = FuncKey::PreserveAllToBuiltinTrampoline(builtin); | ||
| let (namespace, index) = key.into_raw_parts(); | ||
| let name = ir::ExternalName::User( | ||
| func.declare_imported_user_function(ir::UserExternalName { namespace, index }), | ||
| ); | ||
| func.import_function(ir::ExtFuncData { | ||
| name, | ||
| signature, | ||
| colocated: true, | ||
| patchable, | ||
| }) | ||
| } | ||
|
|
||
| pub(crate) fn patchable_breakpoint(&mut self, func: &mut Function) -> ir::FuncRef { | ||
| let ptr = self.types.pointer_type; | ||
| *self.breakpoint_trampoline.get_or_insert_with(|| { | ||
| let mut signature = ir::Signature::new(CallConv::PreserveAll); | ||
| signature | ||
| .params | ||
| .push(ir::AbiParam::new(self.types.pointer_type)); | ||
| let signature = func.import_signature(signature); | ||
| let key = FuncKey::PatchableToBuiltinTrampoline(BuiltinFunctionIndex::breakpoint()); | ||
| let (namespace, index) = key.into_raw_parts(); | ||
| let name = ir::ExternalName::User( | ||
| func.declare_imported_user_function(ir::UserExternalName { namespace, index }), | ||
| ); | ||
| func.import_function(ir::ExtFuncData { | ||
| name, | ||
| signature, | ||
| colocated: true, | ||
| patchable: true, | ||
| }) | ||
| Self::preserve_all(func, ptr, BuiltinFunctionIndex::breakpoint(), true) | ||
| }) | ||
| } | ||
| } | ||
|
|
@@ -785,6 +828,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> { | |
| fn fuel_check(&mut self, builder: &mut FunctionBuilder) { | ||
| self.fuel_increment_var(builder); | ||
| let out_of_gas_block = builder.create_block(); | ||
| builder.set_cold_block(out_of_gas_block); | ||
| let continuation_block = builder.create_block(); | ||
|
|
||
| // Note that our fuel is encoded as adding positive values to a | ||
|
|
@@ -812,7 +856,7 @@ impl<'module_environment> FuncEnvironment<'module_environment> { | |
| // intrinsic may alter how much fuel is in the system. | ||
| builder.switch_to_block(out_of_gas_block); | ||
| self.fuel_save_from_var(builder); | ||
| let out_of_gas = self.builtin_functions.out_of_gas(builder.func); | ||
| let out_of_gas = self.builtin_functions.out_of_gas_preserve_all(builder.func); | ||
| let vmctx = self.vmctx_val(&mut builder.cursor()); | ||
| self.call_builtin(builder, out_of_gas, &[vmctx]); | ||
| self.fuel_load_into_var(builder); | ||
|
|
@@ -964,12 +1008,18 @@ impl<'module_environment> FuncEnvironment<'module_environment> { | |
| builder.def_var(self.epoch_deadline_var, deadline); | ||
| self.epoch_check_cached(builder, cur_epoch_value, continuation_block); | ||
|
|
||
| let new_epoch = self.builtin_functions.new_epoch(builder.func); | ||
| // Like the fuel check, call through a `PreserveAll` trampoline so the | ||
| // (cold) call does not clobber registers in loop headers. It does not | ||
| // return the new deadline, so reload it. | ||
| let new_epoch = self.builtin_functions.new_epoch_preserve_all(builder.func); | ||
| let vmctx = self.vmctx_val(&mut builder.cursor()); | ||
| // new_epoch() returns the new deadline, so we don't have to | ||
| // reload it. | ||
| let call = self.call_builtin(builder, new_epoch, &[vmctx]); | ||
| let new_deadline = *builder.func.dfg.inst_results(call).first().unwrap(); | ||
| self.call_builtin(builder, new_epoch, &[vmctx]); | ||
| let vmstore_ctx = self.get_vmstore_context_ptr(builder); | ||
| let new_deadline = self | ||
| .alias_regions | ||
| .vm_store_context() | ||
| .epoch_deadline() | ||
| .load(&mut builder.cursor(), vmstore_ctx); | ||
| builder.def_var(self.epoch_deadline_var, new_deadline); | ||
| builder.ins().jump(continuation_block, &[]); | ||
| builder.seal_block(continuation_block); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactorings like this FWIW are generally fine to include in PRs, but where possible we prefer them to be separated out commit-wise to make review easier. No problem this time, but if you're interested in making future PRs we'd appreciate that.