rapx/analysis/core/ssa_transform/
mod.rs

1#![allow(unused_imports)]
2#![allow(unused_variables)]
3#![allow(dead_code)]
4#![allow(unused_assignments)]
5#![allow(unused_parens)]
6#![allow(non_snake_case)]
7
8pub mod Replacer;
9pub mod SSATransformer;
10
11use crate::{rap_info, rap_warn};
12use rustc_hir::{
13    def::DefKind,
14    def_id::{DefId, LocalDefId},
15};
16use rustc_middle::{
17    mir::{
18        pretty::{self, MirWriter, PrettyPrintMirOptions},
19        *,
20    },
21    ty::TyCtxt,
22};
23use std::{
24    collections::{HashMap, HashSet},
25    fs::{self, File},
26    io::{self, Cursor, Write},
27    path::PathBuf,
28};
29
30pub struct SSATrans<'tcx> {
31    pub tcx: TyCtxt<'tcx>,
32    pub debug: bool,
33}
34
35impl<'tcx> SSATrans<'tcx> {
36    pub fn new(tcx: TyCtxt<'tcx>, debug: bool) -> Self {
37        Self { tcx: tcx, debug }
38    }
39
40    pub fn start(&mut self) {
41        for local_def_id in self.tcx.iter_local_def_id() {
42            if matches!(self.tcx.def_kind(local_def_id), DefKind::Fn) {
43                if self.tcx.hir_maybe_body_owned_by(local_def_id).is_some() {
44                    if let Some(def_id) = self
45                        .tcx
46                        .hir_body_owners()
47                        .find(|id| self.tcx.def_path_str(*id) == "main")
48                    {
49                        if let Some(ssa_def_id) =
50                            self.tcx.hir_crate_items(()).free_items().find(|id| {
51                                let hir_id = id.hir_id();
52                                if let Some(ident_name) = self.tcx.hir_opt_name(hir_id) {
53                                    ident_name.to_string() == "SSAstmt"
54                                } else {
55                                    false
56                                }
57                            })
58                        {
59                            let ssa_def_id = ssa_def_id.owner_id.to_def_id();
60                            if let Some(essa_def_id) =
61                                self.tcx.hir_crate_items(()).free_items().find(|id| {
62                                    let hir_id = id.hir_id();
63                                    if let Some(ident_name) = self.tcx.hir_opt_name(hir_id) {
64                                        ident_name.to_string() == "ESSAstmt"
65                                    } else {
66                                        false
67                                    }
68                                })
69                            {
70                                let essa_def_id = essa_def_id.owner_id.to_def_id();
71                                self.analyze_mir(self.tcx, def_id, ssa_def_id, essa_def_id);
72                            }
73                        }
74                    }
75                }
76            }
77        }
78    }
79    fn analyze_mir(
80        &mut self,
81        tcx: TyCtxt<'tcx>,
82        def_id: LocalDefId,
83        ssa_def_id: DefId,
84        essa_def_id: DefId,
85    ) {
86        let mut body = tcx.optimized_mir(def_id).clone();
87        {
88            let body_mut_ref: &mut Body<'tcx> = unsafe { &mut *(&mut body as *mut Body<'tcx>) };
89            let mut passrunner = PassRunner::new(tcx);
90            passrunner.run_pass(body_mut_ref, ssa_def_id, essa_def_id);
91            // passrunner.print_diff(body_mut_ref);
92            let essa_mir_string = passrunner.get_final_ssa_as_string(body_mut_ref);
93            // rap_info!("final SSA {:?}\n", &essa_mir_string);
94            rap_info!("ssa lvalue check {:?}", lvalue_check(&essa_mir_string));
95        }
96    }
97}
98pub struct PassRunner<'tcx> {
99    tcx: TyCtxt<'tcx>,
100    pub places_map: HashMap<Place<'tcx>, HashSet<Place<'tcx>>>,
101}
102pub fn lvalue_check(mir_string: &str) -> bool {
103    let re = regex::Regex::new(r"_(\d+)\s*=").unwrap();
104    let mut counts = HashMap::new();
105    let mut has_duplicate = false;
106
107    for cap in re.captures_iter(mir_string) {
108        let var = cap[1].parse::<u32>().unwrap();
109        let counter = counts.entry(var).or_insert(0);
110        *counter += 1;
111        if *counter > 1 {
112            has_duplicate = true;
113        }
114    }
115
116    for (var, count) in counts {
117        if count > 1 {
118            rap_warn!("Variable _ {} is used {} times", var, count);
119        }
120    }
121
122    !has_duplicate
123}
124pub fn print_diff<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) {
125    let dir_path = "ssa_mir";
126    fs::create_dir_all(dir_path).unwrap();
127    // PassRunner::new(self.tcx);
128    let name = tcx.def_path_str(def_id);
129    let mir_file_path = format!("{}/origin_mir.txt", dir_path);
130    let phi_mir_file_path = format!("{}/{}_after_rename_mir.txt", dir_path, name);
131    let mut file = File::create(&mir_file_path).unwrap();
132    let mut w = io::BufWriter::new(&mut file);
133    write_mir_pretty(tcx, None, &mut w).unwrap();
134    let mut file2 = File::create(&phi_mir_file_path).unwrap();
135    let mut w2 = io::BufWriter::new(&mut file2);
136    let writer = pretty::MirWriter::new(tcx);
137    writer.write_mir_fn(body, &mut w2).unwrap();
138}
139pub fn print_mir_graph<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: DefId) {
140    let dir_path = PathBuf::from("passrunner_mir_dot");
141    fs::create_dir_all(dir_path.clone()).unwrap();
142
143    let dot_graph = mir_to_dot(tcx, body);
144    let function_name = tcx.def_path_str(def_id);
145    let safe_filename = format!("{}_after_rename_mir.dot", function_name);
146    let output_path = dir_path.join(format!("{}", safe_filename));
147
148    let mut file = File::create(&output_path).expect("cannot create file");
149    let _ = file.write_all(dot_graph.as_bytes());
150}
151//for f in *.dot; do dot -Tpng "$f" -o "${f%.dot}.png"; done
152fn mir_to_dot<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> String {
153    let mut dot = String::new();
154    dot.push_str("digraph MIR {\n");
155    dot.push_str("  node [shape=box];\n");
156
157    for (bb, bb_data) in body.basic_blocks.iter_enumerated() {
158        let statements_str = bb_data
159            .statements
160            .iter()
161            .filter(|stmt| {
162                !matches!(
163                    stmt.kind,
164                    StatementKind::StorageLive(_) | StatementKind::StorageDead(_)
165                )
166            })
167            .map(|stmt| format!("{:?}", stmt).replace('\n', " "))
168            .collect::<Vec<_>>()
169            .join("\\l");
170
171        let terminator_str = match &bb_data.terminator {
172            Some(term) => match term.kind {
173                TerminatorKind::Assert { .. } => String::new(),
174                _ => format!("{:?}", term.kind).replace("->", "-->"),
175            },
176            None => "NoTerminator".to_string(),
177        };
178
179        let label = format!("{}\\l{}\\l", statements_str, terminator_str);
180
181        dot.push_str(&format!(
182            "  {} [label=\"{:?}:\\l{}\"];\n",
183            bb.index(),
184            bb,
185            label
186        ));
187
188        if let Some(terminator) = &bb_data.terminator {
189            for successor in terminator.successors() {
190                dot.push_str(&format!("  {} -> {};\n", bb.index(), successor.index()));
191            }
192        }
193    }
194
195    dot.push_str("}\n");
196    dot
197}
198
199impl<'tcx> PassRunner<'tcx> {
200    pub fn new(tcx: TyCtxt<'tcx>) -> Self {
201        Self {
202            tcx,
203            places_map: HashMap::default(),
204        }
205    }
206
207    pub fn get_final_ssa_as_string(&self, body: &Body<'tcx>) -> String {
208        let mut buffer2 = Cursor::new(Vec::new());
209        let writer = pretty::MirWriter::new(self.tcx);
210        writer.write_mir_fn(body, &mut buffer2).unwrap();
211        let after_mir = String::from_utf8(buffer2.into_inner()).unwrap();
212        after_mir
213    }
214
215    pub fn run_pass(&mut self, body: &mut Body<'tcx>, ssa_def_id: DefId, essa_def_id: DefId) {
216        let arg_count = body.arg_count;
217        let ssatransformer =
218            SSATransformer::SSATransformer::new(self.tcx, body, ssa_def_id, essa_def_id, arg_count);
219        let mut replacer = Replacer::Replacer {
220            tcx: self.tcx,
221            ssatransformer,
222            new_local_collection: HashSet::default(),
223        };
224        replacer.insert_phi_statment(body);
225        replacer.insert_essa_statement(body);
226        replacer.rename_variables(body);
227        self.places_map = replacer.ssatransformer.places_map.clone();
228    }
229}