From 7dba0ecc5de13d81f71501688b21512f075e0d71 Mon Sep 17 00:00:00 2001 From: Gerard Gorman Date: Wed, 14 Oct 2026 21:43:04 +0000 Subject: [PATCH] compiler: Run linearize after DataManager when listed as a pass Listing 'linearize' in a custom pass list ran it before the symbol definitions, emitting both linearized macros and VLA casts (compile error). Turn it into the homonymous option, as already done for 'openmp'. --- devito/operator/operator.py | 6 ++++++ tests/test_linearize.py | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/devito/operator/operator.py b/devito/operator/operator.py index b3690caf74..70ca7eb4a3 100644 --- a/devito/operator/operator.py +++ b/devito/operator/operator.py @@ -1667,6 +1667,12 @@ def parse_kwargs(**kwargs): if isinstance(overrides, dict): options.update(overrides) + # A listed `linearize` (e.g., `opt=('linearize', {})`) must run after the + # symbol definitions, so it is turned into the homonymous option + if mode and 'linearize' in as_tuple(mode): + options.setdefault('linearize', True) + mode = tuple(i for i in as_tuple(mode) if i != 'linearize') + # `opt`, deprecated kwargs kwopenmp = kwargs.get('openmp', options.get('openmp')) if kwopenmp is None: diff --git a/tests/test_linearize.py b/tests/test_linearize.py index c78880924d..0cbb1bbf5e 100644 --- a/tests/test_linearize.py +++ b/tests/test_linearize.py @@ -737,3 +737,26 @@ def test_bundle_derived_dim_stride(): assert bundle.symbolic_shape[p] is not bundle.symbolic_shape[rp] assert bundle.symbolic_shape[rp] == 2 assert bundle.symbolic_shape.dindex(rp) == 1 + + +@pytest.mark.parametrize('opt', [ + (('linearize',), {}), + (('linearize',), {'linearize': True}), + (('openmp', 'linearize'), {'linearize': True}), +]) +def test_linearize_as_pass(opt): + """ + Listing `linearize` among the passes enables linearization after the + symbol definitions are in place, as with `opt=('advanced', {'linearize': True})`. + """ + grid = Grid(shape=(4, 4)) + + u = TimeFunction(name='u', grid=grid) + + op = Operator(Eq(u.forward, u + 1), opt=opt, name='LinearizeAsPass') + + assert 'uL0' in str(op) + + op.apply(time_M=2) + + assert np.all(u.data[1] == 3)