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

optimize in for low memory device #2535

Open
wants to merge 13 commits into
base: main
Choose a base branch
from

Conversation

primstar-cool
Copy link

  • add step OnSkipFunctionBodyExpr when read with skip_function_bodies
  • add virtual op code OpCodeRaw, when read with skip_function_bodies, the virtual struct will save byte code in a uint8 array, and will write directly on output
  • add macro wabt_expr_make_unique, for override operator new
  • add MemoryAllocStream can output byte code in an allocated buffer

+ add step OnSkipFunctionBodyExpr when read with skip_function_bodies
+ add virtual op code OpCodeRaw, when read with skip_function_bodies, the virtual struct will save byte code in a uint8 array, and will write directly on output
+ add macro wabt_expr_make_unique,  for override operator new
+ add MemoryAllocStream can output byte code in a allocated buffer
Copy link
Member

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

I'm not sure I understand the motivation here. The wabt interpreter (IIUC) is really just designed a reference interpreter. I'm not sure the goal should be to run large program efficiently on resource constrained devices. I think we can leave that the production runtimes out there.

Perhaps you could link to the motivating issue and we could discuss more there?

@@ -851,6 +860,8 @@ DEFINE_INDEX_DESC(OnDelegateExpr, "depth");
DEFINE0(OnDropExpr)
DEFINE0(OnElseExpr)
DEFINE0(OnEndExpr)
DEFINE_SKIP(OnSkipFunctionBodyExpr)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think there is any need to define a macro if there is only one caller.

Copy link
Author

Choose a reason for hiding this comment

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

i will expand the macro in next commit

@@ -1939,6 +1940,17 @@ Result BinaryReader::ReadInstructions(Offset end_offset, const char* context) {
return Result::Error;
}

Result BinaryReader::ReadSkipedFunctionBody(Offset end_offset) {
std::vector<uint8_t> opcode_buffer;
opcode_buffer.resize(end_offset - state_.offset);
Copy link
Member

Choose a reason for hiding this comment

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

Can you just combine there two lines?

std::vector<uint8_t> opcode_buffer(end_offset - state_.offset)

Copy link
Author

Choose a reason for hiding this comment

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

fixed in next commit

@@ -195,6 +195,36 @@ class MemoryStream : public Stream {
std::unique_ptr<OutputBuffer> buf_;
};

class MemoryAllocStream : public wabt::Stream {
Copy link
Member

Choose a reason for hiding this comment

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

I don't see any uses of this new class. Can you perhaps split this out into its own PR along with a description of why its needed?

Copy link
Author

@primstar-cool primstar-cool Jan 26, 2025

Choose a reason for hiding this comment

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

sometimes we can't use std mem alloc (such as run in a pre-allocated mempool)
if we use MemoryStream and copy to managed buffer, it will cost double size.
in low-mem device, we need prevent it.
it can be removed from this feature, and impl by proj.

and macro wabt_expr_make_unique is the same reason. we need prevent huge mem allocate(size or times).
final export override for its size, expr override alloc for frequency

Copy link
Author

Choose a reason for hiding this comment

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

i'v remove it in new commit

Copy link
Collaborator

@SoniEx2 SoniEx2 left a comment

Choose a reason for hiding this comment

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

we would also like to see some tests, see src/test-*.cc.

@@ -429,7 +429,8 @@ enum class ExprType {
Unreachable,

First = AtomicLoad,
Last = Unreachable
Last = Unreachable,
OpCodeRaw = Last + 1, // virtual type
Copy link
Collaborator

Choose a reason for hiding this comment

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

correct us if we're wrong, but you should be able to put it into the enum as normal, between Nop and RefIsNull. you'll need to update the matching array in uh ir.cc (and maybe other places) but the alternative would be to make the code reject OpCodeRaw everywhere else.

also, how about calling this UndecodedOpcodes (between Unary and Unreachable)?

namespace wabt {

#ifdef WABT_OVERRIDE_ALLOC_EXPR
Copy link
Collaborator

Choose a reason for hiding this comment

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

would you mind limiting this PR to skip_function_bodies for ReadBinaryIr?

Location& loc,
BinaryReaderDelegate* delegate,
const ReadBinaryOptions& options) {
BinaryReader reader(static_cast<const uint8_t*>(data) - loc.offset,
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is undefined behavior. would it be possible to add a "virtual offset" to the BinaryReader instead?

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.

3 participants