Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple simplify-cfg optimization for assembly #6197

Merged
merged 3 commits into from
Jun 28, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ impl AbstractInstructionSet {
pub(crate) fn optimize(self, data_section: &DataSection) -> AbstractInstructionSet {
self.const_indexing_aggregates_function(data_section)
.dce()
.simplify_cfg()
.remove_sequential_jumps()
.remove_redundant_moves()
.remove_unused_ops()
Expand Down
44 changes: 42 additions & 2 deletions sway-core/src/asm_generation/fuel/optimizations.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::collections::BTreeSet;
use std::collections::{BTreeSet, HashMap};

use either::Either;
use rustc_hash::{FxHashMap, FxHashSet};

use crate::{
asm_generation::fuel::compiler_constants,
asm_lang::{ControlFlowOp, VirtualImmediate12, VirtualOp, VirtualRegister},
asm_lang::{ControlFlowOp, Label, VirtualImmediate12, VirtualOp, VirtualRegister},
};

use super::{
Expand Down Expand Up @@ -269,4 +269,44 @@ impl AbstractInstructionSet {

self
}

// Remove unreachable instructions.
pub(crate) fn simplify_cfg(mut self) -> AbstractInstructionSet {
let ops = &self.ops;

if ops.is_empty() {
return self;
}

// Keep track of a map between jump labels and op indices. Useful to compute op successors.
let mut label_to_index: HashMap<Label, usize> = HashMap::default();
for (idx, op) in ops.iter().enumerate() {
if let Either::Right(ControlFlowOp::Label(op_label)) = op.opcode {
label_to_index.insert(op_label, idx);
}
}

let mut reachables = vec![false; ops.len()];
let mut worklist = vec![0];
while let Some(op_idx) = worklist.pop() {
assert!(!reachables[op_idx]);
reachables[op_idx] = true;
let op = &ops[op_idx];
for s in &op.successors(op_idx, ops, &label_to_index) {
if !reachables[*s] {
worklist.push(*s);
}
}
}

let reachable_ops = self
.ops
.into_iter()
.enumerate()
.filter_map(|(idx, op)| if reachables[idx] { Some(op) } else { None })
.collect();
self.ops = reachable_ops;

self
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
category = "disabled" # TODO: Enable once https://github.com/FuelLabs/sway/issues/6174 is fixed.
category = "run"
expected_result = { action = "revert", value = 42 }
expected_result_new_encoding = { action = "revert", value = 42 }
validate_abi = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
category = "disabled" # TODO: Enable once https://github.com/FuelLabs/sway/issues/6174 is fixed.
category = "compile"

# not: $()This returns a value of type unknown, which is not assigned to anything and is ignored.
Loading