Skip to content

Commit

Permalink
feat: implement o flags for disabling optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
kayagokalp committed Dec 12, 2023
1 parent 88e92f5 commit 2d8a924
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 37 deletions.
3 changes: 3 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ pub struct BuildProfile {
#[serde(default)]
pub error_on_warnings: bool,
pub reverse_results: bool,
pub optimization_level: usize,
}

impl DependencyDetails {
Expand Down Expand Up @@ -718,6 +719,7 @@ impl BuildProfile {
json_abi_with_callpaths: false,
error_on_warnings: false,
reverse_results: false,
optimization_level: 0,
}
}

Expand All @@ -736,6 +738,7 @@ impl BuildProfile {
json_abi_with_callpaths: false,
error_on_warnings: false,
reverse_results: false,
optimization_level: 1,
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,7 +1558,8 @@ pub fn sway_build_config(
.print_ir(build_profile.print_ir)
.include_tests(build_profile.include_tests)
.time_phases(build_profile.time_phases)
.metrics(build_profile.metrics_outfile.clone());
.metrics(build_profile.metrics_outfile.clone())
.optimization_level(build_profile.optimization_level);
Ok(build_config)
}

Expand Down
9 changes: 9 additions & 0 deletions sway-core/src/build_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct BuildConfig {
pub(crate) print_finalized_asm: bool,
pub(crate) print_ir: bool,
pub(crate) include_tests: bool,
pub(crate) optimization_level: usize,
pub time_phases: bool,
pub metrics_outfile: Option<String>,
}
Expand Down Expand Up @@ -92,6 +93,7 @@ impl BuildConfig {
include_tests: false,
time_phases: false,
metrics_outfile: None,
optimization_level: 0,
}
}

Expand Down Expand Up @@ -144,6 +146,13 @@ impl BuildConfig {
}
}

pub fn optimization_level(self, optimization_level: usize) -> Self {
Self {
optimization_level,
..self
}
}

/// Whether or not to include test functions in parsing, type-checking and codegen.
///
/// This should be set to `true` by invocations like `forc test` or `forc check --tests`.
Expand Down
74 changes: 38 additions & 36 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,44 +768,46 @@ pub(crate) fn compile_ast_to_ir_to_asm(
// Initialize the pass manager and register known passes.
let mut pass_mgr = PassManager::default();
register_known_passes(&mut pass_mgr);
let mut pass_group = create_o1_pass_group();

// Target specific transforms should be moved into something more configured.
if build_config.build_target == BuildTarget::Fuel {
// FuelVM target specific transforms.
//
// Demote large by-value constants, arguments and return values to by-reference values
// using temporaries.
pass_group.append_pass(CONSTDEMOTION_NAME);
pass_group.append_pass(ARGDEMOTION_NAME);
pass_group.append_pass(RETDEMOTION_NAME);
pass_group.append_pass(MISCDEMOTION_NAME);

// Convert loads and stores to mem_copys where possible.
pass_group.append_pass(MEMCPYOPT_NAME);

// Run a DCE and simplify-cfg to clean up any obsolete instructions.
pass_group.append_pass(DCE_NAME);
pass_group.append_pass(SIMPLIFYCFG_NAME);
pass_group.append_pass(SROA_NAME);
pass_group.append_pass(MEM2REG_NAME);
pass_group.append_pass(DCE_NAME);
}
if build_config.optimization_level != 0 {
let mut pass_group = create_o1_pass_group();

// Target specific transforms should be moved into something more configured.
if build_config.build_target == BuildTarget::Fuel {
// FuelVM target specific transforms.
//
// Demote large by-value constants, arguments and return values to by-reference values
// using temporaries.
pass_group.append_pass(CONSTDEMOTION_NAME);
pass_group.append_pass(ARGDEMOTION_NAME);
pass_group.append_pass(RETDEMOTION_NAME);
pass_group.append_pass(MISCDEMOTION_NAME);

// Convert loads and stores to mem_copys where possible.
pass_group.append_pass(MEMCPYOPT_NAME);

// Run a DCE and simplify-cfg to clean up any obsolete instructions.
pass_group.append_pass(DCE_NAME);
pass_group.append_pass(SIMPLIFYCFG_NAME);
pass_group.append_pass(SROA_NAME);
pass_group.append_pass(MEM2REG_NAME);
pass_group.append_pass(DCE_NAME);
}

if build_config.print_ir {
pass_group.append_pass(MODULEPRINTER_NAME);
}
if build_config.print_ir {
pass_group.append_pass(MODULEPRINTER_NAME);
}

// Run the passes.
let res = if let Err(ir_error) = pass_mgr.run(&mut ir, &pass_group) {
Err(handler.emit_err(CompileError::InternalOwned(
ir_error.to_string(),
span::Span::dummy(),
)))
} else {
Ok(())
};
res?;
// Run the passes.
let res = if let Err(ir_error) = pass_mgr.run(&mut ir, &pass_group) {
Err(handler.emit_err(CompileError::InternalOwned(
ir_error.to_string(),
span::Span::dummy(),
)))
} else {
Ok(())
};
res?;
}

let final_asm = compile_ir_to_asm(handler, &ir, Some(build_config))?;

Expand Down

0 comments on commit 2d8a924

Please sign in to comment.