diff options
| author | Max Bernstein <rubybugs@bernsteinbear.com> | 2025-10-14 15:09:53 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-14 19:09:53 +0000 |
| commit | d75207d0043c56034e68c306f6dfe5e7b4f09438 (patch) | |
| tree | 1c76a70689c6ca9808187daec25e04af6d7846a9 | |
| parent | 8baf170e936525bbda4838db5bbfb138cf724229 (diff) | |
ZJIT: Profile opt_ltlt and opt_aset (#14834)
These bring `send_without_block_no_profiles` numbers down dramatically.
On lobsters:
Before: send_without_block_no_profiles: 3,466,375
After: send_without_block_no_profiles: 1,293,375
all stats before:
```
***ZJIT: Printing ZJIT statistics on exit***
Top-20 not inlined C methods (70.4% of total 14,174,061):
Hash#[]: 4,519,771 (31.9%)
Kernel#is_a?: 1,030,757 ( 7.3%)
Regexp#match?: 399,885 ( 2.8%)
String#empty?: 353,775 ( 2.5%)
Hash#key?: 349,125 ( 2.5%)
Hash#[]=: 344,348 ( 2.4%)
String#start_with?: 334,961 ( 2.4%)
Kernel#respond_to?: 316,527 ( 2.2%)
ObjectSpace::WeakKeyMap#[]: 238,978 ( 1.7%)
TrueClass#===: 235,770 ( 1.7%)
FalseClass#===: 231,143 ( 1.6%)
Array#include?: 211,383 ( 1.5%)
Hash#fetch: 204,702 ( 1.4%)
Kernel#block_given?: 181,793 ( 1.3%)
ActiveSupport::OrderedOptions#_get: 181,272 ( 1.3%)
Kernel#dup: 179,341 ( 1.3%)
BasicObject#!=: 175,996 ( 1.2%)
Class#new: 168,079 ( 1.2%)
Kernel#kind_of?: 165,600 ( 1.2%)
String#==: 157,734 ( 1.1%)
Top-20 not annotated C methods (71.1% of total 14,336,035):
Hash#[]: 4,519,781 (31.5%)
Kernel#is_a?: 1,212,647 ( 8.5%)
Regexp#match?: 399,885 ( 2.8%)
String#empty?: 361,013 ( 2.5%)
Hash#key?: 349,125 ( 2.4%)
Hash#[]=: 344,348 ( 2.4%)
String#start_with?: 334,961 ( 2.3%)
Kernel#respond_to?: 316,527 ( 2.2%)
ObjectSpace::WeakKeyMap#[]: 238,978 ( 1.7%)
TrueClass#===: 235,770 ( 1.6%)
FalseClass#===: 231,143 ( 1.6%)
Array#include?: 211,383 ( 1.5%)
Hash#fetch: 204,702 ( 1.4%)
Kernel#block_given?: 191,662 ( 1.3%)
ActiveSupport::OrderedOptions#_get: 181,272 ( 1.3%)
Kernel#dup: 179,348 ( 1.3%)
BasicObject#!=: 176,180 ( 1.2%)
Class#new: 168,079 ( 1.2%)
Kernel#kind_of?: 165,634 ( 1.2%)
String#==: 163,666 ( 1.1%)
Top-2 not optimized method types for send (100.0% of total 72,318):
cfunc: 48,055 (66.4%)
iseq: 24,263 (33.6%)
Top-6 not optimized method types for send_without_block (100.0% of total 4,536,895):
iseq: 2,281,897 (50.3%)
bmethod: 985,679 (21.7%)
optimized: 952,914 (21.0%)
alias: 310,745 ( 6.8%)
null: 5,106 ( 0.1%)
cfunc: 554 ( 0.0%)
Top-13 not optimized instructions (100.0% of total 4,293,123):
invokesuper: 2,373,396 (55.3%)
invokeblock: 811,891 (18.9%)
sendforward: 505,449 (11.8%)
opt_eq: 451,754 (10.5%)
opt_plus: 74,403 ( 1.7%)
opt_minus: 36,227 ( 0.8%)
opt_send_without_block: 21,792 ( 0.5%)
opt_neq: 7,231 ( 0.2%)
opt_mult: 6,752 ( 0.2%)
opt_or: 3,753 ( 0.1%)
opt_lt: 348 ( 0.0%)
opt_ge: 91 ( 0.0%)
opt_gt: 36 ( 0.0%)
Top-9 send fallback reasons (100.0% of total 27,795,022):
send_without_block_polymorphic: 9,505,835 (34.2%)
send_no_profiles: 5,894,763 (21.2%)
send_without_block_not_optimized_method_type: 4,536,895 (16.3%)
not_optimized_instruction: 4,293,123 (15.4%)
send_without_block_no_profiles: 3,466,407 (12.5%)
send_not_optimized_method_type: 72,318 ( 0.3%)
send_without_block_cfunc_array_variadic: 15,134 ( 0.1%)
obj_to_string_not_string: 9,918 ( 0.0%)
send_without_block_direct_too_many_args: 629 ( 0.0%)
Top-9 unhandled YARV insns (100.0% of total 690,482):
expandarray: 328,490 (47.6%)
checkkeyword: 190,694 (27.6%)
getclassvariable: 59,901 ( 8.7%)
invokesuperforward: 49,503 ( 7.2%)
getblockparam: 48,651 ( 7.0%)
opt_duparray_send: 11,978 ( 1.7%)
getconstant: 952 ( 0.1%)
checkmatch: 290 ( 0.0%)
once: 23 ( 0.0%)
Top-3 compile error reasons (100.0% of total 3,752,391):
register_spill_on_alloc: 3,457,680 (92.1%)
register_spill_on_ccall: 176,348 ( 4.7%)
exception_handler: 118,363 ( 3.2%)
Top-14 side exit reasons (100.0% of total 10,852,021):
compile_error: 3,752,391 (34.6%)
guard_type_failure: 2,630,877 (24.2%)
guard_shape_failure: 1,917,208 (17.7%)
unhandled_yarv_insn: 690,482 ( 6.4%)
block_param_proxy_not_iseq_or_ifunc: 535,784 ( 4.9%)
unhandled_kwarg: 421,989 ( 3.9%)
patchpoint: 369,799 ( 3.4%)
unknown_newarray_send: 314,786 ( 2.9%)
unhandled_splat: 122,062 ( 1.1%)
unhandled_hir_insn: 76,394 ( 0.7%)
block_param_proxy_modified: 19,193 ( 0.2%)
obj_to_string_fallback: 566 ( 0.0%)
interrupt: 468 ( 0.0%)
guard_type_not_failure: 22 ( 0.0%)
send_count: 66,989,407
dynamic_send_count: 27,795,022 (41.5%)
optimized_send_count: 39,194,385 (58.5%)
iseq_optimized_send_count: 18,060,194 (27.0%)
inline_cfunc_optimized_send_count: 6,960,130 (10.4%)
non_variadic_cfunc_optimized_send_count: 11,523,682 (17.2%)
variadic_cfunc_optimized_send_count: 2,650,379 ( 4.0%)
dynamic_getivar_count: 7,365,982
dynamic_setivar_count: 7,245,929
compiled_iseq_count: 4,795
failed_iseq_count: 449
compile_time: 846ms
profile_time: 12ms
gc_time: 9ms
invalidation_time: 61ms
vm_write_pc_count: 64,326,442
vm_write_sp_count: 62,982,524
vm_write_locals_count: 62,982,524
vm_write_stack_count: 62,982,524
vm_write_to_parent_iseq_local_count: 292,448
vm_read_from_parent_iseq_local_count: 6,471,353
code_region_bytes: 22,708,224
side_exit_count: 10,852,021
total_insn_count: 517,550,288
vm_insn_count: 162,946,459
zjit_insn_count: 354,603,829
ratio_in_zjit: 68.5%
```
all stats after:
```
***ZJIT: Printing ZJIT statistics on exit***
Top-20 not inlined C methods (71.1% of total 15,575,343):
Hash#[]: 4,519,778 (29.0%)
Kernel#is_a?: 1,030,758 ( 6.6%)
String#<<: 851,931 ( 5.5%)
Hash#[]=: 742,938 ( 4.8%)
Regexp#match?: 399,886 ( 2.6%)
String#empty?: 353,775 ( 2.3%)
Hash#key?: 349,127 ( 2.2%)
String#start_with?: 334,961 ( 2.2%)
Kernel#respond_to?: 316,529 ( 2.0%)
ObjectSpace::WeakKeyMap#[]: 238,978 ( 1.5%)
TrueClass#===: 235,771 ( 1.5%)
FalseClass#===: 231,144 ( 1.5%)
Array#include?: 211,380 ( 1.4%)
Hash#fetch: 204,701 ( 1.3%)
Kernel#block_given?: 181,792 ( 1.2%)
ActiveSupport::OrderedOptions#_get: 181,272 ( 1.2%)
Kernel#dup: 179,341 ( 1.2%)
BasicObject#!=: 175,997 ( 1.1%)
Class#new: 168,079 ( 1.1%)
Kernel#kind_of?: 165,600 ( 1.1%)
Top-20 not annotated C methods (71.6% of total 15,737,486):
Hash#[]: 4,519,788 (28.7%)
Kernel#is_a?: 1,212,649 ( 7.7%)
String#<<: 851,931 ( 5.4%)
Hash#[]=: 743,117 ( 4.7%)
Regexp#match?: 399,886 ( 2.5%)
String#empty?: 361,013 ( 2.3%)
Hash#key?: 349,127 ( 2.2%)
String#start_with?: 334,961 ( 2.1%)
Kernel#respond_to?: 316,529 ( 2.0%)
ObjectSpace::WeakKeyMap#[]: 238,978 ( 1.5%)
TrueClass#===: 235,771 ( 1.5%)
FalseClass#===: 231,144 ( 1.5%)
Array#include?: 211,380 ( 1.3%)
Hash#fetch: 204,701 ( 1.3%)
Kernel#block_given?: 191,661 ( 1.2%)
ActiveSupport::OrderedOptions#_get: 181,272 ( 1.2%)
Kernel#dup: 179,348 ( 1.1%)
BasicObject#!=: 176,181 ( 1.1%)
Class#new: 168,079 ( 1.1%)
Kernel#kind_of?: 165,634 ( 1.1%)
Top-2 not optimized method types for send (100.0% of total 72,318):
cfunc: 48,055 (66.4%)
iseq: 24,263 (33.6%)
Top-6 not optimized method types for send_without_block (100.0% of total 4,523,650):
iseq: 2,271,911 (50.2%)
bmethod: 985,636 (21.8%)
optimized: 949,696 (21.0%)
alias: 310,747 ( 6.9%)
null: 5,106 ( 0.1%)
cfunc: 554 ( 0.0%)
Top-13 not optimized instructions (100.0% of total 4,293,126):
invokesuper: 2,373,395 (55.3%)
invokeblock: 811,894 (18.9%)
sendforward: 505,449 (11.8%)
opt_eq: 451,754 (10.5%)
opt_plus: 74,403 ( 1.7%)
opt_minus: 36,228 ( 0.8%)
opt_send_without_block: 21,792 ( 0.5%)
opt_neq: 7,231 ( 0.2%)
opt_mult: 6,752 ( 0.2%)
opt_or: 3,753 ( 0.1%)
opt_lt: 348 ( 0.0%)
opt_ge: 91 ( 0.0%)
opt_gt: 36 ( 0.0%)
Top-9 send fallback reasons (100.0% of total 25,824,512):
send_without_block_polymorphic: 9,721,725 (37.6%)
send_no_profiles: 5,894,761 (22.8%)
send_without_block_not_optimized_method_type: 4,523,650 (17.5%)
not_optimized_instruction: 4,293,126 (16.6%)
send_without_block_no_profiles: 1,293,404 ( 5.0%)
send_not_optimized_method_type: 72,318 ( 0.3%)
send_without_block_cfunc_array_variadic: 15,134 ( 0.1%)
obj_to_string_not_string: 9,765 ( 0.0%)
send_without_block_direct_too_many_args: 629 ( 0.0%)
Top-9 unhandled YARV insns (100.0% of total 690,482):
expandarray: 328,490 (47.6%)
checkkeyword: 190,694 (27.6%)
getclassvariable: 59,901 ( 8.7%)
invokesuperforward: 49,503 ( 7.2%)
getblockparam: 48,651 ( 7.0%)
opt_duparray_send: 11,978 ( 1.7%)
getconstant: 952 ( 0.1%)
checkmatch: 290 ( 0.0%)
once: 23 ( 0.0%)
Top-3 compile error reasons (100.0% of total 3,752,504):
register_spill_on_alloc: 3,457,793 (92.1%)
register_spill_on_ccall: 176,348 ( 4.7%)
exception_handler: 118,363 ( 3.2%)
Top-14 side exit reasons (100.0% of total 10,860,754):
compile_error: 3,752,504 (34.6%)
guard_type_failure: 2,638,901 (24.3%)
guard_shape_failure: 1,917,198 (17.7%)
unhandled_yarv_insn: 690,482 ( 6.4%)
block_param_proxy_not_iseq_or_ifunc: 535,785 ( 4.9%)
unhandled_kwarg: 421,947 ( 3.9%)
patchpoint: 370,447 ( 3.4%)
unknown_newarray_send: 314,786 ( 2.9%)
unhandled_splat: 122,065 ( 1.1%)
unhandled_hir_insn: 76,395 ( 0.7%)
block_param_proxy_modified: 19,193 ( 0.2%)
obj_to_string_fallback: 566 ( 0.0%)
interrupt: 463 ( 0.0%)
guard_type_not_failure: 22 ( 0.0%)
send_count: 66,945,926
dynamic_send_count: 25,824,512 (38.6%)
optimized_send_count: 41,121,414 (61.4%)
iseq_optimized_send_count: 18,587,430 (27.8%)
inline_cfunc_optimized_send_count: 6,958,641 (10.4%)
non_variadic_cfunc_optimized_send_count: 12,911,166 (19.3%)
variadic_cfunc_optimized_send_count: 2,664,177 ( 4.0%)
dynamic_getivar_count: 7,365,985
dynamic_setivar_count: 7,245,942
compiled_iseq_count: 4,794
failed_iseq_count: 450
compile_time: 852ms
profile_time: 13ms
gc_time: 11ms
invalidation_time: 63ms
vm_write_pc_count: 64,284,194
vm_write_sp_count: 62,940,427
vm_write_locals_count: 62,940,427
vm_write_stack_count: 62,940,427
vm_write_to_parent_iseq_local_count: 292,447
vm_read_from_parent_iseq_local_count: 6,470,931
code_region_bytes: 23,019,520
side_exit_count: 10,860,754
total_insn_count: 517,576,267
vm_insn_count: 163,188,187
zjit_insn_count: 354,388,080
ratio_in_zjit: 68.5%
```
| -rw-r--r-- | insns.def | 2 | ||||
| -rw-r--r-- | zjit/src/cruby_bindings.inc.rs | 14 | ||||
| -rw-r--r-- | zjit/src/hir.rs | 59 | ||||
| -rw-r--r-- | zjit/src/profile.rs | 2 |
4 files changed, 71 insertions, 6 deletions
@@ -1470,6 +1470,7 @@ opt_ltlt * string. Then what happens if that codepoint does not exist in the * string's encoding? Of course an exception. That's not a leaf. */ // attr bool leaf = false; /* has "invalid codepoint" exception */ +// attr bool zjit_profile = true; { val = vm_opt_ltlt(recv, obj); @@ -1537,6 +1538,7 @@ opt_aset /* This is another story than opt_aref. When vm_opt_aset() resorts * to rb_hash_aset(), which should call #hash for `obj`. */ // attr bool leaf = false; /* has rb_funcall() */ /* calls #hash */ +// attr bool zjit_profile = true; { val = vm_opt_aset(recv, obj, set); diff --git a/zjit/src/cruby_bindings.inc.rs b/zjit/src/cruby_bindings.inc.rs index ea1bf68acc..6e3ae05194 100644 --- a/zjit/src/cruby_bindings.inc.rs +++ b/zjit/src/cruby_bindings.inc.rs @@ -694,12 +694,14 @@ pub const YARVINSN_zjit_opt_lt: ruby_vminsn_type = 229; pub const YARVINSN_zjit_opt_le: ruby_vminsn_type = 230; pub const YARVINSN_zjit_opt_gt: ruby_vminsn_type = 231; pub const YARVINSN_zjit_opt_ge: ruby_vminsn_type = 232; -pub const YARVINSN_zjit_opt_and: ruby_vminsn_type = 233; -pub const YARVINSN_zjit_opt_or: ruby_vminsn_type = 234; -pub const YARVINSN_zjit_opt_aref: ruby_vminsn_type = 235; -pub const YARVINSN_zjit_opt_empty_p: ruby_vminsn_type = 236; -pub const YARVINSN_zjit_opt_not: ruby_vminsn_type = 237; -pub const VM_INSTRUCTION_SIZE: ruby_vminsn_type = 238; +pub const YARVINSN_zjit_opt_ltlt: ruby_vminsn_type = 233; +pub const YARVINSN_zjit_opt_and: ruby_vminsn_type = 234; +pub const YARVINSN_zjit_opt_or: ruby_vminsn_type = 235; +pub const YARVINSN_zjit_opt_aref: ruby_vminsn_type = 236; +pub const YARVINSN_zjit_opt_aset: ruby_vminsn_type = 237; +pub const YARVINSN_zjit_opt_empty_p: ruby_vminsn_type = 238; +pub const YARVINSN_zjit_opt_not: ruby_vminsn_type = 239; +pub const VM_INSTRUCTION_SIZE: ruby_vminsn_type = 240; pub type ruby_vminsn_type = u32; pub type rb_iseq_callback = ::std::option::Option< unsafe extern "C" fn(arg1: *const rb_iseq_t, arg2: *mut ::std::os::raw::c_void), diff --git a/zjit/src/hir.rs b/zjit/src/hir.rs index e32c15702e..022451e8ab 100644 --- a/zjit/src/hir.rs +++ b/zjit/src/hir.rs @@ -12817,4 +12817,63 @@ mod opt_tests { Return v25 "); } + + #[test] + fn test_optimize_array_aset() { + eval(" + def test(arr) + arr[1] = 10 + end + test([]) + "); + assert_snapshot!(hir_string("test"), @r" + fn test@<compiled>:3: + bb0(): + EntryPoint interpreter + v1:BasicObject = LoadSelf + v2:BasicObject = GetLocal l0, SP@4 + Jump bb2(v1, v2) + bb1(v5:BasicObject, v6:BasicObject): + EntryPoint JIT(0) + Jump bb2(v5, v6) + bb2(v8:BasicObject, v9:BasicObject): + v14:Fixnum[1] = Const Value(1) + v15:Fixnum[10] = Const Value(10) + PatchPoint MethodRedefined(Array@0x1000, []=@0x1008, cme:0x1010) + PatchPoint NoSingletonClass(Array@0x1000) + v28:ArrayExact = GuardType v9, ArrayExact + v29:BasicObject = CCallVariadic []=@0x1038, v28, v14, v15 + CheckInterrupts + Return v15 + "); + } + + #[test] + fn test_optimize_array_ltlt() { + eval(" + def test(arr) + arr << 1 + end + test([]) + "); + assert_snapshot!(hir_string("test"), @r" + fn test@<compiled>:3: + bb0(): + EntryPoint interpreter + v1:BasicObject = LoadSelf + v2:BasicObject = GetLocal l0, SP@4 + Jump bb2(v1, v2) + bb1(v5:BasicObject, v6:BasicObject): + EntryPoint JIT(0) + Jump bb2(v5, v6) + bb2(v8:BasicObject, v9:BasicObject): + v13:Fixnum[1] = Const Value(1) + PatchPoint MethodRedefined(Array@0x1000, <<@0x1008, cme:0x1010) + PatchPoint NoSingletonClass(Array@0x1000) + v26:ArrayExact = GuardType v9, ArrayExact + v27:BasicObject = CCallWithFrame <<@0x1038, v26, v13 + CheckInterrupts + Return v27 + "); + } } diff --git a/zjit/src/profile.rs b/zjit/src/profile.rs index 67f2fdc740..c2c35f687b 100644 --- a/zjit/src/profile.rs +++ b/zjit/src/profile.rs @@ -74,6 +74,8 @@ fn profile_insn(bare_opcode: ruby_vminsn_type, ec: EcPtr) { YARVINSN_opt_or => profile_operands(profiler, profile, 2), YARVINSN_opt_empty_p => profile_operands(profiler, profile, 1), YARVINSN_opt_aref => profile_operands(profiler, profile, 2), + YARVINSN_opt_ltlt => profile_operands(profiler, profile, 2), + YARVINSN_opt_aset => profile_operands(profiler, profile, 3), YARVINSN_opt_not => profile_operands(profiler, profile, 1), YARVINSN_getinstancevariable => profile_self(profiler, profile), YARVINSN_objtostring => profile_operands(profiler, profile, 1), |
