Skip to content

Commit

Permalink
feat: add test case for gvn pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Sep 22, 2024
1 parent ca9cf03 commit a78aeac
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
35 changes: 30 additions & 5 deletions compilers/rustyc/optimizer/src/ir_optimizer/pass/gvn/debugger.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,39 @@
use super::GVNPass;
use crate::ir::function::Function;
use crate::ir::instructions::Instruction;
use crate::ir_optimizer::pass::DebuggerPass;
use crate::ir_optimizer::print_table_helper::{get_max_block_id_len, print_divider, print_header};
use std::collections::HashMap;

impl<'a> DebuggerPass for GVNPass<'a> {
fn debugger(&self, _function: &Function) -> String {
let mut output = String::new();
output.push_str("Remove Inst:\n");
fn debugger(&self, function: &Function) -> String {
let mut output_string = String::new();
// get inst len
let inst_map_string: &HashMap<Instruction, String> = &self.cache_inst_strings;
let mut max_len_of_insts = 0 as usize;
for string in &inst_map_string.values().collect::<Vec<_>>() {
if max_len_of_insts < string.len() {
max_len_of_insts = string.len();
}
}

let max_block_len = get_max_block_id_len(function);
let row_len = max_len_of_insts + max_block_len + "block ".len() + 2;
output_string.push_str(&print_divider(row_len));
output_string.push_str(&print_header("GVN Table", row_len));
output_string.push_str(&print_divider(row_len));
for (block_id, inst) in &self.need_remove_insts {
output.push_str(format!("block {}: {}\n", block_id.0, inst.0).as_str());
output_string.push_str(&print_header(
format!(
"block {}: {}",
block_id.0,
inst_map_string.get(inst).unwrap()
)
.as_str(),
row_len,
));
}
output
output_string.push_str(&print_divider(row_len));
output_string
}
}
7 changes: 7 additions & 0 deletions compilers/rustyc/optimizer/src/ir_optimizer/pass/gvn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct GVNPass<'a> {
cache_inst_table: ScopeInstCacheTable,
/// Post process cache to reomve inst.
need_remove_insts: Vec<(BasicBlock, Instruction)>,
/// Cache for debugger
cache_inst_strings: HashMap<Instruction, String>,
}

impl<'a> OptimizerPass for GVNPass<'a> {
Expand All @@ -41,6 +43,7 @@ impl<'a> GVNPass<'a> {
replaceable_value_table: ScopeReplaceValueCacheTable::new(),
cache_inst_table: ScopeInstCacheTable::new(),
need_remove_insts: Default::default(),
cache_inst_strings: Default::default(),
}
}
/// ## Main Algorithm of GVN
Expand Down Expand Up @@ -87,7 +90,11 @@ impl<'a> GVNPass<'a> {
/// remove inst after gvn traversal.
fn remove_redundant_insts(&mut self, function: &mut Function) {
for (block_id, inst) in &self.need_remove_insts {
let mut string = String::new();
function.print_inst(&mut string, function.instructions.get(inst).unwrap());
string = string.trim().to_string();
function.remove_inst_from_block(&block_id, &inst);
self.cache_inst_strings.insert(inst.clone(), string);
}
}
/// ## Rewrite phi is meanless or redundant,
Expand Down
12 changes: 8 additions & 4 deletions compilers/rustyc/optimizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ fn main() {
// println!("{:#?}", program);
// let mut converter = Converter::new();
// let module = converter.convert(&program);
let func = create_gvn_graph_from_conrnell();
let mut use_def_anaylsier = DFSOrdering::new();
let table = use_def_anaylsier.anaylsis(&func);
let out = use_def_anaylsier.debugger(&func, &table);
let mut func = create_gvn_graph_from_conrnell();
let dom_table = {
let mut anaylsis = DomAnaylsier::new();
anaylsis.anaylsis(&func)
};
let mut use_def_anaylsier = GVNPass::new(&dom_table);
use_def_anaylsier.process(&mut func);
let out = use_def_anaylsier.debugger(&func);
// let mut lcm_pass = LCMPass::new();
// lcm_pass.process(&mut func);
// let mut file = File::create("./test1.txt").unwrap();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
|--------------------------------------|
| GVN Table |
|--------------------------------------|
| block 2: t11 = add t3 t4 |
| block 2: t12 = add t3 t4 |
| block 3: t13 = add t1 t2 |
| block 3: t14 = add t5 t6 |
| block 3: t15 = add t5 t6 |
|block 4: phi t16, block2 t7, block3 t7|
|block 4: phi t18, block2 t8, block3 t9|
| block 4: t20 = add t1 t2 |
|--------------------------------------|
39 changes: 26 additions & 13 deletions compilers/rustyc/optimizer/tests/ir_optimizer_test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use rustyc_optimizer::ir_optimizer::anaylsis::dfs_ordering::DFSOrdering;
use rustyc_optimizer::ir_optimizer::anaylsis::domtree::DomAnaylsier;
use rustyc_optimizer::ir_optimizer::anaylsis::use_def_chain::UseDefAnaylsier;
use rustyc_optimizer::ir_optimizer::anaylsis::{DebuggerAnaylsis, OptimizerAnaylsis};
use rustyc_optimizer::ir_optimizer::pass::gvn::GVNPass;
use rustyc_optimizer::ir_optimizer::pass::lcm::LCMPass;
use rustyc_optimizer::ir_optimizer::pass::{DebuggerPass, OptimizerPass};
use std::{env, fs::read_to_string, path::PathBuf};
Expand All @@ -16,16 +17,14 @@ fn get_root_path() -> PathBuf {
.join("./tests/fixtures/ir_optimizer")
}

fn run_ir_pass_test_case(
suffix_path: &'static str,
mut function: Function,
mut pass: impl OptimizerPass + DebuggerPass,
) {
fn run_ir_pass_test_case<F>(suffix_path: &'static str, get_result: F)
where
F: FnOnce() -> String,
{
let table_path = get_root_path().join(suffix_path).join("output.txt");
let expect_table =
read_to_string(table_path).expect("[Internal Error]: Pass Table is not exist.");
pass.process(&mut function);
let result_string = pass.debugger(&function);
let result_string = get_result();
assert_eq!(expect_table, result_string);
}

Expand All @@ -44,13 +43,13 @@ fn run_ir_anaylsis_test_case<T>(
macro_rules! generate_pass_cases {
(
$(
($func_name: ident, $suffix_path: expr, $function: expr, $pass: expr)
($func_name: ident, $suffix_path: expr, $get_result: expr)
),*
) => {
$(
#[test]
fn $func_name() {
run_ir_pass_test_case($suffix_path, $function, $pass )
run_ir_pass_test_case($suffix_path, $get_result)
}
)*
};
Expand All @@ -70,11 +69,25 @@ macro_rules! generate_anaylsis_cases {
};
}

generate_pass_cases!((test_lcm_pass_cmu_example, "./lcm/cmu_example", || {
let mut func = create_lcm_test_graph();
let mut pass = LCMPass::new();
pass.process(&mut func);

pass.debugger(&func)
}));

generate_pass_cases!((
test_lcm_pass_cmu_example,
"./lcm/cmu_example",
create_lcm_test_graph(),
LCMPass::new()
test_gvn_pass_conrnell_example,
"./gvn/conrnell_example",
|| {
let mut dom_anaylsier = DomAnaylsier::new();
let mut func = create_gvn_graph_from_conrnell();
let table = dom_anaylsier.anaylsis(&func);
let mut pass = GVNPass::new(&table);
pass.process(&mut func);
pass.debugger(&func)
}
));

generate_anaylsis_cases!((
Expand Down

0 comments on commit a78aeac

Please sign in to comment.