loki.transformations.remove_code
Collection of utilities to automatically remove code elements or section and to perform Dead Code Elimination.
Functions
|
Utility routine to remove all |
|
Perform Dead Code Elimination on the given |
|
Utility routine to remove code regions marked with |
|
Utility routine to remove unused arguments from all the :any:`CallStatement`s in a given routine. |
|
Utility routine to remove unused dummy arguments from a given routine. |
|
Utility routine to remove unused variables (or only local arrays) from a given routine. |
|
Utility routine to find all the unused arguments in a |
Classes
|
A |
|
A |
|
|
|
A |
- 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:
TransformationA
Transformationthat provides named call and import removal, code removal of pragma-marked regions and Dead Code Elimination for batch processing via theScheduler.The transformation will apply the following methods in order:
- Parameters:
remove_marked_regions (boolean) – Flag to trigger the use of
remove_marked_regions(); default:Truemark_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:Falseuse_simplify (boolean) – Use
simplifywhen branch pruning in duringremove_dead_code().call_names (list of str) – List of subroutine names against which to match
CallStatementnodes duringremove_calls().intrinsic_names (list of str) – List of module names against which to match
Intrinsicnodes duringremove_calls().remove_imports (boolean) – Flag indicating whether to remove symbols from
Importobjects duringremove_calls(); default:Truekernel_only (boolean) – Only apply the configured removal to subroutines marked as “kernel”; default:
Falseremove_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
Subroutineitems.For transformations that modify
Subroutineobjects, this method should be implemented. It gets called via the dispatch methodapply().- 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
Subroutineobject.- Parameters:
routine (
Subroutine) – The subroutine to which to apply dead code elimination.simplify (boolean) – Use
simplifywhen evaluating expressions for branch pruning.
- class RemoveDeadCodeTransformer(use_simplify=True, **kwargs)
Bases:
TransformerTransformerclass that removes provably unreachable code paths.The primary modification performed is to prune individual code branches under
Conditionalnodes.- Parameters:
use_simplify (boolean) – Use
simplifywhen 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 removepragmas 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:
TransformerA
Transformerthat removes code regions marked with!$loki removepragmas.This
Transformeronly removesPragmaRegionnodes, and thus requires the IR tree to have pragma regions attached, for example viapragma_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
PragmaRegionnodes with!$loki removepragmas
- do_remove_calls(routine, call_names=None, intrinsic_names=None, remove_imports=True)
Utility routine to remove all
CallStatementnodes to specific named subroutines in aSubroutine.For more information, see
RemoveCallsTransformer.- Parameters:
call_names (list of str) – List of subroutine names against which to match
CallStatementnodes.intrinsic_names (list of str) – List of module names against which to match
Intrinsicnodes.remove_imports (boolean) – Flag indicating whether to remove the respective procedure symbols from
Importobjects; default:True.
- class RemoveCallsTransformer(call_names=None, intrinsic_names=None, remove_imports=True, **kwargs)
Bases:
TransformerA
Transformerthat removes allCallStatementnodes to specific named subroutines.This
Transformerwill by default also remove the enclosing inline-conditional when encountering calls of the form`if (flag) call named_procedure().This
Transformerwill also attempt to match and removeIntrinsicnodes against a given list of name strings. This allows removing intrinsic calls likewrite (*,*) "...".In addition, this
Transformercan also attempt to match and removeImportnodes 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
CallStatementnodes.intrinsic_names (list of str) – List of module names against which to match
Intrinsicnodes.remove_imports (boolean) – Flag indicating whether to remove the respective procedure symbols from
Importobjects; default:True.
- visit_CallStatement(o, **kwargs)
Match and remove
CallStatementnodes against name patterns
- visit_Conditional(o, **kwargs)
Remove inline-conditionals after recursing into their body
- 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) – ASubroutinewhose 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_varsutility.remove_only_arrays (bool, optional) – Whether to only remove arrays or all variables/temporaries that are unused within the routine.