Merge pull request #11817 from ethereum/extendKnowledgeBase

Extend knowledge base
This commit is contained in:
chriseth 2021-08-19 14:05:46 +02:00 committed by GitHub
commit 065a303b76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 231 additions and 21 deletions

View File

@ -681,6 +681,22 @@ std::vector<SimplificationRule<Pattern>> simplificationRuleListPart8(
// X - (A + Y) -> (X - Y) + (-A)
Builtins::SUB(X, Builtins::ADD(A, Y)),
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), 0 - A.d()); }
}, {
// (X - A) - Y -> (X - Y) - A
Builtins::SUB(Builtins::SUB(X, A), Y),
[=]() -> Pattern { return Builtins::SUB(Builtins::SUB(X, Y), A); }
}, {
// (A - X) - Y -> A - (X + Y)
Builtins::SUB(Builtins::SUB(A, X), Y),
[=]() -> Pattern { return Builtins::SUB(A, Builtins::ADD(X, Y)); }
}, {
// X - (Y - A) -> (X - Y) + A
Builtins::SUB(X, Builtins::SUB(Y, A)),
[=]() -> Pattern { return Builtins::ADD(Builtins::SUB(X, Y), A.d()); }
}, {
// X - (A - Y) -> (X + Y) + (-A)
Builtins::SUB(X, Builtins::SUB(A, Y)),
[=]() -> Pattern { return Builtins::ADD(Builtins::ADD(X, Y), 0 - A.d()); }
}
};
return rules;

View File

@ -40,9 +40,8 @@ bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
// current values to turn `sub(_a, _b)` into a nonzero constant.
// If that fails, try `eq(_a, _b)`.
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (holds_alternative<Literal>(expr1))
return valueOfLiteral(std::get<Literal>(expr1)) != 0;
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
return difference != 0;
Expression expr2 = simplify(FunctionCall{{}, {{}, "eq"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (holds_alternative<Literal>(expr2))
@ -51,39 +50,59 @@ bool KnowledgeBase::knownToBeDifferent(YulString _a, YulString _b)
return false;
}
optional<u256> KnowledgeBase::differenceIfKnownConstant(YulString _a, YulString _b)
{
// Try to use the simplification rules together with the
// current values to turn `sub(_a, _b)` into a constant.
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (Literal const* value = get_if<Literal>(&expr1))
return valueOfLiteral(*value);
return {};
}
bool KnowledgeBase::knownToBeDifferentByAtLeast32(YulString _a, YulString _b)
{
// Try to use the simplification rules together with the
// current values to turn `sub(_a, _b)` into a constant whose absolute value is at least 32.
Expression expr1 = simplify(FunctionCall{{}, {{}, "sub"_yulstring}, util::make_vector<Expression>(Identifier{{}, _a}, Identifier{{}, _b})});
if (holds_alternative<Literal>(expr1))
{
u256 val = valueOfLiteral(std::get<Literal>(expr1));
return val >= 32 && val <= u256(0) - 32;
}
if (optional<u256> difference = differenceIfKnownConstant(_a, _b))
return difference >= 32 && difference <= u256(0) - 32;
return false;
}
bool KnowledgeBase::knownToBeZero(YulString _a)
{
return valueIfKnownConstant(_a) == u256{};
}
optional<u256> KnowledgeBase::valueIfKnownConstant(YulString _a)
{
if (m_variableValues.count(_a))
if (Literal const* literal = get_if<Literal>(m_variableValues.at(_a).value))
return valueOfLiteral(*literal);
return {};
}
Expression KnowledgeBase::simplify(Expression _expression)
{
bool startedRecursion = (m_recursionCounter == 0);
ScopeGuard{[&] { if (startedRecursion) m_recursionCounter = 0; }};
m_counter = 0;
return simplifyRecursively(move(_expression));
}
if (startedRecursion)
m_recursionCounter = 100;
else if (m_recursionCounter == 1)
Expression KnowledgeBase::simplifyRecursively(Expression _expression)
{
if (m_counter++ > 100)
return _expression;
else
--m_recursionCounter;
if (holds_alternative<FunctionCall>(_expression))
for (Expression& arg: std::get<FunctionCall>(_expression).arguments)
arg = simplify(arg);
arg = simplifyRecursively(arg);
if (auto match = SimplificationRules::findFirstMatch(_expression, m_dialect, m_variableValues))
return simplify(match->action().toExpression(debugDataOf(_expression)));
return simplifyRecursively(match->action().toExpression(debugDataOf(_expression)));
return _expression;
}

View File

@ -24,6 +24,8 @@
#include <libyul/ASTForward.h>
#include <libyul/YulString.h>
#include <libsolutil/Common.h>
#include <map>
namespace solidity::yul
@ -46,15 +48,19 @@ public:
{}
bool knownToBeDifferent(YulString _a, YulString _b);
std::optional<u256> differenceIfKnownConstant(YulString _a, YulString _b);
bool knownToBeDifferentByAtLeast32(YulString _a, YulString _b);
bool knownToBeEqual(YulString _a, YulString _b) const { return _a == _b; }
bool knownToBeZero(YulString _a);
std::optional<u256> valueIfKnownConstant(YulString _a);
private:
Expression simplify(Expression _expression);
Expression simplifyRecursively(Expression _expression);
Dialect const& m_dialect;
std::map<YulString, AssignedValue> const& m_variableValues;
size_t m_recursionCounter = 0;
size_t m_counter = 0;
};
}

View File

@ -136,6 +136,7 @@ set(libyul_sources
libyul/FunctionSideEffects.cpp
libyul/FunctionSideEffects.h
libyul/Inliner.cpp
libyul/KnowledgeBaseTest.cpp
libyul/Metrics.cpp
libyul/ObjectCompilerTest.cpp
libyul/ObjectCompilerTest.h

36
test/formal/sub_sub.py Normal file
View File

@ -0,0 +1,36 @@
from rule import Rule
from opcodes import *
"""
Rules:
SUB(SUB(X, A), Y) -> SUB(SUB(X, Y), A)
SUB(SUB(A, X), Y) -> SUB(A, ADD(X, Y))
SUB(X, SUB(Y, A)) -> ADD(SUB(X, Y), A)
SUB(X, SUB(A, Y)) -> ADD(ADD(X, Y), -A)
"""
rule = Rule()
n_bits = 256
# Input vars
X = BitVec('X', n_bits)
Y = BitVec('Y', n_bits)
A = BitVec('A', n_bits)
rule.check(
SUB(SUB(X, A), Y),
SUB(SUB(X, Y), A)
)
rule.check(
SUB(SUB(A, X), Y),
SUB(A, ADD(X, Y))
)
rule.check(
SUB(X, SUB(Y, A)),
ADD(SUB(X, Y), A)
)
rule.check(
SUB(X, SUB(A, Y)),
ADD(ADD(X, Y), SUB(0, A))
)

View File

@ -37,11 +37,11 @@ contract C {
// compileViaYul: also
// ----
// f() -> 0x40, 0x80, 6, 0x6162636465660000000000000000000000000000000000000000000000000000, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000
// gas irOptimized: 179963
// gas irOptimized: 179947
// gas legacy: 180694
// gas legacyOptimized: 180088
// g() -> 0x40, 0xc0, 0x49, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738393031323334353637383930313233343536373839303120, 0x3132333435363738390000000000000000000000000000000000000000000000, 0x11, 0x3132333435363738393233343536373839000000000000000000000000000000
// gas irOptimized: 107332
// gas irOptimized: 107322
// gas legacy: 107895
// gas legacyOptimized: 107254
// h() -> 0x40, 0x60, 0x00, 0x00

View File

@ -0,0 +1,132 @@
/*
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/>.
*/
/**
* Unit tests for KnowledgeBase
*/
#include <test/Common.h>
#include <test/libyul/Common.h>
#include <libyul/Object.h>
#include <libyul/optimiser/KnowledgeBase.h>
#include <libyul/optimiser/SSAValueTracker.h>
#include <libyul/optimiser/DataFlowAnalyzer.h>
#include <libyul/optimiser/NameDispenser.h>
#include <libyul/optimiser/CommonSubexpressionEliminator.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <liblangutil/ErrorReporter.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace solidity::langutil;
namespace solidity::yul::test
{
class KnowledgeBaseTest
{
protected:
KnowledgeBase constructKnowledgeBase(string const& _source)
{
ErrorList errorList;
shared_ptr<AsmAnalysisInfo> analysisInfo;
std::tie(m_object, analysisInfo) = yul::test::parse(_source, m_dialect, errorList);
BOOST_REQUIRE(m_object && errorList.empty() && m_object->code);
NameDispenser dispenser(m_dialect, *m_object->code);
std::set<YulString> reserved;
OptimiserStepContext context{m_dialect, dispenser, reserved, 0};
CommonSubexpressionEliminator::run(context, *m_object->code);
m_ssaValues(*m_object->code);
for (auto const& [name, expression]: m_ssaValues.values())
m_values[name].value = expression;
return KnowledgeBase(m_dialect, m_values);
}
EVMDialect m_dialect{EVMVersion{}, true};
shared_ptr<Object> m_object;
SSAValueTracker m_ssaValues;
map<YulString, AssignedValue> m_values;
};
BOOST_FIXTURE_TEST_SUITE(KnowledgeBase, KnowledgeBaseTest)
BOOST_AUTO_TEST_CASE(basic)
{
yul::KnowledgeBase kb = constructKnowledgeBase(R"({
let a := calldataload(0)
let b := calldataload(0)
let zero := 0
let c := add(b, 0)
let d := mul(b, 0)
let e := sub(a, b)
})");
BOOST_CHECK(!kb.knownToBeDifferent("a"_yulstring, "b"_yulstring));
// This only works if the variable names are the same.
// It assumes that SSA+CSE+Simplifier actually replaces the variables.
BOOST_CHECK(!kb.knownToBeEqual("a"_yulstring, "b"_yulstring));
BOOST_CHECK(!kb.valueIfKnownConstant("a"_yulstring));
BOOST_CHECK(kb.valueIfKnownConstant("zero"_yulstring) == u256(0));
}
BOOST_AUTO_TEST_CASE(difference)
{
yul::KnowledgeBase kb = constructKnowledgeBase(R"({
let a := calldataload(0)
let b := add(a, 200)
let c := add(a, 220)
let d := add(c, 12)
let e := sub(c, 12)
})");
BOOST_CHECK(
kb.differenceIfKnownConstant("c"_yulstring, "b"_yulstring) ==
u256(20)
);
BOOST_CHECK(
kb.differenceIfKnownConstant("b"_yulstring, "c"_yulstring) ==
u256(-20)
);
BOOST_CHECK(!kb.knownToBeDifferentByAtLeast32("b"_yulstring, "c"_yulstring));
BOOST_CHECK(kb.knownToBeDifferentByAtLeast32("b"_yulstring, "d"_yulstring));
BOOST_CHECK(kb.knownToBeDifferentByAtLeast32("a"_yulstring, "b"_yulstring));
BOOST_CHECK(kb.knownToBeDifferentByAtLeast32("b"_yulstring, "a"_yulstring));
BOOST_CHECK(
kb.differenceIfKnownConstant("e"_yulstring, "a"_yulstring) == u256(208)
);
BOOST_CHECK(
kb.differenceIfKnownConstant("e"_yulstring, "b"_yulstring) == u256(8)
);
BOOST_CHECK(
kb.differenceIfKnownConstant("a"_yulstring, "e"_yulstring) == u256(-208)
);
BOOST_CHECK(
kb.differenceIfKnownConstant("b"_yulstring, "e"_yulstring) == u256(-8)
);
}
BOOST_AUTO_TEST_SUITE_END()
}