Skip to content

Commit a260bbc

Browse files
authored
ZJIT: Set PC before StringCopy (ruby#14141)
ZJIT: Set PC before StringCopy This function allocates.
1 parent 24d0b45 commit a260bbc

2 files changed

Lines changed: 36 additions & 33 deletions

File tree

zjit/src/codegen.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
329329
Insn::NewArray { elements, state } => gen_new_array(asm, opnds!(elements), &function.frame_state(*state)),
330330
Insn::NewRange { low, high, flag, state } => gen_new_range(asm, opnd!(low), opnd!(high), *flag, &function.frame_state(*state)),
331331
Insn::ArrayDup { val, state } => gen_array_dup(asm, opnd!(val), &function.frame_state(*state)),
332-
Insn::StringCopy { val, chilled } => gen_string_copy(asm, opnd!(val), *chilled),
332+
Insn::StringCopy { val, chilled, state } => gen_string_copy(asm, opnd!(val), *chilled, &function.frame_state(*state)),
333333
Insn::Param { idx } => unreachable!("block.insns should not have Insn::Param({idx})"),
334334
Insn::Snapshot { .. } => return Some(()), // we don't need to do anything for this instruction at the moment
335335
Insn::Jump(branch) => return gen_jump(jit, asm, branch),
@@ -871,8 +871,9 @@ fn gen_send_without_block_direct(
871871
}
872872

873873
/// Compile a string resurrection
874-
fn gen_string_copy(asm: &mut Assembler, recv: Opnd, chilled: bool) -> Opnd {
874+
fn gen_string_copy(asm: &mut Assembler, recv: Opnd, chilled: bool, state: &FrameState) -> Opnd {
875875
// TODO: split rb_ec_str_resurrect into separate functions
876+
gen_prepare_call_with_gc(asm, state);
876877
let chilled = if chilled { Opnd::Imm(1) } else { Opnd::Imm(0) };
877878
asm_ccall!(asm, rb_ec_str_resurrect, EC, recv, chilled)
878879
}

zjit/src/hir.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ pub enum Insn {
443443
/// SSA block parameter. Also used for function parameters in the function's entry block.
444444
Param { idx: usize },
445445

446-
StringCopy { val: InsnId, chilled: bool },
446+
StringCopy { val: InsnId, chilled: bool, state: InsnId },
447447
StringIntern { val: InsnId },
448448

449449
/// Put special object (VMCORE, CBASE, etc.) based on value_type
@@ -1116,7 +1116,7 @@ impl Function {
11161116
},
11171117
&Return { val } => Return { val: find!(val) },
11181118
&Throw { throw_state, val } => Throw { throw_state, val: find!(val) },
1119-
&StringCopy { val, chilled } => StringCopy { val: find!(val), chilled },
1119+
&StringCopy { val, chilled, state } => StringCopy { val: find!(val), chilled, state },
11201120
&StringIntern { val } => StringIntern { val: find!(val) },
11211121
&Test { val } => Test { val: find!(val) },
11221122
&IsNil { val } => IsNil { val: find!(val) },
@@ -1870,7 +1870,6 @@ impl Function {
18701870
worklist.push_back(high);
18711871
worklist.push_back(state);
18721872
}
1873-
&Insn::StringCopy { val, .. }
18741873
| &Insn::StringIntern { val }
18751874
| &Insn::Return { val }
18761875
| &Insn::Throw { val, .. }
@@ -1880,6 +1879,7 @@ impl Function {
18801879
| &Insn::IsNil { val } =>
18811880
worklist.push_back(val),
18821881
&Insn::SetGlobal { val, state, .. }
1882+
| &Insn::StringCopy { val, state, .. }
18831883
| &Insn::GuardType { val, state, .. }
18841884
| &Insn::GuardBitEquals { val, state, .. }
18851885
| &Insn::ToArray { val, state }
@@ -2667,12 +2667,14 @@ pub fn iseq_to_hir(iseq: *const rb_iseq_t) -> Result<Function, ParseError> {
26672667
}
26682668
YARVINSN_putstring => {
26692669
let val = fun.push_insn(block, Insn::Const { val: Const::Value(get_arg(pc, 0)) });
2670-
let insn_id = fun.push_insn(block, Insn::StringCopy { val, chilled: false });
2670+
let exit_id = fun.push_insn(block, Insn::Snapshot { state: exit_state });
2671+
let insn_id = fun.push_insn(block, Insn::StringCopy { val, chilled: false, state: exit_id });
26712672
state.stack_push(insn_id);
26722673
}
26732674
YARVINSN_putchilledstring => {
26742675
let val = fun.push_insn(block, Insn::Const { val: Const::Value(get_arg(pc, 0)) });
2675-
let insn_id = fun.push_insn(block, Insn::StringCopy { val, chilled: true });
2676+
let exit_id = fun.push_insn(block, Insn::Snapshot { state: exit_state });
2677+
let insn_id = fun.push_insn(block, Insn::StringCopy { val, chilled: true, state: exit_id });
26762678
state.stack_push(insn_id);
26772679
}
26782680
YARVINSN_putself => { state.stack_push(self_param); }
@@ -3862,8 +3864,8 @@ mod tests {
38623864
fn test@<compiled>:1:
38633865
bb0(v0:BasicObject):
38643866
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
3865-
v3:StringExact = StringCopy v2
3866-
Return v3
3867+
v4:StringExact = StringCopy v2
3868+
Return v4
38673869
"#]]);
38683870
}
38693871

@@ -4390,11 +4392,11 @@ mod tests {
43904392
v5:ArrayExact[VALUE(0x1008)] = Const Value(VALUE(0x1008))
43914393
v7:ArrayExact = ArrayDup v5
43924394
v8:StringExact[VALUE(0x1010)] = Const Value(VALUE(0x1010))
4393-
v9:StringExact = StringCopy v8
4394-
v10:StringExact[VALUE(0x1010)] = Const Value(VALUE(0x1010))
4395-
v11:StringExact = StringCopy v10
4396-
v13:BasicObject = SendWithoutBlock v0, :unknown_method, v4, v7, v9, v11
4397-
Return v13
4395+
v10:StringExact = StringCopy v8
4396+
v11:StringExact[VALUE(0x1010)] = Const Value(VALUE(0x1010))
4397+
v13:StringExact = StringCopy v11
4398+
v15:BasicObject = SendWithoutBlock v0, :unknown_method, v4, v7, v10, v13
4399+
Return v15
43984400
"#]]);
43994401
}
44004402

@@ -4640,7 +4642,7 @@ mod tests {
46404642
v4:NilClass = Const Value(nil)
46414643
v7:BasicObject = SendWithoutBlock v1, :+, v2
46424644
v8:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
4643-
v9:StringExact = StringCopy v8
4645+
v10:StringExact = StringCopy v8
46444646
SideExit UnknownNewarraySend(PACK)
46454647
"#]]);
46464648
}
@@ -5943,8 +5945,8 @@ mod opt_tests {
59435945
assert_optimized_method_hir("test", expect![[r#"
59445946
fn test@<compiled>:3:
59455947
bb0(v0:BasicObject):
5946-
v5:Fixnum[5] = Const Value(5)
5947-
Return v5
5948+
v6:Fixnum[5] = Const Value(5)
5949+
Return v6
59485950
"#]]);
59495951
}
59505952

@@ -6572,10 +6574,10 @@ mod opt_tests {
65726574
fn test@<compiled>:2:
65736575
bb0(v0:BasicObject):
65746576
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
6575-
v3:StringExact = StringCopy v2
6577+
v4:StringExact = StringCopy v2
65766578
PatchPoint MethodRedefined(String@0x1008, bytesize@0x1010, cme:0x1018)
6577-
v8:Fixnum = CCall bytesize@0x1040, v3
6578-
Return v8
6579+
v9:Fixnum = CCall bytesize@0x1040, v4
6580+
Return v9
65796581
"#]]);
65806582
}
65816583

@@ -6918,10 +6920,10 @@ mod opt_tests {
69186920
fn test@<compiled>:2:
69196921
bb0(v0:BasicObject):
69206922
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
6921-
v3:StringExact = StringCopy v2
6922-
v5:BasicObject = SendWithoutBlock v3, :dup
6923-
v7:BasicObject = SendWithoutBlock v5, :freeze
6924-
Return v7
6923+
v4:StringExact = StringCopy v2
6924+
v6:BasicObject = SendWithoutBlock v4, :dup
6925+
v8:BasicObject = SendWithoutBlock v6, :freeze
6926+
Return v8
69256927
"#]]);
69266928
}
69276929

@@ -6934,10 +6936,10 @@ mod opt_tests {
69346936
fn test@<compiled>:2:
69356937
bb0(v0:BasicObject):
69366938
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
6937-
v3:StringExact = StringCopy v2
6938-
v4:NilClass = Const Value(nil)
6939-
v6:BasicObject = SendWithoutBlock v3, :freeze, v4
6940-
Return v6
6939+
v4:StringExact = StringCopy v2
6940+
v5:NilClass = Const Value(nil)
6941+
v7:BasicObject = SendWithoutBlock v4, :freeze, v5
6942+
Return v7
69416943
"#]]);
69426944
}
69436945

@@ -6979,10 +6981,10 @@ mod opt_tests {
69796981
fn test@<compiled>:2:
69806982
bb0(v0:BasicObject):
69816983
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
6982-
v3:StringExact = StringCopy v2
6983-
v5:BasicObject = SendWithoutBlock v3, :dup
6984-
v7:BasicObject = SendWithoutBlock v5, :-@
6985-
Return v7
6984+
v4:StringExact = StringCopy v2
6985+
v6:BasicObject = SendWithoutBlock v4, :dup
6986+
v8:BasicObject = SendWithoutBlock v6, :-@
6987+
Return v8
69866988
"#]]);
69876989
}
69886990

@@ -6996,7 +6998,7 @@ mod opt_tests {
69966998
bb0(v0:BasicObject):
69976999
v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
69987000
v3:StringExact[VALUE(0x1008)] = Const Value(VALUE(0x1008))
6999-
v4:StringExact = StringCopy v3
7001+
v5:StringExact = StringCopy v3
70007002
SideExit UnknownOpcode(concatstrings)
70017003
"#]]);
70027004
}

0 commit comments

Comments
 (0)