From b986d7eba0e40f91826cdaafabb9509fb7867db5 Mon Sep 17 00:00:00 2001 From: JanLJL Date: Fri, 6 Nov 2020 12:57:41 +0100 Subject: [PATCH] added --lines option --- osaca/osaca.py | 28 ++++++++++++++++++++++++++-- tests/test_cli.py | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/osaca/osaca.py b/osaca/osaca.py index d1173da..d50f694 100755 --- a/osaca/osaca.py +++ b/osaca/osaca.py @@ -94,6 +94,13 @@ def create_parser(parser=None): help='Run the throughput analysis with fixed probabilities for all suitable ports per ' 'instruction. Otherwise, OSACA will print the optimal port utilization for the kernel.', ) + parser.add_argument( + '--lines', + type=str, + help='Define lines that should be included in the analysis. This option overwrites any' + ' range defined by markers in the assembly. Add either single lines or ranges defined by' + ' "-" or ":", each entry separated by commas, e.g.: --lines 1,2,8-18,20:24', + ) parser.add_argument( '--db-check', dest='check_db', @@ -272,8 +279,12 @@ def inspect(args, output_file=sys.stdout): traceback.print_exc(file=sys.stderr) sys.exit(1) - # Reduce to marked kernel and add semantics - kernel = reduce_to_section(parsed_code, isa) + # Reduce to marked kernel or chosen section and add semantics + if args.lines: + line_range = get_line_range(args.lines) + kernel = [line for line in parsed_code if line['line_number'] in line_range] + else: + kernel = reduce_to_section(parsed_code, isa) machine_model = MachineModel(arch=arch) semantics = ArchSemantics(machine_model) semantics.add_semantics(kernel) @@ -346,6 +357,19 @@ def get_unmatched_instruction_ratio(kernel): unmatched_counter += 1 return unmatched_counter / len(kernel) +def get_line_range(line_str): + line_str = line_str.replace(':', '-') + lines = line_str.split(',') + lines_int = [] + for l in lines: + if '-' in l: + start = int(l.split('-')[0]) + end = int(l.split('-')[1]) + rnge = list(range(start, end+1)) + lines_int += rnge + else: + lines_int.append(int(l)) + return lines_int def main(): """Initialize and run command line interface.""" diff --git a/tests/test_cli.py b/tests/test_cli.py index 5030b36..a3ee2f1 100755 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -166,6 +166,31 @@ class TestCLI(unittest.TestCase): args = parser.parse_args([self._find_test_file(kernel_aarch64)]) osaca.run(args, output_file=output) + def test_lines_arg(self): + # Run tests with --lines option + parser = osaca.create_parser() + kernel_x86 = 'triad_x86_iaca.s' + args_base = parser.parse_args( + ['--arch', 'csx', self._find_test_file(kernel_x86)] + ) + output_base = StringIO() + osaca.run(args_base, output_file=output_base) + output_base = output_base.getvalue().split('\n')[8:] + args = [] + args.append(parser.parse_args( + ['--lines', '146-154', '--arch', 'csx', self._find_test_file(kernel_x86)] + )) + args.append(parser.parse_args( + ['--lines', '146:154', '--arch', 'csx', self._find_test_file(kernel_x86)] + )) + args.append(parser.parse_args( + ['--lines', '146,147:148,149-154', '--arch', 'csx', self._find_test_file(kernel_x86)] + )) + for a in args: + with self.subTest(params=a): + output = StringIO() + osaca.run(a, output_file=output) + self.assertEqual(output.getvalue().split('\n')[8:], output_base) ################## # Helper functions