From 202b6f3add5d74fba2defd1da7f06c9192babb6f Mon Sep 17 00:00:00 2001 From: Marc Bickel Date: Fri, 2 May 2025 22:10:15 +0800 Subject: [PATCH] Add support for optimizer compiler args --- CONTRIBUTING.md | 3 +++ README.md | 16 ++++++++++++++++ ape_solidity/compiler.py | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 80aa768..201635d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -11,6 +11,9 @@ cd ape-solidity python3 -m venv venv source venv/bin/activate +#install setuptools +pip install setuptools + # install ape-solidity into the virtual environment python setup.py install diff --git a/README.md b/README.md index 7433c5d..2b6fc22 100644 --- a/README.md +++ b/README.md @@ -146,6 +146,22 @@ solidity: via_ir: True ``` +#### Yul Optimization + +You can enable `solc`'s `--yul` flag by adding the following values to your `ape-config.yaml` + +```yaml +solidity: + optimization_yul: True +``` + +You can also set the optimizer steps by adding the following values to your `ape-config.yaml`. You can find the list of optimizer steps [here](https://docs.soliditylang.org/en/v0.8.22/using-the-compiler.html#optimizer-steps). This requires the yul optimizer to be enabled. + +```yaml +solidity: + optimization_steps: "dhfoDgvulfnTUtnIf" +``` + ### Contract Flattening `ape-solidity` has contract-flattening capabilities. diff --git a/ape_solidity/compiler.py b/ape_solidity/compiler.py index dcfaecb..1684ea4 100644 --- a/ape_solidity/compiler.py +++ b/ape_solidity/compiler.py @@ -146,6 +146,20 @@ class SolidityConfig(PluginConfig): the compiler (same as ``False``). """ + optimization_yul: Optional[bool] = None + """ + Set to ``True`` to turn on Yul optimization. + Defaults to ``None`` which does not pass the flag to + the compiler (same as ``False``). + """ + + optimization_steps: Optional[str] = None + """ + Set to a string of optimizer steps to use. + Defaults to ``None`` which does not pass the flag to + the compiler (same as ``False``). + """ + def _get_flattened_source(path: Path, name: Optional[str] = None) -> str: name = name or path.name @@ -361,6 +375,19 @@ def _get_settings_from_version_map( if solc_version >= Version("0.7.5") and config.via_ir is not None: version_settings["viaIR"] = config.via_ir + if config.optimization_yul is not None: + if solc_version >= Version("0.8.22"): + version_settings["optimizer"]["yul"] = config.optimization_yul + if ( + config.optimization_steps is not None + ): # needs yul so is inside the yul check + version_settings["optimizer"]["steps"] = config.optimization_steps + else: + logger.warning( + """Yul optimization and steps are not supported for this version of solc + (Need >= 0.8.22).""" + ) + settings[solc_version] = version_settings # TODO: Filter out libraries that are not used for this version.