← Back to Pull requests

Type analysis through GEPs with a negative constant offset #3922

Open
unknown opened this pull request with 316 lines diff across 3 files
View on GitHub
unknown unknown commented on Nov 24, 2026 10:54 UTC
Author

<!-- claude-projects-attribution: {"github_login":"vchuravy"} -->
_Requested by Valentin · project thread_

At -O1 and above, flang inlines array assignments, and Enzyme then failed with "Cannot deduce type of copy" on the memcpy/memmove through the temporary. Type analysis skipped GEPs with a negative offset, so the element types never reached the arrays. This affects allocatables and dummy arrays of unknown size. Found while looking at the flang -O1 failures from #3913.

flang addresses an element x(i) of a 1-based array at &x[i - 1], which LLVM emits in one of two forms.

After InstCombine:

%hi = getelementptr [4 x i8], ptr %x, i64 %i
%e  = getelementptr i8, ptr %hi, i64 -4

After LICM hoists the step back (seen with flang 21):
%lo = getelementptr i8, ptr %x, i64 -4          ; outside the loop
%e  = getelementptr float, ptr %lo, i64 %i      ; %i >= 1

Change


  • First form: a GEP with a negative constant offset is related to the base of the chain of GEPs it steps back into, at the non-negative offsets of the whole chain. The chain is walked through at most 4 GEPs, with at most one variable index. Index values come from knownIntegralValues, as for a single GEP.

  • Second form: a GEP with a negative constant offset -c relates its own bytes at offsets >= c to those of its base pointer.

  • When a GEP's base gains types, the GEP re-queues its constant-offset GEP users.
  • Testing


  • New tests:

  • - test/TypeAnalysis/gep_negative_offset_chain.ll (first form);
    - test/TypeAnalysis/gep_negative_offset_hoisted.ll (second form);
    - test/Fortran/ReverseMode/assignment_opt.f90, at -O1, -O2, -O3 and through the flang plugin. Without this change it fails with "Cannot deduce type of copy" on flang 21. With it, the gradient is 0 4 6 11, as derived by hand.
  • Checked on flang 21, flang 23 (the version CI uses) and flang 24 with #3430: the new Fortran test fails without this change and passes with it. With flang 23, all Fortran lit tests pass apart from config_file_lto.f90, which needs the LLD plugin my local build skipped and fails on main the same way.

  • With LLVM 21, the lit failures in test/Enzyme, test/Integration and test/TypeAnalysis are the same as on main. They are typed-pointer tests that LLVM 21 cannot run. All Fortran tests pass.
  • 🤖 Generated with Claude Code

    Diff: 316 lines (+237 / -79) across 3 files Reviewer: wsmoses Review duration: 9472m
    All Vouch review confidence checks passed
    Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 316 lines diff across 3 files
    0.45
    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.345
    Shallow Review Depth shallow_review_depth
    0.344
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #3922"
    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: 45.4% (MEDIUM) · Higher = larger blast radius
    45.4%
    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: 24.2% · Safe to merge
    24.2%