Skip to content

Commit 58df797

Browse files
Merge pull request #2946 from devitocodes/hotfix-sizeof
compiler: Fix SizeOf reconstruction
2 parents bad445e + 12df74b commit 58df797

3 files changed

Lines changed: 56 additions & 8 deletions

File tree

devito/ir/cgen/printer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def _print_DefFunction(self, expr):
415415
return f"{expr.name}{template}({args})"
416416

417417
def _print_SizeOf(self, expr):
418-
return f'sizeof({self._print(expr.intype)}{self._print(expr.stars)})'
418+
return f'sizeof({self._print(expr.ctype)}{self._print(expr.stars)})'
419419

420420
def _print_MathFunction(self, expr):
421421
return f"{self.ns(expr)}{self._print_DefFunction(expr)}"

devito/symbolics/extended_sympy.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -980,7 +980,6 @@ class SizeOf(DefFunction):
980980
__rargs__ = ('intype', 'stars')
981981

982982
def __new__(cls, intype, stars=None, **kwargs):
983-
stars = stars or ''
984983
if not isinstance(intype, (str, ReservedWord)):
985984
ctype = dtype_to_ctype(intype)
986985
for k, v in ctypes_vector_mapper.items():
@@ -990,15 +989,40 @@ def __new__(cls, intype, stars=None, **kwargs):
990989
else:
991990
intype = ctypes_to_cstr(ctype)
992991

993-
newobj = super().__new__(cls, 'sizeof', arguments=f'{intype}{stars}', **kwargs)
994-
newobj.stars = stars
995-
newobj.intype = intype
992+
stars = stars or ''
993+
if not all(c == '*' for c in str(stars)):
994+
raise ValueError("`stars` must be a string of zero or more `*` characters")
995+
996+
if not isinstance(intype, (str, ReservedWord)):
997+
intype = ctypes_vector_mapper[intype].__name__
996998

997-
return newobj
999+
return super().__new__(cls, 'sizeof', arguments=(intype, stars), **kwargs)
9981000

9991001
@property
10001002
def args(self):
1001-
return super().args[1]
1003+
return self._arguments
1004+
1005+
@property
1006+
def intype(self):
1007+
return self.arguments[0]
1008+
1009+
@cached_property
1010+
def ctype(self):
1011+
for v in ctypes_vector_mapper.values():
1012+
if str(self.intype) == v.__name__:
1013+
return v
1014+
return self.intype
1015+
1016+
@property
1017+
def stars(self):
1018+
return self.arguments[1]
1019+
1020+
def __str__(self):
1021+
try:
1022+
intype = ctypes_to_cstr(self.ctype)
1023+
except TypeError:
1024+
intype = str(self.ctype)
1025+
return f"sizeof({intype}{self.stars})"
10021026

10031027

10041028
def rfunc(func, item, *args):

tests/test_symbolics.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from ctypes import c_void_p
1+
from ctypes import c_uint64, c_void_p
22

33
import numpy as np
44
import pytest
@@ -1262,6 +1262,30 @@ def test_print_div():
12621262
assert cstr == 'sizeof(int)/sizeof(long)'
12631263

12641264

1265+
def test_sizeof():
1266+
sizeof_ctype = SizeOf(c_uint64)
1267+
str_pointer0 = SizeOf('float', stars='*')
1268+
str_pointer1 = SizeOf('float', '*')
1269+
str_simple = SizeOf('int')
1270+
complex_size = SizeOf(np.complex64)
1271+
1272+
assert sizeof_ctype.arguments == (ReservedWord('unsigned long'), ReservedWord(''))
1273+
assert str_pointer0.arguments == (ReservedWord('float'), ReservedWord('*'))
1274+
assert complex_size.arguments == (ReservedWord('c_complex'), ReservedWord(''))
1275+
1276+
# Printing
1277+
assert ccode(sizeof_ctype) == 'sizeof(unsigned long)'
1278+
assert ccode(str_pointer0) == ccode(str_pointer1) == 'sizeof(float*)'
1279+
assert ccode(str_simple) == 'sizeof(int)'
1280+
assert str(complex_size) == 'sizeof(c_complex)'
1281+
assert ccode(complex_size) == 'sizeof(float _Complex)'
1282+
1283+
# Reconstruction
1284+
assert ccode(str_pointer0.func(*str_pointer0.args)) == 'sizeof(float*)'
1285+
assert ccode(str_pointer0.func('int', stars='**')) == 'sizeof(int**)'
1286+
assert ccode(complex_size.func(*complex_size.args)) == 'sizeof(float _Complex)'
1287+
1288+
12651289
def test_customdtype_complex():
12661290
"""
12671291
Test that `CustomDtype` doesn't brak is_imag

0 commit comments

Comments
 (0)