loki.expression.expr_visitors
Visitor classes for traversing and transforming all expression trees in Loki’s internal representation (IR).
Classes
|
Scoping visitor that traverses the control flow tree and uses |
|
Base class visitor to collect specific sub-expressions, eg. |
|
A visitor to obtain the root node of the expression tree in which a given |
|
A visitor to collect all expression tree nodes (i.e., |
|
A visitor to collect all |
|
A visitor to collect all literals (which includes |
|
A visitor to collect all |
|
A visitor to collect all variables used in an IR tree |
|
A dedicated visitor to perform expression substitution in all IR nodes |
- class FindExpressions(recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to collect all expression tree nodes (i.e.,
pymbolic.primitives.Expression) in an IR tree.See
ExpressionFinder- Parameters:
recurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class FindVariables(recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to collect all variables used in an IR tree
This refers to expression tree nodes
Scalar,Arrayand alsoDeferredTypeSymbol.See
ExpressionFinderfor further details- Parameters:
recurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class FindTypedSymbols(recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to collect all
TypedSymbolused in an IR tree.See
ExpressionFinder- Parameters:
recurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class FindInlineCalls(recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to collect all
InlineCallsymbols used in an IR tree.See
ExpressionFinder- Parameters:
recurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class FindLiterals(recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to collect all literals (which includes
FloatLiteral,IntLiteral,LogicLiteral,StringLiteral, andIntrinsicLiteral) used in an IR tree.See
ExpressionFinder- Parameters:
recurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class FindExpressionRoot(expr, recurse_to_parent=True, **kwargs)
Bases:
ExpressionFinderA visitor to obtain the root node of the expression tree in which a given
pymbolic.primitives.Expressionis located.- Parameters:
expr (
pymbolic.primitives.Expression) – The expression for which to find the root noderecurse_to_parent (bool, optional) – For symbols that belong to a derived type, recurse also to the
parentof that symbol (default: True)
- class SubstituteExpressions(expr_map, invalidate_source=True, **kwargs)
Bases:
TransformerA dedicated visitor to perform expression substitution in all IR nodes
It applies
SubstituteExpressionsMapperwith the providedexpr_mapto every expression in the traversed IR tree.Note
No recursion is performed on substituted expression nodes, they are taken as-is from the map. Otherwise substitutions that involve the original node would result in infinite recursion - for example a replacement that wraps a variable in an inline call:
my_var -> wrapped_in_call(my_var).When there is a need to recursively apply the mapping, the mapping needs to be applied to itself first. A potential use-case is renaming of variables, which may appear as the name of an array subscript as well as in the
dimensionsattribute of the same expression:SOME_ARR(SOME_ARR > SOME_VAL). The mapping can be applied to itself using the utility functionrecursive_expression_map_update.- Parameters:
- visit_Expression(o, **kwargs)
call
SubstituteExpressionsMapperfor the given expression node
- visit_Import(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- visit_VariableDeclaration(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- visit_ProcedureDeclaration(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- class ExpressionFinder(unique=True, retrieve=None, with_ir_node=False)
Bases:
VisitorBase class visitor to collect specific sub-expressions, eg. functions or symbols, from all nodes in an IR tree.
Note that specialized
FindXXXclasses are provided inloki.expressionto find some of the most common sub-expression types, eg. symbols, functions and variables.- Parameters:
retrieve – Custom retrieval function that yields all wanted sub-expressions from an expression.
unique (bool, optional) – If True the visitor will return a set of unique sub-expression instead of a list of possibly repeated instances.
with_ir_node (bool, optional) – If True the visitor will return tuples which contain the sub-expression and the corresponding IR node in which the expression is contained.
- static default_retrieval_function(x)
Default retrieval function that returns nothing
- static retrieval_function(x)
Default retrieval function that returns nothing
- find_uniques(variables)
Reduces the number of matched sub-expressions to a set of unique sub-expressions, if self.unique is True.
Currently, two sub-expressions are considered NOT to be unique if they have the same -
name-parent.name(or None) -dimensions(forArray)
- retrieve(expr)
Internal retrieval function used on expressions.
- visit_tuple(o, **kwargs)
Visit all elements in a tuple and return the results as a tuple.
- visit_list(o, **kwargs)
Visit all elements in a tuple and return the results as a tuple.
- visit_Expression(o, **kwargs)
- visit_TypeDef(o, **kwargs)
Custom handler for
TypeDefnodes that does not traverse the body (reason being that discovering variables used or declared inside the type definition would be unexpected if called on a containingSubroutineorModule)
- class AttachScopes(fail=False)
Bases:
VisitorScoping visitor that traverses the control flow tree and uses
AttachScopesMapperto update allTypedSymbolexpression tree nodes with a pointer to their corresponding scope.- Parameters:
fail (bool, optional) – If set to True, this lets the visitor fail if it encounters a node without a declaration or an entry in any of the symbol tables (default: False).
- visit_object(o, **kwargs)
Return any foreign object unchanged.
- visit(o, *args, **kwargs)
Default visitor method that dispatches the node-specific handler
- visit_Expression(o, **kwargs)
Dispatch
AttachScopesMapperforExpressiontree nodes
- visit_list(o, **kwargs)
Visit each entry in a list and return as a tuple
- visit_tuple(o, **kwargs)
Visit each entry in a list and return as a tuple
- visit_Node(o, **kwargs)
Generic handler for IR
NodeobjectsRecurses to children and updates the node
- visit_Import(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- visit_VariableDeclaration(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- visit_ProcedureDeclaration(o, **kwargs)
For
Import(as well asVariableDeclarationandProcedureDeclaration) we setrecurse_to_declaration_attributes=Trueto make sure properties in the symbol table are updated during dispatch to the expression mapper
- visit_Scope(o, **kwargs)
Generic handler for
ScopeobjectsMakes sure that declared variables and imported symbols have an entry in that node’s symbol table before recursing to children with this node as new scope.
- visit_Subroutine(o, **kwargs)
Handler for
SubroutinenodesMakes sure that declared variables and imported symbols have an entry in the routine’s symbol table before recursing to spec, body, and member routines with this routine as new scope.