Skip to content

Commit 3bf6c86

Browse files
committed
Added debug statements in load_store and added a test
1 parent eeaf800 commit 3bf6c86

3 files changed

Lines changed: 66 additions & 3 deletions

File tree

zjit/src/hir.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4604,12 +4604,19 @@ impl Function {
46044604
self.infer_types();
46054605
}
46064606

4607+
// struct Heap {
4608+
// fn load(obj, offset)
4609+
// fn kill(obj, offset)
4610+
// fn store(obj, offset, val)
4611+
// }
4612+
46074613
fn optimize_load_store(&mut self) {
46084614
// TODO: Add specific tests for load_store
46094615
// TODO: Add dead store elimination
46104616
// The key for the hashmap should be type and offset, with a value of value
46114617
// This lets us to index in with both load and store fields since insn_ids are probably always going to be different and we can't easily match on that
46124618
// So... how do we match against and store the enum label without all the data?? not sure yet :/
4619+
eprintln!("{}", FunctionPrinter::with_snapshot(self));
46134620
let mut compile_time_heap: HashMap<(InsnId, i32), InsnId> = HashMap::new();
46144621
for block in self.rpo() {
46154622
let old_insns = std::mem::take(&mut self.blocks[block.0].insns);
@@ -4618,18 +4625,23 @@ impl Function {
46184625
let replacement_insn: InsnId = match self.find(insn_id) {
46194626
Insn::StoreField { recv, offset, val, .. } => {
46204627
let key = (recv, offset);
4621-
4628+
eprintln!("hi jane");
46224629
let heap_entry = compile_time_heap.get(&key).copied();
4630+
eprintln!("{heap_entry:?}");
46234631
// TODO(Jacob): Switch from actual to partial equality
46244632
if Some(val) == heap_entry {
4633+
eprintln!("Matched value {val}, {heap_entry:?}");
46254634
// TODO(Jacob): Add TBAA to avoid removing so many entries
4626-
compile_time_heap.retain(|(_, off), _| *off == offset);
4635+
eprintln!("Erasing aliasing offsets {offset}");
4636+
compile_time_heap.retain(|(_, off), _| *off != offset);
46274637
// If the value is already stored, short circuit and don't add an instruction to the block
46284638
continue
46294639
}
46304640
// TODO(Jacob): Add TBAA to avoid removing so many entries
4631-
compile_time_heap.retain(|(_, off), _| *off == offset);
4641+
eprintln!("Erasing aliasing offsets {offset}");
4642+
compile_time_heap.retain(|(_, off), _| *off != offset);
46324643
compile_time_heap.insert(key, val);
4644+
eprintln!("Inserted into heap {key:?}, {val}");
46334645
insn_id
46344646
},
46354647
Insn::LoadField { recv, offset, .. } => {
@@ -4650,6 +4662,7 @@ impl Function {
46504662
insn => {
46514663
// If an instruction affects memory and we haven't modeled it, the compile_time_heap is invalidated
46524664
if insn.effects_of().includes(Effect::write(abstract_heaps::Memory)) {
4665+
eprintln!("Clearing... {insn:?}");
46534666
compile_time_heap.clear();
46544667
}
46554668
insn_id
@@ -5477,6 +5490,8 @@ impl Function {
54775490
|| ident_equal!($name, optimize_getivar)
54785491
|| ident_equal!($name, optimize_c_calls) {
54795492
Counter::compile_hir_strength_reduce_time_ns
5493+
} else if ident_equal!($name, optimize_load_store) {
5494+
Counter::compile_hir_optimize_load_store_time_ns
54805495
} else if ident_equal!($name, fold_constants) {
54815496
Counter::compile_hir_fold_constants_time_ns
54825497
} else if ident_equal!($name, clean_cfg) {

zjit/src/hir/opt_tests.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13696,4 +13696,51 @@ mod hir_opt_tests {
1369613696
Jump bb8(v67, v94)
1369713697
");
1369813698
}
13699+
13700+
#[test]
13701+
fn test_double_store_removal() {
13702+
eval("
13703+
class C
13704+
def initialize
13705+
a = 1
13706+
@a = a
13707+
@a = a
13708+
end
13709+
end
13710+
13711+
C.new
13712+
");
13713+
assert_snapshot!(hir_string_proc("C.instance_method(:initialize)"), @r"
13714+
fn initialize@<compiled>:4:
13715+
bb1():
13716+
EntryPoint interpreter
13717+
v1:BasicObject = LoadSelf
13718+
v2:NilClass = Const Value(nil)
13719+
Jump bb3(v1, v2)
13720+
bb2():
13721+
EntryPoint JIT(0)
13722+
v5:BasicObject = LoadArg :self@0
13723+
v6:NilClass = Const Value(nil)
13724+
Jump bb3(v5, v6)
13725+
bb3(v8:BasicObject, v9:NilClass):
13726+
v13:Fixnum[1] = Const Value(1)
13727+
PatchPoint SingleRactorMode
13728+
v35:HeapBasicObject = GuardType v8, HeapBasicObject
13729+
v36:CShape = LoadField v35, :_shape_id@0x1000
13730+
v37:CShape[0x1001] = GuardBitEquals v36, CShape(0x1001)
13731+
StoreField v35, :@a@0x1002, v13
13732+
WriteBarrier v35, v13
13733+
v40:CShape[0x1003] = Const CShape(0x1003)
13734+
StoreField v35, :_shape_id@0x1000, v40
13735+
v20:HeapBasicObject = RefineType v8, HeapBasicObject
13736+
PatchPoint NoEPEscape(initialize)
13737+
PatchPoint SingleRactorMode
13738+
v43:CShape = LoadField v20, :_shape_id@0x1000
13739+
v44:CShape[0x1003] = GuardBitEquals v43, CShape(0x1003)
13740+
StoreField v20, :@a@0x1002, v13
13741+
WriteBarrier v20, v13
13742+
CheckInterrupts
13743+
Return v13
13744+
");
13745+
}
1369913746
}

zjit/src/stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ make_counters! {
167167
compile_hir_time_ns,
168168
compile_hir_build_time_ns,
169169
compile_hir_strength_reduce_time_ns,
170+
compile_hir_optimize_load_store_time_ns,
170171
compile_hir_fold_constants_time_ns,
171172
compile_hir_clean_cfg_time_ns,
172173
compile_hir_remove_redundant_patch_points_time_ns,

0 commit comments

Comments
 (0)