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

Handle "leaf functions" first in full inliner. #9314

Merged
merged 3 commits into from
Sep 14, 2020

Conversation

chriseth
Copy link
Contributor

@chriseth chriseth commented Jul 6, 2020

Fixes #9127

@@ -508,54 +508,52 @@
// for { } lt(i, length) { i := add(i, 1) }
// {
// if iszero(slt(add(src, _1), end)) { revert(0, 0) }
// let _4 := 0x2
// let dst_1 := allocateMemory(array_allocation_size_t_array$_t_uint256_$2_memory(_4))
// let dst_1 := allocateMemory(_3)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

good change!

This comment was marked as spam.

@chriseth
Copy link
Contributor Author

chriseth commented Jul 6, 2020

We should compile some contracts via yul and perform gas checks.

@hrkrshnn
Copy link
Member

hrkrshnn commented Jul 6, 2020

We should compile some contracts via yul and perform gas checks.

It would be really nice if we had a benchmark for optimization rules. What do we currently do?

@chriseth
Copy link
Contributor Author

chriseth commented Jul 6, 2020

There are some very limited gas tests, we should add some more, especially also comparing compiling via yul not!

handleBlock(fun.second->name, fun.second->body);
updateCodeSize(*fun.second);
vector<YulString> removed;
for (auto it = cg.functionCalls.begin(); it != cg.functionCalls.end();)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should not iterate in the YulString order.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please check but I think it is not a problem anymore.

@chriseth
Copy link
Contributor Author

To make the impact of renaming functions as small as possible, this should use std::stable_sort on a source-order vector of function definitions with a comparator that compares according to the call depth.

@chriseth chriseth force-pushed the inlineStartingFromLeaves branch 5 times, most recently from fdca159 to f230638 Compare September 8, 2020 12:02
// }
// function f()
// function f_23_88(a) -> x
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like this does not work anymore.

// }
// function foo(b) -> out1, out2
// function foo_24_75(a, b) -> out1, out2, out3
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same here.

// f()
// sstore(0, 1)
// }
// function f()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is this test still valid?

@chriseth chriseth requested a review from hrkrshnn September 8, 2020 12:08
@chriseth chriseth marked this pull request as ready for review September 8, 2020 12:08
Comment on lines 124 to 151
while (true)
{
vector<YulString> removed;
for (auto it = cg.functionCalls.begin(); it != cg.functionCalls.end();)
{
auto const& [fun, callees] = *it;
if (callees.empty())
{
removed.emplace_back(fun);
depths[fun] = currentDepth;
it = cg.functionCalls.erase(it);
}
else
++it;
}

for (auto& call: cg.functionCalls)
call.second -= removed;

currentDepth++;

if (removed.empty())
break;
}
Copy link
Member

Choose a reason for hiding this comment

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

Assuming that an empty set (for CallGraph map's .second) corresponds to say a starting node s. And if we invert the map, then the problem of depth finding gets reduced to finding the longest path to all vertices starting from s. I think this may be solved easier. (Abstractly this is just putting all edge weights -1 and finding shortest paths from s, but there might be a simple way to do it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you elaborate on this a little? It currently does not sound simpler to me :)

Copy link
Member

@hrkrshnn hrkrshnn Sep 14, 2020

Choose a reason for hiding this comment

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

What I meant is the following. Construct a directed graph where each vertex is a FunctionDefinition. There is an edge from a -> b if b calls a (this is same as the "inverted map" of CallGraph) Define an extra vertex s and extra edges from s to a vertex v if v does not call any other function (i.e., indegree of v is zero)

Let's assign weight -1 to all the edges. Now, the problem of finding depth is the same as finding the shortest path from s to all vertices. Theoretically, Bellman-Ford should solve it. Maybe there is a simpler way since all the weights are the same.

But I guess the current implementation is fine too.

Edit: recursion/cycles will make bellman-ford not work for our case :(.

// totalCost: 615251
// codeDepositCost: 597000
// executionCost: 632
// totalCost: 597632
Copy link
Member

Choose a reason for hiding this comment

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

A diff of the changes. Some functions with unused function parameters exist, so we can further decrease the costs. But still the gas costs are lower than this point

}
{
let _3, _4 := storage_array_index_access_t_array$_t_uint256_$dyn_storage(_1, vloc_i)
Copy link
Member

Choose a reason for hiding this comment

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

The sload(_1) that comes from here is gone. So this is a nice change.

@chriseth chriseth force-pushed the inlineStartingFromLeaves branch 2 times, most recently from e2a0d8d to cb8925e Compare September 9, 2020 16:32
@chriseth chriseth force-pushed the inlineStartingFromLeaves branch from cb8925e to 42c26e0 Compare September 10, 2020 15:02
@chriseth
Copy link
Contributor Author

Rebased.

@chriseth
Copy link
Contributor Author

@hrkrshnn your last commit looks good. Is this ready to be merged then? :)

@chriseth chriseth merged commit 552a5f0 into develop Sep 14, 2020
@chriseth chriseth deleted the inlineStartingFromLeaves branch September 14, 2020 18:22
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.

Change function inlining order
4 participants