-
Notifications
You must be signed in to change notification settings - Fork 1.8k
[stack-switching] GC interaction #14140
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
Open
dhil
wants to merge
1
commit into
bytecodealliance:main
Choose a base branch
from
dhil:gc-stack-switching
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
cranelift/filetests/filetests/isa/x64/stack_switch_stack_map.clif
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| test compile | ||
| set opt_level=speed | ||
| set stack_switch_model=basic | ||
| target x86_64 | ||
|
|
||
| ;; A stack switch is a GC safepoint. Its stack map is associated with the | ||
| ;; resume PC saved in the outgoing control context. | ||
| function %switch_with_stack_map(i64, i64, i64) -> i64 { | ||
| ss0 = explicit_slot 4 | ||
|
|
||
| block0(v0: i64, v1: i64, v2: i64): | ||
| v3 = stack_switch v0, v1, v2, stack_map=[i32 @ ss0+0] | ||
| return v3 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
|
|
||
| use crate::TRAP_ARRAY_OUT_OF_BOUNDS; | ||
| use crate::bounds_checks::BoundsCheck; | ||
| use crate::func_environ::{CheckedEntity, Extension, FuncEnvironment}; | ||
| use crate::func_environ::{CheckedEntity, Extension, FuncEnvironment, stack_switching::fatpointer}; | ||
| use crate::translate::{ | ||
| Heap, HeapData, MemoryKind, StructFieldsVec, TargetEnvironment, VmctxLoadChain, | ||
| }; | ||
|
|
@@ -377,10 +377,7 @@ pub fn read_field_at_addr( | |
| .call(get_interned_func_ref, &[vmctx, func_ref_id, expected_ty]); | ||
| builder.func.dfg.first_result(call_inst) | ||
| } | ||
| WasmHeapTopType::Cont => { | ||
| // TODO(#10248) GC integration for stack switching | ||
| return stack_switching_unsupported(); | ||
| } | ||
| WasmHeapTopType::Cont => read_cont_ref_at_addr(func_env, builder, addr, flags)?, | ||
| }, | ||
| }, | ||
| }; | ||
|
|
@@ -431,6 +428,89 @@ pub fn intern_func_ref( | |
| Ok(builder.ins().ireduce(ir::types::I32, func_ref_id)) | ||
| } | ||
|
|
||
| fn intern_cont_ref( | ||
| func_env: &mut FuncEnvironment<'_>, | ||
| builder: &mut FunctionBuilder<'_>, | ||
| ref_type: WasmRefType, | ||
| contobj: ir::Value, | ||
| ) -> ir::Value { | ||
| assert_eq!(ref_type.heap_type.top(), WasmHeapTopType::Cont); | ||
|
|
||
| if ref_type.heap_type == WasmHeapType::NoCont { | ||
| let zero = builder.ins().iconst(ir::types::I32, 0); | ||
|
|
||
| if !ref_type.nullable { | ||
| builder.ins().trapz(zero, TRAP_INTERNAL_ASSERT); | ||
| } | ||
|
|
||
| return zero; | ||
| } | ||
|
|
||
| let (revision, contref) = fatpointer::deconstruct(func_env, &mut builder.cursor(), contobj); | ||
| let vmctx = func_env.vmctx_val(&mut builder.cursor()); | ||
| let intern = func_env | ||
| .builtin_functions | ||
| .intern_contref_for_gc_heap(builder.func); | ||
| let call = builder.ins().call(intern, &[vmctx, contref, revision]); | ||
| let id = builder.func.dfg.first_result(call); | ||
| builder.ins().ireduce(ir::types::I32, id) | ||
| } | ||
|
|
||
| fn read_cont_ref_at_addr( | ||
| func_env: &mut FuncEnvironment<'_>, | ||
| builder: &mut FunctionBuilder<'_>, | ||
| addr: ir::Value, | ||
| flags: ir::MemFlagsData, | ||
| ) -> WasmResult<ir::Value> { | ||
| let id = builder.ins().load(ir::types::I32, flags, addr, 0); | ||
|
|
||
| // We create a stack slot to hold the continuation values (16 | ||
| // bytes), and pass the address of this slot as the out parameter | ||
| // to the builtin. | ||
| let pointer_type = func_env.pointer_type(); | ||
| let pointer_bytes = pointer_type.bytes(); | ||
| let slot = builder.create_sized_stack_slot(ir::StackSlotData::new( | ||
| ir::StackSlotKind::ExplicitSlot, | ||
| 2 * pointer_bytes, | ||
| u8::try_from(pointer_bytes.trailing_zeros()).unwrap(), | ||
| )); | ||
| let out_result = builder.ins().stack_addr(pointer_type, slot, 0); | ||
|
|
||
| let vmctx = func_env.vmctx_val(&mut builder.cursor()); | ||
| let get = func_env | ||
| .builtin_functions | ||
| .get_interned_contref(builder.func); | ||
| builder.ins().call(get, &[vmctx, id, out_result]); | ||
|
|
||
| let region = func_env.alias_regions.stack_slot_region(builder.func, slot); | ||
| let flags = ir::MemFlagsData::trusted().with_alias_region(Some(region)); | ||
| let contref = builder.ins().load(pointer_type, flags, out_result, 0); | ||
| let revision = builder.ins().load( | ||
| pointer_type, | ||
| flags, | ||
| out_result, | ||
| i32::try_from(pointer_bytes).unwrap(), | ||
| ); | ||
|
Comment on lines
+487
to
+493
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. Can these offsets use constants defined in |
||
| Ok(fatpointer::construct( | ||
| func_env, | ||
| &mut builder.cursor(), | ||
| revision, | ||
| contref, | ||
| )) | ||
| } | ||
|
|
||
| fn write_cont_ref_at_addr( | ||
| func_env: &mut FuncEnvironment<'_>, | ||
| builder: &mut FunctionBuilder<'_>, | ||
| ref_type: WasmRefType, | ||
| flags: ir::MemFlagsData, | ||
| field_addr: ir::Value, | ||
| contobj: ir::Value, | ||
| ) { | ||
| let id = intern_cont_ref(func_env, builder, ref_type, contobj); | ||
| builder.ins().store(flags, id, field_addr, 0); | ||
| } | ||
|
|
||
| fn write_func_ref_at_addr( | ||
| func_env: &mut FuncEnvironment<'_>, | ||
| builder: &mut FunctionBuilder<'_>, | ||
|
|
@@ -487,7 +567,9 @@ pub fn write_field_at_addr( | |
| func_env, builder, r, field_addr, new_val, flags, | ||
| )?; | ||
| } | ||
| WasmHeapTopType::Cont => return stack_switching_unsupported(), | ||
| WasmHeapTopType::Cont => { | ||
| write_cont_ref_at_addr(func_env, builder, r, flags, field_addr, new_val) | ||
| } | ||
| }, | ||
| WasmStorageType::Val(_) => { | ||
| assert_eq!( | ||
|
|
@@ -527,6 +609,10 @@ fn default_value( | |
| } | ||
| WasmValType::Ref(r) => { | ||
| assert!(r.nullable); | ||
| if r.heap_type.top() == WasmHeapTopType::Cont { | ||
| let zero = cursor.ins().iconst(func_env.pointer_type(), 0); | ||
| return fatpointer::construct(func_env, cursor, zero, zero); | ||
| } | ||
| let (ty, needs_stack_map) = func_env.reference_type(r.heap_type); | ||
|
|
||
| // NB: The collector doesn't need to know about null references. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This will create a new stack slot each time we read a contref from a GC object, correct? But each stack slot is only live across one read? It seems like we should have a
get_or_create_contref_stack_slotmethod in that case, or else repeatedly reading a contref could create a whole bunch of stack slots.