Refine normalized bridge direct path and env guard

This commit is contained in:
nyash-codex
2025-12-11 22:50:23 +09:00
parent a4756f3ce1
commit 12e2f87c6f
7 changed files with 484 additions and 60 deletions

View File

@ -6,6 +6,7 @@ use nyash_rust::mir::join_ir::{
normalized_pattern2_to_structured, BinOpKind, ConstValue, JoinContId, JoinFuncId,
JoinFunction, JoinInst, JoinIrPhase, JoinModule, MirLikeInst,
};
use nyash_rust::mir::join_ir::normalized::dev_env::NormalizedDevEnvGuard;
use nyash_rust::mir::join_ir::normalized::fixtures::{
build_jsonparser_atoi_structured_for_normalized_dev,
build_jsonparser_skip_ws_structured_for_normalized_dev,
@ -15,39 +16,6 @@ use nyash_rust::mir::join_ir_runner::run_joinir_function;
use nyash_rust::mir::join_ir_ops::JoinValue;
use nyash_rust::mir::join_ir_vm_bridge::run_joinir_via_vm;
use nyash_rust::mir::ValueId;
use once_cell::sync::Lazy;
use std::sync::Mutex;
static NORMALIZED_ENV_LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
struct NormalizedRunGuard {
_lock: std::sync::MutexGuard<'static, ()>,
prev: Option<String>,
}
impl NormalizedRunGuard {
fn new(enabled: bool) -> Self {
let lock = NORMALIZED_ENV_LOCK.lock().expect("env mutex poisoned");
let prev = std::env::var("NYASH_JOINIR_NORMALIZED_DEV_RUN").ok();
if enabled {
std::env::set_var("NYASH_JOINIR_NORMALIZED_DEV_RUN", "1");
} else {
std::env::remove_var("NYASH_JOINIR_NORMALIZED_DEV_RUN");
}
Self { _lock: lock, prev }
}
}
impl Drop for NormalizedRunGuard {
fn drop(&mut self) {
if let Some(prev) = &self.prev {
std::env::set_var("NYASH_JOINIR_NORMALIZED_DEV_RUN", prev);
} else {
std::env::remove_var("NYASH_JOINIR_NORMALIZED_DEV_RUN");
}
}
}
fn assert_normalized_dev_ready() {
assert!(
nyash_rust::config::env::normalized_dev_enabled(),
@ -61,7 +29,7 @@ fn run_joinir_runner(
args: &[JoinValue],
normalized: bool,
) -> JoinValue {
let _guard = NormalizedRunGuard::new(normalized);
let _guard = NormalizedDevEnvGuard::new(normalized);
if normalized {
assert_normalized_dev_ready();
}
@ -75,7 +43,7 @@ fn run_joinir_vm_bridge(
args: &[JoinValue],
normalized: bool,
) -> JoinValue {
let _guard = NormalizedRunGuard::new(normalized);
let _guard = NormalizedDevEnvGuard::new(normalized);
if normalized {
assert_normalized_dev_ready();
}
@ -160,10 +128,8 @@ fn normalized_pattern1_exec_matches_structured() {
let entry = structured.entry.unwrap_or(JoinFuncId::new(1));
let input = [JoinValue::Int(0)];
let result_structured =
run_joinir_via_vm(&structured, entry, &input).expect("structured run should succeed");
let result_norm = run_joinir_via_vm(&reconstructed, entry, &input)
.expect("normalized roundtrip run should succeed");
let result_structured = run_joinir_vm_bridge(&structured, entry, &input, false);
let result_norm = run_joinir_vm_bridge(&reconstructed, entry, &input, false);
assert_eq!(result_structured, result_norm);
}
@ -180,9 +146,9 @@ fn normalized_pattern1_exec_matches_structured_roundtrip_backup() {
let entry = structured.entry.unwrap_or(JoinFuncId::new(1));
let input = [JoinValue::Int(0)];
let base = run_joinir_via_vm(&structured, entry, &input).expect("structured run");
let recon = run_joinir_via_vm(&reconstructed, entry, &input).expect("reconstructed run");
let restored = run_joinir_via_vm(&restored_backup, entry, &input).expect("backup run");
let base = run_joinir_vm_bridge(&structured, entry, &input, false);
let recon = run_joinir_vm_bridge(&reconstructed, entry, &input, false);
let restored = run_joinir_vm_bridge(&restored_backup, entry, &input, false);
assert_eq!(base, recon);
assert_eq!(base, restored);
@ -218,8 +184,8 @@ fn normalized_pattern2_exec_matches_structured() {
let entry = structured.entry.unwrap_or(JoinFuncId::new(0));
let input = [JoinValue::Int(0)];
let base = run_joinir_via_vm(&structured, entry, &input).expect("structured run");
let recon = run_joinir_via_vm(&reconstructed, entry, &input).expect("normalized roundtrip run");
let base = run_joinir_vm_bridge(&structured, entry, &input, false);
let recon = run_joinir_vm_bridge(&reconstructed, entry, &input, false);
assert_eq!(base, recon);
}
@ -264,10 +230,8 @@ fn normalized_pattern2_real_loop_exec_matches_structured() {
for n in cases {
let input = [JoinValue::Int(n)];
let base =
run_joinir_via_vm(&structured, entry, &input).expect("structured execution should pass");
let recon = run_joinir_via_vm(&reconstructed, entry, &input)
.expect("normalized roundtrip execution should pass");
let base = run_joinir_vm_bridge(&structured, entry, &input, false);
let recon = run_joinir_vm_bridge(&reconstructed, entry, &input, false);
assert_eq!(base, recon, "mismatch at n={}", n);
let expected_sum = n * (n.saturating_sub(1)) / 2;