mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Fix SSA for multi-assignments.
This commit is contained in:
parent
8c570f31c5
commit
887112b1b0
@ -1,5 +1,9 @@
|
|||||||
### 0.5.8 (unreleased)
|
### 0.5.8 (unreleased)
|
||||||
|
|
||||||
|
Important Bugfixes:
|
||||||
|
* Yul Optimizer: Fix SSA transform for multi-assignments.
|
||||||
|
|
||||||
|
|
||||||
Language Features:
|
Language Features:
|
||||||
* Code Generation: Implement copying recursive structs from storage to memory.
|
* Code Generation: Implement copying recursive structs from storage to memory.
|
||||||
|
|
||||||
|
@ -62,17 +62,13 @@ void SSATransform::operator()(Block& _block)
|
|||||||
{
|
{
|
||||||
set<YulString> variablesToClearAtEnd;
|
set<YulString> variablesToClearAtEnd;
|
||||||
|
|
||||||
// Creates a new variable (and returns its declaration) with value _value
|
// Creates a new variable and stores it in the current variable value map.
|
||||||
// and replaces _value by a reference to that new variable.
|
auto newVariable = [&](YulString _varName) -> YulString
|
||||||
|
|
||||||
auto replaceByNew = [&](SourceLocation _loc, YulString _varName, YulString _type, unique_ptr<Expression>& _value) -> VariableDeclaration
|
|
||||||
{
|
{
|
||||||
YulString newName = m_nameDispenser.newName(_varName);
|
YulString newName = m_nameDispenser.newName(_varName);
|
||||||
m_currentVariableValues[_varName] = newName;
|
m_currentVariableValues[_varName] = newName;
|
||||||
variablesToClearAtEnd.emplace(_varName);
|
variablesToClearAtEnd.emplace(_varName);
|
||||||
unique_ptr<Expression> v = make_unique<Expression>(Identifier{_loc, newName});
|
return newName;
|
||||||
_value.swap(v);
|
|
||||||
return VariableDeclaration{_loc, {TypedName{_loc, std::move(newName), std::move(_type)}}, std::move(v)};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
iterateReplacing(
|
iterateReplacing(
|
||||||
@ -84,36 +80,60 @@ void SSATransform::operator()(Block& _block)
|
|||||||
VariableDeclaration& varDecl = boost::get<VariableDeclaration>(_s);
|
VariableDeclaration& varDecl = boost::get<VariableDeclaration>(_s);
|
||||||
if (varDecl.value)
|
if (varDecl.value)
|
||||||
visit(*varDecl.value);
|
visit(*varDecl.value);
|
||||||
if (varDecl.variables.size() != 1 || !m_variablesToReplace.count(varDecl.variables.front().name))
|
|
||||||
|
bool needToReplaceSome = false;
|
||||||
|
for (auto const& var: varDecl.variables)
|
||||||
|
if (m_variablesToReplace.count(var.name))
|
||||||
|
needToReplaceSome = true;
|
||||||
|
if (!needToReplaceSome)
|
||||||
return {};
|
return {};
|
||||||
vector<Statement> v;
|
|
||||||
// Replace "let a := v" by "let a_1 := v let a := a_1"
|
// Replace "let a := v" by "let a_1 := v let a := a_1"
|
||||||
v.emplace_back(replaceByNew(
|
// Replace "let a, b := v" by "let a_1, b_1 := v let a := a_1 let b := b_2"
|
||||||
varDecl.location,
|
auto loc = varDecl.location;
|
||||||
varDecl.variables.front().name,
|
vector<Statement> statements;
|
||||||
varDecl.variables.front().type,
|
statements.emplace_back(VariableDeclaration{loc, {}, std::move(varDecl.value)});
|
||||||
varDecl.value
|
TypedNameList newVariables;
|
||||||
));
|
for (auto const& var: varDecl.variables)
|
||||||
v.emplace_back(move(varDecl));
|
{
|
||||||
return std::move(v);
|
YulString newName = newVariable(var.name);
|
||||||
|
YulString oldName = var.name;
|
||||||
|
newVariables.emplace_back(TypedName{loc, newName, {}});
|
||||||
|
statements.emplace_back(VariableDeclaration{
|
||||||
|
loc,
|
||||||
|
{TypedName{loc, oldName, {}}},
|
||||||
|
make_unique<Expression>(Identifier{loc, newName})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
boost::get<VariableDeclaration>(statements.front()).variables = std::move(newVariables);
|
||||||
|
return std::move(statements);
|
||||||
}
|
}
|
||||||
else if (_s.type() == typeid(Assignment))
|
else if (_s.type() == typeid(Assignment))
|
||||||
{
|
{
|
||||||
Assignment& assignment = boost::get<Assignment>(_s);
|
Assignment& assignment = boost::get<Assignment>(_s);
|
||||||
visit(*assignment.value);
|
visit(*assignment.value);
|
||||||
if (assignment.variableNames.size() != 1)
|
for (auto const& var: assignment.variableNames)
|
||||||
return {};
|
assertThrow(m_variablesToReplace.count(var.name), OptimizerException, "");
|
||||||
assertThrow(m_variablesToReplace.count(assignment.variableNames.front().name), OptimizerException, "");
|
|
||||||
vector<Statement> v;
|
|
||||||
// Replace "a := v" by "let a_1 := v a := v"
|
// Replace "a := v" by "let a_1 := v a := v"
|
||||||
v.emplace_back(replaceByNew(
|
// Replace "a, b := v" by "let a_1, b_1 := v a := a_1 b := b_2"
|
||||||
assignment.location,
|
auto loc = assignment.location;
|
||||||
assignment.variableNames.front().name,
|
vector<Statement> statements;
|
||||||
{}, // TODO determine type
|
statements.emplace_back(VariableDeclaration{loc, {}, std::move(assignment.value)});
|
||||||
assignment.value
|
TypedNameList newVariables;
|
||||||
));
|
for (auto const& var: assignment.variableNames)
|
||||||
v.emplace_back(move(assignment));
|
{
|
||||||
return std::move(v);
|
YulString newName = newVariable(var.name);
|
||||||
|
YulString oldName = var.name;
|
||||||
|
newVariables.emplace_back(TypedName{loc, newName, {}});
|
||||||
|
statements.emplace_back(Assignment{
|
||||||
|
loc,
|
||||||
|
{Identifier{loc, oldName}},
|
||||||
|
make_unique<Expression>(Identifier{loc, newName})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
boost::get<VariableDeclaration>(statements.front()).variables = std::move(newVariables);
|
||||||
|
return std::move(statements);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
visit(_s);
|
visit(_s);
|
||||||
|
@ -87,6 +87,8 @@ private:
|
|||||||
{ }
|
{ }
|
||||||
|
|
||||||
NameDispenser& m_nameDispenser;
|
NameDispenser& m_nameDispenser;
|
||||||
|
/// This is a set of all variables that are assigned to anywhere in the code.
|
||||||
|
/// Variables that are only declared but never re-assigned are not touched.
|
||||||
std::set<YulString> const& m_variablesToReplace;
|
std::set<YulString> const& m_variablesToReplace;
|
||||||
std::map<YulString, YulString> m_currentVariableValues;
|
std::map<YulString, YulString> m_currentVariableValues;
|
||||||
};
|
};
|
||||||
|
@ -36,13 +36,13 @@
|
|||||||
// ----
|
// ----
|
||||||
// {
|
// {
|
||||||
// let a, b := abi_decode_t_bytes_calldata_ptr(mload(0), mload(1))
|
// let a, b := abi_decode_t_bytes_calldata_ptr(mload(0), mload(1))
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_1, b_1 := abi_decode_t_bytes_calldata_ptr(a, b)
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_2, b_2 := abi_decode_t_bytes_calldata_ptr(a_1, b_1)
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_3, b_3 := abi_decode_t_bytes_calldata_ptr(a_2, b_2)
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_4, b_4 := abi_decode_t_bytes_calldata_ptr(a_3, b_3)
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_5, b_5 := abi_decode_t_bytes_calldata_ptr(a_4, b_4)
|
||||||
// a, b := abi_decode_t_bytes_calldata_ptr(a, b)
|
// let a_6, b_6 := abi_decode_t_bytes_calldata_ptr(a_5, b_5)
|
||||||
// mstore(a, b)
|
// mstore(a_6, b_6)
|
||||||
// function abi_decode_t_bytes_calldata_ptr(offset, end) -> arrayPos, length
|
// function abi_decode_t_bytes_calldata_ptr(offset, end) -> arrayPos, length
|
||||||
// {
|
// {
|
||||||
// if iszero(slt(add(offset, 0x1f), end))
|
// if iszero(slt(add(offset, 0x1f), end))
|
||||||
|
29
test/libyul/yulOptimizerTests/ssaTransform/multi_assign.yul
Normal file
29
test/libyul/yulOptimizerTests/ssaTransform/multi_assign.yul
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
let a := mload(0)
|
||||||
|
let b := mload(1)
|
||||||
|
a, b := f()
|
||||||
|
sstore(a, b)
|
||||||
|
a := mload(5)
|
||||||
|
b := mload(a)
|
||||||
|
function f() -> x, y {}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// step: ssaTransform
|
||||||
|
// ----
|
||||||
|
// {
|
||||||
|
// let a_1 := mload(0)
|
||||||
|
// let a := a_1
|
||||||
|
// let b_2 := mload(1)
|
||||||
|
// let b := b_2
|
||||||
|
// let a_3, b_4 := f()
|
||||||
|
// a := a_3
|
||||||
|
// b := b_4
|
||||||
|
// sstore(a_3, b_4)
|
||||||
|
// let a_5 := mload(5)
|
||||||
|
// a := a_5
|
||||||
|
// let b_6 := mload(a_5)
|
||||||
|
// b := b_6
|
||||||
|
// function f() -> x, y
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
// }
|
25
test/libyul/yulOptimizerTests/ssaTransform/multi_decl.yul
Normal file
25
test/libyul/yulOptimizerTests/ssaTransform/multi_decl.yul
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
let x, y := f(1, 2)
|
||||||
|
x := mload(y)
|
||||||
|
y := mload(x)
|
||||||
|
let a, b := f(x, y)
|
||||||
|
sstore(a, b)
|
||||||
|
function f(t, v) -> x, y {}
|
||||||
|
}
|
||||||
|
// ====
|
||||||
|
// step: ssaTransform
|
||||||
|
// ----
|
||||||
|
// {
|
||||||
|
// let x_2, y_3 := f(1, 2)
|
||||||
|
// let x := x_2
|
||||||
|
// let y := y_3
|
||||||
|
// let x_4 := mload(y_3)
|
||||||
|
// x := x_4
|
||||||
|
// let y_5 := mload(x_4)
|
||||||
|
// y := y_5
|
||||||
|
// let a, b := f(x_4, y_5)
|
||||||
|
// sstore(a, b)
|
||||||
|
// function f(t, v) -> x_1, y_2
|
||||||
|
// {
|
||||||
|
// }
|
||||||
|
// }
|
Loading…
Reference in New Issue
Block a user