|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# |
| 3 | +# This file is part of the M2-ISA-R project: https://github.com/tum-ei-eda/M2-ISA-R |
| 4 | +# |
| 5 | +# Copyright (C) 2022 |
| 6 | +# Chair of Electrical Design Automation |
| 7 | +# Technical University of Munich |
| 8 | + |
| 9 | +"""Clean M2-ISA-R/Seal5 metamodel to .core_desc file.""" |
| 10 | + |
| 11 | +import argparse |
| 12 | +import logging |
| 13 | +import pathlib |
| 14 | +import pickle |
| 15 | +import os.path |
| 16 | +from typing import Union |
| 17 | +from dataclasses import dataclass |
| 18 | + |
| 19 | +from m2isar.metamodel import arch |
| 20 | + |
| 21 | +from seal5.index import NamedPatch, write_index_yaml |
| 22 | +from seal5.settings import IntrinsicDefn |
| 23 | + |
| 24 | +logger = logging.getLogger("riscv_intrinsics") |
| 25 | + |
| 26 | + |
| 27 | +def ir_type_to_text(ir_type: str): |
| 28 | + # needs fleshing out with all likely types |
| 29 | + # probably needs to take into account RISC-V bit width, e.g. does "Li" means 32 bit integer on a 128-bit platform? |
| 30 | + if ir_type == 'i32': |
| 31 | + return 'Li' |
| 32 | + raise NotImplementedError(f'Unhandled ir_type "{ir_type}"') |
| 33 | + |
| 34 | + |
| 35 | +def build_target(arch: str, intrinsic: IntrinsicDefn): |
| 36 | + |
| 37 | + # Target couples intrinsic name to argument types and function behaviour |
| 38 | + # Start with return type if not void |
| 39 | + arg_str = '' |
| 40 | + if intrinsic.ret_type: |
| 41 | + arg_str += ir_type_to_text(intrinsic.ret_type) |
| 42 | + for arg in intrinsic.args: |
| 43 | + arg_str += ir_type_to_text(arg.arg_type) |
| 44 | + |
| 45 | + target = f'TARGET_BUILTIN(__builtin_{arch}_{intrinsic.intrinsic_name}, "{arg_str}", "nc", "{arch}")' |
| 46 | + return target |
| 47 | + |
| 48 | + |
| 49 | +def ir_type_to_pattern(ir_type: str): |
| 50 | + # needs fleshing out with all likely types |
| 51 | + if ir_type == 'i32': |
| 52 | + return 'llvm_i32_ty' |
| 53 | + raise NotImplementedError(f'Unhandled ir_type "{ir_type}"') |
| 54 | + |
| 55 | + |
| 56 | +def build_attr(arch: str, intrinsic: IntrinsicDefn): |
| 57 | + uses_mem = False # @todo |
| 58 | + attr = f' def int_riscv_{intrinsic.intrinsic_name} : Intrinsic<\n [' |
| 59 | + if intrinsic.ret_type: |
| 60 | + attr += f'{ir_type_to_pattern(intrinsic.ret_type)}' |
| 61 | + attr += '],\n [' |
| 62 | + for idx, arg in enumerate(intrinsic.args): |
| 63 | + if idx: |
| 64 | + attr += ', ' |
| 65 | + attr += ir_type_to_pattern(arg.arg_type) |
| 66 | + attr += '],\n' |
| 67 | + attr += ' [IntrNoMem, IntrSpeculatable, IntrWillReturn]>;' |
| 68 | + return attr |
| 69 | + |
| 70 | + |
| 71 | +def build_emit(arch: str, intrinsic: IntrinsicDefn): |
| 72 | + emit = (f' case RISCV::BI__builtin_{arch}_{intrinsic.intrinsic_name}:\n' |
| 73 | + f' ID = Intrinsic::riscv_{intrinsic.intrinsic_name};\n' |
| 74 | + f' break;') |
| 75 | + return emit |
| 76 | + |
| 77 | + |
| 78 | +@dataclass |
| 79 | +class PatchFrag: |
| 80 | + """Pairs patch contents to location to apply it""" |
| 81 | + patchee: str |
| 82 | + tag: str |
| 83 | + contents: str = "" |
| 84 | + |
| 85 | + |
| 86 | +def main(): |
| 87 | + """Main app entrypoint.""" |
| 88 | + |
| 89 | + # read command line args |
| 90 | + parser = argparse.ArgumentParser() |
| 91 | + parser.add_argument("top_level", help="A .m2isarmodel or .seal5model file.") |
| 92 | + parser.add_argument("--log", default="info", choices=["critical", "error", "warning", "info", "debug"]) |
| 93 | + parser.add_argument("--output", "-o", type=str, default=None) |
| 94 | + parser.add_argument("--splitted", action="store_true", help="Split per set") |
| 95 | + parser.add_argument("--formats", action="store_true", help="Also generate instruction formats") |
| 96 | + parser.add_argument("--metrics", default=None, help="Output metrics to file") |
| 97 | + parser.add_argument("--index", default=None, help="Output index to file") |
| 98 | + parser.add_argument("--ext", type=str, default="td", help="Default file extension (if using --splitted)") |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + # initialize logging |
| 102 | + logging.basicConfig(level=getattr(logging, args.log.upper())) |
| 103 | + |
| 104 | + # resolve model paths |
| 105 | + top_level = pathlib.Path(args.top_level) |
| 106 | + |
| 107 | + is_seal5_model = False |
| 108 | + if top_level.suffix == ".seal5model": |
| 109 | + is_seal5_model = True |
| 110 | + if args.output is None: |
| 111 | + assert top_level.suffix in [".m2isarmodel", ".seal5model"], "Can not infer model type from file extension." |
| 112 | + raise NotImplementedError |
| 113 | + |
| 114 | + # out_path = top_level.parent / (top_level.stem + ".core_desc") |
| 115 | + else: |
| 116 | + out_path = pathlib.Path(args.output) |
| 117 | + |
| 118 | + logger.info("intrinsics/writer - loading models") |
| 119 | + if not is_seal5_model: |
| 120 | + raise NotImplementedError |
| 121 | + |
| 122 | + # load models |
| 123 | + with open(top_level, "rb") as f: |
| 124 | + # models: "dict[str, arch.CoreDef]" = pickle.load(f) |
| 125 | + if is_seal5_model: |
| 126 | + model: "dict[str, Union[arch.InstructionSet, ...]]" = pickle.load(f) |
| 127 | + model["cores"] = {} |
| 128 | + else: # TODO: core vs. set! |
| 129 | + temp: "dict[str, Union[arch.InstructionSet, arch.CoreDef]]" = pickle.load(f) |
| 130 | + assert len(temp) > 0, "Empty model!" |
| 131 | + if isinstance(list(temp.values())[0], arch.CoreDef): |
| 132 | + model = {"cores": temp, "sets": {}} |
| 133 | + elif isinstance(list(temp.values())[0], arch.InstructionSet): |
| 134 | + model = {"sets": temp, "cores": {}} |
| 135 | + else: |
| 136 | + assert False |
| 137 | + |
| 138 | + metrics = { |
| 139 | + "n_sets": 0, |
| 140 | + "n_skipped": 0, |
| 141 | + "n_failed": 0, |
| 142 | + "n_success": 0, |
| 143 | + } |
| 144 | + # preprocess model |
| 145 | + # print("model", model) |
| 146 | + artifacts = {} |
| 147 | + artifacts[None] = [] # used for global artifacts |
| 148 | + if args.splitted: |
| 149 | + raise NotImplementedError |
| 150 | + else: |
| 151 | + # errs = [] |
| 152 | + settings = model.get("settings", None) |
| 153 | + llvm_version = None |
| 154 | + if not settings or not settings.intrinsics.intrinsics: |
| 155 | + logger.warning("No intrinsics configured; didn't need to invoke intrinsics writer.") |
| 156 | + quit() |
| 157 | + if settings: |
| 158 | + llvm_settings = settings.llvm |
| 159 | + if llvm_settings: |
| 160 | + llvm_state = llvm_settings.state |
| 161 | + if llvm_state: |
| 162 | + llvm_version = llvm_state.version # unused today, but needed very soon |
| 163 | + patch_frags = { |
| 164 | + 'target': PatchFrag(patchee='clang/include/clang/Basic/BuiltinsRISCV.def', tag='builtins_riscv'), |
| 165 | + 'attr': PatchFrag(patchee='llvm/include/llvm/IR/IntrinsicsRISCV.td', tag='intrinsics_riscv'), |
| 166 | + 'emit': PatchFrag(patchee='clang/lib/CodeGen/CGBuiltin.cpp', tag='cg_builtin') |
| 167 | + } |
| 168 | + for set_name, set_def in model["sets"].items(): |
| 169 | + artifacts[set_name] = [] |
| 170 | + metrics["n_sets"] += 1 |
| 171 | + ext_settings = set_def.settings |
| 172 | + if ext_settings is None: |
| 173 | + metrics["n_skipped"] += 1 |
| 174 | + continue |
| 175 | + for intrinsic in settings.intrinsics.intrinsics: |
| 176 | + metrics["n_success"] += 1 |
| 177 | + |
| 178 | + patch_frags['target'].contents += build_target(arch=ext_settings.get_arch(), intrinsic=intrinsic) |
| 179 | + patch_frags['attr'].contents += build_attr(arch=ext_settings.get_arch(), intrinsic=intrinsic) |
| 180 | + patch_frags['emit'].contents += build_emit(arch=ext_settings.get_arch(), intrinsic=intrinsic) |
| 181 | + |
| 182 | + for id, frag in patch_frags.items(): |
| 183 | + contents = frag.contents |
| 184 | + if len(contents) > 0: |
| 185 | + if id == 'target': |
| 186 | + contents = f'// {ext_settings.get_arch()}\n{contents}\n' |
| 187 | + elif id == 'attr': |
| 188 | + contents = f'let TargetPrefix = "riscv" in {{\n{contents}\n}}' |
| 189 | + (root, ext) = os.path.splitext(out_path) |
| 190 | + patch_path = root + '_' + id + ext |
| 191 | + with open(patch_path, "w") as f: |
| 192 | + f.write(contents) |
| 193 | + key = frag.tag |
| 194 | + if ext_settings.experimental: |
| 195 | + key += "_experimental" |
| 196 | + patch = NamedPatch(frag.patchee, key=key, src_path=patch_path, content=contents) |
| 197 | + artifacts[None].append(patch) |
| 198 | + if args.metrics: |
| 199 | + metrics_file = args.metrics |
| 200 | + with open(metrics_file, "w") as f: |
| 201 | + f.write(",".join(metrics.keys())) |
| 202 | + f.write("\n") |
| 203 | + f.write(",".join(map(str, metrics.values()))) |
| 204 | + f.write("\n") |
| 205 | + if args.index: |
| 206 | + if sum(map(lambda x: len(x), artifacts.values())) > 0: |
| 207 | + global_artifacts = artifacts.get(None, []) |
| 208 | + set_artifacts = {key: value for key, value in artifacts.items() if key is not None} |
| 209 | + index_file = args.index |
| 210 | + write_index_yaml(index_file, global_artifacts, set_artifacts, content=True) |
| 211 | + else: |
| 212 | + logger.warning("No patches generated. No index file will be written.") |
| 213 | + |
| 214 | + |
| 215 | +if __name__ == "__main__": |
| 216 | + main() |
0 commit comments