Skip to content

Commit

Permalink
introduce riscv_utils and improve xlen/features handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippvK committed Aug 20, 2024
1 parent 8122a69 commit 7adc0f2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 55 deletions.
32 changes: 16 additions & 16 deletions seal5/backends/llvmir/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from seal5.tools import cdsl2llvm
from seal5.model import Seal5InstrAttribute
from seal5.riscv_utils import build_mattr, get_riscv_defaults

logger = logging.getLogger("llvmir_behavior_writer")

Expand Down Expand Up @@ -89,19 +90,18 @@ def main():
if args.splitted:
# errs = []
# model_includes = []
default_mattr = "+m,+fast-unaligned-access"
# errs = []
if settings:
riscv_settings = settings.riscv
if riscv_settings:
features = riscv_settings.features
if features is None:
pass
else:
default_mattr = ",".join([f"+{f}" for f in features])
# errs = []
else:
riscv_settings = None
default_features, default_xlen = get_riscv_defaults(riscv_settings)

assert out_path.is_dir(), "Expecting output directory when using --splitted"
for set_name, set_def in model["sets"].items():
xlen = set_def.xlen
if xlen is None:
xlen = default_xlen
metrics["n_sets"] += 1
ext_settings = set_def.settings
for instr_def in set_def.instructions.values():
Expand All @@ -125,16 +125,17 @@ def main():
metrics["n_skipped"] += 1
# errs.append(TODO)
output_file = out_path / set_name / f"{instr_def.name}.{args.ext}"

features = [*default_features]
if ext_settings is not None:
arch_ = ext_settings.get_arch(name=set_name)
if arch is not None:
features.append(arch_)
mattr = build_mattr(features, xlen)

install_dir = os.getenv("CDSL2LLVM_DIR", None)
assert install_dir is not None
install_dir = pathlib.Path(install_dir)
mattr = default_mattr
if ext_settings is not None:
# predicate = ext_settings.get_predicate(name=set_name)
arch_ = ext_settings.get_arch(name=set_name)
mattr = ",".join([*mattr.split(","), f"+{arch_}"])
if xlen == 64 and "+64bit" not in mattr:
mattr = ",".join([*mattr.split(","), "+64bit"])
try:
cdsl2llvm.run_pattern_gen(
# install_dir / "llvm" / "build",
Expand All @@ -148,7 +149,6 @@ def main():
)
metrics["n_success"] += 1
except AssertionError:
pass
metrics["n_failed"] += 1
# errs.append((insn_name, str(ex)))
# if len(errs) > 0:
Expand Down
49 changes: 26 additions & 23 deletions seal5/backends/patterngen/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from seal5.tools import cdsl2llvm
from seal5.index import write_index_yaml, File, NamedPatch
from seal5.model import Seal5InstrAttribute
from seal5.riscv_utils import build_mattr, get_riscv_defaults

logger = logging.getLogger("patterngen_tablegen_writer")

Expand Down Expand Up @@ -98,26 +99,34 @@ def main():
if args.splitted:
# errs = []
# model_includes = []
default_mattr = "+m,+fast-unaligned-access"
if settings:
riscv_settings = settings.riscv
if riscv_settings:
features = riscv_settings.features
if features is None:
pass
else:
default_mattr = ",".join([f"+{f}" for f in features])
else:
riscv_settings = None
default_features, default_xlen = get_riscv_defaults(riscv_settings)

assert out_path.is_dir(), "Expecting output directory when using --splitted"
for set_name, set_def in model["sets"].items():
xlen = set_def.xlen
if xlen is None and default_xlen is not None:
xlen = default_xlen
artifacts[set_name] = []
metrics["n_sets"] += 1
ext_settings = set_def.settings
set_dir = out_path / set_name
includes = []

def process_instrunction(instr_def):
predicate = None
features = [*default_features]
if ext_settings is not None:
predicate = ext_settings.get_predicate(name=set_name)
arch_ = ext_settings.get_arch(name=set_name)
if arch_ is not None:
features.append(arch_)

mattr = build_mattr(features, xlen)

def process_instrunction(instr_def, set_name, set_dir, mattr, predicate, xlen):
includes_ = []
metrics["n_instructions"] += 1
input_file = out_path / set_name / f"{instr_def.name}.core_desc"
attrs = instr_def.attributes
Expand All @@ -144,14 +153,6 @@ def process_instrunction(instr_def):
out_name_fmt = f"{instr_def.name}InstrFormat.{args.ext}"
output_file_fmt = set_dir / out_name_fmt
install_dir = os.getenv("CDSL2LLVM_DIR", None)
predicate = None
mattr = default_mattr
if ext_settings is not None:
predicate = ext_settings.get_predicate(name=set_name)
arch_ = ext_settings.get_arch(name=set_name)
mattr = ",".join([*mattr.split(","), f"+{arch_}"])
if xlen == 64 and "+64bit" not in mattr:
mattr = ",".join([*mattr.split(","), "+64bit"])

assert install_dir is not None
install_dir = pathlib.Path(install_dir)
Expand All @@ -174,29 +175,31 @@ def process_instrunction(instr_def):
file_artifact_fmt = File(file_artifact_fmt_dest, src_path=output_file_fmt)
artifacts[set_name].append(file_artifact_fmt)
include_path_fmt = f"{set_name}/{out_name_fmt}"
includes.append(include_path_fmt)
includes_.append(include_path_fmt)
if args.patterns:
file_artifact_dest = f"llvm/lib/Target/RISCV/seal5/{set_name}/{out_name}"
file_artifact = File(file_artifact_dest, src_path=output_file)
artifacts[set_name].append(file_artifact)
include_path = f"{set_name}/{out_name}"
includes.append(include_path)
includes_.append(include_path)
else:
metrics["n_failed"] += 1
except AssertionError:
metrics["n_failed"] += 1
# errs.append((insn_name, str(ex)))
return True
return True, includes_

includes = []
with ThreadPoolExecutor(args.parallel) as executor:
futures = []
for instr_def in set_def.instructions.values():
future = executor.submit(process_instrunction, instr_def)
future = executor.submit(process_instrunction, instr_def, set_name, set_dir, mattr, predicate, xlen)
futures.append(future)
results = []
for future in as_completed(futures):
result = future.result
result, includes_ = future.result
results.append(result)
includes.extend(includes_)
if len(includes) > 0:
set_includes_str = "\n".join([f'include "seal5/{inc}"' for inc in includes])
set_includes_artifact_dest = f"llvm/lib/Target/RISCV/seal5/{set_name}.td"
Expand Down Expand Up @@ -229,7 +232,7 @@ def process_instrunction(instr_def):
f.write(",".join(map(str, metrics.values())))
f.write("\n")
if args.index:
if sum(map(lambda x: len(x), artifacts.values())) > 0:
if sum(map(len, artifacts.values())) > 0:
global_artifacts = artifacts.get(None, [])
set_artifacts = {key: value for key, value in artifacts.items() if key is not None}
index_file = args.index
Expand Down
1 change: 1 addition & 0 deletions seal5/backends/yaml/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def main():
# print("set", set_def)
set_data = {"instructions": []}
set_data["model"] = top_level.stem
set_data["xlen"] = set_def.xlen
for instr in set_def.instructions.values():
set_data["instructions"].append(instr.name)
data["extensions"][set_name] = set_data
Expand Down
29 changes: 14 additions & 15 deletions seal5/pass_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from seal5.passes import Seal5Pass, PassType, PassScope, PassManager, PassResult
from seal5.types import PatchStage
from seal5.settings import Seal5Settings, PatchSettings
from seal5.riscv_utils import build_mattr, get_riscv_defaults

logger = get_logger()

Expand Down Expand Up @@ -1280,24 +1281,27 @@ def convert_llvmir_to_gmir(
# for input_file in input_files:
# name = input_file.name
# sub = name.replace(".seal5model", "")
default_mattr = "+m,+fast-unaligned-access"
if settings:
riscv_settings = settings.riscv
if riscv_settings:
xlen = riscv_settings.xlen
features = riscv_settings.features
if features is None:
pass
else:
default_mattr = ",".join([f"+{f}" for f in features])
if xlen == 64 and "+64bit" not in default_mattr:
default_mattr = ",".join([*default_mattr.split(","), "+64bit"])
else:
riscv_settings = None
default_features, default_xlen = get_riscv_defaults(riscv_settings)

for _ in [None]:
set_names = list(settings.extensions.keys())
assert len(set_names) > 0, "No sets found"
for set_name in set_names:
ext_settings = settings.extensions[set_name]
insn_names = ext_settings.instructions
xlen = ext_settings.xlen
if xlen is None and default_xlen is not None:
xlen = default_xlen
features = [*default_features]
arch_ = ext_settings.get_arch(name=set_name)
features = [*default_features]
if arch_ is not None:
features.append(arch_)
mattr = build_mattr(default_features, xlen)
if insn_names is None:
logger.warning("Skipping empty set %s", set_name)
continue
Expand Down Expand Up @@ -1325,11 +1329,6 @@ def convert_llvmir_to_gmir(
cdsl2llvm_build_dir = str(settings.build_dir / config)
else:
cdsl2llvm_build_dir = str(settings.deps_dir / "cdsl2llvm" / "llvm" / "build")
mattr = default_mattr
if ext_settings is not None:
# predicate = ext_settings.get_predicate(name=set_name)
arch_ = ext_settings.get_arch(name=set_name)
mattr = ",".join([*mattr.split(","), f"+{arch_}"])
# TODO: migrate with pass to cmdline backend
cdsl2llvm.convert_ll_to_gmir(
# settings.deps_dir / "cdsl2llvm" / "llvm" / "build", ll_file, output_file
Expand Down
1 change: 1 addition & 0 deletions seal5/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ class ExtensionsSettings(YAMLSettings):
description: Optional[str] = None
requires: Optional[List[str]] = None
instructions: Optional[List[str]] = None
xlen: Optional[int] = None # TODO: support multiple?
# patches

def get_version(self):
Expand Down
2 changes: 1 addition & 1 deletion seal5/transform/drop_unused/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def run(args):
set_def.constants = {
const_name: const
for const_name, const in set_def.constants.items()
if const_name not in context.to_drop
if const_name not in context.to_drop or const_name == "XLEN"
}
# print("AFTER", len(set_def.constants))
# input("CONT1")
Expand Down

0 comments on commit 7adc0f2

Please sign in to comment.