Skip to content

Commit 933af65

Browse files
committed
fix: have Instance as Cache set key
close #42 close #40
1 parent 8fd726c commit 933af65

File tree

3 files changed

+19
-45
lines changed

3 files changed

+19
-45
lines changed

src/functions/cache.rs

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ use super::utils::{SourceCode, source_code_with};
66
use rustc_data_structures::fx::FxHashMap;
77
use rustc_middle::ty::TyCtxt;
88
use rustc_span::source_map::{SourceMap, get_source_map};
9-
use stable_mir::{
10-
CrateDef, DefId,
11-
mir::{Body, mono::Instance},
12-
};
9+
use stable_mir::mir::{Body, mono::Instance};
1310
use std::{cell::RefCell, cmp::Ordering, sync::Arc};
1411

1512
thread_local! {
@@ -45,39 +42,20 @@ pub fn get_source_code(inst: &Instance) -> Option<SourceCode> {
4542
get_cache_func(inst, |cf| cf.src.clone())
4643
}
4744

48-
/// NOTE: this function doesn't auto insert SourceCode if Instance isn't handled.
49-
pub fn share_same_source_code(a: &Instance, b: &Instance) -> bool {
50-
get_cache(|cache| {
51-
let defid_a = a.def.def_id();
52-
let defid_b = b.def.def_id();
53-
let src_a = cache.set.get(&defid_a);
54-
let src_b = cache.set.get(&defid_b);
55-
match (src_a, src_b) {
56-
(Some(cache_a), Some(cache_b)) => match (cache_a, cache_b) {
57-
(Some(func_a), Some(func_b)) => func_a.src == func_b.src,
58-
(None, None) => defid_a == defid_b,
59-
(None, Some(_)) => false,
60-
(Some(_), None) => false,
61-
},
62-
(None, None) => defid_a == defid_b,
63-
(None, Some(_)) => false,
64-
(Some(_), None) => false,
65-
}
66-
})
67-
}
68-
6945
pub fn cmp_callees(a: &Instance, b: &Instance) -> Ordering {
7046
get_cache(|cache| {
7147
cache.get_or_insert(a);
7248
cache.get_or_insert(b);
73-
let func_a = cache.set.get(&a.def.def_id()).unwrap().as_ref().map(|f| &f.src);
74-
let func_b = cache.set.get(&b.def.def_id()).unwrap().as_ref().map(|f| &f.src);
49+
let func_a = cache.set.get(a).unwrap().as_ref().map(|f| &f.src);
50+
let func_b = cache.set.get(b).unwrap().as_ref().map(|f| &f.src);
7551
func_a.cmp(&func_b)
7652
})
7753
}
7854

7955
struct Cache {
80-
set: FxHashMap<DefId, Option<CacheFunction>>,
56+
/// The reason to have Instance as the key is
57+
/// https://github.com/os-checker/distributed-verification/issues/42
58+
set: FxHashMap<Instance, Option<CacheFunction>>,
8159
rustc: Option<RustcCxt>,
8260
path_prefixes: PathPrefixes,
8361
}
@@ -91,7 +69,7 @@ impl Cache {
9169

9270
fn get_or_insert(&mut self, inst: &Instance) -> Option<&CacheFunction> {
9371
self.set
94-
.entry(inst.def.def_id())
72+
.entry(*inst)
9573
.or_insert_with(|| {
9674
let body = inst.body()?;
9775
let rustc = self.rustc.as_ref()?;

src/functions/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use indexmap::{IndexMap, IndexSet};
1+
use indexmap::IndexSet;
22
use kani::{CallGraph, KANI_TOOL_ATTRS, collect_reachable_items};
33
use rustc_middle::ty::TyCtxt;
44
use stable_mir::{
5-
CrateDef, DefId,
5+
CrateDef,
66
crate_def::Attribute,
77
mir::mono::{Instance, MonoItem},
88
};
@@ -53,7 +53,7 @@ pub struct Function {
5353

5454
/// Recursive fnction calls inside the body.
5555
/// The elements are sorted by file path and fn source code to keep hash value stable.
56-
callees: Vec<Instance>,
56+
callees: IndexSet<Instance>,
5757
}
5858

5959
impl Function {
@@ -75,11 +75,7 @@ impl Function {
7575

7676
let mut callees = IndexSet::new();
7777
callgraph.recursive_callees(item, &mut callees);
78-
79-
// Multiple instances may share the same defid (or rather SourceCode), so deduplicate them.
80-
let mut callees: Vec<_> = callees.into_iter().collect();
8178
callees.sort_by(cache::cmp_callees);
82-
callees.dedup_by(|a, b| cache::share_same_source_code(a, b));
8379

8480
let this = Function { instance, attrs, callees };
8581
filter(&this).then_some(this)

tests/compare.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::fs::{copy, remove_file};
22

33
mod utils;
4-
use utils::*;
4+
use utils::{assert_eq, *};
55

66
fn get(text: &str, start: &str) -> SerFunction {
77
let json = &text[text.find("[\n").unwrap()..];
@@ -30,14 +30,14 @@ fn compare(tmp: &str, v_file: &[&str], f: &str) {
3030
// the hash value must be the same.
3131
for i in 0..len - 1 {
3232
for j in 1..len {
33-
// assert_eq!(
34-
// v_func[i].hash, v_func[j].hash,
35-
// "Hash values of {f:?} are not equal: {} ≠ {}",
36-
// v_file[i], v_file[j]
37-
// );
38-
if v_func[i].hash == v_func[j].hash {
39-
println!("Hash values of {f:?} are not equal: {} ≠ {}", v_file[i], v_file[j]);
40-
}
33+
assert_eq!(
34+
v_func[i].hash, v_func[j].hash,
35+
"Hash values of {f:?} are not equal: {} ≠ {}",
36+
v_file[i], v_file[j]
37+
);
38+
// if v_func[i].hash == v_func[j].hash {
39+
// println!("Hash values of {f:?} are not equal: {} ≠ {}", v_file[i], v_file[j]);
40+
// }
4141
}
4242
}
4343
}

0 commit comments

Comments
 (0)