← Back to Pull requests

Translate llvm.minimum/maximum with NaN-propagating semantics #4032

Open
unknown opened this pull request with 338 lines diff across 4 files
View on GitHub
unknown unknown commented on Sep 23, 2026 10:08 UTC
Author

Fixes #3282: llvm.minimum/llvm.maximum were translated to OpenCL.std fmin/fmax, which ignore NaN operands. They also don't order -0.0 before +0.0, which llvm.minimum/llvm.maximum require.

OpenCL.std has no NaN-propagating min/max, so SPIRVRegularizeLLVM (which already expands bswap, fshl/fshr, umul.with.overflow) now expands the calls into llvm.minnum/llvm.maxnum, translated to fmin/fmax as before, plus two fix-ups:

%m     = call float @llvm.minnum.f32(float %x, float %y)
; unless nsz: x == y is only ambiguous for -0.0 == +0.0, so pick the operand
; with the sign bit set (maximum: clear)
%eq    = fcmp oeq float %x, %y
%xsign = call float @llvm.copysign.f32(float 1.0, float %x)
%xneg  = fcmp olt float %xsign, 0.0
%z     = select i1 %xneg, float %x, float %y
%m.z   = select i1 %eq, float %z, float %m
; unless nnan
%uno   = fcmp uno float %x, %y
%r     = select i1 %uno, float 0x7FF8000000000000, float %m.z

With both nnan and nsz the result is a plain fmin, as before. The sign is tested with copysign rather than an integer bitcast, so no Int16/Int64 capability is added, and the result is always one of the operands (combining bit patterns could produce a value that is neither input on devices that flush denormals in comparisons). Everything used is core SPIR-V or OpenCL.std.

The writer's direct minimum/maximum → fmin/fmax mapping is removed. They stay in isKnownIntrinsic, as otherwise -spirv-allow-unknown-intrinsics emits a declaration left unused by the expansion as an import. As a side effect, llvm.minimum/maximum on bfloat, previously rejected, now translate like llvm.minnum/maxnum on bfloat.

Resulting SPIR-V for llvm.minimum.f32 (vectors are the same, with splat constants):

 %8 = OpExtInst %float %1 fmin %x %y
%10 = OpFOrdEqual %bool %x %y
%12 = OpExtInst %float %1 copysign %float_1 %x
%14 = OpFOrdLessThan %bool %12 %float_0
%15 = OpSelect %float %14 %x %y
%16 = OpSelect %float %10 %15 %8
%17 = OpUnordered %bool %x %y
 %r = OpSelect %float %17 %float_0x1_8p_128 %16

Reverse translation yields the corresponding _Z4fminff/_Z8copysignff calls, fcmps and selects.

Not addressed, and could reuse the same fix-ups:

  • minnum/minimumnum without nsz still map to a bare fmin, which gets NaNs right but, unlike what LangRef requires, may return either zero (the minnum mapping was also questioned in a comment on #3282).

  • vector.reduce.fminimum/fmaximum are lowered to select(FUnordLessThan(a, b), a, b) pairs (FUnordGreaterThan for max), which drop a NaN in b and don't order signed zeros.
  • Assisted by: Opus 5.5

    Diff: 338 lines (+253 / -85) across 4 files Reviewer: MrSidims Review duration: 12541m
    All Vouch review confidence checks passed
    Residual risk 0.23 = Change Risk (0.42) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.42 · 338 lines diff across 4 files
    0.42
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.349
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #4032"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 42.2% (MEDIUM) · Higher = larger blast radius
    42.2%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 22.5% · Safe to merge
    22.5%