1- from collections import OrderedDict , defaultdict
1+ from collections import defaultdict
22from functools import partial , singledispatch , wraps
33
44import numpy as np
55from sympy import Mul
66
77from devito .ir .iet import (
88 Call , ExprStmt , Expression , Iteration , SyncSpot , AsyncCallable , FindNodes ,
9- FindSymbols , MapNodes , MetaCall , Transformer , EntryFunction ,
10- ThreadCallable , Uxreplace , derive_parameters
9+ FindSymbols , MapNodes , MetaCall , Transformer , EntryFunction , ThreadCallable ,
10+ Uxreplace , derive_parameters
1111)
1212from devito .ir .support import SymbolRegistry
1313from devito .mpi .distributed import MPINeighborhood
2828__all__ = ['Graph' , 'iet_pass' , 'iet_visit' ]
2929
3030
31- class Graph :
31+ class Byproduct :
32+
33+ """
34+ A Byproduct is a mutable collection of metadata produced by one or more
35+ compiler passes.
36+
37+ This metadata may be used internally or by the caller itself, typically
38+ for code generation purposes.
39+ """
40+
41+ def __init__ (self , efuncs = None , includes = None , headers = None , namespaces = None ,
42+ globs = None ):
43+ self .efuncs = efuncs or {}
44+ self .includes = includes or []
45+ self .headers = headers or []
46+ self .namespaces = namespaces or []
47+ self .globals = globs or []
48+
49+ @property
50+ def funcs (self ):
51+ return tuple (MetaCall (v , True ) for v in self .efuncs .values ()
52+ if not isinstance (v , EntryFunction ))
53+
54+
55+ class Graph (Byproduct ):
3256
3357 """
3458 DAG representation of a call graph.
@@ -49,16 +73,9 @@ class Graph:
4973 """
5074
5175 def __init__ (self , iet , options = None , sregistry = None , ** kwargs ):
52- self .efuncs = OrderedDict ([(iet .name , iet )])
53-
5476 self .sregistry = sregistry
5577
56- self .includes = []
57- self .headers = []
58- self .namespaces = []
59- self .globals = []
60-
61- # Stash immutable information useful for one or more compiler passes
78+ super ().__init__ ({iet .name : iet })
6279
6380 # All written user-level objects
6481 writes = FindSymbols ('writes' ).visit (iet )
@@ -79,10 +96,6 @@ def __init__(self, iet, options=None, sregistry=None, **kwargs):
7996 def root (self ):
8097 return self .efuncs [list (self .efuncs ).pop (0 )]
8198
82- @property
83- def funcs (self ):
84- return tuple (MetaCall (v , True ) for v in self .efuncs .values ())[1 :]
85-
8699 @property
87100 def sync_mapper (self ):
88101 """
@@ -146,7 +159,7 @@ def apply(self, func, **kwargs):
146159 new_efuncs = metadata .get ('efuncs' , [])
147160
148161 efuncs [i ] = efunc
149- efuncs .update (OrderedDict ([(i .name , i ) for i in new_efuncs ]))
162+ efuncs .update (dict ([(i .name , i ) for i in new_efuncs ]))
150163
151164 # Update the parameters / arguments lists since `func` may have
152165 # introduced or removed objects
@@ -176,10 +189,24 @@ def visit(self, func, **kwargs):
176189 dag = create_call_graph (self .root .name , self .efuncs )
177190 toposort = dag .topological_sort ()
178191
179- mapper = OrderedDict ([(i , func (self .efuncs [i ], ** kwargs )) for i in toposort ])
192+ mapper = dict ([(i , func (self .efuncs [i ], ** kwargs )) for i in toposort ])
180193
181194 return mapper
182195
196+ def filter (self , key ):
197+ """
198+ Return a Byproduct containing only the Callables in the Graph
199+ for which `key` evaluates to True. The resulting object cannot be
200+ further modified by an IET pass.
201+ """
202+ return Byproduct (
203+ efuncs = {i : v for i , v in self .efuncs .items () if key (v )},
204+ includes = as_tuple (self .includes ),
205+ headers = as_tuple (self .headers ),
206+ namespaces = as_tuple (self .namespaces ),
207+ globs = as_tuple (self .globals )
208+ )
209+
183210
184211def iet_pass (func ):
185212 if isinstance (func , tuple ):
@@ -732,7 +759,7 @@ def _filter(v, efunc=None):
732759
733760 return processed
734761
735- efuncs = OrderedDict (efuncs )
762+ efuncs = dict (efuncs )
736763 efuncs [root .name ] = root ._rebuild (parameters = _filter (root .parameters , root ))
737764
738765 # Update all call sites to use the new signature
0 commit comments