Skip to content

Makes PyCallable dimension reversing logic consistent with that of PythonExtensionGen#8931

Open
jiawen wants to merge 4 commits intomainfrom
jiawen-pycallable
Open

Makes PyCallable dimension reversing logic consistent with that of PythonExtensionGen#8931
jiawen wants to merge 4 commits intomainfrom
jiawen-pycallable

Conversation

@jiawen
Copy link
Contributor

@jiawen jiawen commented Feb 4, 2026

When a generator emits a Python extension, the pipeline only accepts buffers that are C or F contiguous (PythonExtensionGen.cpp), throwing an exception if it's neither.

In contrast, a callable constructed from generator.compile_to_callable() constructs a pipeline that always reverses the dimensions of input buffers. It also non-contiguous buffers.

This PR changes the latter to be consistent with the former. Callables constructed from generator.compile_to_callable() now:

  • Reverses the order of dimensions if a buffer is C-contiguous.
  • Does not reverse the order of dimensions if a buffer is F-contiguous.
  • Rejects other buffers.

brew install clang-format lld llvm flatbuffers wabt python pybind11 doxygen eigen libpng libjpeg-turbo openblas
```

The `llvm` package includes `clang`, `clang-format`, and `lld`, too. To ensure
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Homebrew pulled these out long ago.


```shell
$ pipx install cmake
pipx install cmake
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for the drive-by. My vs code auto-formatted this entire file - but for the better I think.

Copy link
Member

Choose a reason for hiding this comment

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

After the changes we've made to CI and Python dependency management in #8942, we should revert this file and consider improvements in a separate PR.

@jiawen
Copy link
Contributor Author

jiawen commented Feb 4, 2026

I'll work on how hl.Buffer wraps existing Buffers in a followup PR?

@@ -1,4 +1,5 @@
import halide as hl
import numpy as np
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are we ok with this requirement for the test?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, unless it's very incidental. Numpy is all but in the standard Python library at this point.

@jiawen
Copy link
Contributor Author

jiawen commented Feb 13, 2026

@alexreinking PTAL?

Copy link
Member

@alexreinking alexreinking left a comment

Choose a reason for hiding this comment

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

The code changes look good. Please drop the doc changes from this; we need a bigger update as a separate work item.

README.md Outdated
Halide requires C++17 (or later) to use.

For more detail about what Halide is, see https://halide-lang.org.
For more detail about what Halide is, see <https://halide-lang.org>.
Copy link
Member

Choose a reason for hiding this comment

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

Same thing here. Separate PR for this.

@jiawen
Copy link
Contributor Author

jiawen commented Feb 13, 2026

@alexreinking Reverted doc changes.

@jiawen jiawen requested a review from alexreinking February 13, 2026 19:35
@alexreinking
Copy link
Member

@jiawen


C:\PROGRA~1\MICROS~1\2022\COMMUN~1\VC\Tools\MSVC\1444~1.352\bin\Hostx64\x64\cl.exe  /nologo /TP -DHALIDE_ENABLE_RTTI -DHALIDE_VERSION_MAJOR=22 -DHALIDE_VERSION_MINOR=0 -DHALIDE_VERSION_PATCH=0 -DHALIDE_WITH_EXCEPTIONS -DHalide_Python_EXPORTS -DPy_NO_LINK_LIB -IC:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-build\include -IC:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\src\runtime -external:IC:\Python311\Include -external:IC:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-build\vcpkg_installed\x64-windows\include -external:W0 /DWIN32 /D_WINDOWS /EHsc /O2 /Ob2 /DNDEBUG -std:c++17 -MD /bigobj /MP /showIncludes /Fopython_bindings\src\halide\CMakeFiles\Halide_Python.dir\halide_\PyCallable.cpp.obj /Fdpython_bindings\src\halide\CMakeFiles\Halide_Python.dir\ /FS -c C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(51): error C2065: 'ssize_t': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(51): error C2146: syntax error: missing ';' before identifier 'c_stride'
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(51): error C2065: 'c_stride': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(52): error C2065: 'ssize_t': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(52): error C2146: syntax error: missing ';' before identifier 'f_stride'
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(52): error C2065: 'f_stride': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(58): error C2065: 'c_stride': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(61): error C2065: 'c_stride': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(63): error C2065: 'f_stride': undeclared identifier
C:\build_bot\worker\halide-llvm_main-x86-64-windows\halide-source\python_bindings\src\halide\halide_\PyCallable.cpp(66): error C2065: 'f_stride': undeclared identifier

Unfortunately, ssize_t isn't a thing on Windows.

Comment on lines +50 to +54
void is_any_contiguous(const py::buffer_info &info, bool &c_contig, bool &f_contig) {
ssize_t c_stride = info.itemsize;
ssize_t f_stride = info.itemsize;
c_contig = true;
f_contig = true;
Copy link
Member

Choose a reason for hiding this comment

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

While we're changing things here for ssize_t, I'd rather this returned a std::pair and we could use structured bindings below to unpack them.

Copy link
Member

Choose a reason for hiding this comment

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

You can probably get away with auto for the type of *_stride.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasted a bunch of time tracking down where ssize_t is defined in PyBind11. py::buffer_info calls it ssize_t, but it's really py::ssize_t and an alias for Py_ssize_t. I didn't realize that ssize_t is POSIX and not part of C. 😠

Copy link
Member

Choose a reason for hiding this comment

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

Well, glad you got to the bottom of it! py::ssize_t is probably nicer to read than auto? Up to you.

Comment on lines +119 to +121
bool c_contig;
bool f_contig;
is_any_contiguous(value_buffer_info, c_contig, f_contig);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
bool c_contig;
bool f_contig;
is_any_contiguous(value_buffer_info, c_contig, f_contig);
auto [c_contig, f_contig] = is_any_contiguous(value_buffer_info);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Grudgingly ok. C++ lawyers at Google would scren "use a struct and name your fields!"

Copy link
Member

@alexreinking alexreinking Feb 13, 2026

Choose a reason for hiding this comment

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

If it were used in two places or not in an anonymous namespace, I would have suggested that. 🙂

Copy link
Member

Choose a reason for hiding this comment

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

Aside: I'm generally more concerned about unexpected non-initialization (sneaky) than switching values around (typically loud logic bugs).

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.

2 participants