Skip to content

Commit 669ef7f

Browse files
gbonikxiaoqiqi177
authored andcommitted
Move calling convention validation to KernelSignature constructor.
So that it is performed regardless of name mangling. Signed-off-by: Greg Bonik <gbonik@nvidia.com>
1 parent aeddd94 commit 669ef7f

4 files changed

Lines changed: 46 additions & 38 deletions

File tree

src/cuda/tile/compilation/_name_mangling.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,6 @@
1616
from .._cext import CallingConvention
1717

1818

19-
def cconv_require_tuple_constraint(cconv: CallingConvention, cursor: "_Cursor | None" = None):
20-
if cconv.version < 2:
21-
msg = (f"Tuple parameters ('T' constraint) are not supported by calling convention"
22-
f" {cconv.name}; version >= 2 is required")
23-
raise cursor.make_error(msg) if cursor is not None else ValueError(msg)
24-
25-
26-
def cconv_require_static_shape(cconv: CallingConvention, cursor: "_Cursor | None" = None):
27-
if cconv.version < 2:
28-
msg = (f"Static array shapes ('s' predicate) are not supported by calling convention"
29-
f" {cconv.name}; version >= 2 is required")
30-
raise cursor.make_error(msg) if cursor is not None else ValueError(msg)
31-
32-
3319
def mangle_kernel_name(function_name: str,
3420
kernel_signature: KernelSignature) -> str:
3521
alias_group_map, alias_group_names = _map_alias_groups(kernel_signature.parameters)
@@ -158,7 +144,6 @@ def _mangle_constraint(p: ParameterConstraint, alias_group_map: dict[str, int],
158144
assert isinstance(p.element, ArrayConstraint)
159145
return "L" + _mangle_list_constraint(p, alias_group_map, cconv)
160146
elif isinstance(p, TupleConstraint):
161-
cconv_require_tuple_constraint(cconv)
162147
return "T" + _mangle_tuple_constraint(p, alias_group_map, cconv)
163148
elif isinstance(p, ScalarConstraint):
164149
return "S" + _mangle_dtype(p.dtype)
@@ -186,7 +171,6 @@ def _demangle_constraint(cursor: _Cursor,
186171
elif c == "L":
187172
return _demangle_list_constraint(cursor, alias_group_demangler, cconv)
188173
elif c == "T":
189-
cconv_require_tuple_constraint(cconv, orig_cursor)
190174
return _demangle_tuple_constraint(cursor, alias_group_demangler, cconv)
191175
elif c == "S":
192176
dtype = _demangle_dtype(cursor)
@@ -206,8 +190,6 @@ def _demangle_constraint(cursor: _Cursor,
206190
def _mangle_array_constraint(a: ArrayConstraint,
207191
alias_group_map: dict[str, int],
208192
cconv: CallingConvention) -> str:
209-
if any(v is not None for v in a.shape_constant):
210-
cconv_require_static_shape(cconv)
211193
ret = f"{a.ndim}{_mangle_dtype(a.dtype)}"
212194

213195
# NOTE: since we encode axis masks as hex, letters a-f can't be used for predicates
@@ -273,10 +255,8 @@ def _demangle_array_constraint(cursor: _Cursor,
273255
raise mask_cursor.make_error(f"Axis mask {axis_mask:x} has more bits"
274256
f" ({axis_mask.bit_length()}) than array ndim ({ndim})")
275257

276-
s_cursor = cursor.clone()
277258
axis_shape_constant = None
278259
if cursor.read("s") is not None:
279-
cconv_require_static_shape(cconv, s_cursor)
280260
axis_shape_constant = _demangle_signed_int(cursor)
281261

282262
axis_shape_div_by = 1

src/cuda/tile/compilation/_signature.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,8 @@ def __init__(self,
330330

331331
parameters = tuple(_to_constraint(c) for c in parameters)
332332
_validate_alias_groups(parameters)
333+
for x in parameters:
334+
_validate_constraint_support(x, calling_convention)
333335

334336
object.__setattr__(self, "parameters", parameters)
335337
object.__setattr__(self, "calling_convention", calling_convention)
@@ -504,3 +506,24 @@ def _remove_redundant_divisibility_constraints(static_values: tuple[int, ...],
504506
f" which is not divisible by {ret[i]}")
505507
ret[i] = 1
506508
return tuple(ret)
509+
510+
511+
def _validate_constraint_support(constraint: ParameterConstraint, cconv: CallingConvention):
512+
if isinstance(constraint, ScalarConstraint):
513+
pass
514+
elif isinstance(constraint, ArrayConstraint):
515+
if any(x is not None for x in constraint.shape_constant) and cconv.version < 2:
516+
raise ValueError(f"Static array shapes are not supported by calling convention"
517+
f" {cconv.name}; version >= 2 is required")
518+
elif isinstance(constraint, ListConstraint):
519+
_validate_constraint_support(constraint.element, cconv)
520+
elif isinstance(constraint, TupleConstraint):
521+
if cconv.version < 2:
522+
raise ValueError(f"Tuple parameters are not supported by calling convention"
523+
f" {cconv.name}; version >= 2 is required")
524+
for x in constraint.items:
525+
_validate_constraint_support(x, cconv)
526+
elif isinstance(constraint, ConstantConstraint):
527+
pass
528+
else:
529+
raise TypeError(f"Unexpected constraint type: {type(constraint)}")

test/test_export_compat.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
# SPDX-License-Identifier: Apache-2.0
44

55
import ctypes
6+
import re
67
from ctypes import (c_char_p, POINTER, c_void_p, c_int, c_uint64, pointer, CFUNCTYPE, c_uint,
78
c_int32, c_float, byref)
89
from io import BytesIO
910

11+
import pytest
1012
import torch.cuda
1113

1214
import cuda.tile as ct
@@ -201,3 +203,24 @@ def test_export_compat_cutile_python_v2():
201203
out = torch.zeros((), dtype=torch.int32, device="cuda")
202204
_call_kernel(io.getvalue(), "kernel_2_Kt2_T2Si32Si32_T1I10_A0i32", ((3, 7), out), is_v2=True)
203205
assert out.item() == 20
206+
207+
208+
def test_static_shape_with_v1_raises():
209+
cconv = ct.compilation.CallingConvention.cutile_python_v1()
210+
expected_message = re.escape("Static array shapes are not supported by calling convention"
211+
" cutile_python_v1; version >= 2 is required")
212+
with pytest.raises(ValueError, match=expected_message):
213+
ct.compilation.KernelSignature(
214+
[ct.compilation.ArrayConstraint(
215+
ct.float32, 1, index_dtype=ct.int32, stride_lower_bound_incl=0,
216+
alias_groups=(), may_alias_internally=False,
217+
shape_constant=(8,))],
218+
cconv)
219+
220+
221+
def test_tuple_with_v1_raises():
222+
cconv = ct.compilation.CallingConvention.cutile_python_v1()
223+
expected_message = re.escape("Tuple parameters are not supported by calling convention"
224+
" cutile_python_v1; version >= 2 is required")
225+
with pytest.raises(ValueError, match=expected_message):
226+
ct.compilation.KernelSignature([(ct.compilation.ScalarConstraint(ct.int32),)], cconv)

test/test_name_mangling.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -269,30 +269,12 @@ def test_name_mangling_cutile_python_v2(parameters, expected_suffix):
269269
assert demangled_name == func_name
270270

271271

272-
def test_mangle_tuple_with_v1_raises():
273-
cconv = CallingConvention.cutile_python_v1()
274-
sig = KernelSignature([TupleConstraint([ScalarConstraint(int32)])], cconv)
275-
with pytest.raises(ValueError, match="version >= 2"):
276-
mangle_kernel_name("my_kernel", sig)
277-
278-
279272
def test_demangle_tuple_with_v1_raises():
280273
symbol = "my_kernel_Kt1_T1Si32"
281274
with pytest.raises(ValueError, match="version >= 2"):
282275
demangle_kernel_name(symbol)
283276

284277

285-
def test_mangle_static_shape_with_v1_raises():
286-
cconv = CallingConvention.cutile_python_v1()
287-
sig = KernelSignature(
288-
[ArrayConstraint(float32, 1, index_dtype=int32, stride_lower_bound_incl=0,
289-
alias_groups=(), may_alias_internally=False,
290-
shape_constant=(8,))],
291-
cconv)
292-
with pytest.raises(ValueError, match="version >= 2"):
293-
mangle_kernel_name("my_kernel", sig)
294-
295-
296278
def test_demangle_static_shape_with_v1_raises():
297279
symbol = "my_kernel_Kt1_A1f32_1s8l0"
298280
with pytest.raises(ValueError, match="version >= 2"):

0 commit comments

Comments
 (0)