https://github.com/halide/Halide
Revision e5ed2260085f7e5ecca4b1426309e2ba93cc4ec3 authored by Steven Johnson on 15 February 2023, 17:03:41 UTC, committed by GitHub on 15 February 2023, 17:03:41 UTC
* Fix Python error handling

Error handling in the Python bindings wasn't quite right for JIT:

We previously replaced halide_error() to throw a C++ exception. Sounds good, but unfortunately, doesn't work reliably: if called from jitted code (which doesn't know about C++ exceptions), the throw statement may be unable to find the enclosing try block (which is outside jitted code), meaning it will call std::terminate. Now, instead, we just leave the JIT error handler unset, and call with an explicit JITUserContext with a custom print handler; in theory, this meant that the code in JITFuncCallContext::finalize() would check for an error after the call into jitted code, and call `halide_runtime_error` if so (which would then trigger an all-in-C++-exception). Unfortunately...

(2) JITFuncCallContext is broken by design; it mutates the input JITUserContext, so that trying to use the same JITUserContext for two calls in a row leaves you with a JITUserContext with (at least) the error_handler set. Since at least one of the realize() calls does this twice (once for bounds query, once for execution), this means that an error in the second call would never be seen, since finalize() only reported errors if there wasn't a custom error handler on input. Per @abadams suggestion, we work around this by treating 'JITErrorBuffer::handler' as 'no custom error handler', which is mostly true. (But really, JITFuncCallContext and JITUserContext are a hard-to-reason-about mess and arguably need to rethought entirely.)

(3) Removed entirely-unnecessary overrides of runtime print and error handlers from PyStubImpl; despite the comments, this code is unnecessary.

* format
1 parent 7963cd4
History
Tip revision: e5ed2260085f7e5ecca4b1426309e2ba93cc4ec3 authored by Steven Johnson on 15 February 2023, 17:03:41 UTC
Fix Python error handling (#7352)
Tip revision: e5ed226
File Mode Size
.github
apps
cmake
dependencies
doc
packaging
python_bindings
src
test
tools
tutorial
util
.clang-format -rw-r--r-- 1.4 KB
.clang-format-ignore -rw-r--r-- 286 bytes
.clang-tidy -rw-r--r-- 2.2 KB
.gitattributes -rw-r--r-- 342 bytes
.gitignore -rw-r--r-- 5.0 KB
.gitmodules -rw-r--r-- 0 bytes
CMakeLists.txt -rw-r--r-- 5.5 KB
CMakePresets.json -rw-r--r-- 5.6 KB
CODE_OF_CONDUCT.md -rw-r--r-- 3.5 KB
LICENSE.txt -rw-r--r-- 13.8 KB
MANIFEST.in -rw-r--r-- 159 bytes
Makefile -rw-r--r-- 103.3 KB
README.md -rw-r--r-- 16.4 KB
README_cmake.md -rw-r--r-- 76.2 KB
README_python.md -rw-r--r-- 32.1 KB
README_rungen.md -rw-r--r-- 12.1 KB
README_webassembly.md -rw-r--r-- 10.5 KB
pyproject.toml -rw-r--r-- 195 bytes
requirements.txt -rw-r--r-- 129 bytes
run-clang-format.sh -rwxr-xr-x 1.4 KB
run-clang-tidy.sh -rwxr-xr-x 3.1 KB
setup.py -rw-r--r-- 1.2 KB

README.md

back to top