Phase 25.1a: selfhost builder hotfix (fn rename, docs)
This commit is contained in:
@ -263,15 +263,23 @@ pub(super) fn lower_program(prog: ProgramV0, imports: std::collections::HashMap<
|
||||
}
|
||||
let env = BridgeEnv::with_imports(imports);
|
||||
let mut module = MirModule::new("ny_json_v0".into());
|
||||
// Treat CLI entry as taking a single parameter `args`.
|
||||
let sig = FunctionSignature {
|
||||
name: "main".into(),
|
||||
params: vec![],
|
||||
params: vec![MirType::Unknown],
|
||||
return_type: MirType::Integer,
|
||||
effects: EffectMask::PURE,
|
||||
};
|
||||
let entry = BasicBlockId::new(0);
|
||||
let mut f = MirFunction::new(sig, entry);
|
||||
let mut var_map: HashMap<String, ValueId> = HashMap::new();
|
||||
// Stage-3 programs (launcher / CLI entry) implicitly reference `args`.
|
||||
let args_param = ValueId::new(1);
|
||||
f.params = vec![args_param];
|
||||
if f.next_value_id < 2 {
|
||||
f.next_value_id = 2;
|
||||
}
|
||||
var_map.insert("args".into(), args_param);
|
||||
let mut loop_stack: Vec<LoopContext> = Vec::new();
|
||||
let start_bb = f.entry_block;
|
||||
let end_bb = lower_stmt_list_with_vars(
|
||||
|
||||
@ -28,6 +28,68 @@ fn create_json_v1_root(functions: serde_json::Value) -> serde_json::Value {
|
||||
})
|
||||
}
|
||||
|
||||
/// Helper: detect residual numeric-core boxcalls that should have been lowered by AotPrepNumericCoreBox.
|
||||
/// Currently we only check for `boxcall` with `method:"mul_naive"` which should become
|
||||
/// `call("NyNumericMatI64.mul_naive", ...)` when NYASH_AOT_NUMERIC_CORE=1 is effective.
|
||||
fn has_numeric_core_boxcall(root: &serde_json::Value) -> bool {
|
||||
let funs = match root.get("functions") {
|
||||
Some(v) => v.as_array().cloned().unwrap_or_default(),
|
||||
None => return false,
|
||||
};
|
||||
for f in funs {
|
||||
let blocks = match f.get("blocks").and_then(|b| b.as_array()) {
|
||||
Some(b) => b,
|
||||
None => continue,
|
||||
};
|
||||
for b in blocks {
|
||||
let insts = match b.get("instructions").and_then(|i| i.as_array()) {
|
||||
Some(i) => i,
|
||||
None => continue,
|
||||
};
|
||||
for inst in insts {
|
||||
let op = inst.get("op").and_then(|v| v.as_str());
|
||||
let method = inst.get("method").and_then(|v| v.as_str());
|
||||
if op == Some("boxcall") && method == Some("mul_naive") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
/// Helper: enforce numeric_core invariants when NYASH_AOT_NUMERIC_CORE=1 is set.
|
||||
/// - Default: emit a warning if mul_naive boxcalls are still present.
|
||||
/// - Strict: if NYASH_AOT_NUMERIC_CORE_STRICT=1, return Err to fail fast.
|
||||
fn check_numeric_core_invariants(root: &serde_json::Value) -> Result<(), String> {
|
||||
let numeric_on = matches!(std::env::var("NYASH_AOT_NUMERIC_CORE").ok().as_deref(), Some("1"));
|
||||
if !numeric_on {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !has_numeric_core_boxcall(root) {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let strict = matches!(
|
||||
std::env::var("NYASH_AOT_NUMERIC_CORE_STRICT").ok().as_deref(),
|
||||
Some("1")
|
||||
);
|
||||
|
||||
eprintln!(
|
||||
"[mir_json/numeric_core] NYASH_AOT_NUMERIC_CORE=1 but MIR JSON still contains boxcall(\"mul_naive\"). \
|
||||
AotPrepNumericCoreBox may not have run or did not match; inspect AotPrep logs or run tools/hakorune_emit_mir.sh with HAKO_SELFHOST_TRACE=1."
|
||||
);
|
||||
|
||||
if strict {
|
||||
return Err(
|
||||
"NYASH_AOT_NUMERIC_CORE_STRICT=1: numeric_core invariants violated (mul_naive boxcall remains)"
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Helper: Emit unified mir_call JSON (v1 format)
|
||||
/// Supports all 6 Callee types in a single unified JSON structure
|
||||
fn emit_unified_mir_call(
|
||||
@ -490,6 +552,11 @@ pub fn emit_mir_json_for_harness(
|
||||
json!({"functions": funs}) // v0 legacy format
|
||||
};
|
||||
|
||||
// NOTE: numeric_core strict validation is applied on the AotPrep output
|
||||
// (tools/hakorune_emit_mir.sh) rather than at raw MIR emit time. This keeps
|
||||
// pre-AotPrep MIR emission usable even when BoxCall(MatI64, mul_naive) is
|
||||
// still present.
|
||||
|
||||
std::fs::write(path, serde_json::to_string_pretty(&root).unwrap())
|
||||
.map_err(|e| format!("write mir json: {}", e))
|
||||
}
|
||||
@ -796,6 +863,12 @@ pub fn emit_mir_json_for_harness_bin(
|
||||
funs.push(json!({"name": name, "params": params, "blocks": blocks}));
|
||||
}
|
||||
let root = json!({"functions": funs});
|
||||
|
||||
// NOTE: numeric_core strict validation is applied on the AotPrep output
|
||||
// (tools/hakorune_emit_mir.sh) rather than at raw MIR emit time. This keeps
|
||||
// pre-AotPrep MIR emission usable even when BoxCall(MatI64, mul_naive) is
|
||||
// still present.
|
||||
|
||||
std::fs::write(path, serde_json::to_string_pretty(&root).unwrap())
|
||||
.map_err(|e| format!("write mir json: {}", e))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user