|
12 | 12 | Interval, IntervalGroup, IterationSpace, LabeledVector, Queue, Vector, extrema, |
13 | 13 | maximum, minimum, normalize_properties, relax_properties, unbounded, vmax, vmin |
14 | 14 | ) |
| 15 | +from devito.ir.support import null_ispace |
15 | 16 | from devito.passes.clusters.cse import _cse |
16 | 17 | from devito.symbolics import ( |
17 | 18 | Uxmapper, estimate_cost, retrieve_functions, reuse_if_untouched, search, sympy_dtype, |
@@ -860,6 +861,7 @@ def lower_schedule(schedule, meta, sregistry, opt_ftemps, opt_min_dtype, |
860 | 861 | make = TempFunction if opt_ftemps else TempArray |
861 | 862 |
|
862 | 863 | clusters = [] |
| 864 | + inits = [] |
863 | 865 | subs = {} |
864 | 866 | for pivot, writeto, ispace, aliaseds, indicess in schedule: |
865 | 867 | name = sregistry.make_name() |
@@ -928,8 +930,11 @@ def lower_schedule(schedule, meta, sregistry, opt_ftemps, opt_min_dtype, |
928 | 930 | assert writeto.size == 0 |
929 | 931 |
|
930 | 932 | dtype = sympy_dtype(pivot, base=meta.dtype, smin=opt_min_dtype) |
931 | | - obj = Temp(name=name, dtype=dtype) |
| 933 | + const = not meta.guards |
| 934 | + obj = Temp(name=name, dtype=dtype, is_const=const) |
932 | 935 | expression = Eq(obj, uxreplace(pivot, subs)) |
| 936 | + if not const: |
| 937 | + inits.append(Eq(obj, 0)) |
933 | 938 |
|
934 | 939 | callback = lambda idx: obj # noqa: B023 |
935 | 940 |
|
@@ -959,6 +964,13 @@ def lower_schedule(schedule, meta, sregistry, opt_ftemps, opt_min_dtype, |
959 | 964 | # Finally, build the alias Cluster |
960 | 965 | clusters.append(Cluster(expression, ispace, meta.guards, properties)) |
961 | 966 |
|
| 967 | + if inits: |
| 968 | + # To avoid undefined variables when an constant (Temp) alias is used |
| 969 | + # within different guards/loop, we need to initialize it outside of the loops |
| 970 | + # so that it's globally defined. |
| 971 | + # See tests/test_operators.py |
| 972 | + clusters.insert(0, Cluster(inits, null_ispace)) |
| 973 | + |
962 | 974 | return clusters, subs |
963 | 975 |
|
964 | 976 |
|
|
0 commit comments