Repository navigation
Preserve NaN when narrowing to half in InstCombine - #762
Closed
pavelsavara wants to merge 1 commit into
Closed
pavelsavara wants to merge 1 commit into
pavelsavara wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates LLVM InstCombine’s visitFPTrunc to preserve NaN semantics when narrowing floating-point values to half (fp16), by rewriting eligible fptrunc ... to half instructions into an explicit isnan-guarded select that yields a quiet NaN on NaN inputs.
Changes:
- Add an
isnan(fcmp uno) +selectguard aroundfptrunctohalfwhen the trunc is not markednnan. - Support both scalar
halfand vector-of-halfdestinations, including splatted NaN constants for vectors. - Add a pattern check (
IsProtectedHalfTrunc) intended to prevent re-applying the transform on the newly-created inner truncation.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2233
to
+2234
| return (Sel->getTrueValue() == &FPT && IsNaNConst(Sel->getFalseValue())) || | ||
| (Sel->getFalseValue() == &FPT && IsNaNConst(Sel->getTrueValue())); |
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.
Summary
Some targets/toolchains mishandle NaN payloads when narrowing a floating-point
value to
half(fp16) and can produce an infinity for a NaN input. Thischange teaches
InstCombinerImpl::visitFPTruncto preserve NaN semantics byguarding
fptrunc … to halfwith an explicitisnancheck, so a NaN input isalways lowered to a (quiet) NaN result rather than risking an infinity.
Motivation
A
fptruncof a NaN must yield a NaN. On the affected lowering paths thisinvariant was broken, turning NaN inputs into infinities and producing wrong
numerical results in Mono workloads. Materializing the NaN guard at the IR
level makes the result correct regardless of how the backend lowers the
narrowing.
Change
File:
llvm/lib/Transforms/InstCombine/InstCombineCasts.cppWhen the destination scalar type is
halfand thefptruncis not markednnan, the truncation is rewritten as:i.e.
select(isnan(src), qnan, fptrunc(src)).Behavior details:
halfand vectors ofhalf; the quiet-NaN constant issplatted for vector destinations.
nnanfast-math flag (NaN can'toccur, so no guard is needed).
fptruncare propagated to the innertruncation.
getQNaN). NaN-nessis preserved; the exact payload is not (which is the intent, since the
affected lowering mishandled payloads).
Idempotency / termination
visitFPTruncrevisits the innerfptruncit creates, so the transform mustrecognize its own output and not re-fire. An
IsProtectedHalfTrunchelperdetects the already-guarded pattern and bails out. Two subtleties are handled so
InstCombine reliably reaches a fixed point:
fcmp uno %src, %srcto
fcmp uno %src, 0.0(seeInstCombineCompares.cpp). The guard thereforeaccepts either the
%src, %srcform or a zero right-hand side(
m_AnyZeroFP()).selectis aConstantFPfor scalars but a splat (ConstantVector) for vectors. The checkuses
m_NaN(), which matches both scalar and splat NaN constants.Without these, the protection check would fail to recognize the freshly emitted
guard and InstCombine would not converge (repeated re-expansion / infinite-loop
detection).