mirror of
https://git.sr.ht/~kivikakk/niar
synced 2024-12-22 18:22:24 +00:00
cxxrtl: opt is between RTL and code.
RTL used to mean "-O3 on the CXXRTL-generated .cc". Now it means running an opt pass in Yosys. Code covers all code: C++ (and Zig if used).
This commit is contained in:
parent
34b8c3b3f2
commit
359962b0fe
|
@ -1,13 +1,13 @@
|
||||||
|
import argparse
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
import shutil
|
||||||
from enum import Enum
|
from enum import Enum, nonmember
|
||||||
from functools import partial
|
from functools import partial
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from amaranth._toolchain.yosys import YosysBinary, find_yosys
|
from amaranth._toolchain.yosys import find_yosys
|
||||||
from amaranth.back import rtlil
|
from amaranth.back import rtlil
|
||||||
|
|
||||||
from .build import construct_top
|
from .build import construct_top
|
||||||
|
@ -29,21 +29,36 @@ CXXFLAGS = [
|
||||||
|
|
||||||
|
|
||||||
class _Optimize(Enum):
|
class _Optimize(Enum):
|
||||||
none = "none"
|
|
||||||
rtl = "rtl"
|
rtl = "rtl"
|
||||||
app = "app"
|
code = "code"
|
||||||
both = "both"
|
none = "none"
|
||||||
|
all = "all"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
@property
|
@nonmember
|
||||||
def opt_rtl(self) -> bool:
|
class ArgparseAction(argparse.Action):
|
||||||
return self in (self.rtl, self.both)
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
match (values, namespace.optimize):
|
||||||
|
case (_Optimize.rtl, _Optimize.code) | (_Optimize.code, _Optimize.rtl):
|
||||||
|
# Choosing rtl or code when the other is chosen becomes all.
|
||||||
|
newval = _Optimize.all
|
||||||
|
case _:
|
||||||
|
# Other cases are:
|
||||||
|
# Choosing all or none always sets it, regardless of old value.
|
||||||
|
# Choosing rtl or code when old value is all/none/same always sets it.
|
||||||
|
newval = values
|
||||||
|
|
||||||
|
setattr(namespace, self.dest, newval)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def opt_app(self) -> bool:
|
def opt_rtl(self) -> bool:
|
||||||
return self in (self.app, self.both)
|
return self in (self.rtl, self.all)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def opt_code(self) -> bool:
|
||||||
|
return self in (self.code, self.all)
|
||||||
|
|
||||||
|
|
||||||
def add_arguments(np: Project, parser):
|
def add_arguments(np: Project, parser):
|
||||||
|
@ -69,10 +84,11 @@ def add_arguments(np: Project, parser):
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-O",
|
"-O",
|
||||||
"--optimize",
|
"--optimize",
|
||||||
|
action=_Optimize.ArgparseAction,
|
||||||
type=_Optimize,
|
type=_Optimize,
|
||||||
choices=_Optimize,
|
choices=_Optimize,
|
||||||
help="build with optimizations (default: rtl)",
|
default=_Optimize.all,
|
||||||
default=_Optimize.rtl,
|
help="build with optimizations — may be specified multiple times. default if unspecified: all.",
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
|
@ -161,7 +177,7 @@ def main(np: Project, args):
|
||||||
|
|
||||||
cxxflags = CXXFLAGS + [
|
cxxflags = CXXFLAGS + [
|
||||||
f"-DCLOCK_HZ={int(platform.default_clk_frequency)}",
|
f"-DCLOCK_HZ={int(platform.default_clk_frequency)}",
|
||||||
*(["-O3"] if args.optimize.opt_rtl else ["-O0"]),
|
*(["-O3"] if args.optimize.opt_code else ["-O0"]),
|
||||||
*(["-g"] if args.debug else []),
|
*(["-g"] if args.debug else []),
|
||||||
]
|
]
|
||||||
if platform.uses_zig:
|
if platform.uses_zig:
|
||||||
|
@ -213,7 +229,7 @@ def main(np: Project, args):
|
||||||
# Zig really wants relative paths.
|
# Zig really wants relative paths.
|
||||||
f"-Dcxxrtl_o_path=../{p.relative_to(np.path())}" for p in cc_o_paths
|
f"-Dcxxrtl_o_path=../{p.relative_to(np.path())}" for p in cc_o_paths
|
||||||
]
|
]
|
||||||
if args.optimize.opt_app:
|
if args.optimize.opt_code:
|
||||||
cmd += ["-Doptimize=ReleaseFast"]
|
cmd += ["-Doptimize=ReleaseFast"]
|
||||||
outf = "cxxrtl/zig-out/bin/cxxrtl"
|
outf = "cxxrtl/zig-out/bin/cxxrtl"
|
||||||
cr.add_process(cmd,
|
cr.add_process(cmd,
|
||||||
|
@ -226,7 +242,7 @@ def main(np: Project, args):
|
||||||
cmd = [
|
cmd = [
|
||||||
"c++",
|
"c++",
|
||||||
# Hard to imagine these flags having any effect.
|
# Hard to imagine these flags having any effect.
|
||||||
*(["-O3"] if args.optimize.opt_app else ["-O0"]),
|
*(["-O3"] if args.optimize.opt_code else ["-O0"]),
|
||||||
*(["-g"] if args.debug else []),
|
*(["-g"] if args.debug else []),
|
||||||
*cc_o_paths,
|
*cc_o_paths,
|
||||||
"-o",
|
"-o",
|
||||||
|
|
Loading…
Reference in a new issue