Skip to content
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

Handle typedefs in ufuncs #6196

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

da-woods
Copy link
Contributor

Extern typedefs get left to the C compiler to sort out since we can't rely on Cython having been told correctly.

Closes #6161

Extern typedefs get left to the C compiler to sort out since
we can't rely on Cython having been told correctly.
Copy link
Contributor

@scoder scoder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, seems good to move the exact typing to C compile time.

Cython/Utility/UFuncs_C.c Outdated Show resolved Hide resolved
Comment on lines 260 to 266
func_type=PyrexTypes.CFuncType(
PyrexTypes.c_int_type,
[PyrexTypes.CFuncTypeArg("types", PyrexTypes.c_char_ptr_type, None),
PyrexTypes.CFuncTypeArg("arg_count", PyrexTypes.c_py_ssize_t_type, None),
PyrexTypes.CFuncTypeArg("input_arg_count", PyrexTypes.c_py_ssize_t_type, None)],
exception_value="-1"
),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try extracting the function type creation into a constant?

Comment on lines 50 to 51
// DW - it's a bit of a shame that this has to be a runtime check since the C compiler does
// know it. We could make this a compile-time error in C++, but not easily in C.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could potentially figure it out if it inlines the validation function. But how many types do you expect here? One, two, three? Probably rarely more, right? Why not call the validation function multiple times, instead of using a loop? You could also inline the NPY_NOTYPE comparison and keep the helper function only for setting the exception. That should make it easy for the C compiler to discard the calls in the normal cases.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I think you're slightly misunderstanding the comment.

What I really want is a failure from the C compiler because it already knows that it's used NPY_NOTYPE. I'm not worried about the cost of the validation function. I wish I didn't have to try to import the module to work out that I've got it wrong.

Copy link
Contributor

@scoder scoder May 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like this?

/* Compile-time sanity check that these are indeed equal. Github issue #2670. */
#ifdef SIZEOF_VOID_P
enum { __pyx_check_sizeof_voidp = 1 / (int)(SIZEOF_VOID_P == sizeof(void*)) };
#endif

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah possibly. I'll have another look and see if I can do something like that.

da-woods and others added 2 commits May 13, 2024 07:33
Co-authored-by: scoder <stefan_ml@behnel.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] numpy types cannot be used for ufunc arguments
2 participants