Stage-A selfhost emitter fixes and LLVM opt toggle
This commit is contained in:
@ -41,6 +41,42 @@ from build_ctx import BuildCtx
|
||||
from resolver import Resolver
|
||||
from mir_reader import MIRReader
|
||||
|
||||
_OPT_ENV_KEYS = ("HAKO_LLVM_OPT_LEVEL", "NYASH_LLVM_OPT_LEVEL")
|
||||
|
||||
def _parse_opt_level_env() -> int:
|
||||
"""Return desired optimization level (0-3). Defaults to 2."""
|
||||
for key in _OPT_ENV_KEYS:
|
||||
raw = os.environ.get(key)
|
||||
if not raw:
|
||||
continue
|
||||
value = raw.strip()
|
||||
if not value:
|
||||
continue
|
||||
upper = value.upper()
|
||||
if upper.startswith("O"):
|
||||
value = upper[1:]
|
||||
try:
|
||||
lvl = int(value)
|
||||
except ValueError:
|
||||
continue
|
||||
if lvl < 0:
|
||||
lvl = 0
|
||||
if lvl > 3:
|
||||
lvl = 3
|
||||
return lvl
|
||||
return 2
|
||||
|
||||
def _resolve_codegen_opt_level():
|
||||
"""Map env level to llvmlite CodeGenOptLevel enum (fallback to int)."""
|
||||
level = _parse_opt_level_env()
|
||||
try:
|
||||
names = {0: "None", 1: "Less", 2: "Default", 3: "Aggressive"}
|
||||
enum = getattr(llvm, "CodeGenOptLevel")
|
||||
attr = names.get(level, "Default")
|
||||
return getattr(enum, attr)
|
||||
except Exception:
|
||||
return level
|
||||
|
||||
class NyashLLVMBuilder:
|
||||
"""Main LLVM IR builder for Nyash MIR"""
|
||||
|
||||
@ -627,7 +663,11 @@ class NyashLLVMBuilder:
|
||||
"""Compile module to object file"""
|
||||
# Create target machine
|
||||
target = llvm.Target.from_default_triple()
|
||||
target_machine = target.create_target_machine()
|
||||
target_machine = target.create_target_machine(opt=_resolve_codegen_opt_level())
|
||||
try:
|
||||
trace_debug(f"[Python LLVM] opt-level={_parse_opt_level_env()}")
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Compile
|
||||
ir_text = str(self.module)
|
||||
|
||||
Reference in New Issue
Block a user