loki.transformations.inline

Collection of utility routines to perform code-level force-inlining.

Functions

inline_constant_parameters(routine[, ...])

Replace instances of variables with known constant values by Literals.

inline_elemental_functions(routine)

Replaces InlineCall expression to elemental functions with the called functions body.

inline_internal_procedures(routine[, ...])

Inline internal subroutines contained in an individual Subroutine.

inline_marked_subroutines(routine[, ...])

Inline Subroutine objects guided by pragma annotations.

inline_member_procedures(routine[, ...])

Inline internal subroutines contained in an individual Subroutine.

inline_statement_functions(routine)

Replaces InlineCall expression to statement functions with the called statement functions rhs.

inline_subroutine_calls(routine, calls, callee)

Inline a set of call to an individual Subroutine at source level.

map_call_to_procedure_body(call, caller)

Resolve arguments of a call and map to the called procedure body.

resolve_sequence_association_for_inlined_calls(...)

Resolve sequence association in calls to all member procedures (if inline_internals = True) or in calls to procedures that have been marked with an inline pragma (if inline_marked = True).

Classes

InlineSubstitutionMapper([invalidate_source])

An expression mapper that defines symbolic substitution for inlining.

InlineTransformation([inline_constants, ...])

Transformation class to apply several types of source inlining when batch-processing large source trees via the Scheduler.

inline_constant_parameters(routine, external_only=True)

Replace instances of variables with known constant values by Literals.

Notes

The .type.initial property is used to derive the replacement value,a which means for symbols imported from external modules, the parent Module needs to be supplied in the definitions to the constructor when creating the Subroutine.

Variables that are replaced are also removed from their corresponding import statements, with empty import statements being removed alltogether.

Parameters:
  • routine (Subroutine) – Procedure in which to inline/resolve constant parameters.

  • external_only (bool, optional) – Do not replace variables declared in the local scope (default: True)

inline_elemental_functions(routine)

Replaces InlineCall expression to elemental functions with the called functions body. This will attempt to resolve the elemental function into a single expression and perform a direct replacement at expression level.

Note, that InlineCall.function.type is used to determine if a function cal be inlined. For functions imported via module use statements. This implies that the module needs to be provided in the definitions argument to the original Subroutine constructor.

inline_internal_procedures(routine, allowed_aliases=None)

Inline internal subroutines contained in an individual Subroutine.

Please note that internal functions are not yet supported!

Parameters:
  • routine (Subroutine) – The subroutine in which to inline all member routines

  • allowed_aliases (tuple or list of str or Expression, optional) – List of variables that will not be renamed in the parent scope, even if they alias with a local declaration.

inline_member_procedures(routine, allowed_aliases=None)

Inline internal subroutines contained in an individual Subroutine.

Please note that internal functions are not yet supported!

Parameters:
  • routine (Subroutine) – The subroutine in which to inline all member routines

  • allowed_aliases (tuple or list of str or Expression, optional) – List of variables that will not be renamed in the parent scope, even if they alias with a local declaration.

inline_marked_subroutines(routine, allowed_aliases=None, adjust_imports=True)

Inline Subroutine objects guided by pragma annotations.

When encountering CallStatement objects that are marked with a !$loki inline pragma, this utility will attempt to replace the call with the body of the called procedure and remap all passed arguments into the calling procedures scope.

Please note that this utility requires CallStatement objects to be “enriched” with external type information.

Parameters:
  • routine (Subroutine) – The subroutine in which to look for pragma-marked procedures to inline

  • allowed_aliases (tuple or list of str or Expression, optional) – List of variables that will not be renamed in the parent scope, even if they alias with a local declaration.

  • adjust_imports (bool) – Adjust imports by removing the symbol of the inlined routine or adding imports needed by the imported routine (optional, default: True)

class InlineTransformation(inline_constants=False, inline_elementals=True, inline_stmt_funcs=False, inline_internals=False, inline_marked=True, remove_dead_code=True, allowed_aliases=None, adjust_imports=True, external_only=True, resolve_sequence_association=False)

Bases: Transformation

Transformation class to apply several types of source inlining when batch-processing large source trees via the Scheduler.

Parameters:
  • inline_constants (bool) – Replace instances of variables with known constant values by Literal (see inline_constant_parameters); default: False.

  • inline_elementals (bool) – Replaces InlineCall expression to elemental functions with the called function’s body (see inline_elemental_functions); default: True.

  • inline_stmt_funcs (bool) – Replaces InlineCall expression to statement functions with the corresponding rhs of the statement function if the statement function declaration is available; default: False.

  • inline_internals (bool) – Inline internal procedure (see inline_internal_procedures); default: False.

  • inline_marked (bool) – Inline Subroutine objects marked by pragma annotations (see inline_marked_subroutines); default: True.

  • remove_dead_code (bool) – Perform dead code elimination, where unreachable branches are trimmed from the code (see dead_code_elimination); default: True

  • allowed_aliases (tuple or list of str or Expression, optional) – List of variables that will not be renamed in the parent scope during internal and pragma-driven inlining.

  • adjust_imports (bool) – Adjust imports by removing the symbol of the inlined routine or adding imports needed by the imported routine (optional, default: True)

  • external_only (bool, optional) – Do not replace variables declared in the local scope when inlining constants (default: True)

  • resolve_sequence_association (bool) – Resolve sequence association for routines that contain calls to inline (default: False)

reverse_traversal = True
creates_items = False
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.

inline_statement_functions(routine)

Replaces InlineCall expression to statement functions with the called statement functions rhs.