IR-Based Optimization
Relevant source files
The following files were used as context for generating this wiki page:
IR-Based Optimization in the Canon Guard project refers to the use of the Solidity Intermediate Representation (IR) pipeline during compilation. This approach is utilized to generate more gas-efficient and optimized bytecode for complex smart contracts, specifically within the context of multisig operations and on-chain action management. While the project supports a "fast" build for development, the optimized build path is recommended for production-grade deployments and comprehensive testing scenarios.
Sources: README.md:21-27, CLAUDE.md:41-43
Compilation Infrastructure
The project utilizes Foundry as its primary build and testing framework, configuring specific profiles to handle the IR-based pipeline. The standard build path is described as "suboptimal but fast," whereas the IR-based path focuses on maximizing efficiency through the Yul intermediate language.
Build Profiles
The foundry.toml file defines distinct profiles that enable the IR pipeline. The via_ir = true setting instructs the Solidity compiler to translate Solidity code into Yul IR before generating the final EVM bytecode. This allows the compiler to perform advanced optimizations that are not available in the legacy pipeline, such as better stack management and cross-function optimizations.
| Profile | via_ir | Output Directory | Purpose |
|---|---|---|---|
default | Not Specified | out | Standard development build |
optimized | true | out-via-ir | Production-ready optimized build |
test | true | out-via-ir | Build used for testing suites |
Sources: foundry.toml:21-30, README.md:21-27
Scripted Execution
The project provides package.json scripts to abstract the Foundry profile complexity from the user.
"scripts": {
"build": "forge build",
"build:optimized": "FOUNDRY_PROFILE=optimized forge build"
}
Sources: package.json:17-18
IR Pipeline Data Flow
The following diagram illustrates how the source code traverses the IR-based compilation pipeline compared to the standard build path within the Canon Guard development environment.
This flow ensures that while developers can iterate quickly using standard compilation, the final artifacts deployed to networks like Ethereum or Optimism benefit from IR optimizations.
Sources: README.md:21-27, CLAUDE.md:41-43, foundry.toml:21-30
Testing and Verification Integration
The IR-based pipeline is not only used for deployments but is also integrated into the testing strategy to ensure that invariants hold true on the optimized bytecode.
Profile Dependency
The test profile in foundry.toml is explicitly configured with via_ir = true. This ensures that unit and integration tests run against bytecode that matches the production environment's optimization level. This is critical for catching potential compiler-introduced bugs or stack-too-deep errors that may only manifest when using IR.
Fuzzing and Invariants
Advanced testing tools like Medusa and Halmos interact with the project's build artifacts. The invariant tests defined for CanonGuard and its associated action builders (e.g., SimpleActions, CappedTokenTransfers) rely on the consistency of the state produced by these builds.
| Component | Test Type | Requirement |
|---|---|---|
CanonGuard | Invariant | Non-reversion/frozen state checks |
ActionTarget | Fuzzing | Correct interaction with arbitrary contracts |
Entrypoint | Symbolic | Formal verification of logic |
Sources: foundry.toml:29-30, test/invariants/PROPERTIES.md:3-30, CLAUDE.md:50-55
Deployment Configuration
When deploying to Ethereum or Optimism, the project configuration encourages the use of high-iteration optimization settings alongside the IR pipeline.
Optimizer Settings
The project is configured for 10,000 optimizer runs. This high number instructs the compiler to prioritize gas-efficient execution (at the cost of higher deployment gas and longer compilation times), which is typical for infrastructure components like the SafeEntrypoint and ActionHub.
[profile.default]
solc_version = '0.8.30'
optimizer_runs = 10_000
evm_version = 'prague'
Sources: foundry.toml:16-19, CLAUDE.md:82-84
Target Networks
Optimized builds are targeted for multiple EVM-compatible chains, as seen in the deployment scripts and RPC configurations:
- Ethereum Mainnet
- Optimism
Sources: package.json:20-23, foundry.toml:40-45
Summary
IR-Based Optimization in Canon Guard is a deliberate architectural choice to provide gas-efficient multisig extensions. By leveraging Foundry's optimized profile and the Solidity via-IR pipeline, the project ensures that the complex logic of on-chain actions and timelocks is minimized for execution cost. This optimization is consistently applied through dedicated build scripts and is a prerequisite for both production deployments and the rigorous testing suites including fuzzing and symbolic execution.