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

Call optimization #554

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/fizzy/execute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,12 +495,11 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance&

stack.drop(num_args);

const auto num_outputs = func_type.outputs.size();
// NOTE: we can assume these two from validation
assert(num_outputs <= 1);
assert(ret.has_value == (num_outputs == 1));
assert(func_type.outputs.size() <= 1);
assert(ret.has_value == !func_type.outputs.empty());
// Push back the result
if (num_outputs != 0)
if (ret.has_value != 0)
stack.push(ret.value);

return true;
Expand All @@ -509,10 +508,25 @@ inline bool invoke_function(const FuncType& func_type, const F& func, Instance&
inline bool invoke_function(const FuncType& func_type, uint32_t func_idx, Instance& instance,
OperandStack& stack, int depth) noexcept
{
const auto func = [func_idx](Instance& _instance, span<const Value> args, int _depth) noexcept {
return execute(_instance, func_idx, args.data(), _depth);
};
return invoke_function(func_type, func, instance, stack, depth);
const auto num_args = func_type.inputs.size();
assert(stack.size() >= num_args);

const auto ret = execute(instance, func_idx, stack.rend() - num_args, depth + 1);
// Bubble up traps
if (ret.trapped)
return false;

stack.drop(num_args);

const auto num_outputs = func_type.outputs.size();
// NOTE: we can assume these two from validation
assert(num_outputs <= 1);
assert(ret.has_value == (num_outputs == 1));
// Push back the result
if (num_outputs != 0)
stack.push(ret.value);

return true;
}
} // namespace

Expand Down