runner(cli): adopt CliConfig::as_groups across runner modules (dispatch/common/pipe_io/mir/bench). llvm-builder: extract ny_main wrapper to builders.entry; add optional env-gated function_lower delegation; keep default behavior unchanged

This commit is contained in:
Selfhosting Dev
2025-09-19 14:29:02 +09:00
parent c8c77d89a6
commit 3c7a5de900
11 changed files with 411 additions and 104 deletions

View File

@ -125,66 +125,35 @@ class NyashLLVMBuilder:
for func_data in functions:
self.lower_function(func_data)
# Create ny_main wrapper if necessary
has_ny_main = any(f.name == 'ny_main' for f in self.module.functions)
# Prefer static box entry: Main.main/1; fallback to plain main (0-arity)
fn_main_box = None
fn_main_plain = None
for f in self.module.functions:
if f.name == 'Main.main/1':
fn_main_box = f
elif f.name == 'main':
fn_main_plain = f
target_fn = fn_main_box or fn_main_plain
if target_fn is not None and not has_ny_main:
# Hide the target to avoid symbol conflicts
# Create ny_main wrapper if necessary (extracted helper)
try:
from builders.entry import ensure_ny_main as _ensure_ny_main
_ensure_ny_main(self)
except Exception:
# Fallback to legacy in-place logic if helper import fails
try:
target_fn.linkage = 'private'
has_ny_main = any(f.name == 'ny_main' for f in self.module.functions)
fn_main_box = None
fn_main_plain = None
for f in self.module.functions:
if f.name == 'Main.main/1':
fn_main_box = f
elif f.name == 'main':
fn_main_plain = f
target_fn = fn_main_box or fn_main_plain
if target_fn is not None and not has_ny_main:
ny_main_ty = ir.FunctionType(self.i64, [])
ny_main = ir.Function(self.module, ny_main_ty, name='ny_main')
entry = ny_main.append_basic_block('entry')
b = ir.IRBuilder(entry)
rv = ir.Constant(self.i64, 0)
if fn_main_box is not None:
rv = b.call(fn_main_box, [], name='call_Main_main_1')
elif fn_main_plain is not None and len(fn_main_plain.args) == 0:
rv = b.call(fn_main_plain, [], name='call_user_main')
b.ret(rv)
except Exception:
pass
# i32 ny_main() { return (i32) Main.main(args) | main(); }
ny_main_ty = ir.FunctionType(self.i64, [])
ny_main = ir.Function(self.module, ny_main_ty, name='ny_main')
entry = ny_main.append_basic_block('entry')
b = ir.IRBuilder(entry)
if fn_main_box is not None:
# Build default args = new ArrayBox() via nyash.env.box.new_i64x
i64 = self.i64
i8 = self.i8
i8p = self.i8p
# Declare callee
callee = None
for f in self.module.functions:
if f.name == 'nyash.env.box.new_i64x':
callee = f
break
if callee is None:
callee = ir.Function(self.module, ir.FunctionType(i64, [i8p, i64, i64, i64, i64, i64]), name='nyash.env.box.new_i64x')
# Create "ArrayBox\0" global
sbytes = b"ArrayBox\0"
arr_ty = ir.ArrayType(i8, len(sbytes))
g = ir.GlobalVariable(self.module, arr_ty, name='.ny_main_arraybox')
g.linkage = 'private'
g.global_constant = True
g.initializer = ir.Constant(arr_ty, bytearray(sbytes))
c0 = ir.Constant(self.i32, 0)
ptr = b.gep(g, [c0, c0], inbounds=True)
zero = ir.Constant(i64, 0)
args_handle = b.call(callee, [ptr, zero, zero, zero, zero, zero], name='ny_main_args')
rv = b.call(fn_main_box, [args_handle], name='call_Main_main_1')
else:
# Plain main() fallback
if len(fn_main_plain.args) == 0:
rv = b.call(fn_main_plain, [], name='call_user_main')
else:
rv = ir.Constant(self.i64, 0)
if hasattr(rv, 'type') and isinstance(rv.type, ir.IntType) and rv.type.width != 32:
rv64 = b.trunc(rv, self.i64) if rv.type.width > 64 else b.zext(rv, self.i64)
b.ret(rv64)
elif hasattr(rv, 'type') and isinstance(rv.type, ir.IntType) and rv.type.width == 64:
b.ret(rv)
else:
b.ret(ir.Constant(self.i64, 0))
ir_text = str(self.module)
# Optional IR dump to file for debugging
@ -215,6 +184,16 @@ class NyashLLVMBuilder:
def lower_function(self, func_data: Dict[str, Any]):
"""Lower a single MIR function to LLVM IR"""
# Optional: delegate to external helper when gated (incremental split)
try:
if os.environ.get('NYASH_LLVM_USE_HELPER_LOWER') == '1':
try:
from builders.function_lower import lower_function as _lower
return _lower(self, func_data)
except Exception as _e:
trace_debug(f"[Python LLVM] helper lower_function failed, falling back: {_e}")
except Exception:
pass
name = func_data.get("name", "unknown")
self.current_function_name = name
import re