Support passing non-constant values to Exceptions in JIT
See original GitHub issueFeature Request
In many cases it seems like it would be desirable to raise exceptions with strings that aren’t constants. My strongest use case is that I have an exception that happens in object mode, but because the message may be unclear and hard to determine, I want to append some additional information about the jit function is triggering this exception (for example function name and maybe argument values).
Here is an example where my exception message arises from using object mode.
import numba
@numba.njit
def f():
with numba.objmode(res="unicode_type"):
res = g()
raise ValueError(f"Exception in f(): {res}")
def g():
return "This is an Exception"
f()
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Error using Numba to improve performance: non-constant value
Error: "TypingError: Failed in nopython mode pipeline (step: convert make_function into JIT functions) Cannot capture the non-constant value ...
Read more >passing parameters to jax.jit 'ed functions #1922 - GitHub
I was thinking along the lines of a cpp code, where I can define a variable outside my function, and then pass a...
Read more >Just-in-Time compilation - Numba documentation
Recompile all existing signatures. This can be useful for example if a global or closure variable was frozen by your function and its...
Read more >Supported Python features - Numba
exceptions : try .. except , raise , else and finally (See details in this section) ... Once a function is assigned to...
Read more >How to Throw Exceptions in Java - Rollbar
Throwing an exception is as simple as using the "throw" statement. You then specify the Exception object you wish to throw. Every Exception...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

So, I started working on this locally. Since,
ValueErroris a callable so while inferring constants in its arguments it reaches the following line,https://github.com/numba/numba/blob/0f5953df2a2ee6eea1ac44b974bcd954aae91bc6/numba/core/consts.py#L96
Now, since we are passing a non-constant string so the above line fails and hence we get a
ConstantInferenceError. The fix which I have thought of is this, if the callable is an exception then ignore the constaness of the message (i.e., change the above loop with the body havingtry-exceptblock, catchConstantInferenceError, if_excisTruethen addaotherwise raise the caught exception). And in thenumba/core/lowering.pyadd support forir.Raisein a way similar toStaticRaise. I will post a diff on Monday even though if I fail implementing this approach (for learning purposes).cc: @stuartarchibald @guilhermeleobas
I have a question regarding how exceptions are handled in Numba (cc @sklam, @stuartarchibald).
The exception triple
(exc, val, tb)is serialized to the LLVM module as a constant string (ref). Is there any reason to why this step is important? Is there any feature (i.e. cache, object mode, AOT) that requires the exception triple to be serialized to the LLVM module?By inspecting the LLVM module, the exception triple lifetime is:
excinfopointer and calls_helperlib.c::numba_unpickleto reconstruct the triple as a pyobject._helperlib.c::numba_do_raisewhich reraises the exception.The code below generates the following wrapper
Wrapper generated
Unless I am missing something, it seems to be possible to skip the serialization step and use the
excinfoto store the triple directly. And with that, one could support non-constant messages in an exception.Edit: I think I understand now why serialization is used.