|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import argparse |
| 3 | +import enum |
| 4 | +import re |
| 5 | +import sys |
| 6 | +from typing import IO, List |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | + |
| 10 | +class argfile: |
| 11 | + def __init__(self, default: IO, filename: Path, mode: str = 'r'): |
| 12 | + self.__file = default if filename == '-' else open(filename, mode) |
| 13 | + self.__should_close = filename != '-' |
| 14 | + |
| 15 | + def __enter__(self) -> IO: |
| 16 | + return self.__file |
| 17 | + |
| 18 | + def __exit__(self, exc_type, exc_value, exc_traceback): |
| 19 | + self.close() |
| 20 | + |
| 21 | + def close(self): |
| 22 | + if self.__should_close: |
| 23 | + self.__file.close() |
| 24 | + |
| 25 | + |
| 26 | +class SpillStat(enum.Enum): |
| 27 | + RAW = re.compile(r'GREEDY RA: Number of spilled live ranges: ([0-9]+)') |
| 28 | + WEIGHTED = re.compile(r'SC in Function \S+ ([0-9])+') |
| 29 | + |
| 30 | + |
| 31 | +def sum_stat(infile: IO, r: re.Match) -> int: |
| 32 | + return sum( |
| 33 | + sum(int(x) for x in r.findall(line)) for line in infile |
| 34 | + ) |
| 35 | + |
| 36 | + |
| 37 | +def main(infile: IO, outfile: IO, which: SpillStat = SpillStat.RAW): |
| 38 | + spill_count = sum_stat(infile, which.value) |
| 39 | + print(spill_count, file=outfile) |
| 40 | + |
| 41 | + |
| 42 | +def raw_main(argv: List[str]) -> None: |
| 43 | + parser = argparse.ArgumentParser(description='Extract spill counts') |
| 44 | + parser.add_argument('--which', default='raw', choices=('weighted', 'raw'), |
| 45 | + help='Whether to extract weighted or raw spills only. Default: raw') |
| 46 | + parser.add_argument('-o', '--output', default='-', |
| 47 | + help='Where to output the information to, - for stdout. Defaults to stdout') |
| 48 | + parser.add_argument('file', help='The file to process, - for stdin.') |
| 49 | + |
| 50 | + args = parser.parse_args(argv) |
| 51 | + |
| 52 | + with argfile(sys.stdin, args.file, 'r') as infile, argfile(sys.stdout, args.output, 'w') as outfile: |
| 53 | + main(infile, outfile, SpillStat[args.which.upper()]) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == '__main__': |
| 57 | + raw_main(sys.argv[1:]) |
0 commit comments