Skip to content

Commit

Permalink
control flow graph to structured control flow
Browse files Browse the repository at this point in the history
  • Loading branch information
gga0wp committed Mar 2, 2022
1 parent 06e9b0e commit 1060513
Show file tree
Hide file tree
Showing 9 changed files with 724 additions and 26 deletions.
4 changes: 4 additions & 0 deletions language/evm/move-to-yul/src/experiments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ impl Experiment {
/// during tests this is off.
/// Retention: permanent
pub const CAPTURE_SOURCE_INFO: &'static str = "capture-source-info";
/// Transform control flow graph to structured control flow.
/// This is off by default for now, we might want to make it default
/// if the performance improvement is significant.
pub const APPLY_CFG_TO_SCF: &'static str = "apply-cfg-to-scf";
}
128 changes: 109 additions & 19 deletions language/evm/move-to-yul/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use move_stackless_bytecode::{
function_target_pipeline::FunctionVariant,
stackless_bytecode::{Bytecode, Constant, Label, Operation},
stackless_control_flow_graph::{BlockContent, BlockId, StacklessControlFlowGraph},
stackless_structured_control_flow::StacklessStructuredControlFlow,
};
use sha3::{Digest, Keccak256};
use std::collections::{btree_map::Entry, BTreeMap};
Expand Down Expand Up @@ -105,30 +106,119 @@ impl<'a> FunctionGenerator<'a> {
// Compute control flow graph, entry block, and label map
let code = target.data.code.as_slice();
let cfg = StacklessControlFlowGraph::new_forward(code);
let entry_bb = Self::get_actual_entry_block(&cfg);
let label_map = Self::compute_label_map(&cfg, code);

// Emit state machine to represent control flow.
// TODO: Eliminate the need for this, see also
// https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2
if cfg.successors(entry_bb).iter().all(|b| cfg.is_dummmy(*b)) {
// In this trivial case, we have only one block and can omit the state machine
if let BlockContent::Basic { lower, upper } = cfg.content(entry_bb) {
for offs in *lower..*upper + 1 {
self.bytecode(ctx, fun_id, target, &label_map, &code[offs as usize], false);
if !ctx.options.apply_cfg_to_scf() {
let entry_bb = Self::get_actual_entry_block(&cfg);
// Emit state machine to represent control flow.
// TODO: Eliminate the need for this, see also
// https://medium.com/leaningtech/solving-the-structured-control-flow-problem-once-and-for-all-5123117b1ee2
if cfg.successors(entry_bb).iter().all(|b| cfg.is_dummmy(*b)) {
// In this trivial case, we have only one block and can omit the state machine
if let BlockContent::Basic { lower, upper } = cfg.content(entry_bb) {
for offs in *lower..*upper + 1 {
self.bytecode(
ctx,
fun_id,
target,
&label_map,
&code[offs as usize],
false,
);
}
} else {
panic!("effective entry block is not basic")
}
} else {
panic!("effective entry block is not basic")
emitln!(ctx.writer, "let $block := {}", entry_bb);
emit!(ctx.writer, "for {} true {} ");
ctx.emit_block(|| {
emitln!(ctx.writer, "switch $block");
for blk_id in &cfg.blocks() {
if let BlockContent::Basic { lower, upper } = cfg.content(*blk_id) {
// Emit code for this basic block.
emit!(ctx.writer, "case {} ", blk_id);
ctx.emit_block(|| {
for offs in *lower..*upper + 1 {
self.bytecode(
ctx,
fun_id,
target,
&label_map,
&code[offs as usize],
true,
);
}
})
}
}
})
}
} else {
emitln!(ctx.writer, "let $block := {}", entry_bb);
emit!(ctx.writer, "for {} true {} ");
ctx.emit_block(|| {
emitln!(ctx.writer, "switch $block");
for blk_id in &cfg.blocks() {
if let BlockContent::Basic { lower, upper } = cfg.content(*blk_id) {
// Emit code for this basic block.
emit!(ctx.writer, "case {} ", blk_id);
let prover_graph = cfg.to_prover_graph();
let mut reduced_cfg = cfg.clone();
let loop_map = reduced_cfg.reduce_cfg_loop(&prover_graph);

let mut scf_top_sort = StacklessStructuredControlFlow::new(&reduced_cfg).top_sort;
while let Some(blocks) = scf_top_sort.pop() {
for blk_id in blocks {
if let Some(one_loop) = loop_map.get(&blk_id) {
emitln!(ctx.writer, "let $block := {}", one_loop.loop_header);
emit!(ctx.writer, "for {} true {} ");
ctx.emit_block(|| {
if let BlockContent::Basic { lower, upper } =
cfg.content(one_loop.loop_header)
{
ctx.emit_block(|| {
for offs in *lower..*upper + 1 {
self.bytecode(
ctx,
fun_id,
target,
&label_map,
&code[offs as usize],
true,
);
}
})
}
for loop_body_blk_id in &one_loop.loop_body {
if let BlockContent::Basic { lower, upper } =
cfg.content(*loop_body_blk_id)
{
ctx.emit_block(|| {
for offs in *lower..*upper + 1 {
self.bytecode(
ctx,
fun_id,
target,
&label_map,
&code[offs as usize],
true,
);
}
})
}
}
if let BlockContent::Basic { lower, upper } =
cfg.content(one_loop.loop_latch)
{
ctx.emit_block(|| {
for offs in *lower..*upper + 1 {
self.bytecode(
ctx,
fun_id,
target,
&label_map,
&code[offs as usize],
true,
);
}
})
}
});
} else if let BlockContent::Basic { lower, upper } = cfg.content(blk_id) {
emitln!(ctx.writer, "let $block := {}", blk_id);
ctx.emit_block(|| {
for offs in *lower..*upper + 1 {
self.bytecode(
Expand All @@ -143,7 +233,7 @@ impl<'a> FunctionGenerator<'a> {
})
}
}
})
}
}
});
emitln!(ctx.writer)
Expand Down
5 changes: 5 additions & 0 deletions language/evm/move-to-yul/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ impl Options {
pub fn generate_source_info(&self) -> bool {
!self.testing || self.experiment_on(Experiment::CAPTURE_SOURCE_INFO)
}

/// Returns true if control flow graph to structured control flow is applied.
pub fn apply_cfg_to_scf(&self) -> bool {
self.experiment_on(Experiment::APPLY_CFG_TO_SCF)
}
}
Loading

0 comments on commit 1060513

Please sign in to comment.