Skip to content

Commit

Permalink
feat: add a simple test case for licm from cmu letcure
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Sep 24, 2024
1 parent ac1ce55 commit 457ed01
Show file tree
Hide file tree
Showing 6 changed files with 213 additions and 17 deletions.
125 changes: 109 additions & 16 deletions compilers/rustyc/optimizer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,20 @@ fn anaylsiser_example() {

#[allow(dead_code)]
fn optimizer_example() {
// let mut fun = create_gvn_graph_from_conrnell();
// let mut file1 = File::create("./before.txt").unwrap();
// write!(file1, "{}", fun.print_to_string()).unwrap();
// let mut dom = DomAnaylsier::new();
// let dom_table = dom.anaylsis(&fun);

// let mut use_def = UseDefAnaylsier::new();
// let use_def_table = use_def.anaylsis(&fun);
// let mut pass = GVNPass::new( &dom_table);
// pass.process(&mut fun);

// write_string_to_file(pass.debugger(&fun));
// let mut file1 = File::create("./after.txt").unwrap();
// write!(file1, "{}", fun.print_to_string()).unwrap();
let mut fun = create_licm_graph_simple_example_from_cmu();
let mut file1 = File::create("./before.txt").unwrap();
write!(file1, "{}", fun.print_to_string()).unwrap();
let mut dom = DomAnaylsier::new();
let dom_table = dom.anaylsis(&fun);

let mut use_def = UseDefAnaylsier::new();
let use_def_table = use_def.anaylsis(&fun);
let mut pass = LICMPass::new(&use_def_table, &dom_table);
pass.process(&mut fun);

write_string_to_file(pass.debugger(&fun));
let mut file1 = File::create("./after.txt").unwrap();
write!(file1, "{}", fun.print_to_string()).unwrap();
}

fn create_simple_loop() -> Function {
Expand Down Expand Up @@ -161,6 +161,99 @@ pub fn create_dom_graph_example() -> Function {

fn main() {
// converter_example();
// optimizer_example();
anaylsiser_example();
optimizer_example();
// anaylsiser_example();
}


pub fn create_licm_graph_simple_example_from_cmu() -> Function {
let mut function = Function::new(String::from("test_fun"));
// create blocks
let b0 = function.create_block(); // entry
function.mark_as_entry(b0);
let b1 = function.create_block(); // header
let b2 = function.create_block();
let b3 = function.create_block();
function.mark_as_exit(b3);
// connect blocks
function.connect_block(b0, b1);
function.connect_block(b1, b2);
function.connect_block(b2, b3);
function.connect_block(b2, b2);
// instruction
function.switch_to_block(b0);
// entry
let i16_10 = function.create_i16_const(10);
let a = function.build_mov_inst(i16_10);
let b = function.build_mov_inst(i16_10);
let c = function.build_mov_inst(i16_10);
function.build_jump_inst(b1);
// header
function.switch_to_block(b1);
function.build_jump_inst(b2);
function.switch_to_block(b2);
let a_inner = function.build_add_inst(b, c);
let e = function.add_register(IrValueType::I16);
function.insert_inst_to_block_front(
&b2,
ir::instructions::InstructionData::Phi { opcode: ir::instructions::OpCode::Phi, dst: e , from: vec![(b2, a_inner), (b1, a)] }
);
// exit
function.switch_to_block(b3);
function.build_ret_inst(None);

function
}

pub fn create_licm_graph_example_from_cmu() -> Function {
let mut function = Function::new(String::from("test_fun"));
// create blocks
let b0 = function.create_block(); // entry
function.mark_as_entry(b0);
let b1 = function.create_block(); // header
let b2 = function.create_block();
let b3 = function.create_block();
let b4 = function.create_block(); // tail
let b5 = function.create_block(); // exit of loop
let b6 = function.create_block(); // exit
function.mark_as_exit(b6);
// connect
function.connect_block(b0, b1);
function.connect_block(b1, b2);
function.connect_block(b2, b4);
function.connect_block(b4, b1);
function.connect_block(b1, b3);
function.connect_block(b3, b5);
function.connect_block(b3, b4);
function.connect_block(b5, b6);
// instructions
// entry
function.switch_to_block(b0);
let i16_10 = function.create_i16_const(10);
let a = function.build_mov_inst(i16_10);
let b = function.build_mov_inst(i16_10);
let c = function.build_mov_inst(i16_10);
function.build_jump_inst(b1);
// header
function.switch_to_block(b1);
function.build_brif_inst(i16_10, b2, b3);
function.switch_to_block(b2);
let a_1 = function.build_add_inst(b, c);
let i16_2 = function.create_i16_const(2);
let _f = function.build_add_inst(a_1, i16_2);
function.build_jump_inst(b4);
function.switch_to_block(b3);
let e = function.build_mov_inst(i16_10);
function.build_brif_inst(e, b4, b5);
function.switch_to_block(b4);
let a_in_b4 = function.build_phi_inst(vec![(b2, a_1), (b0, a)]);
let i16_1 = function.create_i16_const(1);
let _d = function.build_add_inst(a_in_b4, i16_1);
function.build_jump_inst(b1);
function.switch_to_block(b5);
function.build_jump_inst(b6);
function.switch_to_block(b6);
function.build_ret_inst(None);

function
}
39 changes: 39 additions & 0 deletions compilers/rustyc/optimizer/tests/build_ir_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,45 @@ pub fn create_licm_graph_example_from_cmu() -> Function {
function
}

pub fn create_licm_graph_simple_example_from_cmu() -> Function {
let mut function = Function::new(String::from("test_fun"));
// create blocks
let b0 = function.create_block(); // entry
function.mark_as_entry(b0);
let b1 = function.create_block(); // header
let b2 = function.create_block();
let b3 = function.create_block();
function.mark_as_exit(b3);
// connect blocks
function.connect_block(b0, b1);
function.connect_block(b1, b2);
function.connect_block(b2, b3);
function.connect_block(b2, b2);
// instruction
function.switch_to_block(b0);
// entry
let i16_10 = function.create_i16_const(10);
let a = function.build_mov_inst(i16_10);
let b = function.build_mov_inst(i16_10);
let c = function.build_mov_inst(i16_10);
function.build_jump_inst(b1);
// header
function.switch_to_block(b1);
function.build_jump_inst(b2);
function.switch_to_block(b2);
let a_inner = function.build_add_inst(b, c);
let e = function.add_register(IrValueType::I16);
function.insert_inst_to_block_front(
&b2,
ir::instructions::InstructionData::Phi { opcode: ir::instructions::OpCode::Phi, dst: e , from: vec![(b2, a_inner), (b1, a)] }
);
// exit
function.switch_to_block(b3);
function.build_ret_inst(None);

function
}

pub fn create_simple_loop() -> Function {
let mut function = Function::new(String::from("test_fun"));
let b1 = function.create_block();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function test_fun () -> void {
block1:
t1 = 10
t2 = 10
t3 = 10
jump 2
block2:
jump 5
block3:
phi t5, block3 t4, block2 t1
block4:
ret void
block5:
t4 = add t2 t3
jump 3
}
;; t1 -> i16
;; t2 -> i16
;; t3 -> i16
;; t4 -> i16
;; t5 -> i16
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function test_fun () -> void {
block1:
t1 = 10
t2 = 10
t3 = 10
jump 2
block2:
jump 3
block3:
phi t5, block3 t4, block2 t1
t4 = add t2 t3
block4:
ret void
}
;; t1 -> i16
;; t2 -> i16
;; t3 -> i16
;; t4 -> i16
;; t5 -> i16
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
|---------------|
| Loop 1 |
|---------------|
| Header | BB 3 |
| Tail | BB 3 |
|---------------|
| Blocks | BB 3 |
|---------------|
|---------------|
17 changes: 16 additions & 1 deletion compilers/rustyc/optimizer/tests/ir_optimizer_test_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,22 @@ generate_pass_cases!(
let table = pass.debugger(&fun);
let after = fun.print_to_string();
(before, table, after)
})
}),
(
test_licm_pass_cmu_example_2, "./licm/cmu_example_2", || {
let mut fun = create_licm_graph_simple_example_from_cmu();
let before = fun.print_to_string();
let mut dom = DomAnaylsier::new();
let dom_table = dom.anaylsis(&fun);
let mut use_def = UseDefAnaylsier::new();
let use_def_table = use_def.anaylsis(&fun);
let mut pass = LICMPass::new(&use_def_table, &dom_table);
pass.process(&mut fun);
let table = pass.debugger(&fun);
let after = fun.print_to_string();
(before, table, after)
}
)
);

#[test]
Expand Down

0 comments on commit 457ed01

Please sign in to comment.