Skip to content

Commit 5032b9c

Browse files
committed
Clean up for PR review
1 parent 5373375 commit 5032b9c

2 files changed

Lines changed: 155 additions & 15 deletions

File tree

zjit/src/hir.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4487,18 +4487,7 @@ impl Function {
44874487
self.infer_types();
44884488
}
44894489

4490-
// struct Heap {
4491-
// fn load(obj, offset)
4492-
// fn kill(obj, offset)
4493-
// fn store(obj, offset, val)
4494-
// }
4495-
44964490
fn optimize_load_store(&mut self) {
4497-
// TODO: Add specific tests for load_store
4498-
// TODO: Add dead store elimination
4499-
// The key for the hashmap should be type and offset, with a value of value
4500-
// 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
4501-
// So... how do we match against and store the enum label without all the data?? not sure yet :/
45024491
let mut compile_time_heap: HashMap<(InsnId, i32), InsnId> = HashMap::new();
45034492
for block in self.rpo() {
45044493
let old_insns = std::mem::take(&mut self.blocks[block.0].insns);
@@ -4524,13 +4513,12 @@ impl Function {
45244513
let key = (self.chase_insn(recv), offset);
45254514
match compile_time_heap.entry(key) {
45264515
std::collections::hash_map::Entry::Occupied(entry) => {
4527-
// If the value is already saved, we can't short circuit like we can with a store.
4528-
// However, we can avoid the load with a reference to the representative to the union from SSA.
4516+
// If the value is stored already, we should short circuit.
4517+
// However, we need to replace insn_id with its representative in the SSA union.
45294518
self.make_equal_to(insn_id, *entry.get());
45304519
continue
45314520
}
45324521
std::collections::hash_map::Entry::Vacant(_) => {
4533-
// TODO(Jacob): Make sure this is correct, could be wrong?
45344522
compile_time_heap.insert(key, insn_id);
45354523
}
45364524
}

zjit/src/hir/opt_tests.rs

Lines changed: 153 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13196,7 +13196,7 @@ mod hir_opt_tests {
1319613196
}
1319713197

1319813198
#[test]
13199-
fn test_double_store_removal() {
13199+
fn test_double_store_with_removal() {
1320013200
eval("
1320113201
class C
1320213202
def initialize
@@ -13238,4 +13238,156 @@ mod hir_opt_tests {
1323813238
Return v13
1323913239
");
1324013240
}
13241+
13242+
#[test]
13243+
fn test_double_store_no_removal_with_alias_between() {
13244+
eval("
13245+
class C
13246+
def initialize
13247+
a = 1
13248+
@a = a
13249+
@b = a
13250+
@a = a
13251+
end
13252+
end
13253+
13254+
C.new
13255+
");
13256+
assert_snapshot!(hir_string_proc("C.instance_method(:initialize)"), @r"
13257+
fn initialize@<compiled>:4:
13258+
bb1():
13259+
EntryPoint interpreter
13260+
v1:BasicObject = LoadSelf
13261+
v2:NilClass = Const Value(nil)
13262+
Jump bb3(v1, v2)
13263+
bb2():
13264+
EntryPoint JIT(0)
13265+
v5:BasicObject = LoadArg :self@0
13266+
v6:NilClass = Const Value(nil)
13267+
Jump bb3(v5, v6)
13268+
bb3(v8:BasicObject, v9:NilClass):
13269+
v13:Fixnum[1] = Const Value(1)
13270+
PatchPoint SingleRactorMode
13271+
v43:HeapBasicObject = GuardType v8, HeapBasicObject
13272+
v44:CShape = LoadField v43, :_shape_id@0x1000
13273+
v45:CShape[0x1001] = GuardBitEquals v44, CShape(0x1001)
13274+
StoreField v43, :@a@0x1002, v13
13275+
WriteBarrier v43, v13
13276+
v48:CShape[0x1003] = Const CShape(0x1003)
13277+
StoreField v43, :_shape_id@0x1000, v48
13278+
v20:HeapBasicObject = RefineType v8, HeapBasicObject
13279+
PatchPoint NoEPEscape(initialize)
13280+
PatchPoint SingleRactorMode
13281+
StoreField v20, :@b@0x1004, v13
13282+
WriteBarrier v20, v13
13283+
v55:CShape[0x1005] = Const CShape(0x1005)
13284+
StoreField v20, :_shape_id@0x1000, v55
13285+
v28:HeapBasicObject = RefineType v20, HeapBasicObject
13286+
PatchPoint NoEPEscape(initialize)
13287+
PatchPoint SingleRactorMode
13288+
WriteBarrier v28, v13
13289+
CheckInterrupts
13290+
Return v13
13291+
");
13292+
}
13293+
13294+
#[test]
13295+
fn test_double_store_with_removal_and_insns_between() {
13296+
eval("
13297+
class C
13298+
def initialize
13299+
a = 1
13300+
@a = a
13301+
b = 5
13302+
b += a
13303+
@a = a
13304+
end
13305+
end
13306+
13307+
C.new
13308+
");
13309+
assert_snapshot!(hir_string_proc("C.instance_method(:initialize)"), @r"
13310+
fn initialize@<compiled>:4:
13311+
bb1():
13312+
EntryPoint interpreter
13313+
v1:BasicObject = LoadSelf
13314+
v2:NilClass = Const Value(nil)
13315+
v3:NilClass = Const Value(nil)
13316+
Jump bb3(v1, v2, v3)
13317+
bb2():
13318+
EntryPoint JIT(0)
13319+
v6:BasicObject = LoadArg :self@0
13320+
v7:NilClass = Const Value(nil)
13321+
v8:NilClass = Const Value(nil)
13322+
Jump bb3(v6, v7, v8)
13323+
bb3(v10:BasicObject, v11:NilClass, v12:NilClass):
13324+
v16:Fixnum[1] = Const Value(1)
13325+
PatchPoint SingleRactorMode
13326+
v49:HeapBasicObject = GuardType v10, HeapBasicObject
13327+
v50:CShape = LoadField v49, :_shape_id@0x1000
13328+
v51:CShape[0x1001] = GuardBitEquals v50, CShape(0x1001)
13329+
StoreField v49, :@a@0x1002, v16
13330+
WriteBarrier v49, v16
13331+
v54:CShape[0x1003] = Const CShape(0x1003)
13332+
StoreField v49, :_shape_id@0x1000, v54
13333+
v23:HeapBasicObject = RefineType v10, HeapBasicObject
13334+
v26:Fixnum[5] = Const Value(5)
13335+
PatchPoint NoEPEscape(initialize)
13336+
PatchPoint MethodRedefined(Integer@0x1008, +@0x1010, cme:0x1018)
13337+
v65:Fixnum[6] = Const Value(6)
13338+
IncrCounter inline_cfunc_optimized_send_count
13339+
PatchPoint SingleRactorMode
13340+
WriteBarrier v23, v16
13341+
CheckInterrupts
13342+
Return v16
13343+
");
13344+
}
13345+
13346+
#[test]
13347+
fn test_triple_store_with_removal() {
13348+
eval("
13349+
class C
13350+
def initialize
13351+
a = 1
13352+
@a = a
13353+
@a = a
13354+
@a = a
13355+
end
13356+
end
13357+
13358+
C.new
13359+
");
13360+
assert_snapshot!(hir_string_proc("C.instance_method(:initialize)"), @r"
13361+
fn initialize@<compiled>:4:
13362+
bb1():
13363+
EntryPoint interpreter
13364+
v1:BasicObject = LoadSelf
13365+
v2:NilClass = Const Value(nil)
13366+
Jump bb3(v1, v2)
13367+
bb2():
13368+
EntryPoint JIT(0)
13369+
v5:BasicObject = LoadArg :self@0
13370+
v6:NilClass = Const Value(nil)
13371+
Jump bb3(v5, v6)
13372+
bb3(v8:BasicObject, v9:NilClass):
13373+
v13:Fixnum[1] = Const Value(1)
13374+
PatchPoint SingleRactorMode
13375+
v43:HeapBasicObject = GuardType v8, HeapBasicObject
13376+
v44:CShape = LoadField v43, :_shape_id@0x1000
13377+
v45:CShape[0x1001] = GuardBitEquals v44, CShape(0x1001)
13378+
StoreField v43, :@a@0x1002, v13
13379+
WriteBarrier v43, v13
13380+
v48:CShape[0x1003] = Const CShape(0x1003)
13381+
StoreField v43, :_shape_id@0x1000, v48
13382+
v20:HeapBasicObject = RefineType v8, HeapBasicObject
13383+
PatchPoint NoEPEscape(initialize)
13384+
PatchPoint SingleRactorMode
13385+
WriteBarrier v20, v13
13386+
v28:HeapBasicObject = RefineType v20, HeapBasicObject
13387+
StoreField v28, :@a@0x1002, v13
13388+
WriteBarrier v28, v13
13389+
CheckInterrupts
13390+
Return v13
13391+
");
13392+
}
1324113393
}

0 commit comments

Comments
 (0)