Repository navigation
Conversation
4a03c63 to
1e5e29a
Compare
e8641b3 to
3cbd046
Compare
|
e3be97e to
d123347
Compare
d123347 to
deb4e4c
Compare
blishko
left a comment
There was a problem hiding this comment.
This seems very promising!
Could we put the optimizations to a separate PR? Like the KnowledgeBase change map -> unordered_map, for example.
It would be easier to review just the core necessary for the fix.
I am not sure which commits are necessary for the fix and which are just performance improvements.
|
Ok. I will stack the PRs. The KB change does not change anything in develop branch though. |
980304c to
551dd67
Compare
There was a problem hiding this comment.
I just have a few comments but in general it seems like the fix (for the sake of fixing the bug) is perhaps too much and could be separated out (by removing the change to knowledge base, @blishko probably has some ideas there, or doing it as i suggest below and first just fix the bug as cheaply as possible and do performance stuff later).
For example, you could just keep locations as variable names and the KnowledgeBase in SSA mode, as on develop. Then remove every variable from the SSA value map whose value refers to a variable not in the map, repeating until nothing changes and before the second walk of a loop body, mark every active store whose location's base variable is declared inside the loop as used. That should be relatively small and self-contained at the expense of a few more stores remaining.
| std::unordered_map<VersionedVariable, CachedOffset, VersionedVariableHash> m_offsets; | ||
|
|
||
| /// Current version of each variable that changed at least once. | ||
| std::map<YulName, std::size_t> m_currentVersions; | ||
| std::unordered_map<YulName, std::size_t> m_currentVersions; |
There was a problem hiding this comment.
this change doesn't really belong in a bugfix pr, can we extract it?
There was a problem hiding this comment.
I think it’s separated now to https://github.com/argotorg/solidity/pull/17349/changes
| * UnusedStoreEliminator is an optimizer component that removes sstore and memory store statements if conditions are met | ||
| * for their removal. |
There was a problem hiding this comment.
this seems unnecessary to change, it is the UnusedStoreEliminator header after all
There was a problem hiding this comment.
I added this because this PR moves some structures before the UnusedStoreEliminator. I wanted to make sure that we are talking about this class but not the structure right below. I can revert it if think it’s still unnecessary.
| * To decide whether two operations can touch the same location, the step keeps track of the values of variables | ||
| * while it walks the code. Each location is recorded as a constant offset from the value a variable had at that | ||
| * point in the code. For example, in `let q := add(p, 32) mstore(q, 1)` the location of the store is recorded | ||
| * as 32 more than the value `p` had when the store was executed. When a variable is reassigned, it is treated | ||
| * as a new variable, so a location recorded earlier keeps referring to the old value. This is what allows a store | ||
| * to be compared correctly with reads and writes that happen later, after some of the variables have changed. | ||
| * Where control flow joins, the values of variables assigned inside a branch or loop are forgotten. | ||
| * | ||
| * A store whose result might still be read is remembered together with the location it wrote to. A store inside | ||
| * a loop can write to a different location in each iteration, as `mstore(p, 1)` does in a loop body like | ||
| * `let p := sload(0) mstore(p, 1) sstore(0, add(p, 32))`. Loop bodies are walked twice, so such a store is | ||
| * remembered twice, once with the location from each walk. The store is only removed if neither of these | ||
| * locations is read later. Each walk starts by forgetting the values of the variables assigned in the loop, | ||
| * because a store might be read any number of iterations later. Without that, the second walk would treat | ||
| * a location from the first walk as if it was always read in the very next iteration. |
There was a problem hiding this comment.
I found this very hard to parse. What do you think about this?
| * To decide whether two operations can touch the same location, the step keeps track of the values of variables | |
| * while it walks the code. Each location is recorded as a constant offset from the value a variable had at that | |
| * point in the code. For example, in `let q := add(p, 32) mstore(q, 1)` the location of the store is recorded | |
| * as 32 more than the value `p` had when the store was executed. When a variable is reassigned, it is treated | |
| * as a new variable, so a location recorded earlier keeps referring to the old value. This is what allows a store | |
| * to be compared correctly with reads and writes that happen later, after some of the variables have changed. | |
| * Where control flow joins, the values of variables assigned inside a branch or loop are forgotten. | |
| * | |
| * A store whose result might still be read is remembered together with the location it wrote to. A store inside | |
| * a loop can write to a different location in each iteration, as `mstore(p, 1)` does in a loop body like | |
| * `let p := sload(0) mstore(p, 1) sstore(0, add(p, 32))`. Loop bodies are walked twice, so such a store is | |
| * remembered twice, once with the location from each walk. The store is only removed if neither of these | |
| * locations is read later. Each walk starts by forgetting the values of the variables assigned in the loop, | |
| * because a store might be read any number of iterations later. Without that, the second walk would treat | |
| * a location from the first walk as if it was always read in the very next iteration. | |
| * Locations are regoraded as constant offsets from a variable, e.g., `add(p, 32)` as `p + 32`. | |
| * Every assignment or declaration gives the variable a new version and a recorded location keeps the version it was recorded with. | |
| * Therefore, reassigning `p` later does not move it. | |
| * Two locations are only known to be equal or different if they are offsets from the same version of a variable. | |
| * After a branch or a loop, the variables assigned inside it get a new version. | |
| * | |
| * Loop bodies are walked twice. | |
| * A store in a loop is recorded once per walk, each time with the location from that walk, and is kept if either location may be read later. | |
| * Each walk starts by giving the variables assigned in the loop a new version. | |
| * Therefore, a location that depends on them cannot be compared with one from the other walk. | |
| * This matters because a value stored in one iteration may be read in any later iteration, not only in the next. |
| { | ||
| std::size_t operator()(VersionedVariable const& _variable) const | ||
| { | ||
| std::size_t hash = static_cast<std::size_t>(_variable.name.hash()); |
There was a problem hiding this comment.
is this actually sane? emscripten targets 32 bits.
There was a problem hiding this comment.
Moved to the separated PR. Comment also referenced there. https://github.com/argotorg/solidity/pull/17349/changes#r4754254444
| bool operator==(VariableOffset const&) const = default; | ||
| bool operator<(VariableOffset const& _other) const | ||
| { | ||
| return std::tie(reference, offset) < std::tie(_other.reference, _other.offset); | ||
| } |
There was a problem hiding this comment.
| bool operator==(VariableOffset const&) const = default; | |
| bool operator<(VariableOffset const& _other) const | |
| { | |
| return std::tie(reference, offset) < std::tie(_other.reference, _other.offset); | |
| } | |
| std::strong_ordering operator<=>(VariableOffset const&) const = default; |
| bool operator<(UnusedStoreEliminatorActiveStore const& _other) const | ||
| { | ||
| return | ||
| std::tie(statement, operation.location, operation.effect, operation.start, operation.length) < | ||
| std::tie( | ||
| _other.statement, | ||
| _other.operation.location, | ||
| _other.operation.effect, | ||
| _other.operation.start, | ||
| _other.operation.length | ||
| ); | ||
| } |
There was a problem hiding this comment.
this should use a defaulted spaceship operator
| if ( | ||
| _value && | ||
| _variables.size() == 1 && | ||
| !VariableReferencesCounter::countReferences(*_value).count(_variables.front()) |
cf6c185 to
fb3eeb1
Compare
|
Addressed the comments. Regarding the general comment I lean towards some general solution and don’t wanna leave other improvements for later because we know that it will probably never happen. Let’s wait for Matrin comments. I have to analyse your proposal deeper but if I get it right it will have this property. |
|
I don't think that is the right approach for a bugfix pr. |
|
It addressed concerns from the previous version. #16459 (comment) |
That was my opinion back then. Feel free to push back against my opinion :) |
…nd loop iterations The knowledge base evaluates the values of SSA variables lazily. A value that references a reassigned variable was evaluated with the value that variable has at the point of use, not the one it had at the declaration. So copies of the same variable taken at different points were considered equal. Such values are now treated as unknown. Variables declared inside a loop have the same name but a different value in each iteration. A store that is still active when the next iteration starts and whose location refers to such a variable cannot be compared with the operations of the following iterations, so it is now marked as used. See #17274. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
812cb1f to
5ed22e3
Compare
Description
UnusedStoreEliminator recorded store locations as variable names and assumed their values never change. This does not hold for variables reassigned in a branch, nor for stores in a loop whose location differs between iterations. A later read of the original location then went unnoticed and the store was removed.
Store locations are now recorded as KnowledgeBase offsets relative to the value a variable had when the store was executed, so they stay valid after variables are reassigned. Each execution of a store keeps its own location, so the two walks of a loop body are tracked separately. The step no longer relies on SSAValueTracker.
It’s a draft because I haven’t added the bug to the bug list and updated the change log.
The first commit is the most important. The rest are tests and optimisations. It’s better to start reviewing from the first commit to get the idea of the change.
AI Disclosure
Claude Opus 5.5 assisted in preparing this PR.