loki.transformations.remove_code

Collection of utilities to automatically remove code elements or section and to perform Dead Code Elimination.

Functions

do_remove_calls(routine[, call_names, ...])

Utility routine to remove all CallStatement nodes to specific named subroutines in a Subroutine.

do_remove_dead_code(routine[, use_simplify])

Perform Dead Code Elimination on the given Subroutine object.

do_remove_marked_regions(routine[, ...])

Utility routine to remove code regions marked with !$loki remove pragmas from a subroutine's body.

do_remove_unused_call_args(routine, ...)

Utility routine to remove unused arguments from all the :any:`CallStatement`s in a given routine.

do_remove_unused_dummy_args(routine, unused_args)

Utility routine to remove unused dummy arguments from a given routine.

do_remove_unused_vars(routine[, ...])

Utility routine to remove unused variables (or only local arrays) from a given routine.

find_unused_dummy_args_and_vars(routine)

Utility routine to find all the unused arguments in a Subroutine.

Classes

RemoveCallsTransformer([call_names, ...])

A Transformer that removes all CallStatement nodes to specific named subroutines.

RemoveCodeTransformation([...])

A Transformation that provides named call and import removal, code removal of pragma-marked regions and Dead Code Elimination for batch processing via the Scheduler.

RemoveDeadCodeTransformer([use_simplify])

Transformer class that removes provably unreachable code paths.

RemoveRegionTransformer([mark_with_comment, ...])

A Transformer that removes code regions marked with !$loki remove pragmas.

class RemoveCodeTransformation(remove_marked_regions=True, mark_with_comment=True, replacement_call=None, replacement_msg=None, replacement_module=None, remove_dead_code=False, use_simplify=True, call_names=None, intrinsic_names=None, remove_imports=True, kernel_only=False, remove_unused_args=False, remove_unused_vars=False, remove_only_arrays=True)

Bases: Transformation

A Transformation that provides named call and import removal, code removal of pragma-marked regions and Dead Code Elimination for batch processing via the Scheduler.

The transformation will apply the following methods in order:

Parameters:
  • remove_marked_regions (boolean) – Flag to trigger the use of remove_marked_regions(); default: True

  • mark_with_comment (boolean) – Flag to trigger the insertion of a marker comment when removing a region; default: True.

  • replacement_call (optional, str) – Name of the “abort” subroutine to call if a replacement call is to be inserted in do_remove_marked_regions().

  • replacement_msg (optional, str) – Optional error message that will be passed as argument to the replacement call in do_remove_marked_regions().

  • replacement_module (optional, str) – Optional name of the module from which to import the replacement subroutine in do_remove_marked_regions().

  • remove_dead_code (boolean) – Flag to trigger the use of remove_dead_code(); default: False

  • use_simplify (boolean) – Use simplify when branch pruning in during remove_dead_code().

  • call_names (list of str) – List of subroutine names against which to match CallStatement nodes during remove_calls().

  • intrinsic_names (list of str) – List of module names against which to match Intrinsic nodes during remove_calls().

  • remove_imports (boolean) – Flag indicating whether to remove symbols from Import objects during remove_calls(); default: True

  • kernel_only (boolean) – Only apply the configured removal to subroutines marked as “kernel”; default: False

  • remove_unused_args (boolean) – Remove unused dummy arguments from routines.

  • remove_unused_vars (boolean) – Remove unused variables/locals from routines.

  • remove_only_arrays (boolean) – Whether to only remove unused arrays from routines or all variables/locals.

recurse_to_internal_procedures = True
reverse_traversal = True
transform_subroutine(routine, **kwargs)

Defines the transformation to apply to Subroutine items.

For transformations that modify Subroutine objects, this method should be implemented. It gets called via the dispatch method apply().

Parameters:
  • routine (Subroutine) – The subroutine to be transformed.

  • **kwargs (optional) – Keyword arguments for the transformation.

do_remove_dead_code(routine, use_simplify=True)

Perform Dead Code Elimination on the given Subroutine object.

Parameters:
  • routine (Subroutine) – The subroutine to which to apply dead code elimination.

  • simplify (boolean) – Use simplify when evaluating expressions for branch pruning.

class RemoveDeadCodeTransformer(use_simplify=True, **kwargs)

Bases: Transformer

Transformer class that removes provably unreachable code paths.

The primary modification performed is to prune individual code branches under Conditional nodes.

Parameters:

use_simplify (boolean) – Use simplify when evaluating expressions for branch pruning.

visit_Conditional(o, **kwargs)
visit_MultiConditional(o, **kwargs)
do_remove_marked_regions(routine, mark_with_comment=True, replacement_call=None, replacement_msg=None, replacement_module=None)

Utility routine to remove code regions marked with !$loki remove pragmas from a subroutine’s body.

Optionally, any removed region might be marked with a comment and/or a simple single-argument “abort” call. For this, a subroutine name and message can be specified and an optional check for an import can also be defined to ensure the interface for the abort procedure is available. To bypass the replacement call insertion for individual pragma regions use !$loki remove no-replacement-call.

Parameters:
  • routine (Subroutine) – The subroutine to which to apply dead code elimination.

  • mark_with_comment (boolean) – Flag to trigger the insertion of a marker comment when removing a region; default: True.

  • replacement_call (optional, str) – Name of the “abort” subroutine to call if a replacement call is to be inserted.

  • replacement_msg (optional, str) – Optional error message that will be passed as a single argument to the replacement call.

  • replacement_module (optional, str) – Optional name of the module from which to import the replacement subroutine. This will only be inserted if a replacement was perfored and will not replace existing imports of the same module or symbols.

class RemoveRegionTransformer(mark_with_comment=True, replacement_call=None, replacement_msg=None, **kwargs)

Bases: Transformer

A Transformer that removes code regions marked with !$loki remove pragmas.

This Transformer only removes PragmaRegion nodes, and thus requires the IR tree to have pragma regions attached, for example via pragma_regions_attached().

When removing a marked code region the transformer may leave a comment or a replacement call to trigger “abort” errors in the source to mark the previous location.

Parameters:
  • mark_with_comment (boolean) – Flag to trigger the insertion of a marker comment when removing a region; default: True.

  • replacement_call (optional, str) – Name of the “abort” subroutine to call if a replacement call is to be inserted.

  • replacement_msg (optional, str) – Optional error message that will be passed as a single argument to the replacmeent call.

visit_PragmaRegion(o, **kwargs)

Remove PragmaRegion nodes with !$loki remove pragmas

do_remove_calls(routine, call_names=None, intrinsic_names=None, remove_imports=True)

Utility routine to remove all CallStatement nodes to specific named subroutines in a Subroutine.

For more information, see RemoveCallsTransformer.

Parameters:
  • call_names (list of str) – List of subroutine names against which to match CallStatement nodes.

  • intrinsic_names (list of str) – List of module names against which to match Intrinsic nodes.

  • remove_imports (boolean) – Flag indicating whether to remove the respective procedure symbols from Import objects; default: True.

class RemoveCallsTransformer(call_names=None, intrinsic_names=None, remove_imports=True, **kwargs)

Bases: Transformer

A Transformer that removes all CallStatement nodes to specific named subroutines.

This Transformer will by default also remove the enclosing inline-conditional when encountering calls of the form `if (flag) call named_procedure().

This Transformer will also attempt to match and remove Intrinsic nodes against a given list of name strings. This allows removing intrinsic calls like write (*,*) "...".

In addition, this Transformer can also attempt to match and remove Import nodes if given a list of strings to match. This can be used to remove the associated imports of the removed subroutines.

Parameters:
  • call_names (list of str) – List of subroutine names against which to match CallStatement nodes.

  • intrinsic_names (list of str) – List of module names against which to match Intrinsic nodes.

  • remove_imports (boolean) – Flag indicating whether to remove the respective procedure symbols from Import objects; default: True.

visit_CallStatement(o, **kwargs)

Match and remove CallStatement nodes against name patterns

visit_Conditional(o, **kwargs)

Remove inline-conditionals after recursing into their body

visit_Intrinsic(o, **kwargs)

Match and remove Intrinsic nodes against name patterns

visit_Import(o, **kwargs)

Remove the symbol of any named calls from Import nodes

do_remove_unused_vars(routine, unused_vars=None, remove_only_arrays=True)

Utility routine to remove unused variables (or only local arrays) from a given routine.

Parameters:
  • routine (Subroutine) – A Subroutine whose unused dummy arguments will be removed.

  • unused_args (dict, optional) – A list of unused vars. This can be retrieved using the find_unused_dummy_args_and_vars utility.

  • remove_only_arrays (bool, optional) – Whether to only remove arrays or all variables/temporaries that are unused within the routine.