Skip to content

Commit 9db2da0

Browse files
authored
Merge pull request #13687 from ethereum/hashNumberLiterals
Hash number literals according to their value instead of their string representation.
2 parents 19fc395 + cffacac commit 9db2da0

File tree

8 files changed

+32
-11
lines changed

8 files changed

+32
-11
lines changed

Changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Compiler Features:
1313

1414

1515
Bugfixes:
16+
* Yul Optimizer: Hash hex and decimal literals according to their value instead of their representation, improving the detection of equivalent functions.
1617
* Solidity Upgrade Tool ``solidity-upgrade``: Fix the tool returning success code on uncaught exceptions.
1718

1819

libyul/optimiser/BlockHasher.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ std::map<Block const*, uint64_t> BlockHasher::run(Block const& _block)
5454
void BlockHasher::operator()(Literal const& _literal)
5555
{
5656
hash64(compileTimeLiteralHash("Literal"));
57-
hash64(_literal.value.hash());
57+
if (_literal.kind == LiteralKind::Number)
58+
hash64(std::hash<u256>{}(valueOfNumberLiteral(_literal)));
59+
else
60+
hash64(_literal.value.hash());
5861
hash64(_literal.type.hash());
5962
hash8(static_cast<uint8_t>(_literal.kind));
6063
}

test/libsolidity/semanticTests/array/copying/elements_of_nested_array_of_structs_memory_to_storage.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ contract C {
3333
// compileViaYul: true
3434
// ----
3535
// test1((uint8[],uint8[2])[][][]): 0x20, 1, 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -> 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29
36-
// gas irOptimized: 332334
36+
// gas irOptimized: 332586
3737
// test2((uint8[],uint8[2])[][1][]): 0x20, 2, 0x40, 0x0160, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13, 0x20, 1, 0x20, 0x60, 31, 37, 2, 23, 29 -> 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13
38-
// gas irOptimized: 145029
38+
// gas irOptimized: 145182
3939
// test3((uint8[],uint8[2])[1][][2]): 0x20, 0x40, 0x60, 0, 2, 0x40, 288, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 2, 0x40, 288, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13
40-
// gas irOptimized: 192344
40+
// gas irOptimized: 192512

test/libsolidity/semanticTests/array/copying/nested_array_of_structs_memory_to_storage.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ contract C {
3131
// compileViaYul: true
3232
// ----
3333
// test1((uint8[],uint8[2])[][]): 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29 -> 0x20, 2, 0x40, 0x0140, 1, 0x20, 0x60, 3, 7, 2, 1, 2, 2, 0x40, 0x0100, 0x60, 17, 19, 2, 11, 13, 0x60, 31, 37, 2, 23, 29
34-
// gas irOptimized: 308790
34+
// gas irOptimized: 309123
3535
// test2((uint8[],uint8[2])[][1]): 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 0x20, 1, 0x20, 0x60, 17, 19, 2, 11, 13
36-
// gas irOptimized: 118077
36+
// gas irOptimized: 118188
3737
// test3((uint8[],uint8[2])[1][]): 0x20, 2, 0x40, 0x0120, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13 -> 0x20, 2, 0x40, 0x0120, 0x20, 0x60, 3, 7, 2, 1, 2, 0x20, 0x60, 17, 19, 2, 11, 13
38-
// gas irOptimized: 190775
38+
// gas irOptimized: 190997

test/libsolidity/semanticTests/byte_array_to_storage_cleanup.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ contract C {
2828
// compileViaYul: also
2929
// ----
3030
// constructor() ->
31-
// gas irOptimized: 518935
31+
// gas irOptimized: 464753
3232
// gas legacy: 729908
3333
// gas legacyOptimized: 493347
3434
// h() -> 0x20, 0x40, 0x00, 0

test/libsolidity/semanticTests/externalContracts/snark.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,6 @@ contract Test {
299299
// gas legacyOptimized: 267239
300300
// verifyTx() -> true
301301
// ~ emit Verified(string): 0x20, 0x16, "Successfully verified."
302-
// gas irOptimized: 783949
302+
// gas irOptimized: 784027
303303
// gas legacy: 805423
304304
// gas legacyOptimized: 772571

test/libsolidity/semanticTests/userDefinedValueType/calldata.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ contract C {
4949
}
5050
// ----
5151
// test_f() -> true
52-
// gas irOptimized: 122053
52+
// gas irOptimized: 122510
5353
// gas legacy: 126150
5454
// gas legacyOptimized: 123163
5555
// test_g() -> true
56-
// gas irOptimized: 106138
56+
// gas irOptimized: 106903
5757
// gas legacy: 112481
5858
// gas legacyOptimized: 107706
5959
// addresses(uint256): 0 -> 0x18
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
f()
3+
g()
4+
function f() { mstore(0x01, mload(0x00)) }
5+
function g() { mstore(1, mload(0)) }
6+
}
7+
// ----
8+
// step: equivalentFunctionCombiner
9+
//
10+
// {
11+
// f()
12+
// f()
13+
// function f()
14+
// { mstore(0x01, mload(0x00)) }
15+
// function g()
16+
// { mstore(1, mload(0)) }
17+
// }

0 commit comments

Comments
 (0)