Skip to content

[WIP] Inline farcall #669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 850 commits into
base: main
Choose a base branch
from
Draft

[WIP] Inline farcall #669

wants to merge 850 commits into from

Conversation

akiramenai
Copy link
Collaborator

No description provided.

vladimirradosavljevic and others added 30 commits April 8, 2024 00:06
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This patch is fixing bug in block placement pass that
can wrongly reorder fall through BBs when the new jump
table instruction is the last instruction in a BB.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This fixes the crash where inner loop is not a simplified form and
we are traversing over the basic blocks of the outer loop and
attempt to rewrite the inner GEP instruction using the preheader
and latch basic blocks of the outer loop. Additionally, this code
adds a paranoia check to ensure that the PHI node of the BasePtr
has the preheader and latch basic blocks as incoming blocks.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Since GR256 and GRPTR have the same physical registers,
IsFatPtr MI flag needs to be propagated for COPY
instruction after the register allocation phase,
so COPY instruction can be lowered correctly to a
real instruction. This patch implements PHI node
elimination TII hooks, so we can tag fatptr copies
when needed.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This issue occurs when DAGCombiner in visitXOR incorrectly
folds !(x cc y) to (x !cc y) because isConstTrueVal returns
true for -1 constant. Fix this issue by setting that true
and false are represented with 1 and 0 values.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Update codegen pipeline:
* enable more opt passes (also add an option to make possible disabling
  the passes since they hardens testing basic functionality);
* disable passes that do nothing for EraVM;
* test the pipeline.
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
By the time of ifcvt, we deal with LOADCONST pseudo that
isn't predicated.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This will allow IfConversion to optimize more cases,
since it will work on real instructions that can be
predicated.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
In build without asserts, some tests are trapped in the infinite
loop in the Legalizer. Skip running these tests.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
The patch adds a pass that merges two identical basic blocks. The logic
is borrowed from https://reviews.llvm.org/D82730, but unlike the
original PR, it's extracted to a separate pass which is scheduled after
simplify CFG.

The patch also contain a fix by Vladimir Radosavljevic that removes
unreachable blocks before merging.
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This optimization is not profitable for us, since we have
urem instruction.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
`EraVMLinkRuntime` and `EVMLinkRuntime` don't invalidate analyses when
update a module. The patch invalidates all analyses if there were
changes.
In some cases, IfConverter can pass max 32-bit unsigned value
for TrueCycles and 0 for FalseCycles. Problem in this case is
that Denominator will be 0, and BranchProbability will assert.
Add check to avoid assertion in BranchProbability constructor.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
PavelKopyl and others added 6 commits July 23, 2024 11:38
HACK: It seems llvm-config is not supposed to work with lld
by design. At the same time, we need lld support in llvm-config
because the 'inkwell' package gathers the linking dependencies
by llvm-config.
@akiramenai akiramenai marked this pull request as draft August 1, 2024 10:10
Copy link

github-actions bot commented Aug 1, 2024

Benchmark results default:

╔═╡ Size (-%) ╞════════════════╡ All M3B3 ╞═╗
║ Mean                                1.565 ║
║ Best                               30.769 ║
║ Worst                             -25.882 ║
║ Total                               1.413 ║
╠═╡ Cycles (-%) ╞══════════════╡ All M3B3 ╞═╣
║ Mean                                0.633 ║
║ Best                               31.250 ║
║ Worst                             -23.904 ║
║ Total                               1.243 ║
╠═╡ Ergs (-%) ╞════════════════╡ All M3B3 ╞═╣
║ Mean                                0.632 ║
║ Best                               15.301 ║
║ Worst                             -107.650 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════════════╡ All MzB3 ╞═╗
║ Mean                                1.565 ║
║ Best                               29.703 ║
║ Worst                             -20.513 ║
║ Total                               2.592 ║
╠═╡ Cycles (-%) ╞══════════════╡ All MzB3 ╞═╣
║ Mean                                0.633 ║
║ Best                               17.028 ║
║ Worst                             -28.386 ║
║ Total                               1.226 ║
╠═╡ Ergs (-%) ╞════════════════╡ All MzB3 ╞═╣
║ Mean                                0.633 ║
║ Best                               11.777 ║
║ Worst                             -102.656 ║
║ Total                               0.000 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter M3B3 ╞═╣
║ Mean                               -3.574 ║
║ Best                                9.059 ║
║ Worst                             -23.904 ║
║ Total                             -13.516 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter M3B3 ╞═╣
║ Mean                               -1.466 ║
║ Best                                2.055 ║
║ Worst                              -9.073 ║
║ Total                              -1.777 ║
╠═╡ Ergs/gas ╞══════╡ EVMInterpreter M3B3 ╞═╣
║ ADD                                52.750 ║
║ MUL                                31.650 ║
║ SUB                                52.750 ║
║ DIV                                32.850 ║
║ SDIV                               48.450 ║
║ MOD                                32.850 ║
║ SMOD                               46.050 ║
║ ADDMOD                             26.656 ║
║ MULMOD                             28.156 ║
║ EXP                                 7.737 ║
║ SIGNEXTEND                         30.450 ║
║ LT                                 56.750 ║
║ GT                                 56.750 ║
║ SLT                                80.750 ║
║ SGT                                78.750 ║
║ EQ                                 54.750 ║
║ ISZERO                             50.417 ║
║ AND                                52.750 ║
║ OR                                 52.750 ║
║ XOR                                52.750 ║
║ NOT                                46.417 ║
║ BYTE                               60.750 ║
║ SHL                                56.750 ║
║ SHR                                58.750 ║
║ SAR                                74.750 ║
║ SGT                                78.750 ║
║ SHA3                               26.852 ║
║ ADDRESS                            71.812 ║
║ BALANCE                            70.940 ║
║ ORIGIN                           1322.375 ║
║ CALLER                             71.812 ║
║ CALLVALUE                          71.812 ║
║ CALLDATALOAD                       48.750 ║
║ CALLDATASIZE                       72.125 ║
║ CALLDATACOPY                       80.015 ║
║ CODESIZE                           69.625 ║
║ CODECOPY                          138.303 ║
║ GASPRICE                         1325.188 ║
║ EXTCODESIZE                         4.999 ║
║ EXTCODECOPY                         5.046 ║
║ RETURNDATASIZE                     70.500 ║
║ RETURNDATACOPY                     49.889 ║
║ EXTCODEHASH                         6.031 ║
║ BLOCKHASH                         238.319 ║
║ COINBASE                         1325.375 ║
║ TIMESTAMP                        1322.375 ║
║ NUMBER                           1322.375 ║
║ PREVRANDAO                       1322.375 ║
║ GASLIMIT                         1328.375 ║
║ CHAINID                          1322.375 ║
║ SELFBALANCE                       621.875 ║
║ BASEFEE                          1322.375 ║
║ POP                                56.625 ║
║ MLOAD                              73.533 ║
║ MSTORE                             71.705 ║
║ MSTORE8                            83.539 ║
║ SLOAD                              25.892 ║
║ SSTORE                              8.803 ║
║ JUMP                               26.444 ║
║ JUMPI                              24.455 ║
║ PC                                 72.312 ║
║ MSIZE                              78.812 ║
║ GAS                                66.312 ║
║ JUMPDEST                           89.625 ║
║ PUSH0                              63.312 ║
║ PUSH1                              53.958 ║
║ PUSH2                              57.375 ║
║ PUSH4                              60.208 ║
║ PUSH5                              61.625 ║
║ PUSH6                              63.042 ║
║ PUSH7                              64.458 ║
║ PUSH8                              65.875 ║
║ PUSH9                              67.292 ║
║ PUSH10                             68.708 ║
║ PUSH11                             70.125 ║
║ PUSH12                             71.542 ║
║ PUSH13                             72.958 ║
║ PUSH14                             74.375 ║
║ PUSH15                             75.792 ║
║ PUSH16                             77.208 ║
║ PUSH17                             78.625 ║
║ PUSH18                             80.042 ║
║ PUSH19                             81.458 ║
║ PUSH20                             82.875 ║
║ PUSH21                             84.292 ║
║ PUSH22                             85.708 ║
║ PUSH23                             87.125 ║
║ PUSH24                             88.542 ║
║ PUSH25                             89.958 ║
║ PUSH26                             91.375 ║
║ PUSH27                             92.792 ║
║ PUSH28                             94.208 ║
║ PUSH29                             95.625 ║
║ PUSH30                             97.042 ║
║ PUSH31                             98.458 ║
║ PUSH32                             97.875 ║
║ DUP1                               46.417 ║
║ DUP2                               50.417 ║
║ DUP3                               48.417 ║
║ DUP4                               50.417 ║
║ DUP5                               50.417 ║
║ DUP6                               50.417 ║
║ DUP7                               50.417 ║
║ DUP8                               50.417 ║
║ DUP9                               50.417 ║
║ DUP10                              50.417 ║
║ DUP11                              50.417 ║
║ DUP12                              50.417 ║
║ DUP13                              50.417 ║
║ DUP14                              50.417 ║
║ DUP15                              50.417 ║
║ DUP16                              50.417 ║
║ SWAP1                              57.083 ║
║ SWAP2                              57.083 ║
║ SWAP3                              57.083 ║
║ SWAP4                              57.083 ║
║ SWAP5                              55.083 ║
║ SWAP6                              57.083 ║
║ SWAP7                              57.083 ║
║ SWAP8                              57.083 ║
║ SWAP9                              57.083 ║
║ SWAP10                             57.083 ║
║ SWAP11                             57.083 ║
║ SWAP12                             57.083 ║
║ SWAP13                             57.083 ║
║ SWAP14                             57.083 ║
║ SWAP15                             57.083 ║
║ SWAP16                             57.083 ║
║ CALL                               56.757 ║
║ STATICCALL                         56.751 ║
║ DELEGATECALL                       55.791 ║
║ CREATE                              5.007 ║
║ CREATE2                             7.015 ║
║ RETURN                              1.000 ║
║ REVERT                              1.000 ║
╠═╡ Ergs/gas (-%) ╞═╡ EVMInterpreter M3B3 ╞═╣
║ ADD                               -23.392 ║
║ MUL                               -23.392 ║
║ SUB                               -23.392 ║
║ DIV                                -7.882 ║
║ SDIV                               -5.212 ║
║ MOD                                -7.882 ║
║ SMOD                               -5.498 ║
║ ADDMOD                             -9.219 ║
║ MULMOD                             -8.685 ║
║ EXP                                -4.034 ║
║ SIGNEXTEND                         -8.556 ║
║ LT                                -21.390 ║
║ GT                                -21.390 ║
║ SLT                               -14.134 ║
║ SGT                               -14.545 ║
║ EQ                                -17.112 ║
║ ISZERO                            -13.508 ║
║ AND                               -23.392 ║
║ OR                                -23.392 ║
║ XOR                               -23.392 ║
║ NOT                               -20.824 ║
║ BYTE                              -10.959 ║
║ SHL                               -21.390 ║
║ SHR                               -20.513 ║
║ SAR                                -8.727 ║
║ SGT                               -14.545 ║
║ SHA3                                4.926 ║
║ ADDRESS                           -33.449 ║
║ BALANCE                             3.263 ║
║ ORIGIN                              2.010 ║
║ CALLER                            -33.449 ║
║ CALLVALUE                         -33.449 ║
║ CALLDATALOAD                      -19.632 ║
║ CALLDATASIZE                      -33.256 ║
║ CALLDATACOPY                       -9.426 ║
║ CODESIZE                          -27.460 ║
║ CODECOPY                           -3.691 ║
║ GASPRICE                            2.006 ║
║ EXTCODESIZE                         2.520 ║
║ EXTCODECOPY                         2.581 ║
║ RETURNDATASIZE                    -34.286 ║
║ RETURNDATACOPY                    -10.319 ║
║ EXTCODEHASH                         2.826 ║
║ BLOCKHASH                           1.120 ║
║ COINBASE                            2.006 ║
║ TIMESTAMP                           2.010 ║
║ NUMBER                              2.010 ║
║ PREVRANDAO                          2.010 ║
║ GASLIMIT                            2.001 ║
║ CHAINID                             2.010 ║
║ SELFBALANCE                         3.154 ║
║ BASEFEE                             2.010 ║
║ POP                               -26.891 ║
║ MLOAD                             -14.199 ║
║ MSTORE                            -14.614 ║
║ MSTORE8                           -12.697 ║
║ SLOAD                               3.521 ║
║ SSTORE                              2.818 ║
║ JUMP                              -14.423 ║
║ JUMPI                             -12.552 ║
║ PC                                -33.142 ║
║ MSIZE                             -29.599 ║
║ GAS                               -29.233 ║
║ JUMPDEST                          -15.459 ║
║ PUSH0                             -23.386 ║
║ PUSH1                             -17.407 ║
║ PUSH2                             -16.203 ║
║ PUSH4                             -15.323 ║
║ PUSH5                             -14.918 ║
║ PUSH6                             -14.534 ║
║ PUSH7                             -14.170 ║
║ PUSH8                             -13.823 ║
║ PUSH9                             -13.493 ║
║ PUSH10                            -13.178 ║
║ PUSH11                            -12.877 ║
║ PUSH12                            -12.590 ║
║ PUSH13                            -12.316 ║
║ PUSH14                            -12.053 ║
║ PUSH15                            -11.801 ║
║ PUSH16                            -11.559 ║
║ PUSH17                            -11.327 ║
║ PUSH18                            -11.105 ║
║ PUSH19                            -10.891 ║
║ PUSH20                            -10.684 ║
║ PUSH21                            -10.486 ║
║ PUSH22                            -10.295 ║
║ PUSH23                            -10.111 ║
║ PUSH24                             -9.933 ║
║ PUSH25                             -9.761 ║
║ PUSH26                             -9.595 ║
║ PUSH27                             -9.435 ║
║ PUSH28                             -9.280 ║
║ PUSH29                             -9.130 ║
║ PUSH30                             -8.985 ║
║ PUSH31                             -8.844 ║
║ PUSH32                             -8.901 ║
║ DUP1                              -27.460 ║
║ DUP2                              -13.508 ║
║ DUP3                              -14.145 ║
║ DUP4                              -13.508 ║
║ DUP5                              -13.508 ║
║ DUP6                              -13.508 ║
║ DUP7                              -13.508 ║
║ DUP8                              -13.508 ║
║ DUP9                              -13.508 ║
║ DUP10                             -13.508 ║
║ DUP11                             -13.508 ║
║ DUP12                             -13.508 ║
║ DUP13                             -13.508 ║
║ DUP14                             -13.508 ║
║ DUP15                             -13.508 ║
║ DUP16                             -13.508 ║
║ SWAP1                             -21.239 ║
║ SWAP2                             -21.239 ║
║ SWAP3                             -21.239 ║
║ SWAP4                             -21.239 ║
║ SWAP5                             -22.181 ║
║ SWAP6                             -21.239 ║
║ SWAP7                             -21.239 ║
║ SWAP8                             -21.239 ║
║ SWAP9                             -21.239 ║
║ SWAP10                            -21.239 ║
║ SWAP11                            -21.239 ║
║ SWAP12                            -21.239 ║
║ SWAP13                            -21.239 ║
║ SWAP14                            -21.239 ║
║ SWAP15                            -21.239 ║
║ SWAP16                            -21.239 ║
║ CALL                               -1.030 ║
║ STATICCALL                         -1.038 ║
║ DELEGATECALL                       -1.063 ║
║ CREATE                              0.528 ║
║ CREATE2                             0.553 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞═════╡ EVMInterpreter MzB3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                                 NaN ║
╠═╡ Cycles (-%) ╞═══╡ EVMInterpreter MzB3 ╞═╣
║ Mean                               -3.574 ║
║ Best                                9.059 ║
║ Worst                             -23.904 ║
║ Total                             -13.516 ║
╠═╡ Ergs (-%) ╞═════╡ EVMInterpreter MzB3 ╞═╣
║ Mean                               -1.466 ║
║ Best                                2.055 ║
║ Worst                              -9.073 ║
║ Total                              -1.777 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════╡ Precompiles M3B3 ╞═╗
║ Mean                               -3.425 ║
║ Best                                0.576 ║
║ Worst                             -14.815 ║
║ Total                              -0.133 ║
╠═╡ Cycles (-%) ╞══════╡ Precompiles M3B3 ╞═╣
║ Mean                                3.521 ║
║ Best                                6.494 ║
║ Worst                              -2.151 ║
║ Total                               0.046 ║
╠═╡ Ergs (-%) ╞════════╡ Precompiles M3B3 ╞═╣
║ Mean                                0.768 ║
║ Best                                3.433 ║
║ Worst                              -1.993 ║
║ Total                               0.001 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞════════╡ Precompiles MzB3 ╞═╗
║ Mean                                0.000 ║
║ Best                                0.000 ║
║ Worst                               0.000 ║
║ Total                               0.000 ║
╠═╡ Cycles (-%) ╞══════╡ Precompiles MzB3 ╞═╣
║ Mean                                3.641 ║
║ Best                                6.467 ║
║ Worst                               0.000 ║
║ Total                               0.045 ║
╠═╡ Ergs (-%) ╞════════╡ Precompiles MzB3 ╞═╣
║ Mean                                0.902 ║
║ Best                                3.306 ║
║ Worst                              -0.000 ║
║ Total                               0.001 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞══════════╡ Real life M3B3 ╞═╗
║ Mean                                2.413 ║
║ Best                               10.256 ║
║ Worst                              -8.115 ║
║ Total                               0.977 ║
╠═╡ Cycles (-%) ╞════════╡ Real life M3B3 ╞═╣
║ Mean                                6.418 ║
║ Best                               10.277 ║
║ Worst                              -2.740 ║
║ Total                               2.955 ║
╠═╡ Ergs (-%) ╞══════════╡ Real life M3B3 ╞═╣
║ Mean                                1.197 ║
║ Best                                5.648 ║
║ Worst                              -0.251 ║
║ Total                               1.180 ║
╚═══════════════════════════════════════════╝

╔═╡ Size (-%) ╞══════════╡ Real life MzB3 ╞═╗
║ Mean                                1.436 ║
║ Best                                4.082 ║
║ Worst                               0.000 ║
║ Total                               1.270 ║
╠═╡ Cycles (-%) ╞════════╡ Real life MzB3 ╞═╣
║ Mean                                6.192 ║
║ Best                               11.345 ║
║ Worst                              -0.947 ║
║ Total                               2.239 ║
╠═╡ Ergs (-%) ╞══════════╡ Real life MzB3 ╞═╣
║ Mean                                1.185 ║
║ Best                                5.009 ║
║ Worst                              -0.736 ║
║ Total                               1.044 ║
╚═══════════════════════════════════════════╝

@akiramenai akiramenai force-pushed the dborisenkov/inline-farcall branch from 7e6a5de to 6c6b83e Compare August 2, 2024 13:53
PavelKopyl and others added 17 commits August 5, 2024 10:41
This section is not used, so disable generation of it.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This is a workaround for suspicious crashes/hanging ups happening
with lld-as-a-library.
TODO: ##677, needs to figure out exact reason.
This also includes a C-API function to check the bytecode limits.
We don't need TargetPassConfig and TargetTransformInfo
analyses and initialize pass dependencies.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
Refactor code to check whether BasePtr is increased
by one cell per iteration.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
…psPrepare

In case we generate GEP that doesn't dominate latch BB,
we will run into an issue and verifier will complain:
```
Instruction does not dominate all uses!
  %3 = getelementptr inbounds i256, ptr addrspace(1) %0, i256 1
  %0 = phi ptr addrspace(1) [ %arg3, %bb1 ], [ %3, %bb5 ]
```
This patch fixes this issue.

Signed-off-by: Vladimir Radosavljevic <vr@matterlabs.dev>
This includes:

  1. Increasing the stack pointer according to the number of global variables.
    nop    stack+=[NumCells + r0]

  2. Duplication of intializers in the code section and copying them to
     corresponding stack items.
    add    @glob_initializer[0], r0, stack[@glob]
    ...

  3. Generation of default landing pads.
    DEFAULT_UNWIND:
        ret.panic.to_label @DEFAULT_UNWIND
    DEFAULT_FAR_RETURN:
        ret.ok.to_label r1,    @DEFAULT_FAR_RETURN
    DEFAULT_FAR_REVERT:
        ret.revert.to_label r1,    @DEFAULT_FAR_REVERT
1. code[@symbol + reg + imm]
   Denotes indirect code reference. Loads a word from the code page
   at the offset (measured in words): "@symbol + reg + imm".

2. @symbol + imm
   Just denotes the expression value itself. @symbol is resolved
   as an offset to the label measured in instructions.
add @.OUTLINED_FUNCTION_RET2[0], r0, stack[-1]
@akiramenai akiramenai force-pushed the dborisenkov/inline-farcall branch from 6c6b83e to 15b2767 Compare August 17, 2024 13:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.