From 0a9d752b8b9bd033a0a302ffe68b0c7e516cafe7 Mon Sep 17 00:00:00 2001 From: Daniel Kirchner Date: Mon, 7 Mar 2022 13:06:58 +0100 Subject: [PATCH] Add test case and adjust tests. --- .../viair_subobject_optimization/args | 1 + .../viair_subobject_optimization/input.sol | 20 + .../viair_subobject_optimization/output | 379 ++++++++++++++++++ test/cmdlineTests/yul_optimize_runs/input.yul | 6 +- test/cmdlineTests/yul_optimize_runs/output | 25 +- .../semanticTests/array/reusing_memory.sol | 2 +- .../constructor/arrays_in_constructors.sol | 2 +- .../bytes_in_constructors_packer.sol | 2 +- .../creation_function_call_with_args.sol | 2 +- .../creation_function_call_with_salt.sol | 2 +- .../inheritance/value_for_constructor.sol | 2 +- .../salted_create_with_value.sol | 2 +- 12 files changed, 423 insertions(+), 22 deletions(-) create mode 100644 test/cmdlineTests/viair_subobject_optimization/args create mode 100644 test/cmdlineTests/viair_subobject_optimization/input.sol create mode 100644 test/cmdlineTests/viair_subobject_optimization/output diff --git a/test/cmdlineTests/viair_subobject_optimization/args b/test/cmdlineTests/viair_subobject_optimization/args new file mode 100644 index 000000000..51e6fa519 --- /dev/null +++ b/test/cmdlineTests/viair_subobject_optimization/args @@ -0,0 +1 @@ +--experimental-via-ir --optimize --asm \ No newline at end of file diff --git a/test/cmdlineTests/viair_subobject_optimization/input.sol b/test/cmdlineTests/viair_subobject_optimization/input.sol new file mode 100644 index 000000000..74549134a --- /dev/null +++ b/test/cmdlineTests/viair_subobject_optimization/input.sol @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: GPL-3.0 +pragma solidity >0.0.0; + +contract C { + constructor(uint x) { + // In earlier versions of the compiler, the resulting assembly pushed the constant + // 0xFFFFFFFFFFFFFFFF42 directly in the subassembly of D, while it was optimized to + // ``sub(shl(0x48, 0x01), 0xbe)`` when C was compiled in isolation. + // Now the assembly is expected to contain two instances of ``sub(shl(0x48, 0x01), 0xbe)``, + // one in the creation code of ``C`` directly, one in a subassembly of ``D``. + // The constant 0xFFFFFFFFFFFFFFFF42 should not occur in the assembly output at all. + if (x == 0xFFFFFFFFFFFFFFFF42) + revert(); + } +} +contract D { + function f() public pure returns (bytes memory) { + return type(C).creationCode; + } +} diff --git a/test/cmdlineTests/viair_subobject_optimization/output b/test/cmdlineTests/viair_subobject_optimization/output new file mode 100644 index 000000000..8b5666b37 --- /dev/null +++ b/test/cmdlineTests/viair_subobject_optimization/output @@ -0,0 +1,379 @@ + +======= viair_subobject_optimization/input.sol:C ======= +EVM assembly: + /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ + 0x80 + dup1 + 0x40 + mstore + jumpi(tag_6, callvalue) + 0x1f + bytecodeSize + codesize + dup2 + swap1 + sub + swap2 + dup3 + add + not(0x1f) + and + dup4 + add + swap2 + sub(shl(0x40, 0x01), 0x01) + dup4 + gt + dup5 + dup5 + lt + or + tag_4 + jumpi + dup1 + dup5 + swap3 + 0x20 + swap5 + 0x40 + mstore + dup4 + codecopy + dup2 + add + sub + slt + tag_6 + jumpi + tag_8 + swap1 + mload + tag_1 + jump // in +tag_8: + mload(0x40) + dataSize(sub_0) + swap1 + dup2 + dataOffset(sub_0) + dup3 + codecopy + return +tag_6: + pop + 0x00 + dup1 + revert +tag_4: + pop + pop + pop + pop + mstore(0x00, shl(0xe0, 0x4e487b71)) + mstore(0x04, 0x41) + revert(0x00, 0x24) + /* "viair_subobject_optimization/input.sol":76:666 constructor(uint x) {... */ +tag_1: + sub(shl(0x48, 0x01), 0xbe) + /* "viair_subobject_optimization/input.sol":620:645 x == 0xFFFFFFFFFFFFFFFF42 */ + eq + /* "viair_subobject_optimization/input.sol":616:661 if (x == 0xFFFFFFFFFFFFFFFF42)... */ + tag_6 + jumpi + /* "viair_subobject_optimization/input.sol":76:666 constructor(uint x) {... */ + jump // out +stop + +sub_0: assembly { + /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ + mstore(0x40, 0x80) + 0x00 + dup1 + revert + + auxdata: +} + + +======= viair_subobject_optimization/input.sol:D ======= +EVM assembly: + /* "viair_subobject_optimization/input.sol":669:772 contract D {... */ + 0x80 + dup1 + 0x40 + mstore + jumpi(tag_1, callvalue) + dataSize(sub_0) + swap1 + dup2 + dataOffset(sub_0) + dup3 + codecopy + return +tag_1: + pop + 0x00 + dup1 + revert +stop + +sub_0: assembly { + /* "viair_subobject_optimization/input.sol":669:772 contract D {... */ + 0x80 + dup1 + 0x40 + mstore + jumpi(tag_2, iszero(lt(calldatasize, 0x04))) + tag_3: + pop + 0x00 + dup1 + revert + tag_2: + 0x00 + swap1 + dup2 + calldataload + 0xe0 + shr + 0x26121ff0 + eq + tag_4 + jumpi + pop + jump(tag_3) + tag_4: + jumpi(tag_8, callvalue) + dup2 + add(calldatasize, not(0x03)) + slt + tag_8 + jumpi + /* "viair_subobject_optimization/input.sol":745:765 type(C).creationCode */ + dataSize(sub_0) + /* "viair_subobject_optimization/input.sol":669:772 contract D {... */ + 0x3f + dup2 + add + not(0x1f) + and + dup3 + add + 0xffffffffffffffff + dup2 + gt + dup4 + dup3 + lt + or + tag_10 + jumpi + tag_12 + swap4 + pop + 0x40 + mstore + /* "viair_subobject_optimization/input.sol":745:765 type(C).creationCode */ + dup1 + dup3 + mstore + dataOffset(sub_0) + 0x20 + dup4 + add + codecopy + /* "viair_subobject_optimization/input.sol":669:772 contract D {... */ + mload(0x40) + swap2 + dup3 + swap2 + dup3 + tag_1 + jump // in + tag_12: + sub + swap1 + return + tag_10: + pop + pop + shl(0xe0, 0x4e487b71) + dup3 + mstore + pop + mstore(0x04, 0x41) + 0x24 + swap1 + revert + tag_8: + pop + dup1 + revert + tag_1: + swap2 + swap1 + swap2 + 0x20 + dup1 + dup3 + mstore + dup4 + mload + swap1 + dup2 + dup2 + dup5 + add + mstore + 0x00 + swap5 + tag_13: + dup3 + dup7 + lt + tag_14 + jumpi + pop + pop + dup1 + 0x40 + swap4 + swap5 + gt + tag_16 + jumpi + tag_17: + 0x1f + add + not(0x1f) + and + add + add + swap1 + jump // out + tag_16: + 0x00 + dup4 + dup3 + dup5 + add + add + mstore + jump(tag_17) + tag_14: + dup6 + dup2 + add + dup3 + add + mload + dup5 + dup8 + add + 0x40 + add + mstore + swap5 + dup2 + add + swap5 + jump(tag_13) + stop + + sub_0: assembly { + /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ + 0x80 + dup1 + 0x40 + mstore + jumpi(tag_6, callvalue) + 0x1f + bytecodeSize + codesize + dup2 + swap1 + sub + swap2 + dup3 + add + not(0x1f) + and + dup4 + add + swap2 + sub(shl(0x40, 0x01), 0x01) + dup4 + gt + dup5 + dup5 + lt + or + tag_4 + jumpi + dup1 + dup5 + swap3 + 0x20 + swap5 + 0x40 + mstore + dup4 + codecopy + dup2 + add + sub + slt + tag_6 + jumpi + tag_8 + swap1 + mload + tag_1 + jump // in + tag_8: + mload(0x40) + dataSize(sub_0) + swap1 + dup2 + dataOffset(sub_0) + dup3 + codecopy + return + tag_6: + pop + 0x00 + dup1 + revert + tag_4: + pop + pop + pop + pop + mstore(0x00, shl(0xe0, 0x4e487b71)) + mstore(0x04, 0x41) + revert(0x00, 0x24) + /* "viair_subobject_optimization/input.sol":76:666 constructor(uint x) {... */ + tag_1: + sub(shl(0x48, 0x01), 0xbe) + /* "viair_subobject_optimization/input.sol":620:645 x == 0xFFFFFFFFFFFFFFFF42 */ + eq + /* "viair_subobject_optimization/input.sol":616:661 if (x == 0xFFFFFFFFFFFFFFFF42)... */ + tag_6 + jumpi + /* "viair_subobject_optimization/input.sol":76:666 constructor(uint x) {... */ + jump // out + stop + + sub_0: assembly { + /* "viair_subobject_optimization/input.sol":61:668 contract C {... */ + mstore(0x40, 0x80) + 0x00 + dup1 + revert + + auxdata: + } + } + + auxdata: +} diff --git a/test/cmdlineTests/yul_optimize_runs/input.yul b/test/cmdlineTests/yul_optimize_runs/input.yul index 07c602a4a..6088853e6 100644 --- a/test/cmdlineTests/yul_optimize_runs/input.yul +++ b/test/cmdlineTests/yul_optimize_runs/input.yul @@ -1,10 +1,10 @@ object "RunsTest1" { code { // Deploy the contract - datacopy(0, dataoffset("Runtime"), datasize("Runtime")) - return(0, datasize("Runtime")) + datacopy(0, dataoffset("Runtime_deployed"), datasize("Runtime_deployed")) + return(0, datasize("Runtime_deployed")) } - object "Runtime" { + object "Runtime_deployed" { code { let funcSel := shl(224, 0xabc12345) sstore(0, funcSel) diff --git a/test/cmdlineTests/yul_optimize_runs/output b/test/cmdlineTests/yul_optimize_runs/output index 357c796ee..6f4ba8390 100644 --- a/test/cmdlineTests/yul_optimize_runs/output +++ b/test/cmdlineTests/yul_optimize_runs/output @@ -5,12 +5,12 @@ Pretty printed source: object "RunsTest1" { code { { - let _1 := datasize("Runtime") - datacopy(0, dataoffset("Runtime"), _1) + let _1 := datasize("Runtime_deployed") + datacopy(0, dataoffset("Runtime_deployed"), _1) return(0, _1) } } - object "Runtime" { + object "Runtime_deployed" { code { { sstore(0, 0xabc1234500000000000000000000000000000000000000000000000000000000) @@ -21,30 +21,31 @@ object "RunsTest1" { Binary representation: -600c80600c6000396000f3fe63abc1234560e01b60005500 +602580600c6000396000f3fe7fabc123450000000000000000000000000000000000000000000000000000000060005500 Text representation: - /* "yul_optimize_runs/input.yul":106:125 */ + /* "yul_optimize_runs/input.yul":115:143 */ dataSize(sub_0) - /* "yul_optimize_runs/input.yul":83:104 */ + /* "yul_optimize_runs/input.yul":83:113 */ dup1 dataOffset(sub_0) /* "yul_optimize_runs/input.yul":80:81 */ 0x00 - /* "yul_optimize_runs/input.yul":71:126 */ + /* "yul_optimize_runs/input.yul":71:144 */ codecopy /* "yul_optimize_runs/input.yul":80:81 */ 0x00 - /* "yul_optimize_runs/input.yul":135:165 */ + /* "yul_optimize_runs/input.yul":153:192 */ return stop sub_0: assembly { - shl(0xe0, 0xabc12345) - /* "yul_optimize_runs/input.yul":277:278 */ + /* "yul_optimize_runs/input.yul":273:293 */ + 0xabc1234500000000000000000000000000000000000000000000000000000000 + /* "yul_optimize_runs/input.yul":313:314 */ 0x00 - /* "yul_optimize_runs/input.yul":270:288 */ + /* "yul_optimize_runs/input.yul":306:324 */ sstore - /* "yul_optimize_runs/input.yul":208:298 */ + /* "yul_optimize_runs/input.yul":244:334 */ stop } diff --git a/test/libsolidity/semanticTests/array/reusing_memory.sol b/test/libsolidity/semanticTests/array/reusing_memory.sol index ba0ca8abd..7a05b5328 100644 --- a/test/libsolidity/semanticTests/array/reusing_memory.sol +++ b/test/libsolidity/semanticTests/array/reusing_memory.sol @@ -26,6 +26,6 @@ contract Main { // compileViaYul: also // ---- // f(uint256): 0x34 -> 0x46bddb1178e94d7f2892ff5f366840eb658911794f2c3a44c450aa2c505186c1 -// gas irOptimized: 113610 +// gas irOptimized: 113613 // gas legacy: 126596 // gas legacyOptimized: 113823 diff --git a/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol index 21de2ac97..031deb64c 100644 --- a/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol +++ b/test/libsolidity/semanticTests/constructor/arrays_in_constructors.sol @@ -26,6 +26,6 @@ contract Creator { // compileViaYul: also // ---- // f(uint256,address[]): 7, 0x40, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 -> 7, 8 -// gas irOptimized: 444029 +// gas irOptimized: 443989 // gas legacy: 590683 // gas legacyOptimized: 448326 diff --git a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol index d5e633924..8264a93ed 100644 --- a/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol +++ b/test/libsolidity/semanticTests/constructor/bytes_in_constructors_packer.sol @@ -26,6 +26,6 @@ contract Creator { // compileViaYul: also // ---- // f(uint256,bytes): 7, 0x40, 78, "abcdefghijklmnopqrstuvwxyzabcdef", "ghijklmnopqrstuvwxyzabcdefghijkl", "mnopqrstuvwxyz" -> 7, "h" -// gas irOptimized: 300885 +// gas irOptimized: 300837 // gas legacy: 428917 // gas legacyOptimized: 298128 diff --git a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol index c4a65bb55..0b9407814 100644 --- a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol +++ b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_args.sol @@ -17,7 +17,7 @@ contract D { // compileViaYul: also // ---- // constructor(): 2 -> -// gas irOptimized: 203963 +// gas irOptimized: 203982 // gas legacy: 245842 // gas legacyOptimized: 195676 // f() -> 2 diff --git a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol index 7ed638418..a4796ac82 100644 --- a/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol +++ b/test/libsolidity/semanticTests/functionCall/creation_function_call_with_salt.sol @@ -18,7 +18,7 @@ contract D { // compileViaYul: also // ---- // constructor(): 2 -> -// gas irOptimized: 204126 +// gas irOptimized: 204145 // gas legacy: 246202 // gas legacyOptimized: 195914 // f() -> 2 diff --git a/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol b/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol index 462de46a7..aea318e65 100644 --- a/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol +++ b/test/libsolidity/semanticTests/inheritance/value_for_constructor.sol @@ -42,7 +42,7 @@ contract Main { // compileViaYul: also // ---- // constructor(), 22 wei -> -// gas irOptimized: 284283 +// gas irOptimized: 284321 // gas legacy: 402045 // gas legacyOptimized: 266772 // getFlag() -> true diff --git a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol index eb9d93ee2..780cc32b8 100644 --- a/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol +++ b/test/libsolidity/semanticTests/salted_create/salted_create_with_value.sol @@ -22,6 +22,6 @@ contract A { // compileViaYul: also // ---- // f(), 10 ether -> 3007, 3008, 3009 -// gas irOptimized: 272449 +// gas irOptimized: 272467 // gas legacy: 422501 // gas legacyOptimized: 287472