This is an archive of the discontinued LLVM Phabricator instance.

[flang][HLFIR] add more memory effects interfaces
ClosedPublic

Authored by tblah on Aug 23 2023, 12:39 PM.

Details

Summary

Anything that produces a hlfir.expr should have an allocation side
effect so that it is not removed by CSE (which would result in two
hlfir.destroy operations for the same expression). Similarly for
hlfir.associate, which has hlfir.end_associate.

I see no regressions from this change when running llvm-testsuite with
optimization enabled, or from SPEC2017 rate benchmarks.

To test this, I have added MLIR's pass for testing side effect
interfaces to fir-opt.

Diff Detail

Event Timeline

tblah created this revision.Aug 23 2023, 12:39 PM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
tblah requested review of this revision.Aug 23 2023, 12:39 PM
tblah added a comment.EditedAug 23 2023, 12:41 PM

I'm posting this to start a discussion on what these memory effects should be. I'm more than happy to reduce the scope and some back to come trickier operations later.

Matt added a subscriber: Matt.Aug 28 2023, 1:31 PM

Thank you for the changes! I think adding MemAlloc and MemFree is a good idea to make MLIR optimizations aware of the paired nature of some HLFIR operations.

flang/include/flang/Optimizer/HLFIR/HLFIROps.td
352

Should this depend on the kind of the argument? For example, reporting MemRead for hlfir.expr argument seems imprecise.

606

I do not think we need to model MemRead for the hlfir.expr operand here.

tblah updated this revision to Diff 555695.Sep 4 2023, 3:12 AM
  • Made effects dependent upon argument and return types of transformational intrinsics (allocation effect if returning a hlfir.expr; read effect if an argument is a box or is pointer-like).
  • Filled in tests
tblah retitled this revision from WIP: [flang][HLFIR] add more memory effects interfaces to [flang][HLFIR] add more memory effects interfaces.Sep 4 2023, 3:12 AM
tblah edited the summary of this revision. (Show Details)
jeanPerier accepted this revision.Sep 4 2023, 6:45 AM

Thank you! This looks great to me.
Long term, I would try replace hlfir.destroy by an analysis in bufferization that automatically finds when an hlfir.expr buffer must be freed so that we can optimize hlfir.expr more easily. This would allow the alloca side effects of hlfir.expr to be removed (CSE would be OK, and this would allow unused operations to be dropped by dead code elimination at the HLFIR level). But this is not on the hot list, so I like your approach to solve the CSE problem.

flang/include/flang/Optimizer/HLFIR/HLFIROps.td
329

Can't DeclareOpInterfaceMethods<MemoryEffectsOpInterface> and getIntrinsicEffects

1007

I think a MemRead is needed for "temp" that may be read.

This revision is now accepted and ready to land.Sep 4 2023, 6:45 AM
tblah updated this revision to Diff 555767.Sep 4 2023, 10:14 AM
tblah marked 4 inline comments as done.

Thanks for review, and welcome back @jeanPerier!

Changes:

  • Add tests for hlfir.{copy_in, copy_out}
  • fix effects on hlfir.{copy_out, concat}
This revision was landed with ongoing or failed builds.Sep 6 2023, 3:33 AM
This revision was automatically updated to reflect the committed changes.