Report useful error to user if the promise_clamp all fails to losslessly cast. #8238
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[unsafe_]promise_clamped is sort of broken in a subtle way, which I didn't fix in this PR. I didn't change its functionality, but instead of an
internal_assert()
triggering, now a useful user error is reported, which allows the user to fix it. This behavior is better than crashing IMO.The problem is when you for example call:
unsafe_promise_clamped(<uint16_t>, 0, <Variable int32_t>)
.I ran into the situation where I naturally would start from:
Where counter_output_buf (a
GeneratorOutput<Buffer<int32_t>>
) counts the amount of times a certain index is observed inindex_func
. However,index_func
isuint16_t
in my case.But want to promise that the index I have is within bounds of the buffer, so I did:
This now fails, because the first value is
uint16_t
, and my upper bound isint32_t
, which cannot be losslessly cast touint16_t
, as per:Halide/src/IROperator.cpp
Lines 1590 to 1593 in 33d5ba9
The
lossless_cast
returns an undefined Expr if it fails, which then trigger theinternal_assert
that makes sure all arguments to aCall
are defined.My point being that all of this feels a little odd behavior of this function. When you want to promise it's clamped, and it actually doesn't do it because of some type mismatch, feels kinda odd. But maybe that's perfectly on purpose and good? Not sure...