Skip to content

Commit d822390

Browse files
committed
Get load_store optimization working
1 parent 3bf6c86 commit d822390

2 files changed

Lines changed: 11 additions & 26 deletions

File tree

zjit/src/hir.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,7 +1184,7 @@ impl Insn {
11841184
Insn::LoadSelf { .. } => Effect::read_write(abstract_heaps::Frame, abstract_heaps::Empty),
11851185
Insn::LoadField { .. } => Effect::read_write(abstract_heaps::Memory, abstract_heaps::Empty),
11861186
Insn::StoreField { .. } => effects::Any,
1187-
Insn::WriteBarrier { .. } => effects::Any,
1187+
Insn::WriteBarrier { .. } => effects::Allocator,
11881188
Insn::SetLocal { .. } => effects::Any,
11891189
Insn::GetSpecialSymbol { .. } => effects::Any,
11901190
Insn::GetSpecialNumber { .. } => effects::Any,
@@ -1285,6 +1285,9 @@ impl Insn {
12851285
/// Note: These are restrictions on the `write` `EffectSet` only. Even instructions with
12861286
/// `read: effects::Any` could potentially be omitted.
12871287
fn is_elidable(&self) -> bool {
1288+
if let Insn::WriteBarrier { .. } = self {
1289+
return false
1290+
}
12881291
abstract_heaps::Allocator.includes(self.effects_of().write_bits())
12891292
}
12901293
}
@@ -4616,41 +4619,35 @@ impl Function {
46164619
// The key for the hashmap should be type and offset, with a value of value
46174620
// 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
46184621
// So... how do we match against and store the enum label without all the data?? not sure yet :/
4619-
eprintln!("{}", FunctionPrinter::with_snapshot(self));
46204622
let mut compile_time_heap: HashMap<(InsnId, i32), InsnId> = HashMap::new();
46214623
for block in self.rpo() {
46224624
let old_insns = std::mem::take(&mut self.blocks[block.0].insns);
46234625
let mut new_insns = vec![];
46244626
for insn_id in old_insns {
46254627
let replacement_insn: InsnId = match self.find(insn_id) {
46264628
Insn::StoreField { recv, offset, val, .. } => {
4627-
let key = (recv, offset);
4628-
eprintln!("hi jane");
4629+
let key = (self.chase_insn(recv), offset);
46294630
let heap_entry = compile_time_heap.get(&key).copied();
4630-
eprintln!("{heap_entry:?}");
46314631
// TODO(Jacob): Switch from actual to partial equality
46324632
if Some(val) == heap_entry {
4633-
eprintln!("Matched value {val}, {heap_entry:?}");
46344633
// TODO(Jacob): Add TBAA to avoid removing so many entries
4635-
eprintln!("Erasing aliasing offsets {offset}");
46364634
compile_time_heap.retain(|(_, off), _| *off != offset);
46374635
// If the value is already stored, short circuit and don't add an instruction to the block
46384636
continue
46394637
}
46404638
// TODO(Jacob): Add TBAA to avoid removing so many entries
4641-
eprintln!("Erasing aliasing offsets {offset}");
46424639
compile_time_heap.retain(|(_, off), _| *off != offset);
46434640
compile_time_heap.insert(key, val);
4644-
eprintln!("Inserted into heap {key:?}, {val}");
46454641
insn_id
46464642
},
46474643
Insn::LoadField { recv, offset, .. } => {
4648-
let key = (recv, offset);
4644+
let key = (self.chase_insn(recv), offset);
46494645
match compile_time_heap.entry(key) {
46504646
std::collections::hash_map::Entry::Occupied(entry) => {
46514647
// If the value is already saved, we can't short circuit like we can with a store.
46524648
// However, we can avoid the load with a reference to the representative to the union from SSA.
46534649
self.make_equal_to(insn_id, *entry.get());
4650+
continue
46544651
}
46554652
std::collections::hash_map::Entry::Vacant(_) => {
46564653
// TODO(Jacob): Make sure this is correct, could be wrong?
@@ -4662,7 +4659,6 @@ impl Function {
46624659
insn => {
46634660
// If an instruction affects memory and we haven't modeled it, the compile_time_heap is invalidated
46644661
if insn.effects_of().includes(Effect::write(abstract_heaps::Memory)) {
4665-
eprintln!("Clearing... {insn:?}");
46664662
compile_time_heap.clear();
46674663
}
46684664
insn_id

zjit/src/hir/opt_tests.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5205,8 +5205,6 @@ mod hir_opt_tests {
52055205
v14:HeapBasicObject = RefineType v6, HeapBasicObject
52065206
v17:Fixnum[2] = Const Value(2)
52075207
PatchPoint SingleRactorMode
5208-
v36:CShape = LoadField v14, :_shape_id@0x1000
5209-
v37:CShape[0x1003] = GuardBitEquals v36, CShape(0x1003)
52105208
StoreField v14, :@bar@0x1004, v17
52115209
WriteBarrier v14, v17
52125210
v40:CShape[0x1005] = Const CShape(0x1005)
@@ -8761,7 +8759,7 @@ mod hir_opt_tests {
87618759
end
87628760
test([])
87638761
");
8764-
assert_snapshot!(hir_string("test"), @"
8762+
assert_snapshot!(hir_string("test"), @r"
87658763
fn test@<compiled>:3:
87668764
bb1():
87678765
EntryPoint interpreter
@@ -8782,8 +8780,7 @@ mod hir_opt_tests {
87828780
v33:ArrayExact = GuardType v10, ArrayExact
87838781
v34:CUInt64 = LoadField v33, :_rbasic_flags@0x1040
87848782
v35:CUInt64 = GuardNoBitsSet v34, RUBY_FL_FREEZE=CUInt64(2048)
8785-
v36:CUInt64 = LoadField v33, :_rbasic_flags@0x1040
8786-
v37:CUInt64 = GuardNoBitsSet v36, RUBY_ELTS_SHARED=CUInt64(4096)
8783+
v37:CUInt64 = GuardNoBitsSet v34, RUBY_ELTS_SHARED=CUInt64(4096)
87878784
v38:CInt64[1] = UnboxFixnum v17
87888785
v39:CInt64 = ArrayLength v33
87898786
v40:CInt64[1] = GuardLess v38, v39
@@ -8805,7 +8802,7 @@ mod hir_opt_tests {
88058802
end
88068803
test([], 0, 1)
88078804
");
8808-
assert_snapshot!(hir_string("test"), @"
8805+
assert_snapshot!(hir_string("test"), @r"
88098806
fn test@<compiled>:3:
88108807
bb1():
88118808
EntryPoint interpreter
@@ -8829,8 +8826,7 @@ mod hir_opt_tests {
88298826
v38:Fixnum = GuardType v15, Fixnum
88308827
v39:CUInt64 = LoadField v37, :_rbasic_flags@0x1040
88318828
v40:CUInt64 = GuardNoBitsSet v39, RUBY_FL_FREEZE=CUInt64(2048)
8832-
v41:CUInt64 = LoadField v37, :_rbasic_flags@0x1040
8833-
v42:CUInt64 = GuardNoBitsSet v41, RUBY_ELTS_SHARED=CUInt64(4096)
8829+
v42:CUInt64 = GuardNoBitsSet v39, RUBY_ELTS_SHARED=CUInt64(4096)
88348830
v43:CInt64 = UnboxFixnum v38
88358831
v44:CInt64 = ArrayLength v37
88368832
v45:CInt64 = GuardLess v43, v44
@@ -13515,17 +13511,13 @@ mod hir_opt_tests {
1351513511
v14:HeapBasicObject = RefineType v6, HeapBasicObject
1351613512
v17:Fixnum[2] = Const Value(2)
1351713513
PatchPoint SingleRactorMode
13518-
v50:CShape = LoadField v14, :_shape_id@0x1000
13519-
v51:CShape[0x1003] = GuardBitEquals v50, CShape(0x1003)
1352013514
StoreField v14, :@b@0x1004, v17
1352113515
WriteBarrier v14, v17
1352213516
v54:CShape[0x1005] = Const CShape(0x1005)
1352313517
StoreField v14, :_shape_id@0x1000, v54
1352413518
v21:HeapBasicObject = RefineType v14, HeapBasicObject
1352513519
v24:Fixnum[3] = Const Value(3)
1352613520
PatchPoint SingleRactorMode
13527-
v57:CShape = LoadField v21, :_shape_id@0x1000
13528-
v58:CShape[0x1005] = GuardBitEquals v57, CShape(0x1005)
1352913521
StoreField v21, :@c@0x1006, v24
1353013522
WriteBarrier v21, v24
1353113523
v61:CShape[0x1007] = Const CShape(0x1007)
@@ -13735,9 +13727,6 @@ mod hir_opt_tests {
1373513727
v20:HeapBasicObject = RefineType v8, HeapBasicObject
1373613728
PatchPoint NoEPEscape(initialize)
1373713729
PatchPoint SingleRactorMode
13738-
v43:CShape = LoadField v20, :_shape_id@0x1000
13739-
v44:CShape[0x1003] = GuardBitEquals v43, CShape(0x1003)
13740-
StoreField v20, :@a@0x1002, v13
1374113730
WriteBarrier v20, v13
1374213731
CheckInterrupts
1374313732
Return v13

0 commit comments

Comments
 (0)