Skip to content
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

Added codegen for memory.size and memory.grow instructions #14

Merged
merged 2 commits into from
Mar 3, 2021

Conversation

geky
Copy link
Collaborator

@geky geky commented Jul 28, 2020

Creating PR for review, currently building off of #12, happy to rebase on what changes goes in. Only the last commit should be relevant: fc2bbe5

I tried to follow the implementation of instruction->runtime function instructions, so hopefully this is better than my previous attempt (#2).

Currently linking to memory_grow and memory_size_. Need to fix the name conflict with the memory_size variable, though happy to take alternative names.

Have we considered prefixing all of the runtime functions with something like awsmr_memory_grow?

@Others
Copy link
Contributor

Others commented Jul 28, 2020

I would prefix both these functions with "instruction_" for now. I see this builds off your other PR

Sorry for not being fast with the reviews on these, working very slowly on switching away from llvm_alt

@geky
Copy link
Collaborator Author

geky commented Jul 28, 2020

Will update.

No worries on the speed. I'm used to PRs dripping through review processes like a coffee filter. Once I can get changes in PR form and presented to you guys I'm happy to have it sit until needed.

It's unfortunate GitHub doesn't render small inter-dependent PRs nicer.

@Others
Copy link
Contributor

Others commented Jul 31, 2020

Reading through these changes--all looks good. Will rereview after the PRs this one is based off are merged :)

@Others
Copy link
Contributor

Others commented Aug 6, 2020

@geky can you rebase this off master?

geky added 2 commits August 8, 2020 19:25
These simply call the externally linked instruction_memory_grow and
instruction_memory_size functions in the runtime.

Once connected, these runtime functions can use the existing expand_memory
logic that is specific to each runtime-memory implementation.
Note this is only needed when incrementally compiling, which we don't do
because of LTO.
@geky geky force-pushed the memory-instructions branch from fe454f8 to 59e68b4 Compare August 9, 2020 00:26
@geky
Copy link
Collaborator Author

geky commented Aug 9, 2020

Updated! This has also been rolled up into #19, which has a number of other small changes. Feel free to take this in as a part of #19 if that is easier.

Comment on lines +980 to +985
ImportSectionEntryType::Memory(memory_ty) => {
self.memories.push(*memory_ty);
}
ImportSectionEntryType::Table(table_ty) => {
self.tables.push(*table_ty);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kinda nervous about this, as we don't actually support additional memories or tables. I suppose I can't think of anywhere else in the code we assume this (or at least nowhere that'd cause subtle unsafety)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these were needed because clang was emitting empty imports. They were empty, but aWsm was failing to parse the empty stubs.

Though I will admit there was a bit of poking-the-code-until-it-works here.

Copy link
Collaborator Author

@geky geky Mar 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does match the process_memory_section/process_table_section function's behavior:

fn process_memory_section(&mut self, p: &mut Parser) -> ProcessState {

Comment on lines +759 to +760
Operator::MemorySize { .. } => Instruction::MemorySize,
Operator::MemoryGrow { .. } => Instruction::MemoryGrow,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there no useful parameters to pipe down?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, the Wasm spec has a byte reserved for "memory index" which is currently unused
https://webassembly.github.io/spec/core/binary/instructions.html#memory-instructions

Note

In future versions of WebAssembly, the additional zero bytes occurring in the encoding of the memory.size
and memory.grow instructions may be used to index additional memories.

You could pipe down this byte, but then the unused variable would move into more places in the code base.

Comment on lines +25 to +37
i32 instruction_memory_size() {
return memory_size / WASM_PAGE_SIZE;
}

i32 instruction_memory_grow(i32 count) {
i32 prev_size = instruction_memory_size();
for (int i = 0; i < count; i++) {
expand_memory();
}

return prev_size;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In retrospect, I really should've abstracted this repeated code out... this new addition is in line with everything else tho, so keep it this way

Comment on lines +30 to +32
i32 instruction_memory_size() {
return memory_size / WASM_PAGE_SIZE;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to be careful here. If you look at the mmap implementation, technically there is some support for MMAP granularity smaller than wasm page size granularity. This could lead to surprising behavior from using this (seemingly simple) function, and we should at least document that.

Long term we probably should decide if we want to support the mmap hack.

@Others Others merged commit 4618797 into gwsystems:master Mar 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants