Skip to content

Commit

Permalink
Merge e0fa89a into 03c09bf
Browse files Browse the repository at this point in the history
  • Loading branch information
vaivaswatha authored Jun 28, 2024
2 parents 03c09bf + e0fa89a commit 5d5df40
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 4 deletions.
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.

0 comments on commit 5d5df40

Please sign in to comment.