You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Filing to discuss, maybe we already have some semblance of this? cc @meheff@scampanoni
It's a common pattern for people to test whether a value is zero after an add (e.g. with the value 1) has been performed to see if the value has become zero as a way of detecting overflow. Or, they test whether the sign has flipped with respect to the operands signs. We should optimize away these patterns of "overflow tests" if range analysis indicates an overflow is impossible.
(This would be slightly more direct to analyze if we canonicalized to an add-with-carry operation, but I don't think that's strictly necessary, just may make the opportunity a little more directly apparent in the IR.)
As a case study, consider converting an int32 to float as we do in the float32.x library's from_int -- for round-to-nearest even, you may need to bump the fraction based on round/sticky bits. If the fraction overflows, we may need to bump the exponent. If the exponent overflows, we have to produce NaN. However, the maximum exponent we can get based on the most set bit is 127+31=158, which means we can never overflow to inf in practice, even though we may have written that case in the source code for completeness (especially if it were parameterized for different floating point types) -- one way we might have naively detect that overflow would be to compare after the addition if the value has flipped sign -- if we know that was impossible we could optimize away that sel and condition.
Current best alternative workaround (limit 100 words)
Optimization opportunity, so not getting the optimization and relying on the underlying synthesis tool if it's within a single stage.
Your view of the "best case XLS enhancement" (limit 100 words)
We optimize away overflow detections that cannot occur.
The text was updated successfully, but these errors were encountered:
cdleary
changed the title
[enhancement][opt] Optimization opportunity: tie test-against-zero after an add to range analysis (overflow detection)
[enhancement][opt] Optimization opportunity: tie overflow conditions e.g. after an add, in to range analysis (overflow detection)
Jan 18, 2025
What's hard to do? (limit 100 words)
Filing to discuss, maybe we already have some semblance of this? cc @meheff @scampanoni
It's a common pattern for people to test whether a value is zero after an add (e.g. with the value 1) has been performed to see if the value has become zero as a way of detecting overflow. Or, they test whether the sign has flipped with respect to the operands signs. We should optimize away these patterns of "overflow tests" if range analysis indicates an overflow is impossible.
(This would be slightly more direct to analyze if we canonicalized to an add-with-carry operation, but I don't think that's strictly necessary, just may make the opportunity a little more directly apparent in the IR.)
As a case study, consider converting an int32 to float as we do in the
float32.x
library'sfrom_int
-- for round-to-nearest even, you may need to bump the fraction based on round/sticky bits. If the fraction overflows, we may need to bump the exponent. If the exponent overflows, we have to produce NaN. However, the maximum exponent we can get based on the most set bit is 127+31=158, which means we can never overflow to inf in practice, even though we may have written that case in the source code for completeness (especially if it were parameterized for different floating point types) -- one way we might have naively detect that overflow would be to compare after the addition if the value has flipped sign -- if we know that was impossible we could optimize away that sel and condition.Current best alternative workaround (limit 100 words)
Optimization opportunity, so not getting the optimization and relying on the underlying synthesis tool if it's within a single stage.
Your view of the "best case XLS enhancement" (limit 100 words)
We optimize away overflow detections that cannot occur.
The text was updated successfully, but these errors were encountered: