From 5a22a69a84e858d88a02a0039fa057101eb84cb1 Mon Sep 17 00:00:00 2001 From: Edward Caunt Date: Mon, 5 Oct 2026 09:10:38 +0000 Subject: [PATCH 1/2] tests: Add test to ensure operands are cast to long when calculating temp array allocation sizes --- devito/passes/iet/definitions.py | 2 +- tests/test_dse.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/devito/passes/iet/definitions.py b/devito/passes/iet/definitions.py index ceb5b18fab..bf20eced68 100644 --- a/devito/passes/iet/definitions.py +++ b/devito/passes/iet/definitions.py @@ -601,7 +601,7 @@ def _alloc_local_array_on_high_bw_mem(self, site, obj, storage): doalloc = self.langbb['device-alloc'] dofree = self.langbb['device-free'] - nbytes = SizeOf(obj._C_typedata)*obj.size + nbytes = SizeOf(obj._C_typedata)*as_long(obj.size) zeroing, efuncs = self._zero_init(obj, storage) allocs = [doalloc(nbytes, deviceid, retobj=obj), *zeroing] diff --git a/tests/test_dse.py b/tests/test_dse.py index 1a727b1221..6f3cab4053 100644 --- a/tests/test_dse.py +++ b/tests/test_dse.py @@ -2976,6 +2976,30 @@ def test_subdims_wo_block_temps(self): op.apply() + @pytest.mark.parametrize('lang', ['C', 'CXX']) + def test_temp_array_allocation_uses_long(self, lang): + """ + Ensure that operands are cast to long when calculating the size of a + temporary array to prevent int32 wraparound. + """ + grid = Grid(shape=(31, 17, 25)) + + a = Function(name='a', grid=grid) + f = TimeFunction(name='f', grid=grid) + g = TimeFunction(name='g', grid=grid) + + eq0 = Eq(f.forward, g + sin(a).dx) + eq1 = Eq(g.forward, f + sin(a).dx) + + opt = ('advanced', {'cire-minmem': True, 'index-mode': 'int64'}) + with switchconfig(language=lang): + op = Operator([eq0, eq1], opt=opt) + + cast = '(long)' if lang == 'C' else 'static_cast' + + # 3 casts in memalign, 3 in memset + assert str(op).count(cast) == 6 + class TestIsoAcoustic: From e100bb653e56c9c43a6e05dd22fd775b05632dc4 Mon Sep 17 00:00:00 2001 From: Edward Caunt Date: Mon, 5 Oct 2026 13:25:35 +0000 Subject: [PATCH 2/2] tests: Tweak omp offloading test to include casts in allocation string --- tests/test_gpu_openmp.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_gpu_openmp.py b/tests/test_gpu_openmp.py index 6b5458b23f..9c2ab3f991 100644 --- a/tests/test_gpu_openmp.py +++ b/tests/test_gpu_openmp.py @@ -217,7 +217,8 @@ def test_array_rw(self): assert len(op.body.allocs) == 2 assert str(op.body.allocs[0]) ==\ ('float * r0_vec = (float *)' - 'omp_target_alloc(x_size*y_size*z_size*sizeof(float),' + 'omp_target_alloc(' + 'sizeof(float)*(long)z_size*(long)y_size*(long)x_size,' 'omp_get_default_device());') assert str(op.body.allocs[1]) ==\ 'init0(x_M,x_m,y_M,y_m,z_M,z_m,r0_vec,y_size,z_size);'