← Back to Pull requests

feat(isel): route *sat ops through GMIR #1926

Closed
unknown closed this pull request without merging on Nov 03, 2026 15:17 UTC
View on GitHub
unknown unknown commented on Oct 28, 2026 20:17 UTC
Author

Routes the saturating intrinsics (llvm.intr.{s,u}{add,sub,shl}.sat) through gMIR and the RISC-V 64
legalizer, following LLVM's GlobalISel, instead of selecting them directly from the LLVM dialect with
SelectionDAG-style patterns.

For the legalization, we copy the patterns LLVM uses:

  • lowerAddSubSatToMinMax

  • lowerAddSubSatToAddoSubo

  • lowerShlSat

  • lowerUAddSubO and lowerSAddSubO

  • widenScalarAddSubOverflow
  • We pick the lowerAddSubSatToMinMax lowering when the min/max ops are legal (s64) and lowerAddSubSatToAddoSubo otherwise. This slightly differs from LLVM, which picks min/max when min/max
    are legal or custom for the type, and addo/subo otherwise. However, afaict, implementing the same mechanism would be premature in our design as we would need multiple passes.

    The generated code for the i64 sat intrinsics changes from SelectionDAG's sequences to GlobalISel's:

  • {s,u}{add,sub}.sat now clamp with Zbb min/max/minu (usub.sat is x - minu x y instead

  • of maxu x y - y).
  • The signed adds and subs no longer use an overflow test and a Zicond select.

  • {s,u}shl.sat still end in Zicond selects, now built from shifting the result back and comparing.
  • Also, legalizing a g_select with an i1 condition coming from a g_icmp left a redundant mask behind:

    %c = "gmir.g_icmp"(%x, %y) <{"predicate" = 2 : i64}> : (i64, i64) -> i1
    %r = "gmir.g_select"(%c, %a, %b) : (i1, i64, i64) -> i64

    became

    %w = "gmir.g_icmp"(%x, %y) <{"predicate" = 2 : i64}> : (i64, i64) -> i64
    %one = "gmir.g_constant"() <{"value" = 1 : i64}> : () -> i64
    %m = "gmir.g_and"(%w, %one) : (i64, i64) -> i64
    %r = "gmir.g_select"(%m, %a, %b) : (i64, i64, i64) -> i64

    LLVM's Legalizer keeps two worklists. In each round it legalizes every non-artifact instruction first,
    then runs the artifact combiner. An artifact it cannot combine goes back to the instruction list and is
    legalized in a later round.

    Instead, our legalizeInstrStep now skips an artifact (g_trunc, g_[zsa]ext, g_merge_values,
    g_unmerge_values) while any of its operands is the result of a gMIR op that is not legal yet. In the
    example, the g_zext waits. The g_icmp is widened, and the combiner then folds
    g_zext (g_trunc %w) to %w, matching LLVM.

    🤖 Assisted by Claude Code

    Diff: 298 lines (+223 / -75) across 3 files Reviewer: regehr Review duration: 8339m
    All Vouch review confidence checks passed
    Residual risk 0.16 = Change Risk (0.40) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 298 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.341
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #1926"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 40% (MEDIUM) · Higher = larger blast radius
    40%
    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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 15.8% · Safe to merge
    15.8%
    Repository