Skip to content

Commit c0924d8

Browse files
committed
ZJIT: Add IncrDynamicCounter HIR instead
1 parent 36b68ae commit c0924d8

4 files changed

Lines changed: 29 additions & 30 deletions

File tree

zjit.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def stats_string
4343
print_counters_with_prefix(prefix: 'dynamic_send_type_', prompt: 'dynamic send types', buf:, stats:, limit: 20)
4444
print_counters_with_prefix(prefix: 'unspecialized_def_type_', prompt: 'send fallback unspecialized def_types', buf:, stats:, limit: 20)
4545
print_counters_with_prefix(prefix: 'send_fallback_', prompt: 'dynamic send types', buf:, stats:, limit: 20)
46-
print_counters_with_prefix(prefix: 'not_optimized_cfuncs_', prompt: 'Unoptimized C functions', buf:, stats:, limit: 20)
46+
print_counters_with_prefix(prefix: 'not_optimized_cfuncs_', prompt: 'unoptimized sends to C functions', buf:, stats:, limit: 20)
4747

4848
# Show exit counters, ordered by the typical amount of exits for the prefix at the time
4949
print_counters_with_prefix(prefix: 'unhandled_yarv_insn_', prompt: 'unhandled YARV insns', buf:, stats:, limit: 20)

zjit/src/codegen.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ fn gen_insn(cb: &mut CodeBlock, jit: &mut JITState, asm: &mut Assembler, functio
420420
Insn::GetSpecialSymbol { symbol_type, state: _ } => gen_getspecial_symbol(asm, *symbol_type),
421421
Insn::GetSpecialNumber { nth, state } => gen_getspecial_number(asm, *nth, &function.frame_state(*state)),
422422
&Insn::IncrCounter(counter) => no_output!(gen_incr_counter(asm, counter)),
423-
Insn::CountUnoptimizedCFunc { signature, counter_ptr } => no_output!(gen_count_unoptimized_cfunc(asm, signature, *counter_ptr)),
423+
Insn::IncrDynamicCounter { counter_ptr } => no_output!(gen_incr_dynamic_counter(asm, *counter_ptr)),
424424
Insn::ObjToString { val, cd, state, .. } => gen_objtostring(jit, asm, opnd!(val), *cd, &function.frame_state(*state)),
425425
&Insn::CheckInterrupts { state } => no_output!(gen_check_interrupts(jit, asm, &function.frame_state(state))),
426426
&Insn::HashDup { val, state } => { gen_hash_dup(asm, opnd!(val), &function.frame_state(state)) },
@@ -1586,13 +1586,12 @@ fn gen_guard_bit_equals(jit: &mut JITState, asm: &mut Assembler, val: lir::Opnd,
15861586
}
15871587

15881588
/// Generate code that records unoptimized C functions if --zjit-stats is enabled
1589-
fn gen_count_unoptimized_cfunc(asm: &mut Assembler, signature: &str, counter_ptr: *mut u64) {
1589+
fn gen_incr_dynamic_counter(asm: &mut Assembler, counter_ptr: *mut u64) {
15901590
if get_option!(stats) {
15911591
unsafe extern "C" {
1592-
fn rb_zjit_count_unoptimized_cfunc(counter_ptr: *mut u64);
1592+
fn rb_zjit_increment_dynamic_counter(counter_ptr: *mut u64);
15931593
}
1594-
asm_comment!(asm, "count unoptimized cfunc: {}", signature);
1595-
asm_ccall!(asm, rb_zjit_count_unoptimized_cfunc, Opnd::const_ptr(counter_ptr as *const u8));
1594+
asm_ccall!(asm, rb_zjit_increment_dynamic_counter, Opnd::const_ptr(counter_ptr as *const u8));
15961595
}
15971596
}
15981597

zjit/src/hir.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -707,8 +707,8 @@ pub enum Insn {
707707
/// Increment a counter in ZJIT stats
708708
IncrCounter(Counter),
709709

710-
/// Increment a counter in ZJIT stats for the given unoptimized C function
711-
CountUnoptimizedCFunc { signature: String, counter_ptr: *mut u64 },
710+
/// Increment a counter in ZJIT stats for the given counter pointer
711+
IncrDynamicCounter { counter_ptr: *mut u64 },
712712

713713
/// Equivalent of RUBY_VM_CHECK_INTS. Automatically inserted by the compiler before jumps and
714714
/// return instructions.
@@ -723,7 +723,7 @@ impl Insn {
723723
| Insn::IfTrue { .. } | Insn::IfFalse { .. } | Insn::EntryPoint { .. } | Insn::Return { .. }
724724
| Insn::PatchPoint { .. } | Insn::SetIvar { .. } | Insn::ArrayExtend { .. }
725725
| Insn::ArrayPush { .. } | Insn::SideExit { .. } | Insn::SetGlobal { .. }
726-
| Insn::SetLocal { .. } | Insn::Throw { .. } | Insn::IncrCounter(_) | Insn::CountUnoptimizedCFunc { .. }
726+
| Insn::SetLocal { .. } | Insn::Throw { .. } | Insn::IncrCounter(_) | Insn::IncrDynamicCounter { .. }
727727
| Insn::CheckInterrupts { .. } | Insn::GuardBlockParamProxy { .. } => false,
728728
_ => true,
729729
}
@@ -975,7 +975,7 @@ impl<'a> std::fmt::Display for InsnPrinter<'a> {
975975
}
976976
Ok(())
977977
},
978-
Insn::CountUnoptimizedCFunc { signature, .. } => write!(f, "CountUnoptimizedCFunc {}", signature),
978+
Insn::IncrDynamicCounter { .. } => write!(f, "IncrDynamicCounter"),
979979
Insn::Snapshot { state } => write!(f, "Snapshot {}", state.print(self.ptr_map)),
980980
Insn::Defined { op_type, v, .. } => {
981981
// op_type (enum defined_type) printing logic from iseq.c.
@@ -1380,7 +1380,7 @@ impl Function {
13801380
| SideExit {..}
13811381
| EntryPoint {..}
13821382
| LoadPC
1383-
| CountUnoptimizedCFunc {..}
1383+
| IncrDynamicCounter {..}
13841384
| IncrCounter(_)) => result.clone(),
13851385
&Snapshot { state: FrameState { iseq, insn_idx, pc, ref stack, ref locals } } =>
13861386
Snapshot {
@@ -1530,7 +1530,7 @@ impl Function {
15301530
| Insn::IfTrue { .. } | Insn::IfFalse { .. } | Insn::Return { .. } | Insn::Throw { .. }
15311531
| Insn::PatchPoint { .. } | Insn::SetIvar { .. } | Insn::ArrayExtend { .. }
15321532
| Insn::ArrayPush { .. } | Insn::SideExit { .. } | Insn::SetLocal { .. } | Insn::IncrCounter(_)
1533-
| Insn::CheckInterrupts { .. } | Insn::GuardBlockParamProxy { .. } | Insn::CountUnoptimizedCFunc { .. } =>
1533+
| Insn::CheckInterrupts { .. } | Insn::GuardBlockParamProxy { .. } | Insn::IncrDynamicCounter { .. } =>
15341534
panic!("Cannot infer type of instruction with no output: {}", self.insns[insn.0]),
15351535
Insn::Const { val: Const::Value(val) } => Type::from_value(*val),
15361536
Insn::Const { val: Const::CBool(val) } => Type::from_cbool(*val),
@@ -2284,10 +2284,10 @@ impl Function {
22842284
let called_id = unsafe { (*cme).called_id };
22852285
let class_name = get_class_name(owner);
22862286
let method_name = called_id.contents_lossy();
2287-
let signature = format!("{}#{}", class_name, method_name);
2288-
let counter_ptr = get_or_create_unoptimized_cfunc_counter_ptr(signature.clone());
2287+
let qualified_method_name = format!("{}#{}", class_name, method_name);
2288+
let counter_ptr = get_or_create_unoptimized_cfunc_counter_ptr(qualified_method_name.clone());
22892289

2290-
self.push_insn(block, Insn::CountUnoptimizedCFunc { signature, counter_ptr });
2290+
self.push_insn(block, Insn::IncrDynamicCounter { counter_ptr });
22912291
}
22922292
_ => {}
22932293
}
@@ -2435,7 +2435,7 @@ impl Function {
24352435
| &Insn::GetLocal { .. }
24362436
| &Insn::PutSpecialObject { .. }
24372437
| &Insn::IncrCounter(_)
2438-
| &Insn::CountUnoptimizedCFunc { .. } =>
2438+
| &Insn::IncrDynamicCounter { .. } =>
24392439
{}
24402440
&Insn::PatchPoint { state, .. }
24412441
| &Insn::CheckInterrupts { state }
@@ -9502,7 +9502,7 @@ mod opt_tests {
95029502
bb2(v6:BasicObject):
95039503
v10:Fixnum[1] = Const Value(1)
95049504
v11:Fixnum[0] = Const Value(0)
9505-
CountUnoptimizedCFunc Kernel#itself
9505+
IncrDynamicCounter
95069506
v13:BasicObject = SendWithoutBlock v10, :itself, v11
95079507
CheckInterrupts
95089508
Return v13
@@ -10341,7 +10341,7 @@ mod opt_tests {
1034110341
Jump bb2(v4)
1034210342
bb2(v6:BasicObject):
1034310343
v11:HashExact = NewHash
10344-
CountUnoptimizedCFunc Kernel#dup
10344+
IncrDynamicCounter
1034510345
v13:BasicObject = SendWithoutBlock v11, :dup
1034610346
v15:BasicObject = SendWithoutBlock v13, :freeze
1034710347
CheckInterrupts
@@ -10365,7 +10365,7 @@ mod opt_tests {
1036510365
bb2(v6:BasicObject):
1036610366
v11:HashExact = NewHash
1036710367
v12:NilClass = Const Value(nil)
10368-
CountUnoptimizedCFunc Hash#freeze
10368+
IncrDynamicCounter
1036910369
v14:BasicObject = SendWithoutBlock v11, :freeze, v12
1037010370
CheckInterrupts
1037110371
Return v14
@@ -10430,7 +10430,7 @@ mod opt_tests {
1043010430
Jump bb2(v4)
1043110431
bb2(v6:BasicObject):
1043210432
v11:ArrayExact = NewArray
10433-
CountUnoptimizedCFunc Kernel#dup
10433+
IncrDynamicCounter
1043410434
v13:BasicObject = SendWithoutBlock v11, :dup
1043510435
v15:BasicObject = SendWithoutBlock v13, :freeze
1043610436
CheckInterrupts
@@ -10454,7 +10454,7 @@ mod opt_tests {
1045410454
bb2(v6:BasicObject):
1045510455
v11:ArrayExact = NewArray
1045610456
v12:NilClass = Const Value(nil)
10457-
CountUnoptimizedCFunc Array#freeze
10457+
IncrDynamicCounter
1045810458
v14:BasicObject = SendWithoutBlock v11, :freeze, v12
1045910459
CheckInterrupts
1046010460
Return v14
@@ -10520,7 +10520,7 @@ mod opt_tests {
1052010520
bb2(v6:BasicObject):
1052110521
v10:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
1052210522
v12:StringExact = StringCopy v10
10523-
CountUnoptimizedCFunc String#dup
10523+
IncrDynamicCounter
1052410524
v14:BasicObject = SendWithoutBlock v12, :dup
1052510525
v16:BasicObject = SendWithoutBlock v14, :freeze
1052610526
CheckInterrupts
@@ -10545,7 +10545,7 @@ mod opt_tests {
1054510545
v10:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
1054610546
v12:StringExact = StringCopy v10
1054710547
v13:NilClass = Const Value(nil)
10548-
CountUnoptimizedCFunc String#freeze
10548+
IncrDynamicCounter
1054910549
v15:BasicObject = SendWithoutBlock v12, :freeze, v13
1055010550
CheckInterrupts
1055110551
Return v15
@@ -10611,7 +10611,7 @@ mod opt_tests {
1061110611
bb2(v6:BasicObject):
1061210612
v10:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
1061310613
v12:StringExact = StringCopy v10
10614-
CountUnoptimizedCFunc String#dup
10614+
IncrDynamicCounter
1061510615
v14:BasicObject = SendWithoutBlock v12, :dup
1061610616
v16:BasicObject = SendWithoutBlock v14, :-@
1061710617
CheckInterrupts
@@ -10741,7 +10741,7 @@ mod opt_tests {
1074110741
bb2(v8:BasicObject, v9:BasicObject):
1074210742
v13:StringExact[VALUE(0x1000)] = Const Value(VALUE(0x1000))
1074310743
v25:BasicObject = GuardTypeNot v9, String
10744-
CountUnoptimizedCFunc Array#to_s
10744+
IncrDynamicCounter
1074510745
v26:BasicObject = SendWithoutBlock v9, :to_s
1074610746
v17:String = AnyToString v9, str: v26
1074710747
v19:StringExact = StringConcat v13, v17

zjit/src/stats.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,18 @@ pub(crate) use incr_counter;
190190
/// The number of side exits from each YARV instruction
191191
pub type ExitCounters = [u64; VM_INSTRUCTION_SIZE as usize];
192192

193-
/// Store signature ("Klass#method") to counter pointer mappings for unoptimized cfuncs
193+
/// Store qualified method name ("Klass#method") to counter pointer mappings for unoptimized cfuncs
194194
pub static UNOPTIMIZED_CFUNC_COUNTER_POINTERS: LazyLock<Mutex<HashMap<String, Box<u64>>>> = LazyLock::new(|| Mutex::new(HashMap::new()));
195195

196-
/// Get or create a counter pointer for the given signature
197-
pub fn get_or_create_unoptimized_cfunc_counter_ptr(signature: String) -> *mut u64 {
196+
/// Get or create a counter pointer for the given qualified method name
197+
pub fn get_or_create_unoptimized_cfunc_counter_ptr(qualified_method_name: String) -> *mut u64 {
198198
let mut map = UNOPTIMIZED_CFUNC_COUNTER_POINTERS.lock().unwrap();
199-
let counter = map.entry(signature).or_insert_with(|| Box::new(0));
199+
let counter = map.entry(qualified_method_name).or_insert_with(|| Box::new(0));
200200
&mut **counter as *mut u64
201201
}
202202

203203
#[unsafe(no_mangle)]
204-
pub extern "C" fn rb_zjit_count_unoptimized_cfunc(counter_ptr: *mut u64) {
204+
pub extern "C" fn rb_zjit_increment_dynamic_counter(counter_ptr: *mut u64) {
205205
unsafe { *counter_ptr += 1 }
206206
}
207207

0 commit comments

Comments
 (0)