-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Struct locals etc #12513
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
khagankhan
wants to merge
3
commits into
bytecodealliance:main
Choose a base branch
from
khagankhan:struct-locals-etc
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.
+277
−36
Open
Struct locals etc #12513
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,19 @@ use wasm_encoder::{ | |
| TypeSection, ValType, | ||
| }; | ||
|
|
||
| // This is used as ctx to pass to the function | ||
| #[derive(Clone, Copy)] | ||
| struct StorageBases { | ||
| struct_type_base: u32, | ||
| typed_first_func_index: u32, | ||
| struct_local_idx: u32, | ||
| typed_local_base: u32, | ||
| struct_global_idx: u32, | ||
| typed_global_base: u32, | ||
| struct_table_idx: u32, | ||
| typed_table_base: u32, | ||
| } | ||
|
|
||
| /// A description of a Wasm module that makes a series of `externref` table | ||
| /// operations. | ||
| #[derive(Debug, Default, Serialize, Deserialize)] | ||
|
|
@@ -60,6 +73,8 @@ impl GcOps { | |
| for _i in 0..self.limits.num_params { | ||
| params.push(ValType::EXTERNREF); | ||
| } | ||
| let params_len = | ||
| u32::try_from(params.len()).expect("params len should be within u32 range"); | ||
| let results = vec![]; | ||
| types.ty().function(params, results); | ||
|
|
||
|
|
@@ -78,8 +93,11 @@ impl GcOps { | |
| // 4: `take_struct` | ||
| types.ty().function( | ||
| vec![ValType::Ref(RefType { | ||
| nullable: false, | ||
| heap_type: wasm_encoder::HeapType::ANY, | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Abstract { | ||
| shared: false, | ||
| ty: wasm_encoder::AbstractHeapType::Struct, | ||
| }, | ||
| })], | ||
| vec![], | ||
| ); | ||
|
|
@@ -130,7 +148,7 @@ impl GcOps { | |
| let concrete = struct_type_base + i; | ||
| types.ty().function( | ||
| vec![ValType::Ref(RefType { | ||
| nullable: false, | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Concrete(concrete), | ||
| })], | ||
| vec![], | ||
|
|
@@ -163,6 +181,37 @@ impl GcOps { | |
| table64: false, | ||
| shared: false, | ||
| }); | ||
|
|
||
| let struct_table_idx = tables.len(); | ||
| tables.table(TableType { | ||
| element_type: RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Abstract { | ||
| shared: false, | ||
| ty: wasm_encoder::AbstractHeapType::Struct, | ||
| }, | ||
| }, | ||
| minimum: u64::from(self.limits.table_size), | ||
| maximum: None, | ||
| table64: false, | ||
| shared: false, | ||
| }); | ||
|
|
||
| let typed_table_base = tables.len(); | ||
| for i in 0..struct_count { | ||
| let concrete = struct_type_base + i; | ||
| tables.table(TableType { | ||
| element_type: RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Concrete(concrete), | ||
| }, | ||
| minimum: u64::from(self.limits.table_size), | ||
| maximum: None, | ||
| table64: false, | ||
| shared: false, | ||
| }); | ||
| } | ||
|
|
||
| // Define our globals. | ||
| let mut globals = GlobalSection::new(); | ||
| for _ in 0..self.limits.num_globals { | ||
|
|
@@ -176,6 +225,43 @@ impl GcOps { | |
| ); | ||
| } | ||
|
|
||
| // Add exactly one (ref any) global. | ||
| let struct_global_idx = globals.len(); | ||
| globals.global( | ||
| wasm_encoder::GlobalType { | ||
| val_type: ValType::Ref(RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Abstract { | ||
| shared: false, | ||
| ty: wasm_encoder::AbstractHeapType::Struct, | ||
| }, | ||
| }), | ||
| mutable: true, | ||
| shared: false, | ||
| }, | ||
| &ConstExpr::ref_null(wasm_encoder::HeapType::Abstract { | ||
| shared: false, | ||
| ty: wasm_encoder::AbstractHeapType::Struct, | ||
| }), | ||
| ); | ||
|
Comment on lines
+228
to
+246
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. Comment says At minimum, we should make the comment match the actual code. In the fullness of time (okay as a follow up PR) we should probably have both a |
||
|
|
||
| // Add one typed (ref <type>) global per struct type. | ||
| let typed_global_base = globals.len(); | ||
| for i in 0..struct_count { | ||
| let concrete = struct_type_base + i; | ||
| globals.global( | ||
| wasm_encoder::GlobalType { | ||
| val_type: ValType::Ref(RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Concrete(concrete), | ||
| }), | ||
| mutable: true, | ||
| shared: false, | ||
| }, | ||
| &ConstExpr::ref_null(wasm_encoder::HeapType::Concrete(concrete)), | ||
| ); | ||
| } | ||
|
|
||
| // Define the "run" function export. | ||
| let mut functions = FunctionSection::new(); | ||
| let mut exports = ExportSection::new(); | ||
|
|
@@ -187,17 +273,48 @@ impl GcOps { | |
|
|
||
| // Give ourselves one scratch local that we can use in various `GcOp` | ||
| // implementations. | ||
| let local_decls: Vec<(u32, ValType)> = vec![(1, ValType::EXTERNREF)]; | ||
| let mut local_decls: Vec<(u32, ValType)> = vec![(1, ValType::EXTERNREF)]; | ||
|
|
||
| let scratch_local = params_len; | ||
| let struct_local_idx = scratch_local + 1; | ||
| local_decls.push(( | ||
| 1, | ||
| ValType::Ref(RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Abstract { | ||
| shared: false, | ||
| ty: wasm_encoder::AbstractHeapType::Struct, | ||
| }, | ||
| }), | ||
| )); | ||
|
|
||
| let typed_local_base: u32 = struct_local_idx + 1; | ||
| for i in 0..struct_count { | ||
| let concrete = struct_type_base + i; | ||
| local_decls.push(( | ||
| 1, | ||
| ValType::Ref(RefType { | ||
| nullable: true, | ||
| heap_type: wasm_encoder::HeapType::Concrete(concrete), | ||
| }), | ||
| )); | ||
| } | ||
|
|
||
| let storage_bases = StorageBases { | ||
| struct_type_base, | ||
| typed_first_func_index, | ||
| struct_local_idx, | ||
| typed_local_base, | ||
| struct_global_idx, | ||
| typed_global_base, | ||
| struct_table_idx, | ||
| typed_table_base, | ||
| }; | ||
|
|
||
| let mut func = Function::new(local_decls); | ||
| func.instruction(&Instruction::Loop(wasm_encoder::BlockType::Empty)); | ||
| for op in &self.ops { | ||
| op.insert( | ||
| &mut func, | ||
| self.limits.num_params, | ||
| struct_type_base, | ||
| typed_first_func_index, | ||
| ); | ||
| op.insert(&mut func, scratch_local, storage_bases); | ||
| } | ||
| func.instruction(&Instruction::Br(0)); | ||
| func.instruction(&Instruction::End); | ||
|
|
@@ -244,7 +361,21 @@ impl GcOps { | |
| if self.limits.max_types == 0 | ||
| && matches!( | ||
| op, | ||
| GcOp::StructNew(..) | GcOp::TakeStructCall(..) | GcOp::TakeTypedStructCall(..) | ||
| GcOp::StructNew(..) | ||
| | GcOp::TakeStructCall(..) | ||
| | GcOp::TakeTypedStructCall(..) | ||
| | GcOp::TypedStructLocalSet(..) | ||
| | GcOp::TypedStructLocalGet(..) | ||
| | GcOp::TypedStructGlobalSet(..) | ||
| | GcOp::TypedStructGlobalGet(..) | ||
| | GcOp::TypedStructTableSet(..) | ||
| | GcOp::TypedStructTableGet(..) | ||
| | GcOp::StructTableSet(..) | ||
| | GcOp::StructTableGet(..) | ||
| | GcOp::StructLocalSet(..) | ||
| | GcOp::StructLocalGet(..) | ||
| | GcOp::StructGlobalSet(..) | ||
| | GcOp::StructGlobalGet(..) | ||
| ) | ||
| { | ||
| continue; | ||
|
|
@@ -330,6 +461,15 @@ macro_rules! define_gc_ops { | |
| Self::TakeTypedStructCall(t) => { | ||
| out.push(Some(StackType::Struct(Some(*t)))); | ||
| } | ||
| Self::TypedStructLocalSet(t) => { | ||
| out.push(Some(StackType::Struct(Some(*t)))); | ||
| } | ||
| Self::TypedStructGlobalSet(t) => { | ||
| out.push(Some(StackType::Struct(Some(*t)))); | ||
| } | ||
| Self::TypedStructTableSet(_, t) => { | ||
| out.push(Some(StackType::Struct(Some(*t)))); | ||
| } | ||
| $( | ||
| Self::$op(..) => { | ||
| $( out.push($operand); )* | ||
|
|
@@ -344,6 +484,15 @@ macro_rules! define_gc_ops { | |
| Self::StructNew(t) => { | ||
| out.push(StackType::Struct(Some(*t))); | ||
| } | ||
| Self::TypedStructLocalGet(t) => { | ||
| out.push(StackType::Struct(Some(*t))); | ||
| } | ||
| Self::TypedStructGlobalGet(t) => { | ||
| out.push(StackType::Struct(Some(*t))); | ||
| } | ||
| Self::TypedStructTableGet(_, t) => { | ||
| out.push(StackType::Struct(Some(*t))); | ||
| } | ||
| $( Self::$op(..) => { $( out.push($result); )* }, )* | ||
| } | ||
| } | ||
|
|
@@ -355,7 +504,9 @@ macro_rules! define_gc_ops { | |
| $( $( | ||
| let limit_fn = $limit as fn(&GcOpsLimits) -> $ty; | ||
| let limit = (limit_fn)(limits); | ||
| debug_assert!(limit > 0); | ||
| if limit == 0 { | ||
| return None; | ||
| } | ||
| *$limit_var = *$limit_var % limit; | ||
| )* )? | ||
| } | ||
|
|
@@ -364,7 +515,15 @@ macro_rules! define_gc_ops { | |
| match self { | ||
| Self::StructNew(t) | ||
| | Self::TakeStructCall(t) | ||
| | Self::TakeTypedStructCall(t) => { | ||
| | Self::TakeTypedStructCall(t) | ||
| | Self::TypedStructLocalSet(t) | ||
| | Self::TypedStructLocalGet(t) | ||
| | Self::TypedStructGlobalSet(t) | ||
| | Self::TypedStructGlobalGet(t) | ||
| | Self::TypedStructTableSet(_, t) | ||
| | Self::TypedStructTableGet(_, t) | ||
|
|
||
| => { | ||
| if num_types == 0 { | ||
| return None; | ||
| } | ||
|
|
@@ -454,19 +613,34 @@ define_gc_ops! { | |
| TakeTypedStructCall(type_index: |ops| ops.max_types => u32) | ||
| : [] => [], | ||
|
|
||
| StructLocalSet() : [Some(StackType::Struct(None))] => [], | ||
| StructLocalGet() : [] => [StackType::Struct(None)], | ||
| // `TypedStructLocalSet` operand is special-cased to require `Struct(Some(t))`, so operands list is empty. | ||
| TypedStructLocalSet(type_index: |ops| ops.max_types => u32) : [] => [], | ||
| // `TypedStructLocalGet` result is special-cased to push `Struct(Some(t))`, so results list is empty. | ||
| TypedStructLocalGet(type_index: |ops| ops.max_types => u32) : [] => [], | ||
|
|
||
| StructGlobalSet() : [Some(StackType::Struct(None))] => [], | ||
| StructGlobalGet() : [] => [StackType::Struct(None)], | ||
| // `TypedStructGlobalSet` operand is special-cased to require `Struct(Some(t))`, so operands list is empty. | ||
| TypedStructGlobalSet(type_index: |ops| ops.max_types => u32) : [] => [], | ||
| // `TypedStructGlobalGet` result is special-cased to push `Struct(Some(t))`, so results list is empty. | ||
| TypedStructGlobalGet(type_index: |ops| ops.max_types => u32) : [] => [], | ||
|
|
||
| StructTableSet(elem_index: |ops| ops.table_size => u32) : [Some(StackType::Struct(None))] => [], | ||
| StructTableGet(elem_index: |ops| ops.table_size => u32) : [] => [StackType::Struct(None)], | ||
| // `TypedStructTableSet` operand is special-cased to require `Struct(Some(t))`, so operands list is empty. | ||
| TypedStructTableSet(elem_index: |ops| ops.table_size => u32, type_index: |ops| ops.max_types => u32) : [] => [], | ||
| // `TypedStructTableGet` result is special-cased to push `Struct(Some(t))`, so results list is empty. | ||
| TypedStructTableGet(elem_index: |ops| ops.table_size => u32, type_index: |ops| ops.max_types => u32) : [] => [], | ||
|
|
||
| Drop : [None] => [], | ||
|
|
||
| Null : [] => [StackType ::ExternRef], | ||
| } | ||
|
|
||
| impl GcOp { | ||
| fn insert( | ||
| self, | ||
| func: &mut Function, | ||
| scratch_local: u32, | ||
| struct_type_base: u32, | ||
| typed_first_func_index: u32, | ||
| ) { | ||
| fn insert(self, func: &mut Function, scratch_local: u32, storage_bases: StorageBases) { | ||
| let gc_func_idx = 0; | ||
| let take_refs_func_idx = 1; | ||
| let make_refs_func_idx = 2; | ||
|
|
@@ -511,15 +685,60 @@ impl GcOp { | |
| func.instruction(&Instruction::RefNull(wasm_encoder::HeapType::EXTERN)); | ||
| } | ||
| Self::StructNew(x) => { | ||
| func.instruction(&Instruction::StructNew(x + struct_type_base)); | ||
| func.instruction(&Instruction::StructNew(x + storage_bases.struct_type_base)); | ||
| } | ||
| Self::TakeStructCall(_x) => { | ||
| func.instruction(&Instruction::Call(take_structref_idx)); | ||
| } | ||
| Self::TakeTypedStructCall(x) => { | ||
| let f = typed_first_func_index + x; | ||
| let f = storage_bases.typed_first_func_index + x; | ||
| func.instruction(&Instruction::Call(f)); | ||
| } | ||
| Self::StructLocalGet() => { | ||
| func.instruction(&Instruction::LocalGet(storage_bases.struct_local_idx)); | ||
| } | ||
| Self::TypedStructLocalGet(x) => { | ||
| func.instruction(&Instruction::LocalGet(storage_bases.typed_local_base + x)); | ||
| } | ||
| Self::StructLocalSet() => { | ||
| func.instruction(&Instruction::LocalSet(storage_bases.struct_local_idx)); | ||
| } | ||
| Self::TypedStructLocalSet(x) => { | ||
| func.instruction(&Instruction::LocalSet(storage_bases.typed_local_base + x)); | ||
| } | ||
| Self::StructGlobalGet() => { | ||
| func.instruction(&Instruction::GlobalGet(storage_bases.struct_global_idx)); | ||
| } | ||
| Self::TypedStructGlobalGet(x) => { | ||
| func.instruction(&Instruction::GlobalGet(storage_bases.typed_global_base + x)); | ||
| } | ||
| Self::StructGlobalSet() => { | ||
| func.instruction(&Instruction::GlobalSet(storage_bases.struct_global_idx)); | ||
| } | ||
| Self::TypedStructGlobalSet(x) => { | ||
| func.instruction(&Instruction::GlobalSet(storage_bases.typed_global_base + x)); | ||
| } | ||
| Self::StructTableGet(elem_index) => { | ||
| func.instruction(&Instruction::I32Const(elem_index.cast_signed())); | ||
| func.instruction(&Instruction::TableGet(storage_bases.struct_table_idx)); | ||
| } | ||
| Self::TypedStructTableGet(elem_index, x) => { | ||
| func.instruction(&Instruction::I32Const(elem_index.cast_signed())); | ||
| func.instruction(&Instruction::TableGet(storage_bases.typed_table_base + x)); | ||
| } | ||
| Self::StructTableSet(elem_index) => { | ||
| // Use struct_local_idx (anyref) to temporarily store the value before table.set | ||
| func.instruction(&Instruction::LocalSet(storage_bases.struct_local_idx)); | ||
| func.instruction(&Instruction::I32Const(elem_index.cast_signed())); | ||
| func.instruction(&Instruction::LocalGet(storage_bases.struct_local_idx)); | ||
| func.instruction(&Instruction::TableSet(storage_bases.struct_table_idx)); | ||
| } | ||
| Self::TypedStructTableSet(elem_index, x) => { | ||
| func.instruction(&Instruction::LocalSet(storage_bases.typed_local_base + x)); | ||
| func.instruction(&Instruction::I32Const(elem_index.cast_signed())); | ||
| func.instruction(&Instruction::LocalGet(storage_bases.typed_local_base + x)); | ||
| func.instruction(&Instruction::TableSet(storage_bases.typed_table_base + x)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
I like that you bundled this stuff up into a struct that we can pass around as a single thing, that's a great clean up.
A couple of nitpicks/bikesheds however:
The comment is not particularly helpful to someone reading the code for the first time, e.g. which function is "the function" and is "ctx" a parameter name or something else? I think something like this would be better:
Also, three slashes (
///) for doc comments (even though this isn'tpub, its still the idiomatic style).Perhaps we can rename this to
WasmEncodingIndicesorWasmEncodingBasesor something along those lines? "Storage" feels like a bit of a misnomer to me.