Skip to content

Commit 3f56fde

Browse files
committed
ZJIT: Move string encoding flags loading to HIR
1 parent 1f717ae commit 3f56fde

5 files changed

Lines changed: 101 additions & 27 deletions

File tree

‎zjit/src/codegen.rs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
654654
&Insn::StringGetbyte { string, index } => gen_string_getbyte(asm, opnd!(string), opnd!(index)),
655655
Insn::StringByteslice { string, beg, len, state } => gen_string_byteslice(asm, opnd!(string), opnd!(beg), opnd!(len), &function.frame_state(*state)),
656656
Insn::StringSetbyteFixnum { string, index, value } => gen_string_setbyte_fixnum(asm, opnd!(string), opnd!(index), opnd!(value)),
657-
Insn::StringAppend { recv, other, state } => gen_string_append(jit, asm, function, opnd!(recv), opnd!(other), &function.frame_state(*state)),
657+
Insn::StringAppend { recv, other, recv_flags, other_flags, state } => gen_string_append(jit, asm, function, opnd!(recv), opnd!(other), opnd!(recv_flags), opnd!(other_flags), &function.frame_state(*state)),
658658
Insn::StringAppendCodepoint { recv, other, state } => gen_string_append_codepoint(jit, asm, function, opnd!(recv), opnd!(other), &function.frame_state(*state)),
659659
Insn::StringEqual { left, right } => gen_string_equal(asm, opnd!(left), opnd!(right)),
660660
Insn::StringIntern { val, state } => gen_intern(jit, asm, function, opnd!(val), &function.frame_state(*state)),
@@ -4258,22 +4258,16 @@ fn gen_string_setbyte_fixnum(asm: &mut Assembler, string: Opnd, index: Opnd, val
42584258
asm_ccall!(asm, rb_str_setbyte, string, index, value)
42594259
}
42604260

4261-
fn gen_string_append(jit: &mut JITState, asm: &mut Assembler, function: &Function, string: Opnd, val: Opnd, state: &FrameState) -> Opnd {
4261+
fn gen_string_append(jit: &mut JITState, asm: &mut Assembler, function: &Function, string: Opnd, val: Opnd, recv_flags: Opnd, other_flags: Opnd, state: &FrameState) -> Opnd {
42624262
gen_prepare_non_leaf_call(jit, asm, function, state);
42634263

42644264
// Test if string encodings differ. If different, use rb_str_buf_append. If the same,
42654265
// use rb_jit_str_simple_append, which calls rb_str_cat.
42664266
asm_comment!(asm, "<< on strings");
42674267

4268-
// Take receiver's object flags XOR arg's flags. If any
4269-
// string-encoding flags are different between the two,
4270-
// the encodings don't match.
4271-
let string_reg = asm.load_mem(string);
4272-
let val_reg = asm.load_mem(val);
4273-
let flags_xor = asm.xor(
4274-
Opnd::mem(VALUE_BITS, string_reg, RUBY_OFFSET_RBASIC_FLAGS),
4275-
Opnd::mem(VALUE_BITS, val_reg, RUBY_OFFSET_RBASIC_FLAGS)
4276-
);
4268+
// XOR the object flags. If any string-encoding flags differ between the two,
4269+
// the encodings do not match.
4270+
let flags_xor = asm.xor(recv_flags, other_flags);
42774271
asm.test(flags_xor, Opnd::UImm(RUBY_ENCODING_MASK as u64));
42784272

42794273
let hir_block_id = asm.current_block().hir_block_id;

‎zjit/src/codegen_tests.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4568,6 +4568,26 @@ fn test_string_append_encoding_mismatch() {
45684568
"#), @r#"["éé", "UTF-8", true]"#);
45694569
}
45704570

4571+
#[test]
4572+
fn test_string_append_encoding_mutation_between_appends() {
4573+
eval(r#"
4574+
def test(string, first, second)
4575+
string << first
4576+
string << second
4577+
end
4578+
"#);
4579+
assert_contains_opcode("test", YARVINSN_opt_ltlt);
4580+
assert_snapshot!(assert_compiles(r#"
4581+
string = String.new(encoding: Encoding::BINARY)
4582+
begin
4583+
test(string, "é", "\xFF".b)
4584+
:no_error
4585+
rescue Encoding::CompatibilityError
4586+
[string.bytes, string.encoding.name, string.valid_encoding?]
4587+
end
4588+
"#), @"[[195, 169], \"UTF-8\", true]");
4589+
}
4590+
45714591
#[test]
45724592
fn test_string_append_incompatible_encoding() {
45734593
eval(r#"

‎zjit/src/cruby_methods.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,9 @@ fn inline_string_append(fun: &mut hir::Function, block: hir::BlockId, recv: hir:
590590
if fun.likely_a(recv, types::StringExact, state) && fun.likely_a(other, types::String, state) {
591591
let recv = fun.coerce_to(block, recv, types::StringExact, state);
592592
let other = fun.coerce_to(block, other, types::String, state);
593-
let _ = fun.push_insn(block, hir::Insn::StringAppend { recv, other, state });
593+
let recv_flags = fun.load_rbasic_flags(block, recv);
594+
let other_flags = fun.load_rbasic_flags(block, other);
595+
let _ = fun.push_insn(block, hir::Insn::StringAppend { recv, other, recv_flags, other_flags, state });
594596
return Some(recv);
595597
}
596598
if fun.likely_a(recv, types::StringExact, state) && fun.likely_a(other, types::Fixnum, state) {

‎zjit/src/hir.rs‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,7 +1008,8 @@ pub enum Insn {
10081008
/// Call rb_str_byte_substr with known-Fixnum beg/len
10091009
StringByteslice { string: InsnId, beg: InsnId, len: InsnId, state: InsnId },
10101010
StringSetbyteFixnum { string: InsnId, index: InsnId, value: InsnId },
1011-
StringAppend { recv: InsnId, other: InsnId, state: InsnId },
1011+
/// Append `other` to `recv`. HIR loads both flags for load reuse. Codegen XORs the flags.
1012+
StringAppend { recv: InsnId, other: InsnId, recv_flags: InsnId, other_flags: InsnId, state: InsnId },
10121013
StringAppendCodepoint { recv: InsnId, other: InsnId, state: InsnId },
10131014
StringEqual { left: InsnId, right: InsnId },
10141015

@@ -1441,8 +1442,14 @@ macro_rules! for_each_operand_impl {
14411442
$visit_one!(*index);
14421443
$visit_one!(*value);
14431444
}
1444-
Insn::StringAppend { recv, other, state }
1445-
| Insn::StringAppendCodepoint { recv, other, state } => {
1445+
Insn::StringAppend { recv, other, recv_flags, other_flags, state } => {
1446+
$visit_one!(*recv);
1447+
$visit_one!(*other);
1448+
$visit_one!(*recv_flags);
1449+
$visit_one!(*other_flags);
1450+
$visit_one!(*state);
1451+
}
1452+
Insn::StringAppendCodepoint { recv, other, state } => {
14461453
$visit_one!(*recv);
14471454
$visit_one!(*other);
14481455
$visit_one!(*state);
@@ -2165,8 +2172,8 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
21652172
Insn::StringSetbyteFixnum { string, index, value, .. } => {
21662173
write!(f, "StringSetbyteFixnum {string}, {index}, {value}")
21672174
}
2168-
Insn::StringAppend { recv, other, .. } => {
2169-
write!(f, "StringAppend {recv}, {other}")
2175+
Insn::StringAppend { recv, other, recv_flags, other_flags, .. } => {
2176+
write!(f, "StringAppend {recv}, {other}, recv_flags: {recv_flags}, other_flags: {other_flags}")
21702177
}
21712178
Insn::StringAppendCodepoint { recv, other, .. } => {
21722179
write!(f, "StringAppendCodepoint {recv}, {other}")
@@ -7937,9 +7944,11 @@ impl Function {
79377944
// Instructions with String operands
79387945
Insn::StringCopy { val, .. } => self.assert_subtype(insn_id, val, types::StringExact),
79397946
Insn::StringIntern { val, .. } => self.assert_subtype(insn_id, val, types::StringExact),
7940-
Insn::StringAppend { recv, other, .. } => {
7947+
Insn::StringAppend { recv, other, recv_flags, other_flags, .. } => {
79417948
self.assert_subtype(insn_id, recv, types::StringExact)?;
7942-
self.assert_subtype(insn_id, other, types::String)
7949+
self.assert_subtype(insn_id, other, types::String)?;
7950+
self.assert_subtype(insn_id, recv_flags, types::CUInt64)?;
7951+
self.assert_subtype(insn_id, other_flags, types::CUInt64)
79437952
}
79447953
Insn::StringAppendCodepoint { recv, other, .. } => {
79457954
self.assert_subtype(insn_id, recv, types::StringExact)?;

‎zjit/src/hir/opt_tests.rs‎

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14395,12 +14395,57 @@ mod hir_opt_tests {
1439514395
PatchPoint MethodRedefined(String@0x1008, <<@0x1010, cme:0x1018)
1439614396
v29:StringExact = GuardType v12, StringExact recompile
1439714397
v30:String = GuardType v13, String
14398-
v31:StringExact = StringAppend v29, v30
14398+
v31:CUInt64 = LoadField v29, :RBASIC_FLAGS@0x1040
14399+
v32:CUInt64 = LoadField v30, :RBASIC_FLAGS@0x1040
14400+
v33:StringExact = StringAppend v29, v30, recv_flags: v31, other_flags: v32
1439914401
CheckInterrupts
1440014402
Return v29
1440114403
");
1440214404
}
1440314405

14406+
#[test]
14407+
fn test_optimize_string_append_reuses_flags_load() {
14408+
eval(r#"
14409+
def test(s, t)
14410+
s.ascii_only?
14411+
s << t
14412+
end
14413+
test("iron", "fish")
14414+
"#);
14415+
assert_snapshot!(hir_string("test"), @"
14416+
fn test@<compiled>:3:
14417+
bb1():
14418+
EntryPoint interpreter
14419+
v1:BasicObject = LoadSelf
14420+
v2:CPtr = LoadSP
14421+
v3:BasicObject = LoadField v2, :s@0x1000
14422+
v4:BasicObject = LoadField v2, :t@0x1001
14423+
Jump bb3(v1, v3, v4)
14424+
bb2():
14425+
EntryPoint JIT(0)
14426+
v7:BasicObject = LoadArg :self@0
14427+
v8:BasicObject = LoadArg :s@1
14428+
v9:BasicObject = LoadArg :t@2
14429+
Jump bb3(v7, v8, v9)
14430+
bb3(v11:BasicObject, v12:BasicObject, v13:BasicObject):
14431+
PatchPoint NoSingletonClass(String@0x1008)
14432+
PatchPoint MethodRedefined(String@0x1008, ascii_only?@0x1010, cme:0x1018)
14433+
v35:StringExact = GuardType v12, StringExact recompile
14434+
v36:CUInt64 = LoadField v35, :RBASIC_FLAGS@0x1040
14435+
v37:CUInt64[3145728] = Const CUInt64(3145728)
14436+
v38:CInt64 = IntAnd v36, v37
14437+
v39:CInt64[1048576] = Const CInt64(1048576)
14438+
v40:CInt64 = GuardGreaterEq v38, v39
14439+
PatchPoint NoEPEscape(test)
14440+
PatchPoint MethodRedefined(String@0x1008, <<@0x1041, cme:0x1048)
14441+
v48:String = GuardType v13, String
14442+
v50:CUInt64 = LoadField v48, :RBASIC_FLAGS@0x1040
14443+
v51:StringExact = StringAppend v35, v48, recv_flags: v36, other_flags: v50
14444+
CheckInterrupts
14445+
Return v35
14446+
");
14447+
}
14448+
1440414449
#[test]
1440514450
fn test_optimize_string_append_codepoint() {
1440614451
eval(r#"
@@ -14461,7 +14506,9 @@ mod hir_opt_tests {
1446114506
PatchPoint MethodRedefined(String@0x1008, <<@0x1010, cme:0x1018)
1446214507
v29:StringExact = GuardType v12, StringExact recompile
1446314508
v30:String = GuardType v13, String
14464-
v31:StringExact = StringAppend v29, v30
14509+
v31:CUInt64 = LoadField v29, :RBASIC_FLAGS@0x1040
14510+
v32:CUInt64 = LoadField v30, :RBASIC_FLAGS@0x1040
14511+
v33:StringExact = StringAppend v29, v30, recv_flags: v31, other_flags: v32
1446514512
CheckInterrupts
1446614513
Return v29
1446714514
");
@@ -16916,8 +16963,8 @@ mod hir_opt_tests {
1691616963
v6:BasicObject = LoadArg :self@0
1691716964
Jump bb3(v6)
1691816965
bb3(v10:BasicObject):
16919-
v57:NilClass = Const Value(nil)
16920-
v56:NilClass = Const Value(nil)
16966+
v59:NilClass = Const Value(nil)
16967+
v58:NilClass = Const Value(nil)
1692116968
v16:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
1692216969
v17:StringExact = StringCopy v16
1692316970
v21:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
@@ -16926,13 +16973,15 @@ mod hir_opt_tests {
1692616973
v28:StringExact = StringCopy v27
1692716974
PatchPoint NoSingletonClass(String@0x1008)
1692816975
PatchPoint MethodRedefined(String@0x1008, <<@0x1010, cme:0x1018)
16929-
v50:StringExact = StringAppend v17, v28
16976+
v50:CUInt64 = LoadField v17, :RBASIC_FLAGS@0x1040
16977+
v51:CUInt64 = LoadField v28, :RBASIC_FLAGS@0x1040
16978+
v52:StringExact = StringAppend v17, v28, recv_flags: v50, other_flags: v51
1693016979
PatchPoint NoEPEscape(test)
1693116980
PatchPoint NoSingletonClass(String@0x1008)
16932-
PatchPoint MethodRedefined(String@0x1008, ==@0x1040, cme:0x1048)
16933-
v55:BoolExact = StringEqual v17, v22
16981+
PatchPoint MethodRedefined(String@0x1008, ==@0x1041, cme:0x1048)
16982+
v57:BoolExact = StringEqual v17, v22
1693416983
CheckInterrupts
16935-
Return v55
16984+
Return v57
1693616985
");
1693716986
}
1693816987

0 commit comments

Comments
 (0)