Skip to content
Open
Changes from 3 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
19 changes: 14 additions & 5 deletions src/gt4py/next/otf/compilation/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import dataclasses
import pathlib
from typing import Protocol, TypeVar
from typing import Protocol, TypeVar, cast

import factory

Expand Down Expand Up @@ -83,11 +83,20 @@ def __call__(
f"On-the-fly compilation unsuccessful for '{inp.program_source.entry_point.name}'."
)

compiled_prog = getattr(
importer.import_from_path(src_dir / new_data.module), new_data.entry_point_name
)
m = importer.import_from_path(src_dir / new_data.module)
func = getattr(m, new_data.entry_point_name)

return compiled_prog
# Since nanobind 2.10, calling functions with ndarray args crashes (SEGFAULT)
# when there are not live references to their extension module (see: https://github.com/wjakob/nanobind/issues/1283)
# Here we dynamically create a new callable class holding a reference to the
# module and the function, whose `__call__` is exactly the `__call__` method
# of the returned (nanobind) nbfunction object. As long as this object is alive,
# the module reference is kept alive too, preventing the SEGFAULT.
managed_entry_point = type(
f"{m.__name__}__{id(m)}", (), dict(__call__=func.__call__, module_ref=m, func_ref=func)
)()

return cast(stages.CompiledProgram, managed_entry_point)


class CompilerFactory(factory.Factory):
Expand Down
Loading