Phase 21.7 normalization: optimization pre-work + bench harness expansion

- Add opt-in optimizations (defaults OFF)
  - Ret purity verifier: NYASH_VERIFY_RET_PURITY=1
  - strlen FAST enhancement for const handles
  - FAST_INT gate for same-BB SSA optimization
  - length cache for string literals in llvmlite
- Expand bench harness (tools/perf/microbench.sh)
  - Add branch/call/stringchain/arraymap/chip8/kilo cases
  - Auto-calculate ratio vs C reference
  - Document in benchmarks/README.md
- Compiler health improvements
  - Unify PHI insertion to insert_phi_at_head()
  - Add NYASH_LLVM_SKIP_BUILD=1 for build reuse
- Runtime & safety enhancements
  - Clarify Rust/Hako ownership boundaries
  - Strengthen receiver localization (LocalSSA/pin/after-PHIs)
  - Stop excessive PluginInvoke→BoxCall rewrites
- Update CURRENT_TASK.md, docs, and canaries

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
nyash-codex
2025-11-13 16:40:58 +09:00
parent 9e2fa1e36e
commit dda65b94b7
160 changed files with 6773 additions and 1692 deletions

81
tools/perf/dump_mir.sh Normal file
View File

@ -0,0 +1,81 @@
#!/usr/bin/env bash
set -euo pipefail
# dump_mir.sh — Stable helper to emit MIR(JSON) and print a quick histogram
#
# Usage:
# tools/perf/dump_mir.sh <input.hako> [--out out.json] [--mode {provider|jsonfrag}]
#
# Notes:
# - provider: 普通の MirBuilder ルート(失敗する環境では自動で jsonfrag にフォールバック)
# - jsonfrag : ループを while-form に純化した最小 MIR構造検証用
INPUT="${1:-}"
OUT=""
MODE="provider"
shift || true
while [[ $# -gt 0 ]]; do
case "$1" in
--out) OUT="$2"; shift 2;;
--mode) MODE="$2"; shift 2;;
-h|--help) echo "Usage: $0 <input.hako> [--out out.json] [--mode {provider|jsonfrag}]"; exit 0;;
*) echo "Unknown arg: $1"; exit 2;;
esac
done
if [[ -z "$INPUT" || ! -f "$INPUT" ]]; then
echo "[FAIL] input .hako not found: $INPUT" >&2; exit 2
fi
ROOT="$(git -C "$(dirname "$0")" rev-parse --show-toplevel 2>/dev/null || true)"
[[ -z "$ROOT" ]] && ROOT="$(cd "$(dirname "$0")/../.." && pwd)"
TMP_OUT=$(mktemp --suffix .mir.json)
trap 'rm -f "$TMP_OUT" >/dev/null 2>&1 || true' EXIT
emit_provider() {
# Provider/selfhost-first with min fallback; keep plugins ON to satisfy core boxes
set +e
NYASH_SKIP_TOML_ENV=1 NYASH_DISABLE_PLUGINS=0 NYASH_ENABLE_USING=1 HAKO_ENABLE_USING=1 \
HAKO_SELFHOST_BUILDER_FIRST=1 HAKO_SELFHOST_TRY_MIN=1 HAKO_MIR_NORMALIZE_PROVIDER=0 NYASH_JSON_ONLY=1 \
"$ROOT/tools/hakorune_emit_mir.sh" "$INPUT" "$TMP_OUT" >/dev/null 2>&1
local rc=$?
set -e
return $rc
}
emit_jsonfrag() {
NYASH_SKIP_TOML_ENV=1 NYASH_DISABLE_PLUGINS=1 \
HAKO_SELFHOST_BUILDER_FIRST=1 HAKO_MIR_BUILDER_LOOP_JSONFRAG=1 HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG=1 \
HAKO_MIR_BUILDER_JSONFRAG_PURIFY=1 NYASH_JSON_ONLY=1 \
"$ROOT/tools/hakorune_emit_mir.sh" "$INPUT" "$TMP_OUT" >/dev/null
}
if [[ "$MODE" = "provider" ]]; then
if ! emit_provider; then
echo "[WARN] provider emit failed; falling back to jsonfrag" >&2
emit_jsonfrag
fi
else
emit_jsonfrag
fi
if [[ -n "$OUT" ]]; then
cp -f "$TMP_OUT" "$OUT"
echo "[OK] MIR JSON -> $OUT"
fi
# Print a quick histogram
python3 - "$TMP_OUT" <<'PY'
import json,sys
p=sys.argv[1]
j=json.load(open(p))
for f in j.get('functions',[]):
print('Function:', f.get('name'))
for b in (f.get('blocks') or []):
ops=[(i or {}).get('op') for i in (b.get('instructions') or [])]
if not ops: continue
from collections import Counter
c=Counter(ops)
print(' bb', b.get('id'), dict(c))
PY