Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions libyul/optimiser/VarNameCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,16 @@ void VarNameCleaner::operator()(FunctionDefinition& _funDef)
m_usedNames = m_namesToKeep;
std::map<YulName, YulName> globalTranslatedNames;
swap(globalTranslatedNames, m_translatedNames);
std::map<YulName, size_t> globalNextSuffix;
swap(globalNextSuffix, m_nextSuffix);

renameVariables(_funDef.parameters);
renameVariables(_funDef.returnVariables);
ASTModifier::operator()(_funDef);

swap(globalUsedNames, m_usedNames);
swap(globalTranslatedNames, m_translatedNames);
swap(globalNextSuffix, m_nextSuffix);

m_insideFunction = false;
}
Expand Down Expand Up @@ -93,18 +96,25 @@ void VarNameCleaner::operator()(Identifier& _identifier)
_identifier.name = name->second;
}

YulName VarNameCleaner::findCleanName(YulName const& _name) const
YulName VarNameCleaner::findCleanName(YulName const& _name)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
YulName VarNameCleaner::findCleanName(YulName const& _name)
YulName VarNameCleaner::findCleanName(YulName const& _name) const

since the cache is now mutable, you can put back the const :)

{
auto newName = stripSuffix(_name);
if (!isUsedName(newName))
return newName;

// create new name with suffix (by finding a free identifier)
for (size_t i = 1; i < std::numeric_limits<decltype(i)>::max(); ++i)
// Use a per-base-name counter to avoid O(n²) probing when many
// variables share the same stripped base name.
size_t& nextSuffix = m_nextSuffix[newName];
if (nextSuffix == 0)
nextSuffix = 1;
for (; nextSuffix < std::numeric_limits<size_t>::max(); ++nextSuffix)
{
YulName newNameSuffixed = YulName{newName.str() + "_" + std::to_string(i)};
YulName newNameSuffixed = YulName{newName.str() + "_" + std::to_string(nextSuffix)};
if (!isUsedName(newNameSuffixed))
{
++nextSuffix;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if i interpret everything correctly, this is supposed to increment the suffix in the map, right? but I don't think it does, it's retrieved by value so the map value isnt updated, ever.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes. I'm sorry. It was written correctly and then broken. in 4b13837

Fixing now.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Haha some idiot probably suggested removing the reference. Wait a minute...

return newNameSuffixed;
}
}
yulAssert(false, "Exhausted by attempting to find an available suffix.");
}
Expand Down
5 changes: 4 additions & 1 deletion libyul/optimiser/VarNameCleaner.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class VarNameCleaner: public ASTModifier

/// Looks out for a "clean name" the given @p name could be trimmed down to.
/// @returns a trimmed down and "clean name" in case it found one, none otherwise.
YulName findCleanName(YulName const& name) const;
YulName findCleanName(YulName const& name);

/// Tests whether a given name was already used within this pass
/// or was set to be kept.
Expand All @@ -89,6 +89,9 @@ class VarNameCleaner: public ASTModifier
/// Set of names that are in use.
std::set<YulName> m_usedNames;

/// Next suffix to try per stripped base name, avoids O(n²) probing.
mutable std::map<YulName, size_t> m_nextSuffix;

/// Maps old to new names.
std::map<YulName, YulName> m_translatedNames;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
let x__1 := 1
let x__2 := 2
let x___3 := 3
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// }
// }
25 changes: 25 additions & 0 deletions test/libyul/yulOptimizerTests/varNameCleaner/interleaved_bases.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
let x_5 := 1
let y_3 := 2
let x_10 := 3
let y_7 := 4
let z_1 := 5
let x_20 := 6
let y_15 := 7
let z_8 := 8
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let y := 2
// let x_1 := 3
// let y_1 := 4
// let z := 5
// let x_2 := 6
// let y_2 := 7
// let z_1 := 8
// }
// }
25 changes: 25 additions & 0 deletions test/libyul/yulOptimizerTests/varNameCleaner/many_same_base.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
let x_10 := 1
let x_20 := 2
let x_30 := 3
let x_40 := 4
let x_50 := 5
let x_60 := 6
let x_70 := 7
let x_80 := 8
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// let x_3 := 4
// let x_4 := 5
// let x_5 := 6
// let x_6 := 7
// let x_7 := 8
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
let x_1 := 1
function f()
{
let x_10 := 1
let x_20 := 2
let x_30 := 3
let x_40 := 4
let x_50 := 5
}
let x_2 := 2
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// }
// function f()
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// let x_3 := 4
// let x_4 := 5
// }
// }
17 changes: 17 additions & 0 deletions test/libyul/yulOptimizerTests/varNameCleaner/nested_suffixes.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
let x_1_2 := 1
let x_3_4_5 := 2
let x_6 := 3
let x_1_2_3_4 := 4
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// let x_3 := 4
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
let x_5 := 42
let y_3 := x_5
let z_1 := add(x_5, y_3)
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 42
// let y := x
// let z := add(x, y)
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
let x := 1
let x_1 := 2
let x_5 := 3
let x_10 := 4
let x_20 := 5
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// let x_3 := 4
// let x_4 := 5
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// x_1 is a function name, so the suffix counter must skip it
// when cleaning x_100 and x_200 which both strip to "x"
function x_1() {}
let x := 1
let x_100 := 2
let x_200 := 3
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_2 := 2
// let x_3 := 3
// }
// function x_1()
// { }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
let x := 1
let x_2 := 2
let x_100 := 3
let x_200 := 4
let x_300 := 5
}
// ----
// step: varNameCleaner
//
// {
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// let x_3 := 4
// let x_4 := 5
// }
// }
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
function f()
{
let x_10 := 1
let x_20 := 2
let x_30 := 3
}
function g()
{
let x_40 := 4
let x_50 := 5
let x_60 := 6
}
}
// ----
// step: varNameCleaner
//
// {
// { }
// function f()
// {
// let x := 1
// let x_1 := 2
// let x_2 := 3
// }
// function g()
// {
// let x := 4
// let x_1 := 5
// let x_2 := 6
// }
// }