Reverse lookup for DataFlowAnalyzer

This commit is contained in:
Nikola Matic 2023-04-12 15:40:41 +02:00
parent 8c7404f639
commit a8cc9bde9b
6 changed files with 135 additions and 8 deletions

View File

@ -5,6 +5,7 @@ Language Features:
Compiler Features:
* EWasm: Remove EWasm backend.
* Optimizer: Introduced an optimized variable assignment map to improve the performance of Yul optimizer steps that rely on data flow analysis.
* Parser: Introduce ``pragma experimental solidity``, which will enable an experimental language mode that in particular has no stability guarantees between non-breaking releases and is not suited for production use.

View File

@ -189,6 +189,8 @@ add_library(yul
optimiser/UnusedPruner.h
optimiser/VarDeclInitializer.cpp
optimiser/VarDeclInitializer.h
optimiser/VariableAssignmentMap.cpp
optimiser/VariableAssignmentMap.h
optimiser/VarNameCleaner.cpp
optimiser/VarNameCleaner.h
)

View File

@ -271,7 +271,7 @@ void DataFlowAnalyzer::handleAssignment(set<YulString> const& _variables, Expres
auto const& referencedVariables = movableChecker.referencedVariables();
for (auto const& name: _variables)
{
m_state.references[name] = referencedVariables;
m_state.references.set(name, referencedVariables);
if (!_isDeclaration)
{
// assignment to slot denoted by "name"
@ -353,9 +353,8 @@ void DataFlowAnalyzer::clearValues(set<YulString> _variables)
// Also clear variables that reference variables to be cleared.
for (auto const& variableToClear: _variables)
for (auto const& [ref, names]: m_state.references)
if (names.count(variableToClear))
_variables.emplace(ref);
if (auto&& references = m_state.references.getReversedOrNullptr(variableToClear))
_variables += *references;
// Clear the value and update the reference relation.
for (auto const& name: _variables)

View File

@ -25,6 +25,7 @@
#include <libyul/optimiser/ASTWalker.h>
#include <libyul/optimiser/KnowledgeBase.h>
#include <libyul/optimiser/VariableAssignmentMap.h>
#include <libyul/YulString.h>
#include <libyul/AST.h> // Needed for m_zero below.
#include <libyul/SideEffects.h>
@ -104,7 +105,7 @@ public:
/// @returns the current value of the given variable, if known - always movable.
AssignedValue const* variableValue(YulString _variable) const { return util::valueOrNullptr(m_state.value, _variable); }
std::set<YulString> const* references(YulString _variable) const { return util::valueOrNullptr(m_state.references, _variable); }
std::set<YulString> const* references(YulString _variable) const { return m_state.references.getOrderedOrNullptr(_variable); }
std::map<YulString, AssignedValue> const& allValues() const { return m_state.value; }
std::optional<YulString> storageValue(YulString _key) const;
std::optional<YulString> memoryValue(YulString _key) const;
@ -179,9 +180,9 @@ private:
{
/// Current values of variables, always movable.
std::map<YulString, AssignedValue> value;
/// m_references[a].contains(b) <=> the current expression assigned to a references b
std::unordered_map<YulString, std::set<YulString>> references;
/// references.m_ordered[a].contains(b) <=> the current expression assigned to a references b
/// references.m_reversed[b].contains(a) <=> b from current expression assigned to a is references by a
VariableAssignmentMap references;
Environment environment;
};

View File

@ -0,0 +1,39 @@
#include <libyul/optimiser/VariableAssignmentMap.h>
using std::set;
using namespace solidity::yul;
void VariableAssignmentMap::set(YulString const& _variable, std::set<YulString> const& _references)
{
erase(_variable);
m_ordered[_variable] = _references;
for (auto&& reference: _references)
m_reversed[reference].emplace(_variable);
}
void VariableAssignmentMap::erase(YulString const& _variable)
{
for (auto&& reference: m_ordered[_variable])
if (m_reversed.find(reference) != m_reversed.end())
{
if (m_reversed[reference].size() > 1)
m_reversed[reference].erase(_variable);
else
// Only fully remove an entry if no variables other than _variable
// are contained in the set pointed to by reference.
m_reversed.erase(reference);
}
m_ordered.erase(_variable);
}
set<YulString> const* VariableAssignmentMap::getOrderedOrNullptr(YulString const& _variable) const
{
auto&& it = m_ordered.find(_variable);
return (it != m_ordered.end()) ? &it->second : nullptr;
}
set<YulString> const* VariableAssignmentMap::getReversedOrNullptr(YulString const& _variable) const
{
auto&& it = m_reversed.find(_variable);
return (it != m_reversed.end()) ? &it->second : nullptr;
}

View File

@ -0,0 +1,85 @@
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <libyul/YulString.h>
#include <set>
#include <unordered_map>
namespace solidity::yul
{
/**
* Class that implements a reverse lookup for an ``unordered_map<YulString, set<YulString>>`` by wrapping the
* two such maps - one ordered, and one reversed, e.g.
*
* m_ordered m_reversed
* f -> (g,) g -> (f,)
* c -> (b,d,e,) d -> (c,)
* a -> (b,c,) c -> (a,)
* e -> (c,)
* b -> (a,c,)
*
* The above example will from here onwards be referenced as ```Ref 1```.
*
* This allows us to have simultaneously managed insertion and deletion via a single interface, instead of manually
* managing this at the point of usage (see ``DataFlowAnalyzer``).
*/
class VariableAssignmentMap
{
public:
VariableAssignmentMap() = default;
/**
* Insert a set of values for the provided key ``_variable`` into ``m_ordered`` and ``m_reversed``.
* This method will erase all references of ``_variable`` from both sets before performing the insertion,
* akin to container assignment with subscript operator, i.e. container[index] = value.
* For example, if ``_variable`` is ``x`` and ``_references`` is ``{"y", "z"}``, the following would be added to ``Ref 1``:
*
* m_ordered m_reversed
* x -> (y, z,) y -> (x,)
* y -> (z,)
*
* @param _variable current expression variable
* @param _references all referenced variables in the expression assigned to ``_variable``
*/
void set(YulString const& _variable, std::set<YulString> const& _references);
/**
* Erase entries in both maps based on provided ``_variable``. The behaviour is the same as ``set("x", {})``.
* For example, after deleting ``c`` for ``Ref 1``, ``Ref 1`` would contain the following:
*
* m_ordered m_reversed
* f -> (g,) g -> (f,)
* a -> (b,c,) c -> (a,)
* b -> (a,)
*
* @param _variable variable to erase
*/
void erase(YulString const& _variable);
std::set<YulString> const* getOrderedOrNullptr(YulString const& _variable) const;
std::set<YulString> const* getReversedOrNullptr(YulString const& _variable) const;
private:
/// m_ordered[a].contains[b] <=> the current expression assigned to ``a`` references ``b``
std::unordered_map<YulString, std::set<YulString>> m_ordered;
/// m_reversed[b].contains[a] <=> the current expression assigned to ``a`` references ``b``
std::unordered_map<YulString, std::set<YulString>> m_reversed;
};
} // solidity::yul