1

According to documentation, one of the steps of solidity compilation is the optimization on opcodes level, with the following set of rules.
How can I apply optimization directly on opcodes?

Details
Let's say I compiled a contract with the --opcodes option and opcodes optimization disabled:

solc --opcodes --evm-version shanghai example_contract.sol

Resulting in opcodes, like this:

PUSH1 0x80 PUSH1 0x40 MSTORE CALLVALUE DUP1 ISZERO PUSH2 0xF JUMPI PUSH0 DUP1 REVERT JUMPDEST POP PUSH1 0x40 ...

Now I want to optimize it on the opcodes level for 200 runs, to get result, same as output of:

solc --opcodes --evm-version shanghai --optimize --optimizate-runs 200 example_contract.sol

The goal is smart contracts opcodes dataset augmentation with different optimization parameters, for non-verified smart contracts, so I don't have access to the source code, and can't just recompile from the source code.

2
  • Could you rephrase your question? I'm not sure i fully understand it Commented Nov 24, 2023 at 15:58
  • @PatrickCollins Roughly compilation works like this: solidity -> YUL -> optimize_yul -> opcodes -> optimize_opcodes -> bytecode. I have opcodes, and I want to execute the part: opcodes -> optimize_opcodes Without having any of Solidity or Yul :) Commented Nov 25, 2023 at 13:27

1 Answer 1

1
+50

It is an interesting topic, but please consider the optimizations solc can apply on existing opcodes without the original Solidity or YUL code are probably limited.

It's easier to find something about EVM opcode optimization like EBSO instead of leveraging the optimization features of a compiler that can work with much more data available.

Syrup is another very interesting project that proposes an innovative approach to smart contract optimization starting from their bytecode and based on the "Synthesis of Super-Optimized Smart Contracts Using Max-SMT" research paper.

Going back to Solidity, as you pointed out, the compiler applies some optimization there, and you can probably apply the same rules on your opcodes using the libevmasm code in your C++ program.

Still, I'm not sure that library can be actually used without the Solidity or YUL source code or at least some previous disassembly operation from opcode to mnemonic (i.e., with a quick check, I see there, for example, some processes referring to "PUSH" strings).

I don't see how you can do it via the solc CLI at the moment, but in solc 0.8.22, I noticed the introduction of the --import-asm-json parameter that seems a step in that direction.

That option is still incompatible with the optimization option, but it seems there's already a feature request for that on GitHub, so the goal is to have what you are searching for in solc in the future.

$ solc --asm-json -o . Ballot.sol
Compiler run successful. Artifact(s) can be found in directory ".".
$ solc --import-asm-json Ballot_evm.json --optimize
Error: Option --optimize is not supported with --import-asm-json.
1
  • Thanks! While it's not directly answering my question, so I can't map it as an answer - it gives some useful hints, so I'll upvote and provide bounty for this answer. Commented Nov 25, 2023 at 13:24

Not the answer you're looking for? Browse other questions tagged or ask your own question.