Files
hakorune/src/mir/builder/observe/resolve.rs

72 lines
2.4 KiB
Rust
Raw Normal View History

use super::super::MirBuilder;
use crate::runtime::get_global_ring0;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::OnceLock;
// Dev-only KPI: resolve.choose Known rate
static TOTAL_CHOOSE: AtomicUsize = AtomicUsize::new(0);
static KNOWN_CHOOSE: AtomicUsize = AtomicUsize::new(0);
static KPI_ENABLED: OnceLock<bool> = OnceLock::new();
static SAMPLE_EVERY: OnceLock<usize> = OnceLock::new();
fn kpi_enabled() -> bool {
*KPI_ENABLED.get_or_init(|| std::env::var("NYASH_DEBUG_KPI_KNOWN").ok().as_deref() == Some("1"))
}
fn sample_every() -> usize {
*SAMPLE_EVERY.get_or_init(|| {
std::env::var("NYASH_DEBUG_SAMPLE_EVERY")
.ok()
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(0)
})
}
/// Devonly: emit a resolve.try eventcandidates inspection
pub(crate) fn emit_try(builder: &MirBuilder, meta: serde_json::Value) {
let fn_name = builder
fix(mir): Complete ScopeContext migration (Phase 2-4 補完) Phase 2-4 で移行漏れがあったファイルを修正。 ## Changes - Fixed all remaining compilation errors from incomplete Phase 2-4 migration - Updated access sites to use scope_ctx.* for 7 legacy fields: 1. current_function → scope_ctx.current_function 2. lexical_scope_stack → scope_ctx.lexical_scope_stack 3. if_merge_stack → scope_ctx.if_merge_stack 4. debug_scope_stack → scope_ctx.debug_scope_stack - Updated visibility of ScopeContext to pub(in crate::mir) for cross-module access - Removed dual-write legacy code in lexical_scope.rs, builder.rs - Updated documentation comments in phi_helpers.rs ## Files Modified (20 files) Core access migration: - src/mir/builder/method_call_handlers.rs - src/mir/builder/control_flow/joinir/routing.rs - src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs - src/mir/builder/if_form.rs - src/mir/builder/ops.rs (4 occurrences) - src/mir/builder/observe/resolve.rs (2 occurrences) - src/mir/builder/observe/ssa.rs - src/mir/builder/receiver.rs - src/mir/loop_api.rs (3 occurrences) - src/mir/region/observer.rs (3 occurrences) - src/mir/utils/control_flow.rs - src/mir/utils/phi_helpers.rs (4 occurrences + docs) Dual-write removal: - src/mir/builder/vars/lexical_scope.rs (push/pop/declare) - src/mir/builder.rs (if_merge, debug_scope, emit_instruction) Visibility updates: - src/mir/builder/scope_context.rs (struct + fields) ## Tests - cargo build --release: SUCCESS (0 errors, 191 warnings) - Phase 2-4 migration now fully complete - Note: Test failures exist but are unrelated (Phase 2-5 binding_map issue) Phase 2-4 now fully complete ✅
2025-12-16 03:33:56 +09:00
.scope_ctx.current_function
.as_ref()
.map(|f| f.signature.name.as_str());
let region = builder.debug_current_region_id();
crate::debug::hub::emit("resolve", "try", fn_name, region.as_deref(), meta);
}
/// Devonly: emit a resolve.choose eventdecision
pub(crate) fn emit_choose(builder: &MirBuilder, meta: serde_json::Value) {
let fn_name = builder
fix(mir): Complete ScopeContext migration (Phase 2-4 補完) Phase 2-4 で移行漏れがあったファイルを修正。 ## Changes - Fixed all remaining compilation errors from incomplete Phase 2-4 migration - Updated access sites to use scope_ctx.* for 7 legacy fields: 1. current_function → scope_ctx.current_function 2. lexical_scope_stack → scope_ctx.lexical_scope_stack 3. if_merge_stack → scope_ctx.if_merge_stack 4. debug_scope_stack → scope_ctx.debug_scope_stack - Updated visibility of ScopeContext to pub(in crate::mir) for cross-module access - Removed dual-write legacy code in lexical_scope.rs, builder.rs - Updated documentation comments in phi_helpers.rs ## Files Modified (20 files) Core access migration: - src/mir/builder/method_call_handlers.rs - src/mir/builder/control_flow/joinir/routing.rs - src/mir/builder/control_flow/joinir/merge/loop_header_phi_builder.rs - src/mir/builder/if_form.rs - src/mir/builder/ops.rs (4 occurrences) - src/mir/builder/observe/resolve.rs (2 occurrences) - src/mir/builder/observe/ssa.rs - src/mir/builder/receiver.rs - src/mir/loop_api.rs (3 occurrences) - src/mir/region/observer.rs (3 occurrences) - src/mir/utils/control_flow.rs - src/mir/utils/phi_helpers.rs (4 occurrences + docs) Dual-write removal: - src/mir/builder/vars/lexical_scope.rs (push/pop/declare) - src/mir/builder.rs (if_merge, debug_scope, emit_instruction) Visibility updates: - src/mir/builder/scope_context.rs (struct + fields) ## Tests - cargo build --release: SUCCESS (0 errors, 191 warnings) - Phase 2-4 migration now fully complete - Note: Test failures exist but are unrelated (Phase 2-5 binding_map issue) Phase 2-4 now fully complete ✅
2025-12-16 03:33:56 +09:00
.scope_ctx.current_function
.as_ref()
.map(|f| f.signature.name.as_str());
let region = builder.debug_current_region_id();
// KPI (dev-only)
record_kpi(&meta);
crate::debug::hub::emit("resolve", "choose", fn_name, region.as_deref(), meta);
}
/// Internal: Call from emit_choose wrapper to record KPI if enabled.
#[allow(dead_code)]
fn record_kpi(meta: &serde_json::Value) {
if !kpi_enabled() {
return;
}
let total = TOTAL_CHOOSE.fetch_add(1, Ordering::Relaxed) + 1;
let certainty = meta.get("certainty").and_then(|v| v.as_str()).unwrap_or("");
if certainty == "Known" {
KNOWN_CHOOSE.fetch_add(1, Ordering::Relaxed);
}
let n = sample_every();
if n > 0 && total % n == 0 {
let known = KNOWN_CHOOSE.load(Ordering::Relaxed);
let rate = if total > 0 {
(known as f64) * 100.0 / (total as f64)
} else {
0.0
};
get_global_ring0().log.info(&format!(
"[NYASH-KPI] resolve.choose Known={} Total={} ({:.1}%)",
known, total, rate
));
}
}