duskphantom_middle/transform/
mod.rs1use std::time::Instant;
18
19use anyhow::Result;
20
21use super::Program;
22#[allow(unused)]
23use duskphantom_utils::{cprintln, diff::diff};
24
25pub mod block_fuse;
26pub mod constant_fold;
27pub mod dead_code_elim;
28pub mod func_inline;
29pub mod inst_combine;
30pub mod ldce;
31pub mod licm;
32pub mod load_elim;
33pub mod load_store_elim;
34pub mod loop_depth;
35pub mod loop_optimization;
36pub mod loop_simplify;
37pub mod make_parallel;
38pub mod mem2reg;
39pub mod redundance_elim;
40pub mod sink_code;
41pub mod store_elim;
42pub mod ultimate_pass;
43
44pub trait Transform {
45 fn name() -> String;
46
47 fn get_program_mut(&mut self) -> &mut Program;
48
49 fn run(&mut self) -> Result<bool>;
50
51 #[allow(unused)]
52 fn run_and_log(&mut self) -> Result<bool> {
53 let time_before = Instant::now();
54 let program_before = self.get_program_mut().module.gen_llvm_ir();
55 let changed = self.run()?;
56 let elapsed = time_before.elapsed().as_micros();
57 let program_after = self.get_program_mut().module.gen_llvm_ir();
58 cprintln!(
59 "## Pass {} {}\n\nTime elapsed = {} µs\n\nDiff:\n\n```diff\n{}```\n",
60 Self::name(),
61 if changed { "[CHANGED]" } else { "" },
62 elapsed,
63 diff(&program_before, &program_after)
64 );
65 Ok(changed)
66 }
67}