Add ability for compiler to opt out if it can't work with some Mode/version

This commit is contained in:
James Wilson
2025-08-08 17:43:12 +01:00
parent 7b5ffed288
commit f98c15121d
5 changed files with 115 additions and 54 deletions
+12 -5
View File
@@ -20,7 +20,7 @@ use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments;
// Re-export this as it's a part of the compiler interface.
pub use revive_dt_format::mode::ModeOptimizerSetting;
pub use revive_dt_format::mode::{Mode, ModeOptimizerSetting, ModePipeline};
pub mod revive_js;
pub mod revive_resolc;
@@ -46,13 +46,20 @@ pub trait SolidityCompiler {
) -> impl Future<Output = anyhow::Result<PathBuf>>;
fn version(&self) -> anyhow::Result<Version>;
/// Does the compiler support the provided mode and version settings?
fn supports_mode(
compiler_version: &Version,
optimize_setting: ModeOptimizerSetting,
pipeline: ModePipeline,
) -> bool;
}
/// The generic compilation input configuration.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompilerInput {
pub pipeline: Option<ModePipeline>,
pub optimization: Option<ModeOptimizerSetting>,
pub via_ir: Option<bool>,
pub evm_version: Option<EVMVersion>,
pub allow_paths: Vec<PathBuf>,
pub base_path: Option<PathBuf>,
@@ -87,8 +94,8 @@ where
pub fn new() -> Self {
Self {
input: CompilerInput {
pipeline: Default::default(),
optimization: Default::default(),
via_ir: Default::default(),
evm_version: Default::default(),
allow_paths: Default::default(),
base_path: Default::default(),
@@ -104,8 +111,8 @@ where
self
}
pub fn with_via_ir(mut self, value: impl Into<Option<bool>>) -> Self {
self.input.via_ir = value.into();
pub fn with_pipeline(mut self, value: impl Into<Option<ModePipeline>>) -> Self {
self.input.pipeline = value.into();
self
}
+14 -3
View File
@@ -14,7 +14,7 @@ use revive_solc_json_interface::{
SolcStandardJsonOutput,
};
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, SolidityCompiler};
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler};
use alloy::json_abi::JsonAbi;
use anyhow::Context;
@@ -39,9 +39,9 @@ impl SolidityCompiler for Resolc {
async fn build(
&self,
CompilerInput {
// Ignored as we only support one pipeline (Y)
pipeline,
optimization,
// Ignored and not honored since this is required for the resolc compilation.
via_ir: _via_ir,
evm_version,
allow_paths,
base_path,
@@ -231,6 +231,17 @@ impl SolidityCompiler for Resolc {
Version::parse(version_string).map_err(Into::into)
}
fn supports_mode(
compiler_version: &Version,
_optimize_setting: ModeOptimizerSetting,
pipeline: ModePipeline,
) -> bool {
// We only support the Y (IE compile via Yul IR) mode here, which also means that we can
// only use solc version 0.8.13 and above. We must always compile via Yul IR as resolc
// needs this to translate to LLVM IR and then RISCV.
pipeline == ModePipeline::Y && compiler_version >= &Version::new(0, 8, 13)
}
}
#[cfg(test)]
+14 -3
View File
@@ -10,7 +10,7 @@ use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments;
use revive_dt_solc_binaries::download_solc;
use crate::{CompilerInput, CompilerOutput, SolidityCompiler};
use crate::{CompilerInput, CompilerOutput, ModeOptimizerSetting, ModePipeline, SolidityCompiler};
use anyhow::Context;
use foundry_compilers_artifacts::{
@@ -35,8 +35,8 @@ impl SolidityCompiler for Solc {
async fn build(
&self,
CompilerInput {
pipeline,
optimization,
via_ir,
evm_version,
allow_paths,
base_path,
@@ -70,7 +70,7 @@ impl SolidityCompiler for Solc {
.map(|item| item.to_string()),
),
evm_version: evm_version.map(|version| version.to_string().parse().unwrap()),
via_ir,
via_ir: pipeline.map(|p| p.via_yul_ir()),
libraries: Libraries {
libs: libraries
.into_iter()
@@ -212,6 +212,17 @@ impl SolidityCompiler for Solc {
Version::parse(version_string).map_err(Into::into)
}
fn supports_mode(
compiler_version: &Version,
_optimize_setting: ModeOptimizerSetting,
pipeline: ModePipeline,
) -> bool {
// solc 0.8.13 and above supports --via-ir, and less than that does not. Thus, we support mode E
// (ie no Yul IR) in either case, but only support Y (via Yul IR) if the compiler is new enough.
pipeline == ModePipeline::E
|| (pipeline == ModePipeline::Y && compiler_version >= &Version::new(0, 8, 13))
}
}
#[cfg(test)]