duskphantom_middle/transform/
mod.rs

1// Copyright 2024 Duskphantom Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17use 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}