Skip to content

Commit

Permalink
Memoize expressions in basic blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcoxjay committed Aug 31, 2023
1 parent d9f5a92 commit 166d20b
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 47 deletions.
46 changes: 26 additions & 20 deletions src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ impl TermConverter<'_> {
let name = self.string_term_to_string(name);
let code_vec = self.term_conslist_to_vec(code, "Code");
let mut instrs = vec![];
// let mut memo = HashMap::<Term, String>::new();
let mut memo = HashMap::<TermId, String>::new();

for t in code_vec {
self.term_to_instructions(&t, &mut instrs);
self.term_to_instructions(&t, &mut instrs, &mut memo);
}

BasicBlock {
Expand Down Expand Up @@ -142,10 +142,15 @@ impl TermConverter<'_> {
res
}

fn term_to_instructions(&mut self, id: &TermId, res: &mut Vec<Instruction>) {
fn term_to_instructions(
&mut self,
id: &TermId,
res: &mut Vec<Instruction>,
memo: &mut HashMap<TermId, String>,
) {
match_term_app!(self.get(id); {
("Print", [arg]) => {
let arg = self.term_to_code(arg, res, None);
let arg = self.term_to_code(arg, res, None, memo);

res.push(Instruction::Effect {
op: EffectOps::Print,
Expand All @@ -158,12 +163,12 @@ impl TermConverter<'_> {
("End", []) => {},
("Assign", [dest, src]) => {
let dest = self.string_term_to_string(dest);
self.term_to_code(src, res, Some(dest.to_string()));
self.term_to_code(src, res, Some(dest.to_string()), memo);
},
(op @ ("store" | "free"), args) => {
let args = args
.iter()
.map(|arg| self.term_to_code(arg, res, None))
.map(|arg| self.term_to_code(arg, res, None, memo))
.collect::<Vec<String>>();


Expand All @@ -178,7 +183,7 @@ impl TermConverter<'_> {
("alloc", [atype, dest, arg]) => {
let atype = self.term_to_type(atype);
let dest = self.string_term_to_string(dest);
let arg = self.term_to_code(arg, res, None);
let arg = self.term_to_code(arg, res, None, memo);
res.push(Instruction::Value {
dest,
args: vec![arg],
Expand All @@ -198,13 +203,18 @@ impl TermConverter<'_> {
id: &TermId,
res: &mut Vec<Instruction>,
assign_to: Option<String>,
memo: &mut HashMap<TermId, String>,
) -> String {
if memo.contains_key(id) && assign_to.is_none() {
return memo[id].clone();
}

let dest = match &assign_to {
Some(dest) => dest.clone(),
None => self.optimizer.fresh_var(),
};

match self.get(id) {
let ret = match self.get(id) {
Term::Lit(literal) => {
res.push(Instruction::Constant {
dest: dest.clone(),
Expand All @@ -215,13 +225,6 @@ impl TermConverter<'_> {
});
dest
}
Term::Var(var) => {
if let Some(_output) = assign_to {
panic!("Cannot assign var to var")
} else {
var.to_string()
}
}
t => {
match_term_app!(t; {
("Var", [arg]) => {
Expand All @@ -230,7 +233,7 @@ impl TermConverter<'_> {
_ => panic!("expected string literal for var"),
}
},
("ReturnValue", [arg]) => self.term_to_code(arg, res, assign_to),
("ReturnValue", [arg]) => self.term_to_code(arg, res, assign_to, memo),
(op @ ("True" | "False" | "Int" | "Float" | "Char"), [ty, args @ ..]) => {
let lit = match (op, args) {
("True", []) => Literal::Bool(true),
Expand Down Expand Up @@ -264,8 +267,8 @@ impl TermConverter<'_> {
},
("phi", [etype, arg1, arg2, label1, label2]) => {
let etype = self.term_to_type(etype);
let arg1 = self.term_to_code(arg1, res, None);
let arg2 = self.term_to_code(arg2, res, None);
let arg1 = self.term_to_code(arg1, res, None, memo);
let arg2 = self.term_to_code(arg2, res, None, memo);
let label1 = self.string_term_to_string(label1);
let label2 = self.string_term_to_string(label2);
res.push(Instruction::Value {
Expand All @@ -285,7 +288,7 @@ impl TermConverter<'_> {
let args_vars = args
.iter()
.skip(1)
.map(|arg| self.term_to_code(arg, res, None))
.map(|arg| self.term_to_code(arg, res, None, memo))
.collect::<Vec<String>>();
res.push(Instruction::Value {
dest: dest.clone(),
Expand All @@ -300,7 +303,10 @@ impl TermConverter<'_> {
}
})
}
}
};

memo.insert(*id, ret.clone());
ret
}

pub(crate) fn term_to_type(&self, id: &TermId) -> Type {
Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/files__add_naiive.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ expression: "format!(\"{}\", res)"
v0: int = const 1;
v1: int = const 2;
v2: int = const 3;
v0_: int = const 3;
print v0_;
print v2;
ret;
.sblock___0:
.exit___:
Expand Down
9 changes: 2 additions & 7 deletions tests/snapshots/files__add_naiive_no_opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ expression: "format!(\"{}\", res)"
.entry___:
v0: int = const 1;
v1: int = const 2;
v0_: int = const 1;
v1_: int = const 2;
v2: int = add v0_ v1_;
v3_: int = const 1;
v4_: int = const 2;
v2_: int = add v3_ v4_;
print v2_;
v2: int = add v0 v1;
print v2;
ret;
.sblock___0:
.exit___:
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/files__block-diamond_naiive.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ expression: "format!(\"{}\", res)"
one: int = const 1;
two: int = const 2;
x: int = const 0;
v0_: int = const 1;
v1_: int = const 2;
a_cond: bool = lt v0_ v1_;
a_cond: bool = lt one two;
br a_cond .sblock___4 .sblock___5;
.sblock___4:
jmp .sblock___2;
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/files__block-diamond_naiive_no_opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ expression: "format!(\"{}\", res)"
one: int = const 1;
two: int = const 2;
x: int = const 0;
v0_: int = const 1;
v1_: int = const 2;
a_cond: bool = lt v0_ v1_;
a_cond: bool = lt one two;
br a_cond .sblock___4 .sblock___5;
.sblock___4:
jmp .sblock___2;
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/files__diamond_naiive.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ expression: "format!(\"{}\", res)"
@main {
.entry___:
x: int = const 4;
v0_: int = const 4;
v1_: int = const 4;
cond: bool = lt v0_ v1_;
cond: bool = lt x x;
br cond .sblock___3 .sblock___4;
.sblock___3:
jmp .sblock___1;
Expand Down
4 changes: 1 addition & 3 deletions tests/snapshots/files__diamond_naiive_no_opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ expression: "format!(\"{}\", res)"
@main {
.entry___:
x: int = const 4;
v0_: int = const 4;
v1_: int = const 4;
cond: bool = lt v0_ v1_;
cond: bool = lt x x;
br cond .sblock___3 .sblock___4;
.sblock___3:
jmp .sblock___1;
Expand Down
8 changes: 2 additions & 6 deletions tests/snapshots/files__two_fns_naiive_no_opt.snap
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ expression: "format!(\"{}\", res)"
.entry___:
v0: int = const 1;
v1: int = const 2;
v0_: int = const 1;
v1_: int = const 2;
v2: int = add v0_ v1_;
v2: int = add v0 v1;
ret;
.sblock___0:
.exit___:
Expand All @@ -17,9 +15,7 @@ expression: "format!(\"{}\", res)"
.entry___:
v0: int = const 1;
v1: int = const 2;
v2_: int = const 1;
v3_: int = const 2;
v2: int = sub v2_ v3_;
v2: int = sub v0 v1;
ret;
.sblock___0:
.exit___:
Expand Down

0 comments on commit 166d20b

Please sign in to comment.