Skip to content

Commit

Permalink
Merge pull request #9539 from sklam/misc/revert_retarget
Browse files Browse the repository at this point in the history
Revert PR #6870 `numba.core.retarget`
  • Loading branch information
sklam committed Apr 24, 2024
2 parents 069ad43 + f74526d commit 609c8c9
Show file tree
Hide file tree
Showing 15 changed files with 9 additions and 640 deletions.
2 changes: 0 additions & 2 deletions docs/source/developer/repomap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,6 @@ Dispatching
for different type signatures.
- :ghfile:`numba/_dispatcher.cpp` - C++ dispatcher implementation (for speed on
common data types)
- :ghfile:`numba/core/retarget.py` - Support for dispatcher objects to switch
target via a specific with-context.


Compiler Pipeline
Expand Down
7 changes: 7 additions & 0 deletions docs/upcoming_changes/9539.expired.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Removal of ``numba.core.retarget``
----------------------------------

The experimental features implemented in ``numba.core.retarget`` have been
removed. These features were primarily used in numba-dpex, but that project has
replaced its use of ``numba.core.retarget`` with a preference for
*target extension API*.
58 changes: 0 additions & 58 deletions numba/_dispatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,35 +910,6 @@ find_named_args(Dispatcher *self, PyObject **pargs, PyObject **pkws)
return 0;
}


/*
* Management of thread-local
*/

#ifdef _MSC_VER
#define THREAD_LOCAL(ty) __declspec(thread) ty
#else
/* Non-standard C99 extension that's understood by gcc and clang */
#define THREAD_LOCAL(ty) __thread ty
#endif

static THREAD_LOCAL(bool) use_tls_target_stack;


struct raii_use_tls_target_stack {
bool old_setting;

raii_use_tls_target_stack(bool new_setting)
: old_setting(use_tls_target_stack)
{
use_tls_target_stack = new_setting;
}

~raii_use_tls_target_stack() {
use_tls_target_stack = old_setting;
}
};

static PyObject*
Dispatcher_call(Dispatcher *self, PyObject *args, PyObject *kws)
{
Expand All @@ -952,19 +923,6 @@ Dispatcher_call(Dispatcher *self, PyObject *args, PyObject *kws)
PyThreadState *ts = PyThreadState_Get();
PyObject *locals = NULL;

// Check TLS target stack
if (use_tls_target_stack) {
raii_use_tls_target_stack turn_off(false);
PyObject * meth_call_tls_target;
meth_call_tls_target = PyObject_GetAttrString((PyObject*)self,
"_call_tls_target");
if (!meth_call_tls_target) return NULL;
// Transfer control to self._call_tls_target
retval = PyObject_Call(meth_call_tls_target, args, kws);
Py_DECREF(meth_call_tls_target);
return retval;
}

/* If compilation is enabled, ensure that an exact match is found and if
* not compile one */
int exact_match_required = self->can_compile ? 1 : self->exact_match_required;
Expand Down Expand Up @@ -1291,26 +1249,10 @@ static PyObject *compute_fingerprint(PyObject *self, PyObject *args)
return typeof_compute_fingerprint(val);
}

static PyObject *set_use_tls_target_stack(PyObject *self, PyObject *args)
{
int val;
if (!PyArg_ParseTuple(args, "p", &val))
return NULL;
bool old = use_tls_target_stack;
use_tls_target_stack = val;
// return the old value
if (old) {
Py_RETURN_TRUE;
} else {
Py_RETURN_FALSE;
}
}

static PyMethodDef ext_methods[] = {
#define declmethod(func) { #func , ( PyCFunction )func , METH_VARARGS , NULL }
declmethod(typeof_init),
declmethod(compute_fingerprint),
declmethod(set_use_tls_target_stack),
{ NULL },
#undef declmethod
};
Expand Down
7 changes: 0 additions & 7 deletions numba/core/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,6 @@ class Flags(TargetConfig):
default=cpu.InlineOptions("never"),
doc="TODO",
)
# Defines a new target option for tracking the "target backend".
# This will be the XYZ in @jit(_target=XYZ).
target_backend = Option(
type=str,
default="cpu", # if not set, default to CPU
doc="backend"
)

dbg_extend_lifetimes = Option(
type=bool,
Expand Down
5 changes: 0 additions & 5 deletions numba/core/cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,6 @@ def get_ufunc_info(self, ufunc_key):
"error_model",
"inline",
"forceinline",
# Add "target_backend" as a accepted option for the CPU in @jit(...)
"target_backend",
"_dbg_extend_lifetimes",
"_dbg_optnone",
)
Expand Down Expand Up @@ -320,9 +318,6 @@ def finalize(self, flags, options):

flags.inherit_if_not_set("error_model", default="python")

# Add "target_backend" as a option that inherits from the caller
flags.inherit_if_not_set("target_backend")

flags.inherit_if_not_set("forceinline")

if flags.forceinline:
Expand Down
4 changes: 0 additions & 4 deletions numba/core/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,6 @@ def bar(x, y):
assert type(nopython) is bool, "nopython option must be a bool"
if nopython is True and forceobj:
raise ValueError("Only one of 'nopython' or 'forceobj' can be True.")

if "_target" in options:
# Set the "target_backend" option if "_target" is defined.
options['target_backend'] = options['_target']
target = options.pop('_target', 'cpu')

if nopython is False:
Expand Down
80 changes: 0 additions & 80 deletions numba/core/dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,54 +21,9 @@
from numba.core.bytecode import get_code_object
from numba.core.caching import NullCache, FunctionCache
from numba.core import entrypoints
from numba.core.retarget import BaseRetarget
import numba.core.event as ev


class _RetargetStack(utils.ThreadLocalStack, stack_name="retarget"):
def push(self, state):
super().push(state)
_dispatcher.set_use_tls_target_stack(len(self) > 0)

def pop(self):
super().pop()
_dispatcher.set_use_tls_target_stack(len(self) > 0)


class TargetConfigurationStack:
"""The target configuration stack.
Uses the BORG pattern and stores states in threadlocal storage.
WARNING: features associated with this class are experimental. The API
may change without notice.
"""

def __init__(self):
self._stack = _RetargetStack()

def get(self):
"""Get the current target from the top of the stack.
May raise IndexError if the stack is empty. Users should check the size
of the stack beforehand.
"""
return self._stack.top()

def __len__(self):
"""Size of the stack
"""
return len(self._stack)

@classmethod
def switch_target(cls, retarget: BaseRetarget):
"""Returns a contextmanager that pushes a new retarget handler,
an instance of `numba.core.retarget.BaseRetarget`, onto the
target-config stack for the duration of the context-manager.
"""
return cls()._stack.enter(retarget)


class OmittedArg(object):
"""
A placeholder for omitted arguments with a default value.
Expand Down Expand Up @@ -838,9 +793,6 @@ def __init__(self, py_func, locals={}, targetoptions={},
self._type = types.Dispatcher(self)
self.typingctx.insert_global(self, self._type)

# Remember target restriction
self._required_target_backend = targetoptions.get('target_backend')

def dump(self, tab=''):
print(f'{tab}DUMP {type(self).__name__}[{self.py_func.__name__}'
f', type code={self._type._code}]')
Expand Down Expand Up @@ -905,10 +857,6 @@ def _rebuild(cls, uuid, py_func, locals, targetoptions,
return self

def compile(self, sig):
disp = self._get_dispatcher_for_current_target()
if disp is not self:
return disp.compile(sig)

with ExitStack() as scope:
cres = None

Expand Down Expand Up @@ -1049,34 +997,6 @@ def get_function_type(self):
cres = tuple(self.overloads.values())[0]
return types.FunctionType(cres.signature)

def _get_retarget_dispatcher(self):
"""Returns a dispatcher for the retarget request.
"""
# Check TLS target configuration
tc = TargetConfigurationStack()
retarget = tc.get()
retarget.check_compatible(self)
disp = retarget.retarget(self)
return disp

def _get_dispatcher_for_current_target(self):
"""Returns a dispatcher for the current target registered in
`TargetConfigurationStack`. `self` is returned if no target is
specified.
"""
tc = TargetConfigurationStack()
if tc:
return self._get_retarget_dispatcher()
else:
return self

def _call_tls_target(self, *args, **kwargs):
"""This is called when the C dispatcher logic sees a retarget request.
"""
disp = self._get_retarget_dispatcher()
# Call the new dispatcher
return disp(*args, **kwargs)


class LiftedCode(serialize.ReduceMixin, _MemoMixin, _DispatcherBase):
"""
Expand Down
2 changes: 0 additions & 2 deletions numba/core/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ class DefaultOptions:
inline = _mapping("inline")
forceinline = _mapping("forceinline")

target_backend = _mapping("target_backend")

_dbg_extend_lifetimes = _mapping("dbg_extend_lifetimes")
_dbg_optnone = _mapping("dbg_optnone")

Expand Down
135 changes: 0 additions & 135 deletions numba/core/retarget.py

This file was deleted.

0 comments on commit 609c8c9

Please sign in to comment.