Skip to content

Commit

Permalink
Adds a new forc build flag to print the DCA graph. (FuelLabs#3493)
Browse files Browse the repository at this point in the history
This adds a new `--print-dca-graph` to `forc build` which allows the
user to print the DCA graph.
  • Loading branch information
tritao authored Dec 2, 2022
1 parent ef47e69 commit 487770f
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/src/forc/manifest_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The `[build-profiles]` tables provide a way to customize compiler settings such
The following fields needs to be provided for a build-profile:

* `print-ast` - Whether to print out the generated AST (true) or not (false).
* `print-dca-graph` - Whether to print out the computed DCA graph (in GraphViz DOT format).
* `print-finalized-asm` - Whether to compile to bytecode (false) or to print out the generated ASM (true).
* `print-intermediate-asm` - Whether to compile to bytecode (false) or to print out the generated ASM (true).
* `print-ir` - Whether to compile to bytecode (false) or to print out the generated IR (true).
Expand Down
3 changes: 3 additions & 0 deletions forc-pkg/src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub struct DependencyDetails {
#[serde(rename_all = "kebab-case")]
pub struct BuildProfile {
pub print_ast: bool,
pub print_dca_graph: bool,
pub print_ir: bool,
pub print_finalized_asm: bool,
pub print_intermediate_asm: bool,
Expand Down Expand Up @@ -586,6 +587,7 @@ impl BuildProfile {
pub fn debug() -> Self {
Self {
print_ast: false,
print_dca_graph: false,
print_ir: false,
print_finalized_asm: false,
print_intermediate_asm: false,
Expand All @@ -598,6 +600,7 @@ impl BuildProfile {
pub fn release() -> Self {
Self {
print_ast: false,
print_dca_graph: false,
print_ir: false,
print_finalized_asm: false,
print_intermediate_asm: false,
Expand Down
6 changes: 5 additions & 1 deletion forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ pub struct PkgOpts {
pub struct PrintOpts {
/// Print the generated Sway AST (Abstract Syntax Tree).
pub ast: bool,
/// Print the computed Sway DCA (Dead Code Analysis) graph.
pub dca_graph: bool,
/// Print the finalized ASM.
///
/// This is the state of the ASM with registers allocated and optimisations applied.
Expand Down Expand Up @@ -2084,6 +2086,7 @@ pub fn sway_build_config(
file_name.to_path_buf(),
manifest_dir.to_path_buf(),
)
.print_dca_graph(build_profile.print_dca_graph)
.print_finalized_asm(build_profile.print_finalized_asm)
.print_intermediate_asm(build_profile.print_intermediate_asm)
.print_ir(build_profile.print_ir)
Expand Down Expand Up @@ -2414,6 +2417,7 @@ fn build_profile_from_opts(
Default::default()
});
profile.print_ast |= print.ast;
profile.print_dca_graph |= print.dca_graph;
profile.print_ir |= print.ir;
profile.print_finalized_asm |= print.finalized_asm;
profile.print_intermediate_asm |= print.intermediate_asm;
Expand Down Expand Up @@ -2757,7 +2761,7 @@ pub fn check(
Some(program) => program,
};

let ast_result = sway_core::parsed_to_ast(type_engine, &parse_program, dep_namespace);
let ast_result = sway_core::parsed_to_ast(type_engine, &parse_program, dep_namespace, None);
warnings.extend(ast_result.warnings);
errors.extend(ast_result.errors);

Expand Down
3 changes: 3 additions & 0 deletions forc-plugins/forc-client/src/ops/deploy/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub struct DeployCommand {
/// Print the generated Sway AST (Abstract Syntax Tree).
#[clap(long)]
pub print_ast: bool,
/// Print the computed DCA (Dead Code Analysis) graph.
#[clap(long)]
pub print_dca_graph: bool,
/// Print the finalized ASM.
///
/// This is the state of the ASM with registers allocated and optimisations applied.
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/ops/deploy/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ fn build_opts_from_cmd(cmd: &DeployCommand) -> pkg::BuildOpts {
},
print: pkg::PrintOpts {
ast: cmd.print_ast,
dca_graph: cmd.print_dca_graph,
finalized_asm: cmd.print_finalized_asm,
intermediate_asm: cmd.print_intermediate_asm,
ir: cmd.print_ir,
Expand Down
4 changes: 4 additions & 0 deletions forc-plugins/forc-client/src/ops/run/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct RunCommand {
#[clap(long)]
pub print_ast: bool,

/// Print the computed DCA (Dead Code Analysis) graph.
#[clap(long)]
pub print_dca_graph: bool,

/// Print the finalized ASM.
///
/// This is the state of the ASM with registers allocated and optimisations applied.
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/ops/run/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ fn build_opts_from_cmd(cmd: &RunCommand) -> pkg::BuildOpts {
},
print: pkg::PrintOpts {
ast: cmd.print_ast,
dca_graph: cmd.print_dca_graph,
finalized_asm: cmd.print_finalized_asm,
intermediate_asm: cmd.print_intermediate_asm,
ir: cmd.print_ir,
Expand Down
1 change: 1 addition & 0 deletions forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn opts_from_cmd(cmd: Command) -> forc_test::Opts {
},
print: pkg::PrintOpts {
ast: cmd.build.print_ast,
dca_graph: cmd.build.print_dca_graph,
finalized_asm: cmd.build.print_finalized_asm,
intermediate_asm: cmd.build.print_intermediate_asm,
ir: cmd.build.print_ir,
Expand Down
3 changes: 3 additions & 0 deletions forc/src/cli/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ pub struct Build {
/// Print the generated Sway AST (Abstract Syntax Tree).
#[clap(long)]
pub print_ast: bool,
/// Print the computed Sway DCA graph.
#[clap(long)]
pub print_dca_graph: bool,
/// Print the finalized ASM.
///
/// This is the state of the ASM with registers allocated and optimisations applied.
Expand Down
1 change: 1 addition & 0 deletions forc/src/ops/forc_build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn opts_from_cmd(cmd: BuildCommand) -> pkg::BuildOpts {
},
print: pkg::PrintOpts {
ast: cmd.build.print_ast,
dca_graph: cmd.build.print_dca_graph,
finalized_asm: cmd.build.print_finalized_asm,
intermediate_asm: cmd.build.print_intermediate_asm,
ir: cmd.build.print_ir,
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 @@ -6,6 +6,7 @@ pub struct BuildConfig {
// The canonical file path to the root module.
// E.g. `/home/user/project/src/main.sw`.
pub(crate) canonical_root_module: Arc<PathBuf>,
pub(crate) print_dca_graph: bool,
pub(crate) print_intermediate_asm: bool,
pub(crate) print_finalized_asm: bool,
pub(crate) print_ir: bool,
Expand Down Expand Up @@ -44,13 +45,21 @@ impl BuildConfig {
};
Self {
canonical_root_module: Arc::new(canonical_root_module),
print_dca_graph: false,
print_intermediate_asm: false,
print_finalized_asm: false,
print_ir: false,
include_tests: false,
}
}

pub fn print_dca_graph(self, a: bool) -> Self {
Self {
print_dca_graph: a,
..self
}
}

pub fn print_intermediate_asm(self, a: bool) -> Self {
Self {
print_intermediate_asm: a,
Expand Down
3 changes: 1 addition & 2 deletions sway-core/src/control_flow_analysis/flow_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,7 @@ impl ControlFlowGraph {
}
}

#[allow(dead_code)]
/// Prints out graphviz for this graph
/// Prints out GraphViz DOT format for this graph.
pub(crate) fn visualize(&self) {
use petgraph::dot::{Config, Dot};
tracing::info!(
Expand Down
18 changes: 16 additions & 2 deletions sway-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ pub fn parsed_to_ast(
type_engine: &TypeEngine,
parse_program: &parsed::ParseProgram,
initial_namespace: namespace::Module,
build_config: Option<&BuildConfig>,
) -> CompileResult<ty::TyProgram> {
// Type check the program.
let CompileResult {
Expand Down Expand Up @@ -220,7 +221,14 @@ pub fn parsed_to_ast(
}));

// Perform control flow analysis and extend with any errors.
let cfa_res = perform_control_flow_analysis(type_engine, &typed_program);
let cfa_res = perform_control_flow_analysis(
type_engine,
&typed_program,
match build_config {
Some(cfg) => cfg.print_dca_graph,
None => false,
},
);
errors.extend(cfa_res.errors);
warnings.extend(cfa_res.warnings);

Expand Down Expand Up @@ -294,7 +302,7 @@ pub fn compile_to_ast(
};

// Type check (+ other static analysis) the CST to a typed AST.
let typed_res = parsed_to_ast(type_engine, &parse_program, initial_namespace);
let typed_res = parsed_to_ast(type_engine, &parse_program, initial_namespace, build_config);
errors.extend(typed_res.errors);
warnings.extend(typed_res.warnings);
let typed_program = match typed_res.value {
Expand Down Expand Up @@ -662,6 +670,7 @@ pub fn clear_lazy_statics() {
fn perform_control_flow_analysis(
type_engine: &TypeEngine,
program: &ty::TyProgram,
print_graph: bool,
) -> CompileResult<()> {
let dca_res = dead_code_analysis(type_engine, program);
let rpa_errors = return_path_analysis(type_engine, program);
Expand All @@ -670,6 +679,11 @@ fn perform_control_flow_analysis(
} else {
err(vec![], rpa_errors)
};
if let Some(graph) = dca_res.clone().value {
if print_graph {
graph.visualize();
}
}
dca_res.flat_map(|_| rpa_res)
}

Expand Down

0 comments on commit 487770f

Please sign in to comment.