Skip to content

Differentiate HLFIR, currently matmul - #2990

Draft
vchuravy wants to merge 3 commits into
flang-fir-pluginfrom
flang-hlfir-matmul
Draft

Differentiate HLFIR, currently matmul#2990
vchuravy wants to merge 3 commits into
flang-fir-pluginfrom
flang-hlfir-matmul

Conversation

@vchuravy

@vchuravy vchuravy commented Jul 23, 2026

Copy link
Copy Markdown
Member

Differentiates the Fortran MATMUL intrinsic while it is still a first-class
hlfir.matmul op, on top of the !fir.ref by-reference autodiff layer:

  • HLFIRAutoDiffOpInterfaceImpl.cpp -- !hlfir.expr AutoDiffTypeInterface
    (elementwise shadow/add/zero via hlfir.elemental) and the hlfir.matmul
    rules: forward tangent d(A·B) = dA·B + A·dB, and reverse adjoints
    Ā += Ḡ·Bᵀ, B̄ += Aᵀ·Ḡ (via hlfir.matmul_transpose / hlfir.transpose).
    Added to the flang-dependent MLIREnzymeHLFIRImplementations library and
    registered by registerHLFIRDialectAutoDiffInterface.
  • fir-enzyme-opt and the FlangEnzymeMLIR plugin now also attach the HLFIR
    array-intrinsic models.
  • Tests: matmul_fwd.mlir, matmul_rev.mlir (the op rules), and fortran_matmul.f90
    (end-to-end: Flang lowers MATMUL to hlfir.matmul, fir-enzyme-opt differentiates).

Co-Authored-By: Claude Opus 4.8 noreply@anthropic.com


Stack created with GitHub Stacks CLIGive Feedback 💬

@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from 598f5cc to c202c33 Compare July 23, 2026 13:21
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch 2 times, most recently from 07ed5fc to 5027fab Compare July 23, 2026 15:41
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch 3 times, most recently from 9307049 to c50dbb9 Compare July 23, 2026 16:17
@vchuravy vchuravy changed the title Tier-1 hlfir.matmul forward+reverse autodiff (Fortran array intrinsic) Differentiate HLFIR, currently matmul Jul 23, 2026
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from c50dbb9 to 1d96a01 Compare July 26, 2026 21:16
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch 2 times, most recently from 5722186 to 2141864 Compare July 27, 2026 06:22
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from 2141864 to 96245b6 Compare July 27, 2026 06:50
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from 96245b6 to 1e77c13 Compare July 27, 2026 08:19
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from 1e77c13 to ffff38c Compare July 27, 2026 09:18
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from ffff38c to 13457fd Compare July 27, 2026 12:21
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from 13457fd to d124c63 Compare August 3, 2026 13:38
vchuravy and others added 3 commits August 4, 2026 13:36
Differentiates the Fortran MATMUL intrinsic while it is still a first-class
hlfir.matmul op, on top of the !fir.ref by-reference autodiff layer:

  * HLFIRAutoDiffOpInterfaceImpl.cpp -- !hlfir.expr AutoDiffTypeInterface
    (elementwise shadow/add/zero via hlfir.elemental) and the hlfir.matmul
    rules: forward tangent d(A·B) = dA·B + A·dB, and reverse adjoints
    Ā += Ḡ·Bᵀ, B̄ += Aᵀ·Ḡ (via hlfir.matmul_transpose / hlfir.transpose).
    Added to the flang-dependent MLIREnzymeHLFIRImplementations library and
    registered by registerHLFIRDialectAutoDiffInterface.
  * fir-enzyme-opt and the FlangEnzymeMLIR plugin now also attach the HLFIR
    array-intrinsic models.
  * Tests: matmul_fwd.mlir, matmul_rev.mlir (the op rules), and fortran_matmul.f90
    (end-to-end: Flang lowers MATMUL to hlfir.matmul, fir-enzyme-opt differentiates).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BkvJByk8nTti2GiF4RudNG
…ayer

This entry point is implemented and used by this layer
(HLFIRAutoDiffOpInterfaceImpl.cpp, fir-enzyme-opt, HLFIRFlangPluginRegistration),
so declare it here rather than in the base plugin header.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Fold registerHLFIRDialectAutoDiffInterface into the shared Fortran
registration next to the FIR one, instead of calling both from the flang
plugin. The fir-opt plugin (enzyme-fir-plugin.cpp) only ever calls
registerEnzymeFortranInterfaces, so registering there is what gives it
the hlfir.matmul rules and !hlfir.expr's AutoDiffTypeInterface; both
vehicles now register the same set, as that entry point promises.

Point the three matmul tests at the FIREnzyme plugin (%firenzyme,
firenzyme_plugin) the way the rest of test/MLIR/FIR now does -- they
required the nonexistent fir_enzyme_opt feature and so never ran. All
twelve FIR tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@vchuravy
vchuravy force-pushed the flang-hlfir-matmul branch from d124c63 to c52e4fe Compare August 4, 2026 12:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant