def setUpClass(self): try: self.parser = BaseParser() except NotImplementedError: pass with open(self._find_file("triad_x86_iaca.s")) as f: self.triad_code = f.read() with open(self._find_file("triad_arm_iaca.s")) as f: self.triad_code_arm = f.read() with open(self._find_file("kernel_x86.s")) as f: self.x86_code = f.read() with open(self._find_file("kernel_aarch64.s")) as f: self.aarch64_code = f.read()
def inspect(args, output_file=sys.stdout): """ Does the actual throughput and critical path analysis of OSACA and prints it to the terminal. :param args: arguments given from :class:`~argparse.ArgumentParser` after parsing :param output_file: Define the stream for output, defaults to :class:`sys.stdout` :type output_file: stream, optional """ # Read file code = args.file.read() # Detect ISA if necessary arch = args.arch if args.arch is not None else DEFAULT_ARCHS[BaseParser.detect_ISA(code)] print_arch_warning = False if args.arch else True isa = MachineModel.get_isa_for_arch(arch) verbose = args.verbose ignore_unknown = args.ignore_unknown # Parse file parser = get_asm_parser(arch) try: parsed_code = parser.parse_file(code) except: # probably the wrong parser based on heuristic if args.arch is None: # change ISA and try again arch = DEFAULT_ARCHS['x86'] if BaseParser.detect_ISA(code) == 'aarch64' else DEFAULT_ARCHS['aarch64'] isa = MachineModel.get_isa_for_arch(arch) parser = get_asm_parser(arch) parsed_code = parser.parse_file(code) else: traceback.print_exc(file=sys.stderr) sys.exit(1) # 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] print_length_warning = False else: kernel = reduce_to_section(parsed_code, isa) # Print warning if kernel has no markers and is larger than threshold (100) print_length_warning = True if len(kernel) == len(parsed_code) and len(kernel) > 100 else False machine_model = MachineModel(arch=arch) semantics = ArchSemantics(machine_model) semantics.add_semantics(kernel) # Do optimal schedule for kernel throughput if wished if not args.fixed: semantics.assign_optimal_throughput(kernel) # Create DiGrahps kernel_graph = KernelDG(kernel, parser, machine_model) if args.dotpath is not None: kernel_graph.export_graph(args.dotpath if args.dotpath != '.' else None) # Print analysis frontend = Frontend(args.file.name, arch=arch) print( frontend.full_analysis( kernel, kernel_graph, ignore_unknown=ignore_unknown, arch_warning=print_arch_warning, length_warning=print_length_warning, verbose=verbose ), file=output_file, )
def test_detect_ISA(self): self.assertEqual(BaseParser.detect_ISA(self.triad_code), "x86") self.assertEqual(BaseParser.detect_ISA(self.triad_code_arm), "aarch64") self.assertEqual(BaseParser.detect_ISA(self.x86_code), "x86") self.assertEqual(BaseParser.detect_ISA(self.aarch64_code), "aarch64")
class TestBaseParser(unittest.TestCase): @classmethod def setUpClass(self): try: self.parser = BaseParser() except NotImplementedError: pass with open(self._find_file("triad_x86_iaca.s")) as f: self.triad_code = f.read() with open(self._find_file("triad_arm_iaca.s")) as f: self.triad_code_arm = f.read() with open(self._find_file("kernel_x86.s")) as f: self.x86_code = f.read() with open(self._find_file("kernel_aarch64.s")) as f: self.aarch64_code = f.read() ################## # Test ################## def test_parse_file(self): with self.assertRaises(NotImplementedError): self.parser.parse_file(self.triad_code) def test_parse_line(self): line_instruction = "\t\tlea 2(%rax,%rax), %ecx #12.9" with self.assertRaises(NotImplementedError): self.parser.parse_line(line_instruction) def test_parse_instruction(self): instr1 = "\t\tvcvtsi2ss %edx, %xmm2, %xmm2\t\t\t#12.27" with self.assertRaises(NotImplementedError): self.parser.parse_instruction(instr1) def test_register_funcs(self): reg_a1 = AttrDict({"name": "rax"}) reg_a2 = AttrDict({"name": "eax"}) register_string = "v1.2d" with self.assertRaises(NotImplementedError): self.parser.is_reg_dependend_of(reg_a1, reg_a2) with self.assertRaises(NotImplementedError): self.parser.parse_register(register_string) with self.assertRaises(NotImplementedError): self.parser.is_gpr(reg_a1) with self.assertRaises(NotImplementedError): self.parser.is_vector_register(reg_a1) with self.assertRaises(NotImplementedError): self.parser.process_operand(reg_a1) with self.assertRaises(NotImplementedError): self.parser.get_full_reg_name(reg_a1) def test_normalize_imd(self): imd_hex_1 = {"value": "0x4f"} with self.assertRaises(NotImplementedError): self.parser.normalize_imd(imd_hex_1) def test_detect_ISA(self): self.assertEqual(BaseParser.detect_ISA(self.triad_code), "x86") self.assertEqual(BaseParser.detect_ISA(self.triad_code_arm), "aarch64") self.assertEqual(BaseParser.detect_ISA(self.x86_code), "x86") self.assertEqual(BaseParser.detect_ISA(self.aarch64_code), "aarch64") ################## # Helper functions ################## @staticmethod def _find_file(name): testdir = os.path.dirname(__file__) name = os.path.join(testdir, "test_files", name) assert os.path.exists(name) return name