diff --git a/.gitignore b/.gitignore index 32e3af5..fbc7613 100644 --- a/.gitignore +++ b/.gitignore @@ -33,4 +33,144 @@ # Build folders [Bb]uild/ -cmake-*/ \ No newline at end of file +cmake-*/ + +## Python begins here. +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ diff --git a/op-summarizer/gcovreader.py b/op-summarizer/gcovreader.py new file mode 100644 index 0000000..dc5a24c --- /dev/null +++ b/op-summarizer/gcovreader.py @@ -0,0 +1,36 @@ +import json + +from dataclasses import dataclass +from typing import Dict, List + + +@dataclass +class GCovLine: + line_number: int + count: int + is_fallthrough: bool = False + + +class GCovFile: + def __init__(self, path: str) -> None: + self._path = path + self._data: dict = {} + + self.files: Dict[str, List[GCovLine]] = {} + + def read(self) -> None: + with open(self._path, "r") as infile: + self._data = json.load(infile) + + for file in self._data["files"]: + name: str = file["file"] + lines: List[GCovLine] = [] + + for line in file["lines"]: + if len(line["branches"]): + for branch in line["branches"]: + lines.append(GCovLine(line["line_number"], branch["count"], branch["fallthrough"])) + else: + lines.append(GCovLine(line["line_number"], line["count"])) + + self.files[name] = lines diff --git a/op-summarizer/op-summarizer.py b/op-summarizer/op-summarizer.py new file mode 100644 index 0000000..e17bab4 --- /dev/null +++ b/op-summarizer/op-summarizer.py @@ -0,0 +1,58 @@ +from dataclasses import dataclass +from typing import Dict, List, Optional + +from gcovreader import GCovFile, GCovLine +from opfinderreader import OperationLogReader, OperationLog + + +@dataclass(frozen=True) +class UniqueOperation: + name: str + type_lhs: Optional[str] + type_rhs: Optional[str] + type_expr: Optional[str] + + @staticmethod + def from_operation_log(op_log: OperationLog) -> "UniqueOperation": + return UniqueOperation( + op_log.operation, + op_log.operand_lhs, + op_log.operand_rhs, + op_log.operand_result + ) + + +if __name__ == "__main__": + gcov = GCovFile("./data/gcov.json") + ops = OperationLogReader("./data/opfinder.json") + + gcov.read() + ops.read() + + for file_name in gcov.files: + op_counter: Dict[UniqueOperation, int] = {} + + if file_name not in ops.files: + print(f"Couldn't find {file_name} in op-finder output. Skipping.") + continue + + loop_stack: List[int] = [] + + for gcov_line in gcov.files[file_name]: + op_lines = ops.get_lines(file_name, gcov_line.line_number) + for opfinder_line in op_lines: + # TODO: revise this. Need a special case for for-loop clauses + # or branching in general. + if opfinder_line.is_fallthrough != gcov_line.is_fallthrough: + continue + + unique_op = UniqueOperation.from_operation_log(opfinder_line) + + if unique_op in op_counter: + op_counter[unique_op] += gcov_line.count + else: + op_counter[unique_op] = gcov_line.count + + print(f"Unique operations for file {file_name}:") + for uop, count in op_counter.items(): + print(f"\t{count}: {uop}") diff --git a/op-summarizer/opfinderreader.py b/op-summarizer/opfinderreader.py new file mode 100644 index 0000000..53f6179 --- /dev/null +++ b/op-summarizer/opfinderreader.py @@ -0,0 +1,43 @@ +import json + +from dataclasses import dataclass, field +from typing import Dict, List + + +@dataclass +class OperationLog: + operation: str + line: int + operand_lhs: str + operand_rhs: str + operand_result: str + is_fallthrough: bool + current_for_loops: list[int] = field(default_factory=list) + + +class OperationLogReader: + def __init__(self, path: str) -> None: + self._path = path + self._data: dict = {} + + self.files: Dict[str, List[OperationLog]] = {} + + def read(self) -> None: + with open(self._path, "r") as infile: + self._data = json.load(infile) + + for name, ops_list in self._data.items(): + ops: List[OperationLog] = [] + + for op_json in ops_list: + ops.append(OperationLog(**op_json)) + + self.files[name] = ops + + def get_lines(self, file: str, line_number: int) -> List[OperationLog]: + res: List[OperationLog] = [] + for line in self.files[file]: + if line.line == line_number: + res.append(line) + + return res