FFFF bump sonatina; change optimize cli flag and defaults by sbillig · Pull Request #1332 · argotorg/fe · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ tracing-tree = "0.4.0"
wasm-bindgen-test = "0.3"
semver = "1.0.26"
petgraph = "0.8"
sonatina-ir = { git = "https://github.com/fe-lang/sonatina", rev = "b5f600f" }
sonatina-triple = { git = "https://github.com/fe-lang/sonatina", rev = "b5f600f" }
sonatina-codegen = { git = "https://github.com/fe-lang/sonatina", rev = "b5f600f" }
sonatina-verifier = { git = "https://github.com/fe-lang/sonatina", rev = "b5f600f" }
sonatina-ir = { git = "https://github.com/fe-lang/sonatina", rev = "c6c0062" }
sonatina-triple = { git = "https://github.com/fe-lang/sonatina", rev = "c6c0062" }
sonatina-codegen = { git = "https://github.com/fe-lang/sonatina", rev = "c6c0062" }
sonatina-verifier = { git = "https://github.com/fe-lang/sonatina", rev = "c6c0062" }

[profile.dev]
# Set to 0 to make the build faster and debugging more difficult.
Expand Down
43 changes: 35 additions & 8 deletions crates/codegen/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ use std::fmt;
pub enum OptLevel {
/// No optimization — maximum debuggability.
O0,
/// Balanced optimization (default).
/// Size-oriented optimization.
Os,
/// Speed-oriented optimization (default).
#[default]
O1,
/// Aggressive optimization.
O2,
}

Expand All @@ -28,10 +28,11 @@ impl std::str::FromStr for OptLevel {
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"0" => Ok(OptLevel::O0),
"1" => Ok(OptLevel::O1),
"s" => Ok(OptLevel::Os),
"1" => Ok(OptLevel::O2),
"2" => Ok(OptLevel::O2),
_ => Err(format!(
"unknown optimization level: {s} (expected '0', '1', or '2')"
"unknown optimization level: {s} (expected '0', '1', '2', or 's')"
)),
}
}
Expand All @@ -48,7 +49,7 @@ impl fmt::Display for OptLevel {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
OptLevel::O0 => write!(f, "0"),
OptLevel::O1 => write!(f, "1"),
OptLevel::Os => write!(f, "s"),
OptLevel::O2 => write!(f, "2"),
}
}
Expand Down Expand Up @@ -277,8 +278,8 @@ impl Backend for SonatinaBackend {
// Run the optimization pipeline based on opt_level.
match opt_level {
OptLevel::O0 => { /* no optimization */ }
OptLevel::O1 => sonatina_codegen::optim::Pipeline::balanced().run(&mut module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::aggressive().run(&mut module),
OptLevel::Os => sonatina_codegen::optim::Pipeline::size().run(&mut module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::speed().run(&mut module),
}
if opt_level != OptLevel::O0 {
crate::sonatina::ensure_module_sonatina_ir_valid(&module)?;
Expand Down Expand Up @@ -360,3 +361,29 @@ impl Backend for SonatinaBackend {
Ok(BackendOutput::Bytecode(runtime_section.bytes.clone()))
}
}

#[cfg(test)]
mod tests {
use super::OptLevel;
use std::str::FromStr;

#[test]
fn opt_level_parses_os() {
assert_eq!(OptLevel::from_str("s"), Ok(OptLevel::Os));
}

#[test]
fn opt_level_parses_o1_as_o2() {
assert_eq!(OptLevel::from_str("1"), Ok(OptLevel::O2));
}

#[test]
fn opt_level_default_is_o2() {
assert_eq!(OptLevel::default(), OptLevel::O2);
}

#[test]
fn opt_level_display_uses_os() {
assert_eq!(OptLevel::Os.to_string(), "s");
}
}
4 changes: 2 additions & 2 deletions crates/codegen/src/sonatina/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,8 +417,8 @@ fn compile_mir_module_for_sonatina_output<'db>(

match opt_level {
OptLevel::O0 => {}
OptLevel::O1 => sonatina_codegen::optim::Pipeline::balanced().run(&mut module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::aggressive().run(&mut module),
OptLevel::Os => sonatina_codegen::optim::Pipeline::size().run(&mut module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::speed().run(&mut module),
}
if opt_level != OptLevel::O0 {
ensure_module_sonatina_ir_valid(&module)?;
Expand Down
4 changes: 2 additions & 2 deletions crates/codegen/src/sonatina/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,8 @@ pub fn emit_test_module_sonatina(
fn run_sonatina_optimization_pipeline(module: &mut Module, opt_level: OptLevel) {
match opt_level {
OptLevel::O0 => { /* no optimization */ }
OptLevel::O1 => sonatina_codegen::optim::Pipeline::balanced().run(module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::aggressive().run(module),
OptLevel::Os => sonatina_codegen::optim::Pipeline::size().run(module),
OptLevel::O2 => sonatina_codegen::optim::Pipeline::speed().run(module),
}
}

Expand Down
76 changes: 35 additions & 41 deletions crates/fe/src/main.rs
662D
Original file line number Diff line number Diff line change
Expand Up @@ -81,23 +81,23 @@ pub enum Command {
/// Code generation backend to use (yul or sonatina).
#[arg(long, default_value = "sonatina")]
backend: String,
/// Optimization level (0 = none, 1 = balanced, 2 = aggressive).
/// Optimization level (0 = none, s = size-oriented, 1/2 = speed-oriented).
///
/// Defaults to `1`.
/// Defaults to `2`.
///
/// Note: with `--backend yul`, opt levels `1` and `2` are currently equivalent.
/// Note: with `--backend yul`, optimize levels `s`, `1`, and `2` are currently equivalent.
///
/// - Sonatina backend: controls the optimization pipeline.
/// - Yul backend: controls whether solc optimization is enabled (0 = disabled, 1/2 = enabled).
#[arg(long, default_value = "1", value_name = "LEVEL")]
opt_level: String,
/// Enable optimization.
///
/// Shorthand for `--opt-level 1`.
/// `1` is currently an alias for `2`.
///
/// It is an error to pass `--optimize` with `--opt-level 0`.
#[arg(long)]
optimize: bool,
/// - Sonatina backend: controls the optimization pipeline.
/// - Yul backend: controls whether solc optimization is enabled (0 = disabled, s/1/2 = enabled).
#[arg(
long = "optimize",
short = 'O',
value_name = "LEVEL",
value_parser = ["0", "1", "2", "s"]
)]
optimize: Option<String>,
/// solc binary to use (overrides FE_SOLC_PATH).
///
/// Only used with `--backend yul` (ignored with a warning otherwise).
Expand Down Expand Up @@ -234,23 +234,23 @@ pub enum Command {
/// Only used with `--backend yul` (ignored with a warning otherwise).
#[arg(long)]
solc: Option<String>,
/// Optimization level (0 = none, 1 = balanced, 2 = aggressive).
///
/// Defaults to `1`.
/// Optimization level (0 = none, s = size-oriented, 1/2 = speed-oriented).
///
/// Note: with `--backend yul`, opt levels `1` and `2` are currently equivalent.
/// Defaults to `2`.
///
/// - Sonatina backend: controls the optimization pipeline.
/// - Yul backend: controls whether solc optimization is enabled (0 = disabled, 1/2 = enabled).
#[arg(long, default_value = "1", value_name = "LEVEL")]
opt_level: String,
/// Enable optimization.
/// Note: with `--backend yul`, optimize levels `s`, `1`, and `2` are currently equivalent.
///
/// Shorthand for `--opt-level 1`.
/// `1` is currently an alias for `2`.
///
/// It is an error to pass `--optimize` with `--opt-level 0`.
#[arg(long)]
optimize: bool,
/// - Sonatina backend: controls the optimization pipeline.
/// - Yul backend: controls whether solc optimization is enabled (0 = disabled, s/1/2 = enabled).
#[arg(
long = "optimize",
short = 'O',
value_name = "LEVEL",
value_parser = ["0", "1", "2", "s"]
)]
optimize: Option<String>,
/// Trace executed EVM opcodes while running tests.
#[arg(long)]
trace_evm: bool,
Expand Down Expand Up @@ -423,7 +423,6 @@ pub fn run(opts: &Options) {
standalone,
contract,
backend,
opt_level,
optimize,
solc,
out_dir,
Expand All @@ -440,7 +439,7 @@ pub fn run(opts: &Options) {
std::process::exit(1);
}
};
let opt_level = match effective_opt_level(backend_kind, opt_level, *optimize) {
let opt_level = match effective_opt_level(backend_kind, optimize.as_deref()) {
Ok(level) => level,
Err(err) => {
eprintln!("Error: {err}");
Expand Down Expand Up @@ -527,7 +526,6 @@ pub fn run(opts: &Options) {
debug: test_debug,
backend,
solc,
opt_level,
optimize,
trace_evm,
trace_evm_keep,
Expand All @@ -548,7 +546,7 @@ pub fn run(opts: &Options) {
std::process::exit(1);
}
};
let opt_level = match effective_opt_level(backend_kind, opt_level, *optimize) {
let opt_level = match effective_opt_level(backend_kind, optimize.as_deref()) {
Ok(level) => level,
Err(err) => {
eprintln!("Error: {err}");
Expand Down Expand Up @@ -971,22 +969,18 @@ fn generate_lsp_doc_html(resolved_root: Option<&Utf8PathBuf>) -> String {

fn effective_opt_level(
backend_kind: codegen::BackendKind,
opt_level: &str,
optimize: bool,
optimize: Option<&str>,
) -> Result<codegen::OptLevel, String> {
let level: codegen::OptLevel = opt_level.parse()?;
let level: codegen::OptLevel = optimize.unwrap_or("2").parse()?;

if optimize && level == codegen::OptLevel::O0 {
return Err(
"--optimize is shorthand for `--opt-level 1` and cannot be used with `--opt-level 0`"
.to_string(),
if backend_kind == codegen::BackendKind::Yul
&& let Some(optimize @ ("1" | "2")) = optimize
{
eprintln!(
"Warning: --optimize {optimize} has no additional effect for --backend yul (same as s)"
);
}

if backend_kind == codegen::BackendKind::Yul && level == codegen::OptLevel::O2 {
eprintln!("Warning: --opt-level 2 has no additional effect for --backend yul (same as 1)");
}

Ok(level)
}

Expand Down
Loading
Loading
0