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

View File

@ -0,0 +1,67 @@
# Benchmarking MIR Generation & AOT (Quickstart)
This guide shows how to measure Hakorune's MIR emit (StageB → MIR(JSON)) and AOT build (MIR(JSON) → obj/exe) without llvmlite. All commands are semanticspreserving and keep defaults conservative (failfast, O0).
## Prerequisites
- Build binaries once (release):
- `cargo build --release`
- `cargo build --release -p nyash-llvm-compiler` (ny-llvmc)
- `cargo build --release -p nyash_kernel` (NyRT static runtime)
## Scripts
### 1) MIR emit bench (StageB → MIR(JSON))
- Script: `tools/perf/bench_hakorune_emit_mir.sh`
- Usage: `tools/perf/bench_hakorune_emit_mir.sh <input.hako> [rounds]`
- Output CSV: `round,ms,size,sha1` (sha1 is normalized JSON digest; identical = structure equal)
- Useful env toggles:
- `HAKO_USING_RESOLVER_FIRST=1` (resolverfirst)
- `HAKO_SELFHOST_BUILDER_FIRST=1` (selfhost builder → provider fallback)
- `HAKO_MIR_BUILDER_BOX=hako.mir.builder|min` (builder selector)
- `HAKO_SELFHOST_TRACE=1` (stderr trace)
Example:
```
tools/perf/bench_hakorune_emit_mir.sh apps/examples/json_query/main.hako 5
```
### 2) MIR(JSON) → obj/exe benchny-llvmc / crate backend
- Script: `tools/perf/bench_ny_mir_builder.sh`
- Usage: `tools/perf/bench_ny_mir_builder.sh <mir.json> [rounds]`
- Output CSV: `kind,round,ms`kind = obj|exe
- Useful env toggles:
- `NYASH_LLVM_BACKEND=crate`既定。ny-llvmc を使う)
- `HAKO_LLVM_OPT_LEVEL=0|1`(既定は 0O0
Example:
```
tools/perf/bench_ny_mir_builder.sh /path/to/out.json 3
```
### 3) MIR(JSON) 構造比較
- Script: `tools/perf/compare_mir_json.sh`
- Usage: `tools/perf/compare_mir_json.sh <a.json> <b.json>`
- 出力: サイズと正規化 SHA1、差分jq -S 利用時は整形差分)。
## Typical Workflow
1) Emit MIR(JSON)
- `tools/hakorune_emit_mir.sh apps/APP/main.hako out.json`
2) Measure MIR emit time
- `HAKO_USING_RESOLVER_FIRST=1 tools/perf/bench_hakorune_emit_mir.sh apps/APP/main.hako 5`
3) Measure AOTobj/exe
- `NYASH_LLVM_BACKEND=crate tools/perf/bench_ny_mir_builder.sh out.json 3`
4) Compare MIR outputs across toggles/branches
- `tools/perf/compare_mir_json.sh out_before.json out_after.json`
## Notes
- All benches are besteffort micromeasurements; run multiple rounds and compare medians.
- Keep defaults strict: resolver/selfhost togglesは明示時のみON。AOTは O0 既定(`HAKO_LLVM_OPT_LEVEL` で上げられます)。