[wip] fix arithmetic divergences between interpreter, const folding and JIT - #3671
Draft
aleksisch wants to merge 3 commits into
Draft
[wip] fix arithmetic divergences between interpreter, const folding and JIT#3671aleksisch wants to merge 3 commits into
aleksisch wants to merge 3 commits into
Conversation
SimPolicy_MathTT::Abs returns the argument unchanged for -0.0, and v_abs on x86 is max(-a, a), which hands back -0.0 as well. The JIT emits fabs and was already right, so this was a tier divergence; const folding evaluates these policies, so the folded form was wrong in both tiers. The vector fix clears the sign bit directly - include/vecmath is vendored and stays untouched.
The interpreter throws "division overflow" for INT_MIN / -1 and answers 0 for INT_MIN % -1; the JIT emitted a raw sdiv/srem, which LLVM leaves poison. The division now throws like the interpreter, and the modulo selects a divisor of 1, since INT_MIN % 1 is 0 - the same answer. Signed scalars only: the interpreter does not guard vector division either. Bumps LLVM_JIT_CODEGEN_VERSION, or cached DLLs keep serving the unguarded code.
das_float_to_float16's subnormal branch double-counted the 23->10 mantissa reduction the normal branch already applies, so every subnormal came out 0, and for the smallest inputs the shift reached 38 - undefined behaviour, which is where the -nan came from. Source-reachable as half(x) for any x under 6.104e-5. Found as an interp-vs-JIT divergence by the differential fuzzer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Three arithmetic results that differed between the interpreter, const folding and the JIT. All found by differential fuzzing (fold vs runtime, interp vs JIT, edge-value operands); all reachable from plain
.dassource with no macros.abs(-0.0)returned-0.0—SimPolicy_MathTT::Absisa >= 0 ? a : -a, andv_abson x86 clang ismax(-a, a), where MAXPS hands back its second operand for equal inputs. The JIT emitsfabsand was already right, so the interpreter disagreed with the JIT and with itself across targets.1.0 / abs(-0.0)was-inf.INT_MIN / -1was poison under-jit— the interpreter throwsdivision overflowand answers0forINT_MIN % -1; the JIT emitted a rawsdiv/srem. BumpsLLVM_JIT_CODEGEN_VERSION, or cached DLLs keep serving the unguarded code.half(x)flushed every subnormal to zero —das_float_to_float16's subnormal branch double-counted the 23->10 mantissa reduction. For the smallest inputs the shift reached 38, which is UB onuint32_t, and produced-nan. Hits anyxunder 6.104e-5.Marked WIP: split out of #3651, not yet run through full preflight.