← Back to Pull requests

AffineCFG: a conditional a loop bound reads is written as an indicator #3425

Open
unknown opened this pull request with 471 lines diff across 5 files
View on GitHub
unknown unknown commented on Oct 08, 2026 01:12 UTC
Author

MFEM's 3D hdiv/hcurl kernels (PAHdivL2Apply3D, PAHdivL2AssembleDiagonal_ADAt_3D, PAHdivMassSetup3D, …) size an inner loop by the vector component c, the variable of a constant-trip loop inside the element lambda. For example, PAHdivMassAssembleDiagonal3D (fem/integ/bilininteg_hdiv_kernels.cpp):

``c++
mfem::forall(NE, [=] MFEM_HOST_DEVICE (int e)
{
int osc = 0;
for (int c = 0; c < VDIM; ++c) // VDIM = 3
{
const int D1Dz = (c == 2) ? D1D : D1D - 1;
...
for (int dz = 0; dz < D1Dz; ++dz)
...
osc += D1Dx
D1Dy D1Dz;
}
});
`

The hcurl kernels have the arms the other way round ((c == 2) ? D1D - 1 : D1D). D1D is a symbol, but D1Dz is not: it differs from one c iteration to the next. So the dz bound is a function of the loop variable c.

This reaches affine-cfg as an affine.if over c yielding two constants (or D1D - 1 / D1D) that feeds an scf.for bound. That loop has no affine bound, so it stays an scf.for, its stores stay memref.store, and every loop around it is rejected for parallelization because of them (118 of the 293 rejections in the hdiv TU). The one existing pattern for this, SplitOnAffineIfConstants, duplicates the loop per arm and is off in the pipeline.

The rewrite

When the if's operands are loop variables with constant bounds, its constraint c has a constant range [lo, hi]. Over that range the condition is an affine expression:

  • [c >= 0] = (c + M) floordiv M, with M = max(hi + 1, -lo)

  • [c == 0] = [c >= 0] + [-c >= 0] - 1
  • AffineIfConstantsAsIndicator<scf::ForOp> / <scf::IfOp> finds the conditional an op's bounds or condition derive from, using SplitOnAffineIfConstants::findConditional with the arm test now a parameter. Each arm pair must be two constants, or two values a constant apart (D1D - 1 / D1D) where the else arm is defined at the scope's top level, so it can be used as a symbol. The conditional's result becomes b + (a - b) indicator, an affine.apply, with an index_cast back to the yielded type. ForOpRaising then takes the bound, e.g. D1D - 1 + c floordiv 2 over c ∈ [0, 2] for the hdiv form.

    The rewrite only applies where a loop bound or an scf.if condition reads the result. An affine.if of constants anywhere else stays a set that the domain simplifications can read. Applying it everywhere lost a loop merge in raiseimmerse.mlir and affinecfg_ext.mlir.

    Effect

    Running --affine-cfg alone on the MFEM dumps: scf.for left in bilininteg_hdiv_kernels.cpp 61 → 19, and in bilininteg_hcurl_kernels.cpp 20 → 5.

    The floordiv in the indicator goes through lower-affine and back in the pipeline. #3451 (merged, split out of this PR) makes matchLoweredDiv read it back. Without #3451, compiling bilininteg_curlcurl_pa, bilininteg_mixedcurl_pa and test_curlcurl_integ failed.

    With this change on the earlier MFEM stack (sweep final48), the newly batched SmemPAHdivMassApply3D<3,4> matched a NumPy reference to 1e-14, and the MFEM GPU suite was 74/74.

    Tests

  • affinecfg_if_constants_indicator.mlir: == and >= constraints, value arms a constant apart, and a conditional that stays because nothing bounds a loop with it.

  • affinecfg_split_if_constants.mlir: the split cases now use a loop variable of unknown extent. Over a constant range the indicator applies first, with no duplication.
  • Interaction with other open PRs (rebased on main 73603ba52): with #3431, affinecfg_split_on_flag @xor_flag's 0/-1 affine.if becomes this PR's indicator affine.apply`; correct output, regenerate on whichever lands second.

    🤖 Generated with Claude Code

    https://claude.ai/code/session_016zErYp7upmqr4NHfhod9UD

    Diff: 471 lines (+353 / -118) across 5 files Reviewer: ImanHosseini Review duration: 63027m
    All Vouch review confidence checks passed
    Residual risk 0.31 = Change Risk (0.53) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.53 · 471 lines diff across 5 files
    0.53
    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%
    40%
    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.368
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3425"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% 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: 52.7% (MEDIUM) · Higher = larger blast radius
    52.7%
    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: 40.9% · Lower = rushed or shallow review
    40.9%
    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: 31.1% · Safe to merge
    31.1%