From fa4ce15ed2bd6aa4c72684dc20821a4dc8a23413 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 20:31:17 +0300 Subject: [PATCH 1/6] feat: enable -Wall and -Wextra warnings on the _cl module --- CMakeLists.txt | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4557bd8a6..24f475691 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -150,7 +150,13 @@ execute_process( "${PYTHON_EXECUTABLE}" -c "import nanobind; print(nanobind.cmake_dir())" OUTPUT_VARIABLE NANOBIND_DIR OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT) - list(APPEND CMAKE_PREFIX_PATH "${NANOBIND_DIR}") +list(APPEND CMAKE_PREFIX_PATH "${NANOBIND_DIR}") + +execute_process( + COMMAND + "${PYTHON_EXECUTABLE}" -c "import nanobind; print(nanobind.include_dir())" + OUTPUT_VARIABLE NANOBIND_INCLUDE_DIR + OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT) # }}} @@ -176,8 +182,12 @@ nanobind_add_module( src/wrap_mempool.cpp src/bitlog.cpp ) +target_compile_features(_cl PRIVATE cxx_std_17) target_link_libraries(_cl PRIVATE ${OpenCL_LIBRARY}) +target_include_directories(_cl SYSTEM PRIVATE + ${NANOBIND_INCLUDE_DIR} + ${Python_NumPy_INCLUDE_DIRS}) target_compile_definitions(_cl PRIVATE @@ -185,6 +195,13 @@ target_compile_definitions(_cl PYGPU_PYOPENCL ) + if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + target_compile_options(_cl PRIVATE + -Wall + -Wextra + ) + endif() + if (PYOPENCL_PRETEND_CL_VERSION) target_compile_definitions( _cl PRIVATE PYOPENCL_PRETEND_CL_VERSION=${PYOPENCL_PRETEND_CL_VERSION}) From 2794c753ae77e20c19fa7e871947f0c5e810002e Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 20:44:36 +0300 Subject: [PATCH 2/6] feat: fix new warnings from -Wall -Wextra --- src/wrap_cl.hpp | 10 +++++----- src/wrap_cl_part_2.cpp | 2 +- src/wrap_helpers.hpp | 2 +- src/wrap_mempool.cpp | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/wrap_cl.hpp b/src/wrap_cl.hpp index f63cdca6e..1b1fa9ee1 100644 --- a/src/wrap_cl.hpp +++ b/src/wrap_cl.hpp @@ -1565,7 +1565,7 @@ namespace pyopencl (m_queue)); } - const cl_command_queue data() const + cl_command_queue data() const { if (m_finalized) { @@ -1817,7 +1817,7 @@ namespace pyopencl (m_event)); } - const cl_event data() const + cl_event data() const { return m_event; } PYOPENCL_EQUALITY_TESTS(event); @@ -2170,7 +2170,7 @@ namespace pyopencl class memory_object_holder { public: - virtual const cl_mem data() const = 0; + virtual cl_mem data() const = 0; PYOPENCL_EQUALITY_TESTS(memory_object_holder); @@ -2250,7 +2250,7 @@ namespace pyopencl return py::none(); } - const cl_mem data() const + cl_mem data() const { return m_mem; } }; @@ -4875,7 +4875,7 @@ namespace pyopencl { prg = py::cast(prg_py); } - catch (py::cast_error) { + catch (py::cast_error &) { prg = py::cast(prg_py.attr("_get_prg")()); } diff --git a/src/wrap_cl_part_2.cpp b/src/wrap_cl_part_2.cpp index 8d2bb500a..d330023db 100644 --- a/src/wrap_cl_part_2.cpp +++ b/src/wrap_cl_part_2.cpp @@ -35,7 +35,7 @@ namespace pyopencl { #if PYOPENCL_CL_VERSION >= 0x1020 - py::object image_desc_dummy_getter(cl_image_desc &desc) + py::object image_desc_dummy_getter([[maybe_unused]] cl_image_desc &desc) { return py::none(); } diff --git a/src/wrap_helpers.hpp b/src/wrap_helpers.hpp index 041065930..ce974ea56 100644 --- a/src/wrap_helpers.hpp +++ b/src/wrap_helpers.hpp @@ -206,7 +206,7 @@ namespace #define PYOPENCL_EXPOSE_EQUALITY_TESTS \ /* this relies on nanobind overload resolution going in order of registration */ \ .def("__eq__", [](cls const &self, cls const &other) { return self == other; }) \ - .def("__eq__", [](cls const &self, py::object obj) { return false; }, py::arg("obj").none()) + .def("__eq__", []([[maybe_unused]] cls const &self, [[maybe_unused]] py::object obj) { return false; }, py::arg("obj").none()) #endif diff --git a/src/wrap_mempool.cpp b/src/wrap_mempool.cpp index 8fc09d494..081e85952 100644 --- a/src/wrap_mempool.cpp +++ b/src/wrap_mempool.cpp @@ -54,7 +54,7 @@ namespace pyopencl { return false; } - pointer_type allocate(size_type s) + pointer_type allocate([[maybe_unused]] size_type s) { return nullptr; } @@ -67,7 +67,7 @@ namespace pyopencl { ~test_allocator() { } - void free(pointer_type &&p) + void free([[maybe_unused]] pointer_type &&p) { } void try_release_blocks() @@ -236,7 +236,7 @@ namespace pyopencl { virtual ~pooled_buffer() { } - const super::pointer_type data() const + super::pointer_type data() const { return m_ptr; } size_t size() const @@ -611,7 +611,7 @@ void pyopencl_expose_mempool(py::module_ &m) py::class_ wrapper( m, "_TestMemoryPool", py::intrusive_ptr( - [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) + [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) ); wrapper .def("__init__", @@ -663,8 +663,8 @@ void pyopencl_expose_mempool(py::module_ &m) py::class_(m, "PooledBuffer") .def("release", &cls::free) - .def("bind_to_queue", [](cls &self, pyopencl::command_queue &queue) { /* no-op */ }) - .def("unbind_from_queue", [](cls &self) { /* no-op */ }) + .def("bind_to_queue", []([[maybe_unused]] cls &self, [[maybe_unused]] pyopencl::command_queue &queue) { /* no-op */ }) + .def("unbind_from_queue", []([[maybe_unused]] cls &self) { /* no-op */ }) ; } @@ -674,7 +674,7 @@ void pyopencl_expose_mempool(py::module_ &m) py::class_ wrapper( m, "MemoryPool", py::intrusive_ptr( - [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) + [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) ); wrapper .def(py::init, unsigned>(), @@ -694,7 +694,7 @@ void pyopencl_expose_mempool(py::module_ &m) py::class_ wrapper( m, "SVMAllocator", py::intrusive_ptr( - [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) + [](cls *o, PyObject *po) noexcept { o->set_self_py(po); }) ); wrapper .def(py::init const &, cl_uint, cl_uint, pyopencl::command_queue *>(), From 32fafd93ef6c879aa3518eb6c19a43cbac3cff63 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 20:51:14 +0300 Subject: [PATCH 3/6] feat: enable and fix -Wpendantic --- CMakeLists.txt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 24f475691..6143bf036 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,6 +199,9 @@ target_compile_definitions(_cl target_compile_options(_cl PRIVATE -Wall -Wextra + -Wpedantic + # NOTE: VLAs are used by PYOPENCL_STACK_CONTAINER for dubious reasons + -Wno-vla ) endif() From e66317ae1e7bef8e29e2fb158b37c636da1eb99a Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 20:57:07 +0300 Subject: [PATCH 4/6] feat: enable and fix -Wshadow warnings --- CMakeLists.txt | 4 +-- src/wrap_cl.hpp | 72 ++++++++++++++++++++++++------------------------- 2 files changed, 38 insertions(+), 38 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6143bf036..5e8028e40 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -199,9 +199,9 @@ target_compile_definitions(_cl target_compile_options(_cl PRIVATE -Wall -Wextra - -Wpedantic # NOTE: VLAs are used by PYOPENCL_STACK_CONTAINER for dubious reasons - -Wno-vla + -Wpedantic -Wno-vla + -Wshadow ) endif() diff --git a/src/wrap_cl.hpp b/src/wrap_cl.hpp index 1b1fa9ee1..2aa6779ab 100644 --- a/src/wrap_cl.hpp +++ b/src/wrap_cl.hpp @@ -289,54 +289,54 @@ #define PYOPENCL_CALL_GUARDED_THREADED_WITH_TRACE_INFO(NAME, ARGLIST, TRACE_INFO) \ { \ PYOPENCL_PRINT_CALL_TRACE_INFO(#NAME, TRACE_INFO); \ - cl_int status_code; \ + cl_int _cl_status_code; \ { \ py::gil_scoped_release release; \ - status_code = NAME ARGLIST; \ + _cl_status_code = NAME ARGLIST; \ } \ - if (status_code != CL_SUCCESS) \ - throw pyopencl::error(#NAME, status_code);\ + if (_cl_status_code != CL_SUCCESS) \ + throw pyopencl::error(#NAME, _cl_status_code);\ } #define PYOPENCL_CALL_GUARDED_WITH_TRACE_INFO(NAME, ARGLIST, TRACE_INFO) \ { \ PYOPENCL_PRINT_CALL_TRACE_INFO(#NAME, TRACE_INFO); \ - cl_int status_code; \ - status_code = NAME ARGLIST; \ - if (status_code != CL_SUCCESS) \ - throw pyopencl::error(#NAME, status_code);\ + cl_int _cl_status_code; \ + _cl_status_code = NAME ARGLIST; \ + if (_cl_status_code != CL_SUCCESS) \ + throw pyopencl::error(#NAME, _cl_status_code);\ } #define PYOPENCL_CALL_GUARDED_THREADED(NAME, ARGLIST) \ { \ PYOPENCL_PRINT_CALL_TRACE(#NAME); \ - cl_int status_code; \ + cl_int _cl_status_code; \ { \ py::gil_scoped_release release; \ - status_code = NAME ARGLIST; \ + _cl_status_code = NAME ARGLIST; \ } \ - if (status_code != CL_SUCCESS) \ - throw pyopencl::error(#NAME, status_code);\ + if (_cl_status_code != CL_SUCCESS) \ + throw pyopencl::error(#NAME, _cl_status_code);\ } #define PYOPENCL_CALL_GUARDED(NAME, ARGLIST) \ { \ PYOPENCL_PRINT_CALL_TRACE(#NAME); \ - cl_int status_code; \ - status_code = NAME ARGLIST; \ - if (status_code != CL_SUCCESS) \ - throw pyopencl::error(#NAME, status_code);\ + cl_int _cl_status_code; \ + _cl_status_code = NAME ARGLIST; \ + if (_cl_status_code != CL_SUCCESS) \ + throw pyopencl::error(#NAME, _cl_status_code);\ } #define PYOPENCL_CALL_GUARDED_CLEANUP(NAME, ARGLIST) \ { \ PYOPENCL_PRINT_CALL_TRACE(#NAME); \ - cl_int status_code; \ - status_code = NAME ARGLIST; \ - if (status_code != CL_SUCCESS) \ + cl_int _cl_status_code; \ + _cl_status_code = NAME ARGLIST; \ + if (_cl_status_code != CL_SUCCESS) \ std::cerr \ << "PyOpenCL WARNING: a clean-up operation failed (dead context maybe?)" \ << std::endl \ - << #NAME " failed with code " << status_code \ + << #NAME " failed with code " << _cl_status_code \ << std::endl; \ } @@ -346,12 +346,12 @@ // {{{ get_info helpers #define PYOPENCL_GET_OPAQUE_INFO(WHAT, FIRST_ARG, SECOND_ARG, CL_TYPE, TYPE) \ { \ - CL_TYPE param_value; \ + CL_TYPE _cl_param_value; \ PYOPENCL_CALL_GUARDED(clGet##WHAT##Info, \ - (FIRST_ARG, SECOND_ARG, sizeof(param_value), ¶m_value, 0)); \ - if (param_value) \ + (FIRST_ARG, SECOND_ARG, sizeof(_cl_param_value), &_cl_param_value, 0)); \ + if (_cl_param_value) \ return py::object(handle_from_new_ptr( \ - new TYPE(param_value, /*retain*/ true))); \ + new TYPE(_cl_param_value, /*retain*/ true))); \ else \ return py::none(); \ } @@ -371,17 +371,17 @@ #define PYOPENCL_GET_STR_INFO(WHAT, FIRST_ARG, SECOND_ARG) \ { \ - size_t param_value_size; \ + size_t _cl_param_value_size; \ PYOPENCL_CALL_GUARDED(clGet##WHAT##Info, \ - (FIRST_ARG, SECOND_ARG, 0, 0, ¶m_value_size)); \ + (FIRST_ARG, SECOND_ARG, 0, 0, &_cl_param_value_size)); \ \ - std::vector param_value(param_value_size); \ + std::vector _cl_param_value(_cl_param_value_size); \ PYOPENCL_CALL_GUARDED(clGet##WHAT##Info, \ - (FIRST_ARG, SECOND_ARG, param_value_size, \ - param_value.empty( ) ? nullptr : ¶m_value.front(), ¶m_value_size)); \ + (FIRST_ARG, SECOND_ARG, _cl_param_value_size, \ + _cl_param_value.empty( ) ? nullptr : &_cl_param_value.front(), &_cl_param_value_size)); \ \ return py::cast( \ - param_value.empty( ) ? "" : std::string(¶m_value.front(), param_value_size-1)); \ + _cl_param_value.empty( ) ? "" : std::string(&_cl_param_value.front(), _cl_param_value_size-1)); \ } @@ -389,10 +389,10 @@ #define PYOPENCL_GET_TYPED_INFO(WHAT, FIRST_ARG, SECOND_ARG, TYPE) \ { \ - TYPE param_value; \ + TYPE _cl_param_value; \ PYOPENCL_CALL_GUARDED(clGet##WHAT##Info, \ - (FIRST_ARG, SECOND_ARG, sizeof(param_value), ¶m_value, 0)); \ - return py::cast(param_value); \ + (FIRST_ARG, SECOND_ARG, sizeof(_cl_param_value), &_cl_param_value, 0)); \ + return py::cast(_cl_param_value); \ } // }}} @@ -1800,11 +1800,11 @@ namespace pyopencl cl_event m_event; public: - event(cl_event event, bool retain) - : m_event(event) + event(cl_event evt, bool retain) + : m_event(evt) { if (retain) - PYOPENCL_CALL_GUARDED(clRetainEvent, (event)); + PYOPENCL_CALL_GUARDED(clRetainEvent, (evt)); } event(event const &src) From dbab754d86d3f804dc20bee2b1f9734f28259a44 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 21:32:49 +0300 Subject: [PATCH 5/6] feat: enable and fix -Wold-style-cast warnings --- CMakeLists.txt | 5 +++-- src/mempool.hpp | 4 ++-- src/wrap_cl.hpp | 40 +++++++++++++++++----------------------- src/wrap_cl_part_2.cpp | 12 ++++++------ src/wrap_constants.cpp | 12 ++++++------ src/wrap_helpers.hpp | 4 ++-- src/wrap_mempool.cpp | 2 +- 7 files changed, 37 insertions(+), 42 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5e8028e40..38f9edc52 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -161,8 +161,6 @@ execute_process( # }}} link_directories(${CL_LIB_DIR}) -include_directories(${CL_INC_DIR} ${Python_NumPy_INCLUDE_DIRS}) - find_package(nanobind CONFIG REQUIRED) set(OpenCL_ROOT ${CL_LIB_DIR}) @@ -173,6 +171,7 @@ find_package(OpenCL REQUIRED) nanobind_add_module( _cl NB_STATIC # Build static libnanobind (the extension module itself remains a shared library) + NB_SUPPRESS_WARNINGS LTO NOMINSIZE src/wrap_constants.cpp @@ -186,6 +185,7 @@ target_compile_features(_cl PRIVATE cxx_std_17) target_link_libraries(_cl PRIVATE ${OpenCL_LIBRARY}) target_include_directories(_cl SYSTEM PRIVATE + ${CL_INC_DIR} ${NANOBIND_INCLUDE_DIR} ${Python_NumPy_INCLUDE_DIRS}) @@ -202,6 +202,7 @@ target_compile_definitions(_cl # NOTE: VLAs are used by PYOPENCL_STACK_CONTAINER for dubious reasons -Wpedantic -Wno-vla -Wshadow + -Wold-style-cast ) endif() diff --git a/src/mempool.hpp b/src/mempool.hpp index 66af4852f..6ee00d980 100644 --- a/src/mempool.hpp +++ b/src/mempool.hpp @@ -185,13 +185,13 @@ namespace PYGPU_PACKAGE bin_nr_t exponent = bin >> m_leading_bits_in_bin_id; bin_nr_t mantissa = bin & mantissa_mask(); - size_type ones = signed_left_shift((size_type) 1, + size_type ones = signed_left_shift(static_cast(1), signed(exponent)-signed(m_leading_bits_in_bin_id) ); if (ones) ones -= 1; size_type head = signed_left_shift( - (size_type) ((1<((1<= 0x1020 #define PYOPENCL_GET_EXT_FUN(PLATFORM, NAME, VAR) \ - NAME##_fn VAR \ - = (NAME##_fn) \ - clGetExtensionFunctionAddressForPlatform(PLATFORM, #NAME); \ - \ - if (!VAR) \ - throw error(#NAME, CL_INVALID_VALUE, #NAME \ - "not available"); + NAME##_fn VAR = reinterpret_cast( \ + clGetExtensionFunctionAddressForPlatform(PLATFORM, #NAME)); \ + if (!VAR) throw error(#NAME, CL_INVALID_VALUE, #NAME "not available"); #else #define PYOPENCL_GET_EXT_FUN(PLATFORM, NAME, VAR) \ - NAME##_fn VAR \ - = (NAME##_fn) \ - clGetExtensionFunctionAddress(#NAME); \ - \ - if (!VAR) \ - throw error(#NAME, CL_INVALID_VALUE, #NAME \ - "not available"); + NAME##_fn VAR = reinterpret_cast( \ + clGetExtensionFunctionAddress(#NAME)); \ + if (!VAR) throw error(#NAME, CL_INVALID_VALUE, #NAME "not available"); #endif @@ -448,7 +440,7 @@ bool operator!=(cls const &other) const \ { return data() != other.data(); } \ long hash() const \ - { return (long) (intptr_t) data(); } + { return static_cast(reinterpret_cast(data())); } // }}} @@ -2663,7 +2655,9 @@ namespace pyopencl byte_count = py::cast(py_byte_count); } - clEnqueueCopyBufferP2PAMD_fn fn = (clEnqueueCopyBufferP2PAMD_fn)clGetExtensionFunctionAddressForPlatform(plat.data(), "clEnqueueCopyBufferP2PAMD"); + clEnqueueCopyBufferP2PAMD_fn fn = reinterpret_cast( + clGetExtensionFunctionAddressForPlatform(plat.data(), "clEnqueueCopyBufferP2PAMD") + ); if (!fn) throw pyopencl::error("clGetExtensionFunctionAddressForPlatform", CL_INVALID_VALUE, "clEnqueueCopyBufferP2PAMD is not available"); @@ -3579,8 +3573,8 @@ namespace pyopencl strides.empty() ? nullptr : &strides.front(), mapped, ary_flags, /*obj*/nullptr))); - result_arr = (PyArrayObject *) result.ptr(); - if (size_in_bytes != (npy_uintp) PyArray_NBYTES(result_arr)) + result_arr = reinterpret_cast(result.ptr()); + if (size_in_bytes != static_cast(PyArray_NBYTES(result_arr))) throw pyopencl::error("enqueue_map_buffer", CL_INVALID_VALUE, "miscalculated numpy array size (not contiguous?)"); @@ -3666,7 +3660,7 @@ namespace pyopencl shape.empty() ? nullptr : &shape.front(), strides.empty() ? nullptr : &strides.front(), mapped, ary_flags, /*obj*/nullptr)); - PyArrayObject *result_arr = (PyArrayObject *) result.ptr(); + PyArrayObject *result_arr = reinterpret_cast(result.ptr()); py::object map_py(handle_from_new_ptr(map.release())); PyArray_SetBaseObject(result_arr, map_py.ptr()); @@ -4976,7 +4970,7 @@ namespace pyopencl #define PYOPENCL_KERNEL_PACK_AND_SET_ARG(TYPECH_VAL, TYPE, CAST_TYPE) \ case TYPECH_VAL: \ { \ - TYPE val = (TYPE) py::cast(obj); \ + TYPE val = static_cast(py::cast(obj)); \ PYOPENCL_CALL_GUARDED(clSetKernelArg, (m_kernel, arg_index, sizeof(val), &val)); \ break; \ } @@ -5896,7 +5890,7 @@ namespace pyopencl inline py::object memory_object_from_int(intptr_t cl_mem_as_int, bool retain) { - return create_mem_object_wrapper((cl_mem) cl_mem_as_int, retain); + return create_mem_object_wrapper(reinterpret_cast(cl_mem_as_int), retain); } @@ -6029,9 +6023,9 @@ namespace pyopencl &PyArray_Type, tp_descr, dims.size(), &dims.front(), /*strides*/ nullptr, host_ptr, ary_flags, /*obj*/nullptr)); - PyArrayObject *result_arr = (PyArrayObject *) result.ptr(); + PyArrayObject *result_arr = reinterpret_cast(result.ptr()); - if ((size_t) PyArray_NBYTES(result_arr) > mem_obj_size) + if (static_cast(PyArray_NBYTES(result_arr)) > mem_obj_size) throw pyopencl::error("MemoryObject.get_host_array", CL_INVALID_VALUE, "Resulting array is larger than memory object."); diff --git a/src/wrap_cl_part_2.cpp b/src/wrap_cl_part_2.cpp index d330023db..32c0e7ead 100644 --- a/src/wrap_cl_part_2.cpp +++ b/src/wrap_cl_part_2.cpp @@ -75,8 +75,8 @@ namespace pyopencl { using namespace pyopencl; -static PyCFunctionWithKeywords dummy_init = [](PyObject *, PyObject *, - PyObject *) -> PyObject * { +static PyCFunctionWithKeywords dummy_init = + [](PyObject *, PyObject *, PyObject *) -> PyObject * { PyErr_SetString(PyExc_RuntimeError, "This should never be called!"); return nullptr; }; @@ -85,7 +85,7 @@ static PyType_Slot init_slots[] { // the presence of this slot enables normal object construction via __init__ and __new__ // instead of an optimized codepath within nanobind that skips these. That in turn // makes it possible to intercept calls and implement custom logic. - { Py_tp_init, (void *) dummy_init }, + { Py_tp_init, reinterpret_cast(dummy_init) }, { 0, nullptr } }; @@ -334,7 +334,7 @@ void pyopencl_expose_part_2(py::module_ &m) // that would work on both buffers and SVM, and passing a buffer pointer to // a kernel is going to lead to a bad time. .def_prop_ro("svm_ptr", - [](cls &self) { return (intptr_t) self.svm_ptr(); }) + [](cls &self) { return reinterpret_cast(self.svm_ptr()); }) .def_prop_ro("size", [](cls &self) -> py::object { try @@ -401,7 +401,7 @@ void pyopencl_expose_part_2(py::module_ &m) py::arg("wait_for").none()=py::none() ) PYOPENCL_EXPOSE_EQUALITY_TESTS - .def("__hash__", [](cls &self) { return (intptr_t) self.svm_ptr(); }) + .def("__hash__", [](cls &self) { return reinterpret_cast(self.svm_ptr()); }) .def("bind_to_queue", &cls::bind_to_queue, py::arg("queue")) .DEF_SIMPLE_METHOD(unbind_from_queue) @@ -507,7 +507,7 @@ void pyopencl_expose_part_2(py::module_ &m) "__init__", [](cls *self, context &ctx, py::bytes const &src) { - create_program_with_source(self, ctx, std::string((const char *) src.data(), src.size())); + create_program_with_source(self, ctx, std::string(reinterpret_cast(src.data()), src.size())); }, py::arg("context"), py::arg("src")) diff --git a/src/wrap_constants.cpp b/src/wrap_constants.cpp index 7bc220685..29da20b93 100644 --- a/src/wrap_constants.cpp +++ b/src/wrap_constants.cpp @@ -1247,9 +1247,9 @@ void pyopencl_expose_constants(py::module_ &m) [](cls *self, cl_int bus, cl_int device, cl_int function) { self->pcie.type = CL_DEVICE_TOPOLOGY_TYPE_PCIE_AMD; - self->pcie.bus = (cl_char) bus; - self->pcie.device = (cl_char) device; - self->pcie.function = (cl_char) function; + self->pcie.bus = static_cast(bus); + self->pcie.device = static_cast(device); + self->pcie.function = static_cast(function); }, py::arg("bus")=0, py::arg("device")=0, @@ -1262,15 +1262,15 @@ void pyopencl_expose_constants(py::module_ &m) .def_prop_rw("bus", [](cls &t) { return t.pcie.bus; }, // FIXME: Revert to cl_char when possible - [](cls &t, cl_int val) { t.pcie.bus = (cl_char) val; }) + [](cls &t, cl_int val) { t.pcie.bus = static_cast(val); }) .def_prop_rw("device", [](cls &t) { return t.pcie.device; }, // FIXME: Revert to cl_char when possible - [](cls &t, cl_int val) { t.pcie.device = (cl_char) val; }) + [](cls &t, cl_int val) { t.pcie.device = static_cast(val); }) .def_prop_rw("function", [](cls &t) { return t.pcie.function; }, // FIXME: Revert to cl_char when possible - [](cls &t, cl_int val) { t.pcie.function = (cl_char) val; }) + [](cls &t, cl_int val) { t.pcie.function = static_cast(val); }) ; } #endif diff --git a/src/wrap_helpers.hpp b/src/wrap_helpers.hpp index ce974ea56..7b858a74a 100644 --- a/src/wrap_helpers.hpp +++ b/src/wrap_helpers.hpp @@ -173,14 +173,14 @@ namespace template inline T *from_int_ptr(intptr_t obj_ref, bool retain) { - ClType clobj = (ClType) obj_ref; + ClType clobj = reinterpret_cast(obj_ref); return new T(clobj, retain); } template inline intptr_t to_int_ptr(T const &obj) { - return (intptr_t) obj.data(); + return reinterpret_cast(obj.data()); } } diff --git a/src/wrap_mempool.cpp b/src/wrap_mempool.cpp index 081e85952..8d724ee84 100644 --- a/src/wrap_mempool.cpp +++ b/src/wrap_mempool.cpp @@ -715,7 +715,7 @@ void pyopencl_expose_mempool(py::module_ &m) .def("enqueue_release", &cls::free) .def("__eq__", [](const cls &self, const cls &other) { return self.svm_ptr() == other.svm_ptr(); }) - .def("__hash__", [](cls &self) { return (intptr_t) self.svm_ptr(); }) + .def("__hash__", [](cls &self) { return reinterpret_cast(self.svm_ptr()); }) .DEF_SIMPLE_METHOD(bind_to_queue) .DEF_SIMPLE_METHOD(unbind_from_queue) From 418f8ebb234fcef46e41f1147a7e254a656781d2 Mon Sep 17 00:00:00 2001 From: Alexandru Fikl Date: Fri, 2 Oct 2026 22:03:40 +0300 Subject: [PATCH 6/6] feat: enable and fix -Wconversion warnings --- CMakeLists.txt | 10 +++++++ src/bitlog.cpp | 2 +- src/bitlog.hpp | 10 +++---- src/mempool.hpp | 6 ++--- src/wrap_cl.hpp | 70 ++++++++++++++++++++++++------------------------- 5 files changed, 54 insertions(+), 44 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 38f9edc52..422bfd8ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -203,6 +203,16 @@ target_compile_definitions(_cl -Wpedantic -Wno-vla -Wshadow -Wold-style-cast + -Wcast-align + -Wconversion -Wsign-conversion + -Wnon-virtual-dtor + -Woverloaded-virtual + -Wformat=2 + -Wimplicit-fallthrough + -Wdouble-promotion + -Wnull-dereference + # NOTE: mostly calls to deprecated CL functions that we support + -Wno-deprecated-declarations ) endif() diff --git a/src/bitlog.cpp b/src/bitlog.cpp index 37ea93f79..ba867ca19 100644 --- a/src/bitlog.cpp +++ b/src/bitlog.cpp @@ -28,7 +28,7 @@ #include "bitlog.hpp" -const char pyopencl::log_table_8[] = +const unsigned char pyopencl::log_table_8[] = { 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, diff --git a/src/bitlog.hpp b/src/bitlog.hpp index c002fc391..8c0c9d28d 100644 --- a/src/bitlog.hpp +++ b/src/bitlog.hpp @@ -37,7 +37,7 @@ namespace pyopencl { /* from http://graphics.stanford.edu/~seander/bithacks.html */ - extern const char log_table_8[]; + extern const unsigned char log_table_8[]; inline unsigned bitlog2_16(uint16_t v) { @@ -49,19 +49,19 @@ namespace pyopencl inline unsigned bitlog2_32(uint32_t v) { - if (uint16_t t = v >> 16) + if (uint16_t t = static_cast(v >> 16)) return 16+bitlog2_16(t); else - return bitlog2_16(v); + return bitlog2_16(static_cast(v)); } #if defined(UINT64_MAX) inline unsigned bitlog2(uint64_t v) { - if (uint32_t t = v >> 32) + if (uint32_t t = static_cast(v >> 32)) return 32+bitlog2_32(t); else - return bitlog2_32(v); + return bitlog2_32(static_cast(v)); } #else inline unsigned bitlog2(unsigned long v) diff --git a/src/mempool.hpp b/src/mempool.hpp index 6ee00d980..38a517224 100644 --- a/src/mempool.hpp +++ b/src/mempool.hpp @@ -158,18 +158,18 @@ namespace PYGPU_PACKAGE private: unsigned mantissa_mask() const { - return (1 << m_leading_bits_in_bin_id) - 1; + return (1u << m_leading_bits_in_bin_id) - 1; } public: bin_nr_t bin_number(size_type size) { - signed l = bitlog2(size); + signed l = static_cast(bitlog2(size)); size_type shifted = signed_right_shift(size, l-signed(m_leading_bits_in_bin_id)); if (size && (shifted & (1 << m_leading_bits_in_bin_id)) == 0) throw std::runtime_error("memory_pool::bin_number: bitlog2 fault"); size_type chopped = shifted & mantissa_mask(); - return l << m_leading_bits_in_bin_id | chopped; + return static_cast(static_cast(l) << m_leading_bits_in_bin_id | chopped); } void set_trace(bool flag) diff --git a/src/wrap_cl.hpp b/src/wrap_cl.hpp index 3f1b78a10..188a94341 100644 --- a/src/wrap_cl.hpp +++ b/src/wrap_cl.hpp @@ -196,7 +196,7 @@ for (py::handle py_dev: py_devices) \ devices_vec.push_back( \ py::cast(py_dev).data()); \ - num_devices = devices_vec.size(); \ + num_devices = static_cast(devices_vec.size()); \ devices = devices_vec.empty( ) ? nullptr : &devices_vec.front(); \ } \ @@ -1375,7 +1375,7 @@ namespace pyopencl PYOPENCL_PRINT_CALL_TRACE("clCreateContext"); ctx = clCreateContext( props_ptr, - devices.size(), + static_cast(devices.size()), devices.empty( ) ? nullptr : &devices.front(), 0, 0, &status_code); } @@ -2265,7 +2265,7 @@ namespace pyopencl PYOPENCL_RETRY_IF_MEM_ERROR( PYOPENCL_CALL_GUARDED(clEnqueueMigrateMemObjects, ( cq.data(), - mem_objects.size(), mem_objects.empty( ) ? nullptr : &mem_objects.front(), + static_cast(mem_objects.size()), mem_objects.empty( ) ? nullptr : &mem_objects.front(), flags, PYOPENCL_WAITLIST_ARGS, &evt )); @@ -2410,7 +2410,7 @@ namespace pyopencl (data(), CL_MEM_SIZE, sizeof(my_length), &my_length, 0)); if (PySlice_GetIndicesEx(slc.ptr(), - my_length, &start, &end, &stride, &length) != 0) + static_cast(my_length), &start, &end, &stride, &length) != 0) throw py::python_error(); if (stride != 1) @@ -2421,13 +2421,13 @@ namespace pyopencl PYOPENCL_CALL_GUARDED(clGetMemObjectInfo, (data(), CL_MEM_FLAGS, sizeof(my_flags), &my_flags, 0)); - my_flags &= ~CL_MEM_COPY_HOST_PTR; + my_flags &= ~static_cast(CL_MEM_COPY_HOST_PTR); if (end <= start) throw pyopencl::error("Buffer.__getitem__", CL_INVALID_VALUE, "Buffer slice have end > start"); - return get_sub_region(start, end-start, my_flags); + return get_sub_region(static_cast(start), static_cast(end-start), my_flags); } #endif }; @@ -2469,7 +2469,7 @@ namespace pyopencl throw pyopencl::error("Buffer", CL_INVALID_VALUE, "specified size is greater than host buffer size"); if (size == 0) - size = retained_buf_obj->m_buf.len; + size = static_cast(retained_buf_obj->m_buf.len); } cl_mem mem; @@ -2540,7 +2540,7 @@ namespace pyopencl queue, mem.data(), PYOPENCL_CAST_BOOL(is_blocking), - src_offset, len, buf, + src_offset, static_cast(len), buf, PYOPENCL_WAITLIST_ARGS, &evt )) ); @@ -2579,7 +2579,7 @@ namespace pyopencl queue, mem.data(), PYOPENCL_CAST_BOOL(is_blocking), - dst_offset, len, buf, + dst_offset, static_cast(len), buf, PYOPENCL_WAITLIST_ARGS, &evt )) ); @@ -2609,7 +2609,7 @@ namespace pyopencl (src.data(), CL_MEM_SIZE, sizeof(byte_count), &byte_count_src, 0)); PYOPENCL_CALL_GUARDED(clGetMemObjectInfo, (dst.data(), CL_MEM_SIZE, sizeof(byte_count), &byte_count_dst, 0)); - byte_count = std::min(byte_count_src, byte_count_dst); + byte_count = static_cast(std::min(byte_count_src, byte_count_dst)); } cl_event evt; @@ -2618,7 +2618,7 @@ namespace pyopencl cq.data(), src.data(), dst.data(), src_offset, dst_offset, - byte_count, + static_cast(byte_count), PYOPENCL_WAITLIST_ARGS, &evt )) @@ -2648,7 +2648,7 @@ namespace pyopencl (src.data(), CL_MEM_SIZE, sizeof(byte_count), &byte_count_src, 0)); PYOPENCL_CALL_GUARDED(clGetMemObjectInfo, (dst.data(), CL_MEM_SIZE, sizeof(byte_count), &byte_count_dst, 0)); - byte_count = std::min(byte_count_src, byte_count_dst); + byte_count = static_cast(std::min(byte_count_src, byte_count_dst)); } else { @@ -2668,7 +2668,7 @@ namespace pyopencl cq.data(), src.data(), dst.data(), 0, 0, - byte_count, + static_cast(byte_count), PYOPENCL_WAITLIST_ARGS, &evt )) @@ -2851,7 +2851,7 @@ namespace pyopencl PYOPENCL_CALL_GUARDED(clEnqueueFillBuffer, ( cq.data(), mem.data(), - pattern_buf, pattern_len, offset, size, + pattern_buf, static_cast(pattern_len), offset, size, PYOPENCL_WAITLIST_ARGS, &evt )) ); @@ -2941,7 +2941,7 @@ namespace pyopencl std::vector formats(num_image_formats); PYOPENCL_CALL_GUARDED(clGetSupportedImageFormats, ( ctx.data(), flags, image_type, - formats.size(), formats.empty( ) ? nullptr : &formats.front(), nullptr)); + static_cast(formats.size()), formats.empty( ) ? nullptr : &formats.front(), nullptr)); PYOPENCL_RETURN_VECTOR(cl_image_format, formats); } @@ -3039,7 +3039,7 @@ namespace pyopencl len = retained_buf_obj->m_buf.len; } - unsigned dims = py::len(shape); + unsigned dims = static_cast(py::len(shape)); cl_int status_code; cl_mem mem; if (dims == 2) @@ -3057,7 +3057,7 @@ namespace pyopencl } // check buffer size - cl_int itemsize = get_image_format_item_size(fmt); + cl_uint itemsize = get_image_format_item_size(fmt); if (buf && std::max(pitch, width*itemsize)*height > cl_uint(len)) throw pyopencl::error("Image", CL_INVALID_VALUE, "buffer too small"); @@ -3092,7 +3092,7 @@ namespace pyopencl } // check buffer size - cl_int itemsize = get_image_format_item_size(fmt); + cl_uint itemsize = get_image_format_item_size(fmt); if (buf && std::max(std::max(pitch_x, width*itemsize)*height, pitch_y) * depth > cl_uint(len)) @@ -3533,9 +3533,9 @@ namespace pyopencl PYOPENCL_PARSE_WAIT_FOR; PYOPENCL_PARSE_NUMPY_ARRAY_SPEC; - npy_uintp size_in_bytes = PyDataType_ELSIZE(tp_descr); + npy_uintp size_in_bytes = static_cast(PyDataType_ELSIZE(tp_descr)); for (npy_intp sdim: shape) - size_in_bytes *= sdim; + size_in_bytes *= static_cast(sdim); py::object result; PyArrayObject *result_arr; @@ -3568,7 +3568,7 @@ namespace pyopencl { result = py::object(py::steal(PyArray_NewFromDescr( &PyArray_Type, tp_descr, - shape.size(), + static_cast(shape.size()), shape.empty() ? nullptr : &shape.front(), strides.empty() ? nullptr : &strides.front(), mapped, ary_flags, /*obj*/nullptr))); @@ -3656,7 +3656,7 @@ namespace pyopencl py::object result = py::steal(PyArray_NewFromDescr( &PyArray_Type, tp_descr, - shape.size(), + static_cast(shape.size()), shape.empty() ? nullptr : &shape.front(), strides.empty() ? nullptr : &strides.front(), mapped, ary_flags, /*obj*/nullptr)); @@ -3728,7 +3728,7 @@ namespace pyopencl } size_t size() const { - return m_size; + return static_cast(m_size); } py::object mem() const @@ -4069,7 +4069,7 @@ namespace pyopencl ( cq.data(), dst.svm_ptr(), pattern_ptr, - pattern_len, + static_cast(pattern_len), size, PYOPENCL_WAITLIST_ARGS, &evt @@ -4185,7 +4185,7 @@ namespace pyopencl clEnqueueSVMMigrateMem, ( cq.data(), - svm_pointers.size(), + static_cast(svm_pointers.size()), svm_pointers.empty() ? nullptr : &svm_pointers.front(), sizes.empty() ? nullptr : &sizes.front(), flags, @@ -4430,7 +4430,7 @@ namespace pyopencl std::vector sizes; PYOPENCL_GET_VEC_INFO(Program, m_program, CL_PROGRAM_BINARY_SIZES, sizes); - size_t total_size = std::accumulate(sizes.begin(), sizes.end(), 0); + size_t total_size = std::accumulate(sizes.begin(), sizes.end(), size_t{0}); std::unique_ptr result( new unsigned char[total_size]); @@ -4455,10 +4455,10 @@ namespace pyopencl py::steal( #if PY_VERSION_HEX >= 0x03000000 PyBytes_FromStringAndSize( - reinterpret_cast(ptr), sizes[i]) + reinterpret_cast(ptr), static_cast(sizes[i])) #else PyString_FromStringAndSize( - reinterpret_cast(ptr), sizes[i]) + reinterpret_cast(ptr), static_cast(sizes[i])) #endif )); py_result.append(binary_pyobj); @@ -4564,7 +4564,7 @@ namespace pyopencl PYOPENCL_CALL_GUARDED_THREADED(clCompileProgram, (m_program, num_devices, devices, - options.c_str(), header_names.size(), + options.c_str(), static_cast(header_names.size()), programs.empty() ? nullptr : &programs.front(), header_name_ptrs.empty() ? nullptr : &header_name_ptrs.front(), 0, 0)); @@ -4577,7 +4577,7 @@ namespace pyopencl py_buffer_wrapper bufwrap; bufwrap.get(py_buffer.ptr(), PyBUF_ANY_CONTIGUOUS); PYOPENCL_CALL_GUARDED(clSetProgramSpecializationConstant, - (m_program, spec_id, bufwrap.m_buf.len, bufwrap.m_buf.buf)); + (m_program, spec_id, static_cast(bufwrap.m_buf.len), bufwrap.m_buf.buf)); } #endif }; @@ -4646,7 +4646,7 @@ namespace pyopencl len = buf_wrapper.m_buf.len; binaries.push_back(reinterpret_cast(buf)); - sizes.push_back(len); + sizes.push_back(static_cast(len)); } PYOPENCL_STACK_CONTAINER(cl_int, binary_statuses, num_devices); @@ -4654,7 +4654,7 @@ namespace pyopencl cl_int status_code; PYOPENCL_PRINT_CALL_TRACE("clCreateProgramWithBinary"); cl_program result = clCreateProgramWithBinary( - ctx.data(), num_devices, + ctx.data(), static_cast(num_devices), devices.empty( ) ? nullptr : &devices.front(), sizes.empty( ) ? nullptr : &sizes.front(), binaries.empty( ) ? nullptr : &binaries.front(), @@ -4765,7 +4765,7 @@ namespace pyopencl cl_program result = clLinkProgram( ctx.data(), num_devices, devices, options.c_str(), - programs.size(), + static_cast(programs.size()), programs.empty() ? nullptr : &programs.front(), 0, 0, &status_code); @@ -5019,7 +5019,7 @@ namespace pyopencl len = buf_wrapper.m_buf.len; PYOPENCL_CALL_GUARDED(clSetKernelArg, - (m_kernel, arg_index, len, buf)); + (m_kernel, arg_index, static_cast(len), buf)); } #if PYOPENCL_CL_VERSION >= 0x2000 @@ -6021,7 +6021,7 @@ namespace pyopencl py::object result = py::steal(PyArray_NewFromDescr( &PyArray_Type, tp_descr, - dims.size(), &dims.front(), /*strides*/ nullptr, + static_cast(dims.size()), &dims.front(), /*strides*/ nullptr, host_ptr, ary_flags, /*obj*/nullptr)); PyArrayObject *result_arr = reinterpret_cast(result.ptr());