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

Introduce ExecutionContext instead of depth param #766

Merged
merged 5 commits into from
Mar 29, 2021
Merged

Conversation

chfast
Copy link
Collaborator

@chfast chfast commented Mar 23, 2021

No description provided.

@codecov
Copy link

codecov bot commented Mar 23, 2021

Codecov Report

Merging #766 (6e8a0a5) into master (bd76ee3) will decrease coverage by 0.00%.
The diff coverage is 94.54%.

@@            Coverage Diff             @@
##           master     #766      +/-   ##
==========================================
- Coverage   99.26%   99.25%   -0.01%     
==========================================
  Files          77       77              
  Lines       11718    11740      +22     
==========================================
+ Hits        11632    11653      +21     
- Misses         86       87       +1     
Flag Coverage Δ
rust 99.86% <ø> (ø)
spectests 90.57% <94.11%> (+0.02%) ⬆️
unittests 99.21% <94.54%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
lib/fizzy/instantiate.hpp 100.00% <ø> (ø)
tools/wasi/wasi.cpp 59.57% <16.66%> (ø)
lib/fizzy/capi.cpp 98.93% <94.44%> (-0.34%) ⬇️
lib/fizzy/execute.cpp 98.71% <100.00%> (+<0.01%) ⬆️
lib/fizzy/execute.hpp 100.00% <100.00%> (ø)
lib/fizzy/instantiate.cpp 100.00% <100.00%> (ø)
test/unittests/api_test.cpp 100.00% <100.00%> (ø)
test/unittests/capi_test.cpp 99.87% <100.00%> (ø)
test/unittests/execute_call_depth_test.cpp 100.00% <100.00%> (ø)
test/unittests/execute_call_test.cpp 100.00% <100.00%> (ø)
... and 5 more

Copy link
Collaborator

@gumb0 gumb0 left a comment

Choose a reason for hiding this comment

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

Looks good, only some minor comments.

lib/fizzy/execute.hpp Outdated Show resolved Hide resolved
test/unittests/capi_test.cpp Outdated Show resolved Hide resolved
lib/fizzy/execute.hpp Outdated Show resolved Hide resolved
include/fizzy/fizzy.h Show resolved Hide resolved
The ExecutionContext type represents the storage for information shared
by calls in the same execution "thread".
@chfast chfast force-pushed the execution_context branch 2 times, most recently from 6a7e7a7 to 5bb55bb Compare March 23, 2021 17:17
auto* func = static_cast<fizzy::ExternalFunction*>(context);
return wrap((func->function)(*unwrap(instance), unwrap(args), depth));
static constexpr FizzyExternalFn c_function =
[](void* host_context, FizzyInstance* instance, const FizzyValue* args,
Copy link
Member

Choose a reason for hiding this comment

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

Nit: it seems some places use _ctx, while this uses _context. I think a while back we argued to use the long form, but if we go for the short one, please make it consistent.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Shadowing issues here.

Do you want to change everything to context?

Copy link
Member

Choose a reason for hiding this comment

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

It was _context before, I'm happy with _ctx too.

@@ -330,7 +330,9 @@ TEST(capi, find_exported_function)
EXPECT_EQ(function.type.output, FizzyValueTypeI32);
EXPECT_NE(function.context, nullptr);
ASSERT_NE(function.function, nullptr);
EXPECT_THAT(function.function(function.context, instance, nullptr, 0), CResult(42_u32));
fizzy::ExecutionContext ctx;
auto* const c_ctx = reinterpret_cast<FizzyExecutionContext*>(&ctx);
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is this the intended C API? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Calling host function wrapper is not part of C API.

Copy link
Member

Choose a reason for hiding this comment

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

Well function.function is accessible in C, it is the C struct, isn't it ?

What is the goal of this test?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah good point, it was possible to call FizzyExternalFunction before.

Maybe wrapped external function in FizzyExternalFunction wrap(fizzy::ExternalFunction external_func) should call unwrap(ctx) only if context is non-null, otherwise create default one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There should not be two way of executing a wasm function. You should pick if you want to execute returned pointer to external function. Or having execute(index).

Anyway, the solution in #771.

Copy link
Member

Choose a reason for hiding this comment

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

I'm fine with either but should be documented.

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 also leave a comment here in the test too?

Copy link
Collaborator

Choose a reason for hiding this comment

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

There should not be two way of executing a wasm function. You should pick if you want to execute returned pointer to external function. Or having execute(index).

Not sure why it's a big problem, I think it might be convenient and logical: you're calling fizzy_find_exported_function and getting a "function" that you can call.

Anyway, the solution in #771.

So is #771 in your view introducing a problem, that we should avoid?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There should not be two way of executing a wasm function. You should pick if you want to execute returned pointer to external function. Or having execute(index).

Not sure why it's a big problem, I think it might be convenient and logical: you're calling fizzy_find_exported_function and getting a "function" that you can call.

I agree here, but then there should not be fizzy_execute(). I.e. you can only execute exported functions.
In practice, I prefer the default ExecuteContext creation to be done in single place than in multiple places.

Anyway, the solution in #771.

So is #771 in your view introducing a problem, that we should avoid?

No, it looks good. But there are some details there we need to discuss later.

Copy link
Collaborator

@gumb0 gumb0 Mar 25, 2021

Choose a reason for hiding this comment

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

So this PR breaks one of the ways functions are called, #771 will restore it back, but you think in the future on of the ways should be removed?

std::any host_ctx;
ExecutionContext execution_ctx;
EXPECT_THAT(host_fn_1(host_ctx, instance, nullptr, execution_ctx), Traps());
EXPECT_THAT(host_fn_2(host_ctx, instance, nullptr, execution_ctx), Traps());
Copy link
Member

Choose a reason for hiding this comment

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

Not sure why, but this test perhaps intentionally used two separate host contextes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both of them were not initialized so they couldn't be used anyway.

@chfast
Copy link
Collaborator Author

chfast commented Mar 24, 2021

Haswell 4.0 GHz, Clang LTO

fizzy/parse/blake2b_mean                                           -0.0102         -0.0102            24            23            24            23
fizzy/instantiate/blake2b_mean                                     -0.0103         -0.0103            29            29            29            29
fizzy/execute/blake2b/512_bytes_rounds_1_mean                      -0.0120         -0.0120            81            80            81            80
fizzy/execute/blake2b/512_bytes_rounds_16_mean                     -0.0043         -0.0043          1221          1215          1221          1215
fizzy/parse/ecpairing_mean                                         -0.0005         -0.0005          1377          1377          1377          1377
fizzy/instantiate/ecpairing_mean                                   +0.0059         +0.0059          1432          1440          1432          1440
fizzy/execute/ecpairing/onepoint_mean                              -0.0464         -0.0464        416298        396974        416300        396975
fizzy/parse/keccak256_mean                                         -0.0033         -0.0033            43            43            43            43
fizzy/instantiate/keccak256_mean                                   -0.0036         -0.0037            48            47            48            47
fizzy/execute/keccak256/512_bytes_rounds_1_mean                    -0.0420         -0.0420           103            99           103            99
fizzy/execute/keccak256/512_bytes_rounds_16_mean                   -0.0450         -0.0450          1525          1456          1525          1456
fizzy/parse/memset_mean                                            -0.0109         -0.0109             6             6             6             6
fizzy/instantiate/memset_mean                                      -0.0057         -0.0057            10            10            10            10
fizzy/execute/memset/256_bytes_mean                                -0.0217         -0.0217             7             7             7             7
fizzy/execute/memset/60000_bytes_mean                              -0.0225         -0.0225          1522          1488          1522          1488
fizzy/parse/mul256_opt0_mean                                       -0.0102         -0.0102             8             8             8             8
fizzy/instantiate/mul256_opt0_mean                                 -0.0176         -0.0176            11            11            11            11
fizzy/execute/mul256_opt0/input1_mean                              -0.0362         -0.0362            29            28            29            28
fizzy/parse/ramanujan_pi_mean                                      -0.0089         -0.0089            25            25            25            25
fizzy/instantiate/ramanujan_pi_mean                                -0.0078         -0.0078            29            29            29            29
fizzy/execute/ramanujan_pi/33_runs_mean                            -0.0640         -0.0640           115           108           115           108
fizzy/parse/sha1_mean                                              -0.0036         -0.0036            39            39            39            39
fizzy/instantiate/sha1_mean                                        +0.0008         +0.0008            43            43            43            43
fizzy/execute/sha1/512_bytes_rounds_1_mean                         -0.0132         -0.0132            88            87            88            87
fizzy/execute/sha1/512_bytes_rounds_16_mean                        -0.0108         -0.0108          1227          1214          1227          1214
fizzy/parse/sha256_mean                                            -0.0003         -0.0003            64            64            64            64
fizzy/instantiate/sha256_mean                                      -0.0037         -0.0037            69            69            69            69
fizzy/execute/sha256/512_bytes_rounds_1_mean                       +0.0020         +0.0020            81            81            81            81
fizzy/execute/sha256/512_bytes_rounds_16_mean                      +0.0025         +0.0025          1115          1117          1115          1117
fizzy/parse/taylor_pi_mean                                         -0.0022         -0.0022             2             2             2             2
fizzy/instantiate/taylor_pi_mean                                   -0.0098         -0.0098             6             6             6             6
fizzy/execute/taylor_pi/pi_1000000_runs_mean                       -0.0015         -0.0015         40851         40788         40852         40789
fizzy/parse/micro/eli_interpreter_mean                             -0.0202         -0.0202             4             4             4             4
fizzy/instantiate/micro/eli_interpreter_mean                       -0.0043         -0.0043             8             8             8             8
fizzy/execute/micro/eli_interpreter/exec105_mean                   +0.0121         +0.0121             4             5             4             5
fizzy/parse/micro/factorial_mean                                   +0.0099         +0.0099             1             1             1             1
fizzy/instantiate/micro/factorial_mean                             -0.0032         -0.0032             1             1             1             1
fizzy/execute/micro/factorial/20_mean                              -0.0069         -0.0069             1             1             1             1
fizzy/parse/micro/fibonacci_mean                                   -0.0321         -0.0321             1             1             1             1
fizzy/instantiate/micro/fibonacci_mean                             -0.0192         -0.0192             1             1             1             1
fizzy/execute/micro/fibonacci/24_mean                              +0.0660         +0.0660          5125          5464          5125          5464
fizzy/parse/micro/host_adler32_mean                                +0.0151         +0.0151             1             2             1             2
fizzy/instantiate/micro/host_adler32_mean                          -0.0043         -0.0043             4             4             4             4
fizzy/execute/micro/host_adler32/1_mean                            -0.0071         -0.0071             0             0             0             0
fizzy/execute/micro/host_adler32/1000_mean                         -0.0190         -0.0190            31            30            31            30
fizzy/parse/micro/icall_hash_mean                                  +0.0044         +0.0044             3             3             3             3
fizzy/instantiate/micro/icall_hash_mean                            -0.0084         -0.0084             7             7             7             7
fizzy/execute/micro/icall_hash/1000_steps_mean                     +0.0101         +0.0101            69            70            69            70
fizzy/parse/micro/spinner_mean                                     -0.0014         -0.0014             1             1             1             1
fizzy/instantiate/micro/spinner_mean                               -0.0028         -0.0028             1             1             1             1
fizzy/execute/micro/spinner/1_mean                                 +0.0251         +0.0251             0             0             0             0
fizzy/execute/micro/spinner/1000_mean                              -0.0107         -0.0107             9             9             9             9
fizzy/parse/stress/guido-fuzzer-find-1_mean                        -0.0093         -0.0093           138           137           138           137
fizzy/instantiate/stress/guido-fuzzer-find-1_mean                  -0.0076         -0.0076           167           166           167           166

@axic
Copy link
Member

axic commented Mar 24, 2021

Why do we see a speed bump?

/// @param instance Pointer to module instance.
/// @param args Pointer to the argument array. Can be NULL iff function has no inputs.
/// @param depth Call stack depth.
/// @param ctx Opaque pointer to execution context. Execution context cannot be created
Copy link
Collaborator

Choose a reason for hiding this comment

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

Needs a note that it is allowed to be NULL.

@@ -137,9 +137,16 @@ inline FizzyExternalFunction wrap(fizzy::ExternalFunction external_func)
{
static constexpr FizzyExternalFn c_function =
[](void* host_ctx, FizzyInstance* instance, const FizzyValue* args,
FizzyExecutionContext* ctx) -> FizzyExecutionResult {
FizzyExecutionContext* c_ctx) -> FizzyExecutionResult {
// If execution context not provided, allocate new one.
Copy link
Member

@axic axic Mar 26, 2021

Choose a reason for hiding this comment

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

is not provided`

or could just say "Allocate new execution context if none provided."

@chfast chfast merged commit 1bbd7f2 into master Mar 29, 2021
@chfast chfast deleted the execution_context branch March 29, 2021 07:58
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