Files
hakorune/src/llvm_py/instructions/newbox.py

122 lines
4.0 KiB
Python
Raw Normal View History

"""
NewBox instruction lowering
Handles box creation (new StringBox(), new IntegerBox(), etc.)
"""
import llvmlite.ir as ir
from typing import Dict, List, Optional, Any
def lower_newbox(
builder: ir.IRBuilder,
module: ir.Module,
box_type: str,
args: List[int],
dst_vid: int,
vmap: Dict[int, ir.Value],
resolver=None,
ctx: Optional[Any] = None
) -> None:
"""
Lower MIR NewBox instruction
Creates a new box instance and returns its handle.
Args:
builder: Current LLVM IR builder
module: LLVM module
box_type: Box type name (e.g., "StringBox", "IntegerBox")
args: Constructor arguments
dst_vid: Destination value ID for box handle
vmap: Value map
resolver: Optional resolver for type handling
"""
🔍 Research: GPT-5-Codex capabilities and GitHub PR integration ## Summary Investigated OpenAI's new GPT-5-Codex model and Codex GitHub PR review integration capabilities. ## GPT-5-Codex Analysis ### Benchmark Performance (Good) - SWE-bench Verified: 74.5% (vs GPT-5's 72.8%) - Refactoring tasks: 51.3% (vs GPT-5's 33.9%) - Code review: Higher developer ratings ### Real-World Issues (Concerning) - Users report degraded coding performance - Scripts that previously worked now fail - Less consistent than GPT-4.5 - Longer response times (minutes vs instant) - "Creatively and emotionally flat" - Basic errors (e.g., counting letters incorrectly) ### Key Finding Classic case of "optimizing for benchmarks vs real usability" - scores well on tests but performs poorly in practice. ## Codex GitHub PR Integration ### Setup Process 1. Enable MFA and connect GitHub account 2. Authorize Codex GitHub app for repos 3. Enable "Code review" in repository settings ### Usage Methods - **Manual**: Comment '@codex review' in PR - **Automatic**: Triggers when PR moves from draft to ready ### Current Limitations - One-way communication (doesn't respond to review comments) - Prefers creating new PRs over updating existing ones - Better for single-pass reviews than iterative feedback ## 'codex resume' Feature New session management capability: - Resume previous codex exec sessions - Useful for continuing long tasks across days - Maintains context from interrupted work 🐱 The investigation reveals that while GPT-5-Codex shows benchmark improvements, practical developer experience has declined - a reminder that metrics don't always reflect real-world utility\!
2025-09-16 16:28:25 +09:00
# Use NyRT shim: prefer birth_h for core boxes, otherwise env.box.new_i64x
i64 = ir.IntType(64)
i8p = ir.IntType(8).as_pointer()
🔍 Research: GPT-5-Codex capabilities and GitHub PR integration ## Summary Investigated OpenAI's new GPT-5-Codex model and Codex GitHub PR review integration capabilities. ## GPT-5-Codex Analysis ### Benchmark Performance (Good) - SWE-bench Verified: 74.5% (vs GPT-5's 72.8%) - Refactoring tasks: 51.3% (vs GPT-5's 33.9%) - Code review: Higher developer ratings ### Real-World Issues (Concerning) - Users report degraded coding performance - Scripts that previously worked now fail - Less consistent than GPT-4.5 - Longer response times (minutes vs instant) - "Creatively and emotionally flat" - Basic errors (e.g., counting letters incorrectly) ### Key Finding Classic case of "optimizing for benchmarks vs real usability" - scores well on tests but performs poorly in practice. ## Codex GitHub PR Integration ### Setup Process 1. Enable MFA and connect GitHub account 2. Authorize Codex GitHub app for repos 3. Enable "Code review" in repository settings ### Usage Methods - **Manual**: Comment '@codex review' in PR - **Automatic**: Triggers when PR moves from draft to ready ### Current Limitations - One-way communication (doesn't respond to review comments) - Prefers creating new PRs over updating existing ones - Better for single-pass reviews than iterative feedback ## 'codex resume' Feature New session management capability: - Resume previous codex exec sessions - Useful for continuing long tasks across days - Maintains context from interrupted work 🐱 The investigation reveals that while GPT-5-Codex shows benchmark improvements, practical developer experience has declined - a reminder that metrics don't always reflect real-world utility\!
2025-09-16 16:28:25 +09:00
# Core fast paths
if box_type in ("ArrayBox", "MapBox"):
birth_name = "nyash.array.birth_h" if box_type == "ArrayBox" else "nyash.map.birth_h"
birth = None
for f in module.functions:
if f.name == birth_name:
birth = f
break
if not birth:
birth = ir.Function(module, ir.FunctionType(i64, []), name=birth_name)
handle = builder.call(birth, [], name=f"birth_{box_type}")
vmap[dst_vid] = handle
return
# Prefer variadic shim: nyash.env.box.new_i64x(type_name, argc, a1, a2, a3, a4)
new_i64x = None
for f in module.functions:
if f.name == "nyash.env.box.new_i64x":
new_i64x = f
break
if not new_i64x:
new_i64x = ir.Function(module, ir.FunctionType(i64, [i8p, i64, i64, i64, i64, i64]), name="nyash.env.box.new_i64x")
# Build C-string for type name (unique global per function)
sbytes = (box_type + "\0").encode('utf-8')
arr_ty = ir.ArrayType(ir.IntType(8), len(sbytes))
try:
fn = builder.block.parent
fn_name = getattr(fn, 'name', 'fn')
except Exception:
fn_name = 'fn'
base = f".box_ty_{fn_name}_{dst_vid}"
existing = {g.name for g in module.global_values}
name = base
n = 1
while name in existing:
name = f"{base}.{n}"; n += 1
g = ir.GlobalVariable(module, arr_ty, name=name)
g.linkage = 'private'
g.global_constant = True
g.initializer = ir.Constant(arr_ty, bytearray(sbytes))
c0 = ir.Constant(ir.IntType(32), 0)
ptr = builder.gep(g, [c0, c0], inbounds=True)
zero = ir.Constant(i64, 0)
handle = builder.call(new_i64x, [ptr, zero, zero, zero, zero, zero], name=f"new_{box_type}")
vmap[dst_vid] = handle
feat(phase21.5): strlen FAST EXE + loop JSONFrag diagnostics ## Task A: emit v0 boxcall (bin version) ✅ - Fix: emit_mir_json_for_harness_bin now handles I::Call with Callee::Method - Added: Proper v0 boxcall emission when NYASH_MIR_UNIFIED_CALL=0 - Location: src/runner/mir_json_emit.rs:641-707 - Test: emit_boxcall_length_canary_vm.sh → PASS ## Task B: strlen FAST EXE (AOT without plugin) ✅ - Fix: FAST lowering now tracks newbox(StringBox) creation - Added: newbox_string_args fallback in boxcall.py (lines 133-143) - Added: StringBox tracking in newbox.py (lines 82-91) - Benefit: EXE can compute string.length() without StringBox plugin - Test: s3_backend_selector_crate_exe_strlen_fast_canary_vm.sh → PASS (rc=5) ## Task 1: selfhost-first Diagnostic Logging ✅ - Added: HAKO_SELFHOST_TRACE=1 outputs Program JSON stats - Added: HAKO_SELFHOST_NO_DELEGATE=1 shows detailed failure logs - Added: [builder/selfhost-first:fail:*] markers + last 80 lines - Location: tools/hakorune_emit_mir.sh:try_selfhost_builder() ## Task 2: loop JsonFrag Hit Rate Improvement ✅ - Added: FORCE=1 fallback for non-Lt comparison operators - Added: find_any_local_int_before() fallback when strict fails - Location: lang/src/mir/builder/internal/lower_loop_simple_box.hako - Benefit: Higher JSONFrag hit rate under HAKO_MIR_BUILDER_LOOP_FORCE_JSONFRAG=1 ## Task 3: crate EXE Failure Diagnostics ✅ - Added: LLVM IR dump on build failure (first 120 lines) - Added: Build error log capture (last 40 lines) - Location: tools/smokes/v2/profiles/quick/core/phase2100/stageb_loop_jsonfrag_crate_exe_canary_vm.sh ## Test Results - emit_boxcall_length: PASS ✅ - strlen_fast (FAST=1): PASS (rc=5) ✅ - loop_jsonfrag: SKIP (diagnostic enhanced) ⚠️ ## Implementation Principles - 既定挙動不変 (Default unchanged) - Dev toggle guarded (FAST=1, FORCE=1, TRACE=1, NO_DELEGATE=1) - Minimal diff, easy rollback - Clear failure diagnostics for future fixes 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-11 05:50:23 +09:00
# Track StringBox creation for FAST path optimization
# If newbox(StringBox, [string_arg]), store dst_vid -> string_arg mapping
if box_type == "StringBox" and args and resolver is not None:
try:
if not hasattr(resolver, 'newbox_string_args'):
resolver.newbox_string_args = {}
# Map the resulting box handle to the string argument
resolver.newbox_string_args[dst_vid] = args[0]
except Exception:
pass # Silently ignore failures
def lower_newbox_generic(
builder: ir.IRBuilder,
module: ir.Module,
dst_vid: int,
vmap: Dict[int, ir.Value]
) -> None:
"""
Create a generic box with runtime allocation
This is used when box type is not statically known.
"""
# Look up generic allocation function
alloc_func = None
for f in module.functions:
if f.name == "ny_alloc_box":
alloc_func = f
break
if not alloc_func:
# Declare ny_alloc_box(size: i64) -> i64
i64 = ir.IntType(64)
func_type = ir.FunctionType(i64, [i64])
alloc_func = ir.Function(module, func_type, name="ny_alloc_box")
# Default box size (e.g., 64 bytes)
size = ir.Constant(ir.IntType(64), 64)
handle = builder.call(alloc_func, [size], name="new_box")
vmap[dst_vid] = handle