def test_sirwriter_node_1(parser): '''Check the node_node method of the SIRWriter class is called when an unsupported node is found and that it raises the appropriate exception if skip_nodes is false and continues (outputting information about the unsupported node) if skip_nodes is True. Also check for SIR indentation. ''' from psyclone.psyGen import Node schedule = get_schedule(parser, CODE) # pylint: disable=abstract-method class Unsupported(Node): '''A PSyIR node that will not be supported by the SIR writer.''' # pylint: enable=abstract-method unsupported = Unsupported() # Add the unsupported node as the root of the tree unsupported.children = [schedule] sir_writer = SIRWriter(skip_nodes=False) with pytest.raises(VisitorError) as excinfo: sir_writer(unsupported) assert "unsupported node found" in str(excinfo.value) sir_writer = SIRWriter(skip_nodes=True) result = sir_writer(unsupported) assert "[ Unsupported start ]" in result assert "[ Unsupported end ]" in result # Check indentation works. assert " make_assignment_stmt(" in result
def trans(psy): '''Transformation routine for use with PSyclone. Applies the PSyIR2SIR transform to the supplied invokes after replacing any ABS, SIGN or MIN intrinsics with equivalent code. This is done because the SIR does not support intrinsics. :param psy: the PSy object which this script will transform. :type psy: :py:class:`psyclone.psyGen.PSy` :returns: the transformed PSy object. :rtype: :py:class:`psyclone.psyGen.PSy` ''' abs_trans = Abs2CodeTrans() sign_trans = Sign2CodeTrans() min_trans = Min2CodeTrans() nemo_loop_trans = NemoAllArrayRange2LoopTrans() sir_writer = SIRWriter() fortran_writer = FortranWriter() # For each Invoke write out the SIR representation of the # schedule. Note, there is no algorithm layer in the NEMO API so # the invokes represent all of the original code. for invoke in psy.invokes.invoke_list: schedule = invoke.schedule for assignment in schedule.walk(Assignment): nemo_loop_trans.apply(assignment) for kernel in schedule.walk(NemoKern): # The NEMO api currently has no symbol table so create one # to allow the generation of new variables. Note, this # does not guarantee unique names as we don't know any of # the existing names (so generated names could clash). symbol_table = SymbolTable() kernel_schedule = kernel.get_kernel_schedule() for oper in kernel_schedule.walk(Operation): if oper.operator == UnaryOperation.Operator.ABS: # Apply ABS transformation abs_trans.apply(oper, symbol_table) elif oper.operator == BinaryOperation.Operator.SIGN: # Apply SIGN transformation sign_trans.apply(oper, symbol_table) elif oper.operator in [ BinaryOperation.Operator.MIN, NaryOperation.Operator.MIN ]: # Apply (2-n arg) MIN transformation min_trans.apply(oper, symbol_table) kern = fortran_writer(schedule) print(kern) kern = sir_writer(schedule) print(kern) return psy
def test_sirwriter_init_2(): '''Check the __init__ function of the SIRWriter class can change default values as expected. ''' sir_writer = SIRWriter(skip_nodes=True, indent_string="[ooaah]", initial_indent_depth=3) assert sir_writer._skip_nodes assert sir_writer._indent == "[ooaah]" assert sir_writer._depth == 3
def trans(psy): '''Transformation routine for use with PSyclone. Applies the PSyIR2SIR transform to the supplied invokes. This transformation is limited the NEMO API. :param psy: the PSy object which this script will transform. :type psy: :py:class:`psyclone.psyGen.PSy` :returns: the transformed PSy object. :rtype: :py:class:`psyclone.psyGen.PSy` ''' sir_writer = SIRWriter() # For each Invoke write out the SIR representation of the # schedule. Note, there is no algorithm layer in the NEMO API so # the invokes represent all of the original code. for invoke in psy.invokes.invoke_list: sched = invoke.schedule kern = sir_writer(sched) print(kern) return psy
def sir_writer(): '''Create and return an sir SIRWriter object with default settings.''' return SIRWriter()