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.

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 vis 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, remove_dead_code=False, use_simplify=True, call_names=None, intrinsic_names=None, remove_imports=True, kernel_only=False)

Bases: Transformation

A Transformation that provides named call and import removal, code removal of pragma-marked regions and Dead Code Elimination for batch processing vis 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.

  • 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

recurse_to_internal_procedures = 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)
do_remove_marked_regions(routine, mark_with_comment=True)

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

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.

class RemoveRegionTransformer(mark_with_comment=True, **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 in the source to mark the previous location, or remove the code region entirely.

Parameters:

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

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