Skip to content

Commit dffdc2e

Browse files
committed
Move force_encoding flags load into HIR
Reuse RBASIC_FLAGS loads in String#force_encoding codegen.
1 parent 9ff307b commit dffdc2e

4 files changed

Lines changed: 58 additions & 10 deletions

File tree

‎zjit/src/codegen.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
651651
&Insn::ObjectAllocClass { class, state } => gen_object_alloc_class(jit, asm, function, class, &function.frame_state(state)),
652652
Insn::StringCopy { val, chilled, state } => gen_string_copy(jit, asm, function, *val, opnd!(val), *chilled, &function.frame_state(*state)),
653653
Insn::StringConcat { strings, state } => gen_string_concat(jit, asm, function, opnds!(strings), &function.frame_state(*state)),
654-
Insn::StringForceEncoding { string, encoding, state } => gen_string_force_encoding(jit, asm, function, opnd!(string), opnd!(encoding), &function.frame_state(*state)),
654+
Insn::StringForceEncoding { string, encoding, flags, state } => gen_string_force_encoding(jit, asm, function, opnd!(string), opnd!(encoding), opnd!(flags), &function.frame_state(*state)),
655655
&Insn::StringGetbyte { string, index } => gen_string_getbyte(asm, opnd!(string), opnd!(index)),
656656
Insn::StringByteslice { string, beg, len, state } => gen_string_byteslice(asm, opnd!(string), opnd!(beg), opnd!(len), &function.frame_state(*state)),
657657
Insn::StringSetbyteFixnum { string, index, value } => gen_string_setbyte_fixnum(asm, opnd!(string), opnd!(index), opnd!(value)),
@@ -4219,7 +4219,7 @@ fn gen_string_concat(jit: &mut JITState, asm: &mut Assembler, function: &Functio
42194219
asm_ccall!(asm, rb_str_concat_literals, strings.len().into(), first_string_ptr)
42204220
}
42214221

4222-
fn gen_string_force_encoding(jit: &mut JITState, asm: &mut Assembler, function: &Function, string: Opnd, encoding: Opnd, state: &FrameState) -> Opnd {
4222+
fn gen_string_force_encoding(jit: &mut JITState, asm: &mut Assembler, function: &Function, string: Opnd, encoding: Opnd, flags: Opnd, state: &FrameState) -> Opnd {
42234223
asm_comment!(asm, "String#force_encoding fast path");
42244224
// Avoid the C call only when a direct flag update preserves all rb_str_force_encoding behavior.
42254225
let string = asm.load_mem(string);
@@ -4228,7 +4228,6 @@ fn gen_string_force_encoding(jit: &mut JITState, asm: &mut Assembler, function:
42284228
// rb_str_force_encoding checks mutability before it checks whether the encoding changed.
42294229
// Side-exit so CRuby can raise for frozen or locked strings and warn for chilled strings.
42304230
let unmodifiable_exit = side_exit(jit, function, state, GuardNotFrozen);
4231-
let flags = asm.load(Opnd::mem(VALUE_BITS, string, RUBY_OFFSET_RBASIC_FLAGS));
42324231
let unmodifiable_mask = RUBY_FL_FREEZE as u64 | RUBY_FL_USER2 as u64 | RUBY_FL_USER7 as u64;
42334232
asm.test(flags, Opnd::UImm(unmodifiable_mask));
42344233
asm.jnz(jit, unmodifiable_exit);

‎zjit/src/cruby_methods.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,11 +482,14 @@ fn inline_string_force_encoding(fun: &mut hir::Function, block: hir::BlockId, re
482482

483483
let string = fun.coerce_to(block, recv, types::String, state);
484484
let encoding = fun.coerce_to(block, enc, enc_type, state);
485+
let flags = fun.load_rbasic_flags(block, string);
485486
let _ = fun.push_insn(block, hir::Insn::StringForceEncoding {
486487
string,
487488
encoding,
489+
flags,
488490
state,
489491
});
492+
490493
Some(string)
491494
}
492495

‎zjit/src/hir.rs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ pub enum Insn {
10041004
StringIntern { val: InsnId, state: InsnId },
10051005
StringConcat { strings: Vec<InsnId>, state: InsnId },
10061006
/// Apply String#force_encoding for a mutable string and an inline encoding.
1007-
StringForceEncoding { string: InsnId, encoding: InsnId, state: InsnId },
1007+
/// HIR loads the string flags for reuse by other instructions.
1008+
StringForceEncoding { string: InsnId, encoding: InsnId, flags: InsnId, state: InsnId },
10081009
/// Call rb_str_getbyte with known-Fixnum index
10091010
StringGetbyte { string: InsnId, index: InsnId },
10101011
/// Call rb_str_byte_substr with known-Fixnum beg/len
@@ -1428,9 +1429,10 @@ macro_rules! for_each_operand_impl {
14281429
$visit_many!(strings);
14291430
$visit_one!(*state);
14301431
}
1431-
Insn::StringForceEncoding { string, encoding, state, .. } => {
1432+
Insn::StringForceEncoding { string, encoding, flags, state, .. } => {
14321433
$visit_one!(*string);
14331434
$visit_one!(*encoding);
1435+
$visit_one!(*flags);
14341436
$visit_one!(*state);
14351437
}
14361438
Insn::StringGetbyte { string, index } => {
@@ -2164,8 +2166,8 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
21642166
write_separated!(f, " ", ", ", strings);
21652167
Ok(())
21662168
}
2167-
Insn::StringForceEncoding { string, encoding, .. } => {
2168-
write!(f, "StringForceEncoding {string}, {encoding}")
2169+
Insn::StringForceEncoding { string, encoding, flags, .. } => {
2170+
write!(f, "StringForceEncoding {string}, {encoding}, flags: {flags}")
21692171
}
21702172
Insn::StringGetbyte { string, index, .. } => {
21712173
write!(f, "StringGetbyte {string}, {index}")
@@ -8111,9 +8113,10 @@ impl Function {
81118113
}
81128114
}
81138115
}
8114-
Insn::StringForceEncoding { string, encoding, .. } => {
8116+
Insn::StringForceEncoding { string, encoding, flags, .. } => {
81158117
self.assert_subtype(insn_id, string, types::String)?;
8116-
self.assert_subtype(insn_id, encoding, Type::from_class(unsafe { rb_cEncoding }))
8118+
self.assert_subtype(insn_id, encoding, Type::from_class(unsafe { rb_cEncoding }))?;
8119+
self.assert_subtype(insn_id, flags, types::CUInt64)
81178120
}
81188121
Insn::GuardLess { left, right, .. }
81198122
| Insn::GuardGreaterEq { left, right, .. } => {

‎zjit/src/hir/opt_tests.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17198,12 +17198,55 @@ mod hir_opt_tests {
1719817198
PatchPoint MethodRedefined(String@0x1008, force_encoding@0x1010, cme:0x1018)
1719917199
v28:StringExact = GuardType v12, StringExact recompile
1720017200
v29:ObjectSubclass[class_exact:Encoding] = GuardType v13, ObjectSubclass[class_exact:Encoding]
17201-
v30:StringExact = StringForceEncoding v28, v29
17201+
v30:CUInt64 = LoadField v28, :RBASIC_FLAGS@0x1040
17202+
v31:StringExact = StringForceEncoding v28, v29, flags: v30
1720217203
CheckInterrupts
1720317204
Return v28
1720417205
");
1720517206
}
1720617207

17208+
#[test]
17209+
fn test_optimize_string_force_encoding_reuses_flags_load() {
17210+
eval(r#"
17211+
def test(s, enc)
17212+
s.ascii_only?
17213+
s.force_encoding(enc)
17214+
end
17215+
test("iron", Encoding::UTF_8)
17216+
"#);
17217+
assert_snapshot!(hir_string("test"), @"
17218+
fn test@<compiled>:3:
17219+
bb1():
17220+
EntryPoint interpreter
17221+
v1:BasicObject = LoadSelf
17222+
v2:CPtr = LoadSP
17223+
v3:BasicObject = LoadField v2, :s@0x1000
17224+
v4:BasicObject = LoadField v2, :enc@0x1001
17225+
Jump bb3(v1, v3, v4)
17226+
bb2():
17227+
EntryPoint JIT(0)
17228+
v7:BasicObject = LoadArg :self@0
17229+
v8:BasicObject = LoadArg :s@1
17230+
v9:BasicObject = LoadArg :enc@2
17231+
Jump bb3(v7, v8, v9)
17232+
bb3(v11:BasicObject, v12:BasicObject, v13:BasicObject):
17233+
PatchPoint NoSingletonClass(String@0x1008)
17234+
PatchPoint MethodRedefined(String@0x1008, ascii_only?@0x1010, cme:0x1018)
17235+
v34:StringExact = GuardType v12, StringExact recompile
17236+
v35:CUInt64 = LoadField v34, :RBASIC_FLAGS@0x1040
17237+
v36:CUInt64[3145728] = Const CUInt64(3145728)
17238+
v37:CInt64 = IntAnd v35, v36
17239+
v38:CInt64[1048576] = Const CInt64(1048576)
17240+
v39:CInt64 = GuardGreaterEq v37, v38
17241+
PatchPoint NoEPEscape(test)
17242+
PatchPoint MethodRedefined(String@0x1008, force_encoding@0x1041, cme:0x1048)
17243+
v47:ObjectSubclass[class_exact:Encoding] = GuardType v13, ObjectSubclass[class_exact:Encoding]
17244+
v49:StringExact = StringForceEncoding v34, v47, flags: v35
17245+
CheckInterrupts
17246+
Return v34
17247+
");
17248+
}
17249+
1720717250
#[test]
1720817251
fn test_string_force_encoding_does_not_inline_wrong_arity() {
1720917252
eval(r#"

0 commit comments

Comments
 (0)