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

Add a test case for using host functions #320

Merged
merged 6 commits into from
May 25, 2020
Merged

Add a test case for using host functions #320

merged 6 commits into from
May 25, 2020

Conversation

axic
Copy link
Member

@axic axic commented May 19, 2020

Depends on #318.

{
auto engine = engine_create_fn();
ASSERT_TRUE(engine->parse(wasm));
ASSERT_TRUE(engine->instantiate(wasm));
Copy link
Member Author

Choose a reason for hiding this comment

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

Ideally this test case would add the imported function here, but right now I would refrain from that because that introduces a call overhead in our abstraction. Maybe I'm wrong here, @gumb0 @chfast?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this will help in the case were we have only single host function.

Copy link
Member Author

Choose a reason for hiding this comment

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

For this PR it would not make any difference, however if the interface would be flexible, we could add more functions in fizzy-bench or have external tools use it, such as ewasm-bench, with a multitude of functions.

@@ -35,6 +36,17 @@ std::unique_ptr<WasmEngine> create_wabt_engine()
bool WabtEngine::parse(bytes_view input) const
{
wabt::interp::Environment env;

wabt::interp::HostModule* hostModule = env.AppendHostModule("env");
assert(hostModule != nullptr);
Copy link
Member Author

Choose a reason for hiding this comment

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

Should return false.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think this is fine as it is. Would fail on memory allocation only.

@axic
Copy link
Member Author

axic commented May 19, 2020

Will also add a micro-benchmark which calls env::crc32() N-times and adds the results together (v += crc32(v).

@axic axic force-pushed the test-host branch 3 times, most recently from 3cce710 to 7746d7f Compare May 20, 2020 16:35
Copy link
Collaborator

@chfast chfast left a comment

Choose a reason for hiding this comment

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

I see two problem:

  1. Big maintenance burden. Is it number of host function times number of engines?
  2. This predates "call" rework/optimization so it will be another part of code to update.

@axic
Copy link
Member Author

axic commented May 20, 2020

Big maintenance burden. Is it number of host function times number of engines?

Only plan to have this single host function for unit testing and benchmarking.

@axic axic force-pushed the test-host branch 3 times, most recently from a0632b6 to d5b916a Compare May 21, 2020 00:02
Copy link
Collaborator

@chfast chfast left a comment

Choose a reason for hiding this comment

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

Only plan to have this single host function for unit testing and benchmarking.

Then it is fine.

test/utils/fizzy_engine.cpp Outdated Show resolved Hide resolved
@axic axic force-pushed the test-host branch 5 times, most recently from 7637f21 to 182e4ed Compare May 22, 2020 14:51
@axic axic force-pushed the test-host branch 2 times, most recently from 945103e to a9f790a Compare May 22, 2020 15:11
@axic axic requested review from gumb0 and chfast May 22, 2020 15:15
@axic axic force-pushed the test-host branch 5 times, most recently from 832d58d to 13af1cb Compare May 22, 2020 15:37
test/utils/adler32.hpp Outdated Show resolved Hide resolved
@axic axic requested a review from gumb0 May 22, 2020 16:52
test/utils/adler32.hpp Outdated Show resolved Hide resolved
test/utils/fizzy_engine.cpp Outdated Show resolved Hide resolved
test/utils/wasm3_engine.cpp Outdated Show resolved Hide resolved
const void* env_adler32(IM3Runtime /*runtime*/, uint64_t* stack, void* mem)
{
uint64_t* ret = stack;
uint32_t offset = *(uint32_t*)stack++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
uint32_t offset = *(uint32_t*)stack++;
const auto offset = *static_cast<uint32_t*>(stack++);

Copy link
Member Author

Choose a reason for hiding this comment

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

Will do, it was copy paste from wasm3 :)

assert(hostModule != nullptr);

hostModule->AppendFuncExport("adler32", {{wabt::Type::I32, wabt::Type::I32}, {wabt::Type::I32}},
[=](const wabt::interp::HostFunc*, const wabt::interp::FuncSignature*,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Quite big for a lambda, can you make the code inside a separate private method?

Also why not use the same function in parse?

Copy link
Member Author

@axic axic May 22, 2020

Choose a reason for hiding this comment

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

Parse doesn't need it, it only wants the signature.

I had a different version using a unique_ptr but something wasn't working. The problem is GetMemory will only return correctly after instantiation, so it can't be captured prior. Also wabt has no context argument, so we'll need a lambda in any case, but could extract out into a helper.

I would prefer however not to do any of that here, but someone else could optimize these in subsequent PRs.

test/utils/fizzy_engine.cpp Outdated Show resolved Hide resolved
const void* env_adler32(IM3Runtime /*runtime*/, uint64_t* stack, void* mem)
{
uint64_t* ret = stack;
uint32_t offset = *(uint32_t*)stack++;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this ((uint32_t*)stack)++ or (uint32_t*)(stack++)?

Copy link
Member Author

Choose a reason for hiding this comment

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

I believe the intention is *(uint32_t*)(stack++). It is a copy paste from wasm3.

@axic axic force-pushed the test-host branch 2 times, most recently from e90cf0e to a0f952b Compare May 22, 2020 17:38
fizzy::execution_result env_adler32(fizzy::Instance& instance, std::vector<uint64_t> args, int)
{
assert(instance.memory != nullptr);
// Since memory is a bytes_view, substr produces another bytes_view and no copy is performed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

comment is kinda outdated now :)

Copy link
Member Author

Choose a reason for hiding this comment

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

Will remove before merging.

Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

assert(m_env.GetMemoryCount() > 0);
auto memory = m_env.GetMemory(0);
assert(memory != nullptr);
auto memory_data = memory->data;
Copy link
Collaborator

Choose a reason for hiding this comment

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

what's the type of memory_data?

Copy link
Collaborator

Choose a reason for hiding this comment

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

it's vector<char>

Copy link
Member Author

Choose a reason for hiding this comment

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

Some wabt memory type. Apparently std::vector<char> data.

Copy link
Collaborator

@chfast chfast left a comment

Choose a reason for hiding this comment

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

With single outdated comment to be fixed.

{
auto engine = engine_create_fn();
ASSERT_TRUE(engine->parse(wasm));
ASSERT_TRUE(engine->instantiate(wasm));
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure this will help in the case were we have only single host function.

fizzy::execution_result env_adler32(fizzy::Instance& instance, std::vector<uint64_t> args, int)
{
assert(instance.memory != nullptr);
// Since memory is a bytes_view, substr produces another bytes_view and no copy is performed.
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@axic axic merged commit d0b883f into master May 25, 2020
@axic axic deleted the test-host branch May 25, 2020 15:19
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