Reference Guide  2.5.0
psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans Class Reference
Inheritance diagram for psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans:
Collaboration diagram for psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans:

Public Member Functions

def __str__ (self)
 
def name (self)
 
def validate (self, node, options=None)
 
def apply (self, node, options=None)
 

Detailed Description

 Provides a transformation that folds conditional expressions with only
a return statement inside so that the Return statement is moved to the end
of the Routine and therefore it can be safely removed. This simplifies the
control flow of the kernel to facilitate other transformations like kernel
fusions. For example, the following code:

.. code-block:: fortran

    subroutine test(i)
        if (i < 5) then
            return
        endif
        if (i > 10) then
            return
        endif
        ! CODE
    end subroutine

will be transformed to:

.. code-block:: fortran

    subroutine test(i)
        if (.not.(i < 5)) then
            if (.not.(i > 10)) then
                ! CODE
            endif
        endif
    end subroutine

Definition at line 45 of file fold_conditional_return_expressions_trans.py.

Member Function Documentation

◆ apply()

def psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans.apply (   self,
  node,
  options = None 
)
Apply this transformation to the supplied node.

:param node: the node to transform.
:type node: :py:class:`psyclone.psyir.nodes.Routine`
:param options: a dictionary with options for transformations.
:type options: Optional[Dict[str, Any]]

Reimplemented from psyclone.psyGen.Transformation.

Definition at line 105 of file fold_conditional_return_expressions_trans.py.

105  def apply(self, node, options=None):
106  '''Apply this transformation to the supplied node.
107 
108  :param node: the node to transform.
109  :type node: :py:class:`psyclone.psyir.nodes.Routine`
110  :param options: a dictionary with options for transformations.
111  :type options: Optional[Dict[str, Any]]
112 
113  '''
114  routine = node
115  self.validate(routine, options)
116 
117  def is_conditional_return(node):
118  '''
119  :param node: node to evaluate.
120  :type node: :py:class:`psyclone.psyir.nodes.Node`
121 
122  :returns: whether the given node represents a conditional return \
123  expression.
124  '''
125  if not isinstance(node, IfBlock):
126  return False
127  if node.else_body is not None:
128  return False
129  return isinstance(node.if_body[0], Return)
130 
131  for statement in routine[:]:
132  if is_conditional_return(statement):
133  # Reverse condition adding a NOT operator
134  new_condition = UnaryOperation.create(
135  UnaryOperation.Operator.NOT,
136  statement.condition.copy())
137  statement.children[0] = new_condition
138 
139  # Remove return statement (and any dead code inside the loop)
140  statement.if_body.children = []
141  # Then move any remaining statement after the conditional
142  # statement inside the loop body
143  while len(statement.parent.children) > statement.position + 1:
144  move = statement.parent.children.pop()
145  statement.if_body.children.insert(0, move)
146 
147 
148 # For Sphinx AutoAPI documentation generation

References psyclone.domain.lfric.kernel.lfric_kernel_metadata.LFRicKernelMetadata.validate(), psyclone.transformations.MoveTrans.validate(), psyclone.transformations.Dynamo0p3AsyncHaloExchangeTrans.validate(), psyclone.domain.common.transformations.alg_invoke_2_psy_call_trans.AlgInvoke2PSyCallTrans.validate(), psyclone.domain.common.transformations.alg_trans.AlgTrans.validate(), psyclone.domain.common.transformations.kernel_module_inline_trans.KernelModuleInlineTrans.validate(), psyclone.domain.common.transformations.raise_psyir_2_alg_trans.RaisePSyIR2AlgTrans.validate(), psyclone.domain.gocean.transformations.gocean_const_loop_bounds_trans.GOConstLoopBoundsTrans.validate(), psyclone.domain.gocean.transformations.gocean_move_iteration_boundaries_inside_kernel_trans.GOMoveIterationBoundariesInsideKernelTrans.validate(), psyclone.domain.gocean.transformations.gocean_opencl_trans.GOOpenCLTrans.validate(), psyclone.domain.gocean.transformations.raise_psyir_2_gocean_kern_trans.RaisePSyIR2GOceanKernTrans.validate(), psyclone.domain.lfric.transformations.lfric_alg_invoke_2_psy_call_trans.LFRicAlgInvoke2PSyCallTrans.validate(), psyclone.domain.lfric.transformations.raise_psyir_2_lfric_kern_trans.RaisePSyIR2LFRicKernTrans.validate(), psyclone.domain.nemo.transformations.create_nemo_invoke_schedule_trans.CreateNemoInvokeScheduleTrans.validate(), psyclone.domain.nemo.transformations.create_nemo_psy_trans.CreateNemoPSyTrans.validate(), psyclone.domain.nemo.transformations.nemo_allarrayrange2loop_trans.NemoAllArrayRange2LoopTrans.validate(), psyclone.domain.nemo.transformations.nemo_arrayrange2loop_trans.NemoArrayRange2LoopTrans.validate(), psyclone.domain.nemo.transformations.nemo_outerarrayrange2loop_trans.NemoOuterArrayRange2LoopTrans.validate(), psyclone.psyad.transformations.assignment_trans.AssignmentTrans.validate(), psyclone.psyGen.Transformation.validate(), psyclone.psyir.transformations.acc_update_trans.ACCUpdateTrans.validate(), psyclone.psyir.transformations.allarrayaccess2loop_trans.AllArrayAccess2LoopTrans.validate(), psyclone.psyir.transformations.arrayaccess2loop_trans.ArrayAccess2LoopTrans.validate(), psyclone.psyir.transformations.arrayrange2loop_trans.ArrayRange2LoopTrans.validate(), psyclone.psyir.transformations.chunk_loop_trans.ChunkLoopTrans.validate(), psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans.validate(), psyclone.psyir.transformations.hoist_local_arrays_trans.HoistLocalArraysTrans.validate(), psyclone.psyir.transformations.hoist_loop_bound_expr_trans.HoistLoopBoundExprTrans.validate(), psyclone.psyir.transformations.hoist_trans.HoistTrans.validate(), psyclone.psyir.transformations.inline_trans.InlineTrans.validate(), psyclone.psyir.transformations.intrinsics.array_reduction_base_trans.ArrayReductionBaseTrans.validate(), psyclone.psyir.transformations.intrinsics.dotproduct2code_trans.DotProduct2CodeTrans.validate(), psyclone.psyir.transformations.intrinsics.intrinsic2code_trans.Intrinsic2CodeTrans.validate(), psyclone.psyir.transformations.intrinsics.matmul2code_trans.Matmul2CodeTrans.validate(), psyclone.psyir.transformations.loop_swap_trans.LoopSwapTrans.validate(), psyclone.psyir.transformations.loop_tiling_2d_trans.LoopTiling2DTrans.validate(), psyclone.psyir.transformations.loop_trans.LoopTrans.validate(), psyclone.psyir.transformations.omp_task_trans.OMPTaskTrans.validate(), psyclone.psyir.transformations.omp_taskwait_trans.OMPTaskwaitTrans.validate(), psyclone.psyir.transformations.parallel_loop_trans.ParallelLoopTrans.validate(), psyclone.psyir.transformations.reference2arrayrange_trans.Reference2ArrayRangeTrans.validate(), psyclone.psyir.transformations.replace_induction_variables_trans.ReplaceInductionVariablesTrans.validate(), psyclone.transformations.OMPDeclareTargetTrans.validate(), psyclone.transformations.DynamoOMPParallelLoopTrans.validate(), psyclone.transformations.Dynamo0p3OMPLoopTrans.validate(), psyclone.transformations.GOceanOMPLoopTrans.validate(), psyclone.transformations.Dynamo0p3RedundantComputationTrans.validate(), psyclone.transformations.Dynamo0p3KernelConstTrans.validate(), psyclone.transformations.ACCRoutineTrans.validate(), psyclone.transformations.KernelImportsToArguments.validate(), psyclone.domain.gocean.transformations.gocean_loop_fuse_trans.GOceanLoopFuseTrans.validate(), psyclone.domain.lfric.transformations.lfric_loop_fuse_trans.LFRicLoopFuseTrans.validate(), psyclone.psyir.transformations.loop_fuse_trans.LoopFuseTrans.validate(), psyclone.domain.gocean.transformations.gocean_extract_trans.GOceanExtractTrans.validate(), psyclone.domain.lfric.transformations.lfric_extract_trans.LFRicExtractTrans.validate(), psyclone.psyir.transformations.extract_trans.ExtractTrans.validate(), psyclone.psyir.transformations.nan_test_trans.NanTestTrans.validate(), psyclone.psyir.transformations.read_only_verify_trans.ReadOnlyVerifyTrans.validate(), psyclone.transformations.ParallelRegionTrans.validate(), psyclone.transformations.OMPParallelTrans.validate(), psyclone.transformations.ACCParallelTrans.validate(), psyclone.transformations.ACCKernelsTrans.validate(), psyclone.transformations.ACCDataTrans.validate(), psyclone.psyir.transformations.psy_data_trans.PSyDataTrans.validate(), psyclone.psyir.transformations.region_trans.RegionTrans.validate(), and psyclone.transformations.ACCEnterDataTrans.validate().

Here is the call graph for this function:

◆ name()

def psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans.name (   self)
Returns the name of this transformation as a string.

Reimplemented from psyclone.psyGen.Transformation.

Definition at line 83 of file fold_conditional_return_expressions_trans.py.

83  def name(self):
84  '''Returns the name of this transformation as a string.'''
85  return "FoldConditionalReturnExpressionsTrans"
86 
Here is the caller graph for this function:

◆ validate()

def psyclone.psyir.transformations.fold_conditional_return_expressions_trans.FoldConditionalReturnExpressionsTrans.validate (   self,
  node,
  options = None 
)
Ensure that it is valid to apply this transformation to the
supplied node.

:param node: the node to validate.
:type node: :py:class:`psyclone.psyir.nodes.Routine`
:param options: a dictionary with options for transformations.
:type options: Optional[Dict[str, Any]]

:raises TransformationError: if the node is not a Routine.

Reimplemented from psyclone.psyGen.Transformation.

Definition at line 87 of file fold_conditional_return_expressions_trans.py.

87  def validate(self, node, options=None):
88  '''Ensure that it is valid to apply this transformation to the
89  supplied node.
90 
91  :param node: the node to validate.
92  :type node: :py:class:`psyclone.psyir.nodes.Routine`
93  :param options: a dictionary with options for transformations.
94  :type options: Optional[Dict[str, Any]]
95 
96  :raises TransformationError: if the node is not a Routine.
97 
98  '''
99  if not isinstance(node, Routine):
100  raise TransformationError(
101  f"Error in {self.name} transformation. This transformation "
102  f"can only be applied to 'Routine' nodes, but found "
103  f"'{type(node).__name__}'.")
104 
Here is the caller graph for this function:

The documentation for this class was generated from the following file: