summaryrefslogtreecommitdiff
diff options
authorywenc <[email protected]>2025-06-12 18:05:04 -0400
committerAlan Wu <[email protected]>2025-06-20 01:19:38 +0900
commit38d38bd5ceec57d13c1c5250f2e0d0c88c4c47f0 (patch)
treec4e9d99e0a96f65048270bc2987ebc39e1056ec9
parent34eaa6418e5c5b8639add323dbfd531b32a7d4a3 (diff)
ZJIT: objtostring to HIR
Add a fast path for known strings at compile time, otherwise calls method id to_s using Insn::SendWithoutBlock Co-authored-by: composerinteralia <[email protected]> More specific test name in zjit/src/hir.rs Co-authored-by: Max Bernstein <[email protected]>
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/13627
-rw-r--r--zjit/src/hir.rs89
1 files changed, 89 insertions, 0 deletions
diff --git a/zjit/src/hir.rs b/zjit/src/hir.rs
index 2be6031805..2bd7174f2d 100644
--- a/zjit/src/hir.rs
+++ b/zjit/src/hir.rs
@@ -496,6 +496,9 @@ pub enum Insn {
FixnumGt { left: InsnId, right: InsnId },
FixnumGe { left: InsnId, right: InsnId },
+ // Distinct from `SendWithoutBlock` with `mid:to_s` because does not have a patch point for String to_s being redefined
+ ObjToString { val: InsnId, call_info: CallInfo, cd: *const rb_call_data, state: InsnId },
+
/// Side-exit if val doesn't have the expected type.
GuardType { val: InsnId, guard_type: Type, state: InsnId },
/// Side-exit if val is not the expected VALUE.
@@ -695,6 +698,7 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
Insn::ToNewArray { val, .. } => write!(f, "ToNewArray {val}"),
Insn::ArrayExtend { left, right, .. } => write!(f, "ArrayExtend {left}, {right}"),
Insn::ArrayPush { array, val, .. } => write!(f, "ArrayPush {array}, {val}"),
+ Insn::ObjToString { val, .. } => { write!(f, "ObjToString {val}") },
Insn::SideExit { .. } => write!(f, "SideExit"),
Insn::PutSpecialObject { value_type } => {
write!(f, "PutSpecialObject {}", value_type)
@@ -1013,6 +1017,12 @@ impl Function {
FixnumLt { left, right } => FixnumLt { left: find!(*left), right: find!(*right) },
FixnumLe { left, right } => FixnumLe { left: find!(*left), right: find!(*right) },
PutSpecialObject { value_type } => PutSpecialObject { value_type: *value_type },
+ ObjToString { val, call_info, cd, state } => ObjToString {
+ val: find!(*val),
+ call_info: call_info.clone(),
+ cd: *cd,
+ state: *state,
+ },
SendWithoutBlock { self_val, call_info, cd, args, state } => SendWithoutBlock {
self_val: find!(*self_val),
call_info: call_info.clone(),
@@ -1143,6 +1153,7 @@ impl Function {
Insn::GetIvar { .. } => types::BasicObject,
Insn::ToNewArray { .. } => types::ArrayExact,
Insn::ToArray { .. } => types::ArrayExact,
+ Insn::ObjToString { .. } => types::BasicObject,
}
}
@@ -1386,6 +1397,15 @@ impl Function {
let replacement = self.push_insn(block, Insn::Const { val: Const::Value(unsafe { (*ice).value }) });
self.make_equal_to(insn_id, replacement);
}
+ Insn::ObjToString { val, call_info, cd, state, .. } => {
+ if self.is_a(val, types::StringExact) {
+ // behaves differently from `SendWithoutBlock` with `mid:to_s` because ObjToString should not have a patch point for String to_s being redefined
+ self.make_equal_to(insn_id, val);
+ } else {
+ let replacement = self.push_insn(block, Insn::SendWithoutBlock { self_val: val, call_info, cd, args: vec![], state });
+ self.make_equal_to(insn_id, replacement)
+ }
+ }
_ => { self.push_insn_id(block, insn_id); }
}
}
@@ -1758,6 +1778,10 @@ impl Function {
worklist.push_back(val);
worklist.push_back(state);
}
+ Insn::ObjToString { val, state, .. } => {
+ worklist.push_back(val);
+ worklist.push_back(state);
+ }
Insn::GetGlobal { state, .. } |
Insn::SideExit { state } => worklist.push_back(state),
}
@@ -2677,6 +2701,26 @@ pub fn iseq_to_hir(iseq: *const rb_iseq_t) -> Result<Function, ParseError> {
let insn_id = fun.push_insn(block, Insn::InvokeBuiltin { bf, args, state: exit_id });
state.stack_push(insn_id);
}
+ YARVINSN_objtostring => {
+ let cd: *const rb_call_data = get_arg(pc, 0).as_ptr();
+ let call_info = unsafe { rb_get_call_data_ci(cd) };
+
+ if unknown_call_type(unsafe { rb_vm_ci_flag(call_info) }) {
+ assert!(false, "objtostring should not have unknown call type");
+ }
+ let argc = unsafe { vm_ci_argc((*cd).ci) };
+ assert_eq!(0, argc, "objtostring should not have args");
+
+ let method_name: String = unsafe {
+ let mid = rb_vm_ci_mid(call_info);
+ mid.contents_lossy().into_owned()
+ };
+
+ let recv = state.stack_pop()?;
+ let exit_id = fun.push_insn(block, Insn::Snapshot { state: exit_state });
+ let objtostring = fun.push_insn(block, Insn::ObjToString { val: recv, call_info: CallInfo { method_name }, cd, state: exit_id });
+ state.stack_push(objtostring)
+ }
_ => {
// Unknown opcode; side-exit into the interpreter
let exit_id = fun.push_insn(block, Insn::Snapshot { state: exit_state });
@@ -4344,6 +4388,21 @@ mod tests {
Return v20
"#]]);
}
+
+ #[test]
+ fn test_objtostring() {
+ eval("
+ def test = \"#{1}\"
+ ");
+ assert_method_hir_with_opcode("test", YARVINSN_objtostring, expect![[r#"
+ fn test:
+ bb0(v0:BasicObject):
+ v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
+ v3:Fixnum[1] = Const Value(1)
+ v5:BasicObject = ObjToString v3
+ SideExit
+ "#]]);
+ }
}
#[cfg(test)]
@@ -5902,4 +5961,34 @@ mod opt_tests {
Return v7
"#]]);
}
+
+ #[test]
+ fn test_objtostring_string() {
+ eval(r##"
+ def test = "#{('foo')}"
+ "##);
+ assert_optimized_method_hir("test", expect![[r#"
+ fn test:
+ bb0(v0:BasicObject):
+ v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
+ v3:StringExact[VALUE(0x1008)] = Const Value(VALUE(0x1008))
+ v4:StringExact = StringCopy v3
+ SideExit
+ "#]]);
+ }
+
+ #[test]
+ fn test_objtostring_with_non_string() {
+ eval(r##"
+ def test = "#{1}"
+ "##);
+ assert_optimized_method_hir("test", expect![[r#"
+ fn test:
+ bb0(v0:BasicObject):
+ v2:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
+ v3:Fixnum[1] = Const Value(1)
+ v8:BasicObject = SendWithoutBlock v3, :to_s
+ SideExit
+ "#]]);
+ }
}