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
base: master
Are you sure you want to change the base?
Conversation
Extern typedefs get left to the C compiler to sort out since we can't rely on Cython having been told correctly.
There was a problem hiding this 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/Compiler/UFuncs.py
Outdated
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" | ||
), |
There was a problem hiding this comment.
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?
Cython/Utility/UFuncs_C.c
Outdated
// 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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like this?
cython/Cython/Utility/ModuleSetupCode.c
Lines 404 to 407 in bc59042
/* 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 |
There was a problem hiding this comment.
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.
Co-authored-by: scoder <stefan_ml@behnel.de>
Extern typedefs get left to the C compiler to sort out since we can't rely on Cython having been told correctly.
Closes #6161