llvm-context: modularize compiler builtin functions (#234)

- Add the revive runtime function interface to minimize boiler plate
code.
- Outline heavily repeated code into dedicated functions to bring down
code size.
- The code size tests builds optimized for size.
- Function attributes are passed as slices.

This significantly brings down the code size for all OpenZeppelin wizard
contracts (using all possible features) compiled against OpenZeppelin
`v5.0.0` with size optimizations.

|contract|| `-Oz` main | `-Oz` PR || `-O3` main | `-O3` PR |
|-|-|-|-|-|-|-|
|erc1155.sol||100K|67K||114K|147K|
|erc20.sol||120K|90K||160K|191K|
|erc721.sol||128K|101K||178K|214K|
|governor.sol||226K|165K||293K|349K|
|rwa.sol||116K|85K||154K|185K|
|stable.sol||116K|86K||155K|192K|

On the flip side this introduces a heavy penalty for cycle optimized
builds. Setting the no-inline attributes for cycle optimized builds
helps a lot but heavily penalizes runtime speed (LLVM does not yet
inline everything properly - to be investigated later on).

Next steps:
- Modularize more functions
- Refactor the YUL function arguments to use pointers instead of values
- Afterwards check if LLVM still has trouble inline-ing properly on O3
or set the no-inline attribute if it does not penalize runtime
performance too bad.
This commit is contained in:
xermicus
2025-02-25 16:47:01 +01:00
committed by GitHub
parent 7ffe64ed7c
commit a07968205b
32 changed files with 1444 additions and 655 deletions
+12 -2
View File
@@ -6,6 +6,7 @@ use std::path::PathBuf;
use std::sync::Mutex;
use once_cell::sync::Lazy;
use revive_llvm_context::OptimizerSettings;
use crate::project::Project;
use crate::solc::solc_compiler::SolcCompiler;
@@ -31,6 +32,7 @@ struct CachedBlob {
contract_name: String,
solidity: String,
solc_optimizer_enabled: bool,
opt: String,
}
/// Checks if the required executables are present in `${PATH}`.
@@ -254,7 +256,12 @@ pub fn check_solidity_warning(
/// Compile the blob of `contract_name` found in given `source_code`.
/// The `solc` optimizer will be enabled
pub fn compile_blob(contract_name: &str, source_code: &str) -> Vec<u8> {
compile_blob_with_options(contract_name, source_code, true)
compile_blob_with_options(
contract_name,
source_code,
true,
OptimizerSettings::cycles(),
)
}
/// Compile the EVM bin-runtime of `contract_name` found in given `source_code`.
@@ -283,6 +290,7 @@ fn compile_evm(
contract_name: contract_name.to_owned(),
solidity: source_code.to_owned(),
solc_optimizer_enabled,
opt: String::new(),
};
let cache = if runtime {
@@ -322,11 +330,13 @@ pub fn compile_blob_with_options(
contract_name: &str,
source_code: &str,
solc_optimizer_enabled: bool,
optimizer_settings: revive_llvm_context::OptimizerSettings,
) -> Vec<u8> {
let id = CachedBlob {
contract_name: contract_name.to_owned(),
solidity: source_code.to_owned(),
solc_optimizer_enabled,
opt: optimizer_settings.middle_end_as_string(),
};
if let Some(blob) = PVM_BLOB_CACHE.lock().unwrap().get(&id) {
@@ -338,7 +348,7 @@ pub fn compile_blob_with_options(
[(file_name.into(), source_code.into())].into(),
Default::default(),
None,
revive_llvm_context::OptimizerSettings::cycles(),
optimizer_settings,
solc_optimizer_enabled,
)
.expect("source should compile")