Skip to content

Commit

Permalink
Merge pull request numba#8389 from apmasell/cpp_typeconv
Browse files Browse the repository at this point in the history
Make C++ extensions compile with correct compiler
  • Loading branch information
sklam authored Sep 13, 2022
2 parents c87e22e + 3379a21 commit 527a3f4
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 46 deletions.
4 changes: 2 additions & 2 deletions docs/source/developer/repomap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ with CPython APIs.
- :ghfile:`numba/_pymodule.h` - C macros for Python 2/3 portable naming of C
API functions
- :ghfile:`numba/mviewbuf.c` - Handles Python memoryviews
- :ghfile:`numba/_typeof.{h,c}` - C implementation of type fingerprinting,
- :ghfile:`numba/_typeof.{h,cpp}` - C++ implementation of type fingerprinting,
used by dispatcher
- :ghfile:`numba/_numba_common.h` - Portable C macro for marking symbols
that can be shared between object files, but not outside the
Expand Down Expand Up @@ -267,7 +267,7 @@ Misc Support
Core Python Data Types
''''''''''''''''''''''

- :ghfile:`numba/_hashtable.{h,c}` - Adaptation of the Python 3.7 hash table
- :ghfile:`numba/_hashtable.{h,cpp}` - Adaptation of the Python 3.7 hash table
implementation
- :ghfile:`numba/cext/dictobject.{h,c}` - C level implementation of typed
dictionary
Expand Down
38 changes: 19 additions & 19 deletions numba/_hashtable.c → numba/_hashtable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,19 @@ _Py_slist_remove(_Py_slist_t *list, _Py_slist_item_t *previous,
list->head = item->next;
}

Py_uhash_t
extern "C" Py_uhash_t
_Numba_hashtable_hash_int(const void *key)
{
return (Py_uhash_t)key;
}

Py_uhash_t
extern "C" Py_uhash_t
_Numba_hashtable_hash_ptr(const void *key)
{
return (Py_uhash_t)_Py_HashPointer((void *)key);
}

int
extern "C" int
_Numba_hashtable_compare_direct(const void *key, const _Numba_hashtable_entry_t *entry)
{
return entry->key == key;
Expand All @@ -129,7 +129,7 @@ round_size(size_t s)
return i;
}

_Numba_hashtable_t *
extern "C" _Numba_hashtable_t *
_Numba_hashtable_new_full(size_t data_size, size_t init_size,
_Numba_hashtable_hash_func hash_func,
_Numba_hashtable_compare_func compare_func,
Expand Down Expand Up @@ -158,7 +158,7 @@ _Numba_hashtable_new_full(size_t data_size, size_t init_size,
ht->data_size = data_size;

buckets_size = ht->num_buckets * sizeof(ht->buckets[0]);
ht->buckets = alloc.malloc(buckets_size);
ht->buckets = (_Py_slist_t *) alloc.malloc(buckets_size);
if (ht->buckets == NULL) {
alloc.free(ht);
return NULL;
Expand All @@ -174,7 +174,7 @@ _Numba_hashtable_new_full(size_t data_size, size_t init_size,
return ht;
}

_Numba_hashtable_t *
extern "C" _Numba_hashtable_t *
_Numba_hashtable_new(size_t data_size,
_Numba_hashtable_hash_func hash_func,
_Numba_hashtable_compare_func compare_func)
Expand All @@ -184,7 +184,7 @@ _Numba_hashtable_new(size_t data_size,
NULL, NULL, NULL, NULL);
}

size_t
extern "C" size_t
_Numba_hashtable_size(_Numba_hashtable_t *ht)
{
size_t size;
Expand Down Expand Up @@ -215,7 +215,7 @@ _Numba_hashtable_size(_Numba_hashtable_t *ht)
}

#ifdef Py_DEBUG
void
extern "C" void
_Numba_hashtable_print_stats(_Numba_hashtable_t *ht)
{
size_t size;
Expand Down Expand Up @@ -255,7 +255,7 @@ _Numba_hashtable_print_stats(_Numba_hashtable_t *ht)
#endif

/* Get an entry. Return NULL if the key does not exist. */
_Numba_hashtable_entry_t *
extern "C" _Numba_hashtable_entry_t *
_Numba_hashtable_get_entry(_Numba_hashtable_t *ht, const void *key)
{
Py_uhash_t key_hash;
Expand Down Expand Up @@ -308,7 +308,7 @@ _hashtable_pop_entry(_Numba_hashtable_t *ht, const void *key, void *data, size_t

/* Add a new entry to the hash. The key must not be present in the hash table.
Return 0 on success, -1 on memory error. */
int
extern "C" int
_Numba_hashtable_set(_Numba_hashtable_t *ht, const void *key,
void *data, size_t data_size)
{
Expand All @@ -328,7 +328,7 @@ _Numba_hashtable_set(_Numba_hashtable_t *ht, const void *key,
key_hash = ht->hash_func(key);
index = key_hash & (ht->num_buckets - 1);

entry = ht->alloc.malloc(HASHTABLE_ITEM_SIZE(ht));
entry = (_Numba_hashtable_entry_t *) ht->alloc.malloc(HASHTABLE_ITEM_SIZE(ht));
if (entry == NULL) {
/* memory allocation failed */
return -1;
Expand All @@ -350,7 +350,7 @@ _Numba_hashtable_set(_Numba_hashtable_t *ht, const void *key,

/* Get data from an entry. Copy entry data into data and return 1 if the entry
exists, return 0 if the entry does not exist. */
int
extern "C" int
_Numba_hashtable_get(_Numba_hashtable_t *ht, const void *key, void *data, size_t data_size)
{
_Numba_hashtable_entry_t *entry;
Expand All @@ -364,7 +364,7 @@ _Numba_hashtable_get(_Numba_hashtable_t *ht, const void *key, void *data, size_t
return 1;
}

int
extern "C" int
_Numba_hashtable_pop(_Numba_hashtable_t *ht, const void *key, void *data, size_t data_size)
{
assert(data != NULL);
Expand All @@ -373,7 +373,7 @@ _Numba_hashtable_pop(_Numba_hashtable_t *ht, const void *key, void *data, size_t
}

/* Delete an entry. The entry must exist. */
void
extern "C" void
_Numba_hashtable_delete(_Numba_hashtable_t *ht, const void *key)
{
#ifndef NDEBUG
Expand All @@ -387,7 +387,7 @@ _Numba_hashtable_delete(_Numba_hashtable_t *ht, const void *key)
/* Prototype for a pointer to a function to be called foreach
key/value pair in the hash by hashtable_foreach(). Iteration
stops if a non-zero value is returned. */
int
extern "C" int
_Numba_hashtable_foreach(_Numba_hashtable_t *ht,
int (*func) (_Numba_hashtable_entry_t *entry, void *arg),
void *arg)
Expand Down Expand Up @@ -420,7 +420,7 @@ hashtable_rehash(_Numba_hashtable_t *ht)

buckets_size = new_size * sizeof(ht->buckets[0]);
old_buckets = ht->buckets;
ht->buckets = ht->alloc.malloc(buckets_size);
ht->buckets = (_Py_slist_t *) ht->alloc.malloc(buckets_size);
if (ht->buckets == NULL) {
/* cancel rehash on memory allocation failure */
ht->buckets = old_buckets ;
Expand All @@ -447,7 +447,7 @@ hashtable_rehash(_Numba_hashtable_t *ht)
ht->alloc.free(old_buckets);
}

void
extern "C" void
_Numba_hashtable_clear(_Numba_hashtable_t *ht)
{
_Numba_hashtable_entry_t *entry, *next;
Expand All @@ -466,7 +466,7 @@ _Numba_hashtable_clear(_Numba_hashtable_t *ht)
hashtable_rehash(ht);
}

void
extern "C" void
_Numba_hashtable_destroy(_Numba_hashtable_t *ht)
{
size_t i;
Expand All @@ -487,7 +487,7 @@ _Numba_hashtable_destroy(_Numba_hashtable_t *ht)
}

/* Return a copy of the hash table */
_Numba_hashtable_t *
extern "C" _Numba_hashtable_t *
_Numba_hashtable_copy(_Numba_hashtable_t *src)
{
_Numba_hashtable_t *dst;
Expand Down
30 changes: 15 additions & 15 deletions numba/_hashtable.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ typedef struct {
} _Numba_hashtable_t;

/* hash and compare functions for integers and pointers */
PyAPI_FUNC(Py_uhash_t) _Numba_hashtable_hash_ptr(const void *key);
PyAPI_FUNC(Py_uhash_t) _Numba_hashtable_hash_int(const void *key);
PyAPI_FUNC(int) _Numba_hashtable_compare_direct(const void *key, const _Numba_hashtable_entry_t *entry);
extern "C" PyAPI_FUNC(Py_uhash_t) _Numba_hashtable_hash_ptr(const void *key);
extern "C" PyAPI_FUNC(Py_uhash_t) _Numba_hashtable_hash_int(const void *key);
extern "C" PyAPI_FUNC(int) _Numba_hashtable_compare_direct(const void *key, const _Numba_hashtable_entry_t *entry);

PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_new(
extern "C" PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_new(
size_t data_size,
_Numba_hashtable_hash_func hash_func,
_Numba_hashtable_compare_func compare_func);
PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_new_full(
extern "C" PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_new_full(
size_t data_size,
size_t init_size,
_Numba_hashtable_hash_func hash_func,
Expand All @@ -88,36 +88,36 @@ PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_new_full(
_Numba_hashtable_free_data_func free_data_func,
_Numba_hashtable_get_data_size_func get_data_size_func,
_Numba_hashtable_allocator_t *allocator);
PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_copy(_Numba_hashtable_t *src);
PyAPI_FUNC(void) _Numba_hashtable_clear(_Numba_hashtable_t *ht);
PyAPI_FUNC(void) _Numba_hashtable_destroy(_Numba_hashtable_t *ht);
extern "C" PyAPI_FUNC(_Numba_hashtable_t *) _Numba_hashtable_copy(_Numba_hashtable_t *src);
extern "C" PyAPI_FUNC(void) _Numba_hashtable_clear(_Numba_hashtable_t *ht);
extern "C" PyAPI_FUNC(void) _Numba_hashtable_destroy(_Numba_hashtable_t *ht);

typedef int (*_Numba_hashtable_foreach_func) (_Numba_hashtable_entry_t *entry, void *arg);

PyAPI_FUNC(int) _Numba_hashtable_foreach(
extern "C" PyAPI_FUNC(int) _Numba_hashtable_foreach(
_Numba_hashtable_t *ht,
_Numba_hashtable_foreach_func func, void *arg);
PyAPI_FUNC(size_t) _Numba_hashtable_size(_Numba_hashtable_t *ht);
extern "C" PyAPI_FUNC(size_t) _Numba_hashtable_size(_Numba_hashtable_t *ht);

PyAPI_FUNC(_Numba_hashtable_entry_t*) _Numba_hashtable_get_entry(
extern "C" PyAPI_FUNC(_Numba_hashtable_entry_t*) _Numba_hashtable_get_entry(
_Numba_hashtable_t *ht,
const void *key);
PyAPI_FUNC(int) _Numba_hashtable_set(
extern "C" PyAPI_FUNC(int) _Numba_hashtable_set(
_Numba_hashtable_t *ht,
const void *key,
void *data,
size_t data_size);
PyAPI_FUNC(int) _Numba_hashtable_get(
extern "C" PyAPI_FUNC(int) _Numba_hashtable_get(
_Numba_hashtable_t *ht,
const void *key,
void *data,
size_t data_size);
PyAPI_FUNC(int) _Numba_hashtable_pop(
extern "C" PyAPI_FUNC(int) _Numba_hashtable_pop(
_Numba_hashtable_t *ht,
const void *key,
void *data,
size_t data_size);
PyAPI_FUNC(void) _Numba_hashtable_delete(
extern "C" PyAPI_FUNC(void) _Numba_hashtable_delete(
_Numba_hashtable_t *ht,
const void *key);

Expand Down
20 changes: 12 additions & 8 deletions numba/_typeof.c → numba/_typeof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ string_writer_ensure(string_writer_t *w, size_t bytes)
if (newsize < bytes)
newsize = bytes;
if (w->buf == w->static_buf)
w->buf = malloc(newsize);
w->buf = (char *) malloc(newsize);
else
w->buf = realloc(w->buf, newsize);
w->buf = (char *) realloc(w->buf, newsize);
if (w->buf) {
w->allocated = newsize;
return 0;
Expand Down Expand Up @@ -842,6 +842,10 @@ int typecode_devicendarray(PyObject *dispatcher, PyObject *ary)
int dtype;
int ndim;
int layout = 0;
PyObject *ndim_obj = nullptr;
PyObject *num_obj = nullptr;
PyObject *dtype_obj = nullptr;
int dtype_num = 0;

PyObject* flags = PyObject_GetAttrString(ary, "flags");
if (flags == NULL)
Expand All @@ -858,7 +862,7 @@ int typecode_devicendarray(PyObject *dispatcher, PyObject *ary)

Py_DECREF(flags);

PyObject *ndim_obj = PyObject_GetAttrString(ary, "ndim");
ndim_obj = PyObject_GetAttrString(ary, "ndim");
if (ndim_obj == NULL) {
/* If there's no ndim, try to proceed by clearing the error and using the
* fallback. */
Expand All @@ -879,14 +883,14 @@ int typecode_devicendarray(PyObject *dispatcher, PyObject *ary)
if (ndim <= 0 || ndim > N_NDIM)
goto FALLBACK;

PyObject* dtype_obj = PyObject_GetAttrString(ary, "dtype");
dtype_obj = PyObject_GetAttrString(ary, "dtype");
if (dtype_obj == NULL) {
/* No dtype: try the fallback. */
PyErr_Clear();
goto FALLBACK;
}

PyObject* num_obj = PyObject_GetAttrString(dtype_obj, "num");
num_obj = PyObject_GetAttrString(dtype_obj, "num");
Py_DECREF(dtype_obj);

if (num_obj == NULL) {
Expand All @@ -895,7 +899,7 @@ int typecode_devicendarray(PyObject *dispatcher, PyObject *ary)
goto FALLBACK;
}

int dtype_num = PyLong_AsLong(num_obj);
dtype_num = PyLong_AsLong(num_obj);
Py_DECREF(num_obj);

if (PyErr_Occurred()) {
Expand Down Expand Up @@ -934,7 +938,7 @@ int typecode_devicendarray(PyObject *dispatcher, PyObject *ary)
return typecode_using_fingerprint(dispatcher, (PyObject *) ary);
}

int
extern "C" int
typeof_typecode(PyObject *dispatcher, PyObject *val)
{
PyTypeObject *tyobj = Py_TYPE(val);
Expand Down Expand Up @@ -1051,7 +1055,7 @@ int init_numpy(void) {
* typeof_init(omittedarg_type, typecode_dict)
* (called from dispatcher.py to fill in missing information)
*/
PyObject *
extern "C" PyObject *
typeof_init(PyObject *self, PyObject *args)
{
PyObject *tmpobj;
Expand Down
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ def get_ext_modules():

ext_dispatcher = Extension(name="numba._dispatcher",
sources=['numba/_dispatcher.cpp',
'numba/_typeof.c',
'numba/_hashtable.c',
'numba/_typeof.cpp',
'numba/_hashtable.cpp',
'numba/core/typeconv/typeconv.cpp'],
depends=["numba/_pymodule.h",
"numba/_typeof.h",
"numba/_hashtable.h"],
extra_compile_args=['-std=c++11'],
**np_compile_args)

ext_helperlib = Extension(name="numba._helperlib",
Expand All @@ -191,6 +192,7 @@ def get_ext_modules():
sources=["numba/core/typeconv/typeconv.cpp",
"numba/core/typeconv/_typeconv.cpp"],
depends=["numba/_pymodule.h"],
extra_compile_args=['-std=c++11'],
)

ext_np_ufunc = Extension(name="numba.np.ufunc._internal",
Expand Down

0 comments on commit 527a3f4

Please sign in to comment.