Skip to content

Fixes to reflection cache #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions slangpy/reflection/reflectiontypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,7 @@ def find_function_by_name(self, name: str) -> Optional[SlangFunction]:
if func_refl is None:
return None
res = self._get_or_create_function(func_refl, None, name)
self._functions_by_name[name] = res
return res

def require_function_by_name(self, name: str) -> SlangFunction:
Expand Down Expand Up @@ -1157,6 +1158,7 @@ def find_function_by_name_in_type(self, type: SlangType, name: str) -> Optional[
self._get_or_create_type(type_refl),
name,
)
self._functions_by_name[qualified_name] = res
return res

def require_function_by_name_in_type(self, type: SlangType, name: str) -> SlangFunction:
Expand Down Expand Up @@ -1221,11 +1223,6 @@ def _get_or_create_function(
return existing
res = self._reflect_function(refl, this, full_name)
self._functions_by_reflection[refl] = res

if this is not None:
self._functions_by_name[f"{this.full_name}::{res.name}"] = res
else:
self._functions_by_name[res.name] = res
return res

def _reflect_type(self, refl: TypeReflection):
Expand Down
27 changes: 27 additions & 0 deletions slangpy/tests/slangpy_tests/test_reflection.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,28 @@ def test_method(device_type: DeviceType):
assert res.return_type == layout.scalar_type(TypeReflection.ScalarType.int32)


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_overloads(device_type: DeviceType):
device = helpers.get_device(device_type)
m = helpers.create_module(device, MODULE)

layout = m.layout

res = layout.find_function_by_name("foo_ol")
assert res is not None
assert res.is_overloaded

# Look up specialization
specialization = res.overloads[0]
reflected_specialization = layout.find_function(specialization.reflection, None)
assert reflected_specialization is not None

# ...and make sure it did not overwrite the cache
re_lookup = layout.find_function_by_name("foo_ol")
assert re_lookup is not None
assert re_lookup.is_overloaded


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_generic_specialization(device_type: DeviceType):
device = helpers.get_device(device_type)
Expand All @@ -110,6 +132,11 @@ def test_generic_specialization(device_type: DeviceType):
assert res.parameters[0].name == "a"
assert res.parameters[0].type == layout.scalar_type(TypeReflection.ScalarType.float32)

# Check that the specialization did not get cached under the unspecialized name
unspecialized = layout.find_function_by_name("foo_generic")
assert unspecialized is not None
assert unspecialized.full_name == "foo_generic"


@pytest.mark.parametrize("device_type", helpers.DEFAULT_DEVICE_TYPES)
def test_generic_parsing(device_type: DeviceType):
Expand Down
Loading