This is an archive of the discontinued LLVM Phabricator instance.

[flang][hlfir] inline hlfir.transpose as hlfir.elemental
ClosedPublic

Authored by tblah on Apr 24 2023, 7:56 AM.

Details

Summary

Inlining as a hlfir.elemental will allow the transpose to be inlined
into subsequent operations in some cases. For example,

y = TRANSPOSE(x)
z = y * 2

Will operate in a single loop without creating a temporary for the
TRANSPOSE (unlike the runtime call, which always allocates).

This is in a new SimplifyHLFIRIntriniscs pass. The intention is that some
day that pass might replace the FIR SimplifyIntrinsics pass.

Depends on: D149060

Diff Detail

Event Timeline

tblah created this revision.Apr 24 2023, 7:56 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
tblah requested review of this revision.Apr 24 2023, 7:56 AM
tblah updated this revision to Diff 516431.Apr 24 2023, 9:04 AM

Empty bump to re-run CI with the parent revision set

vzakhari accepted this revision.Apr 24 2023, 1:49 PM

Looks great to me! Thank you for the changes!

flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp
9

typo: transoformational

This revision is now accepted and ready to land.Apr 24 2023, 1:49 PM
jeanPerier accepted this revision.Apr 25 2023, 12:43 AM

Thanks, aside from the nit about the character case, looks great!

flang/include/flang/Optimizer/Builder/HLFIRTools.h
36

Can you move it near getIndexExtents declaration below to keep the Extent related helper together?

flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp
61

You should provide the type parameters too here, this is needed for character transpose (and later maybe for derived type with length parameters).

You can get them with:

llvm::SmallVector<mlir::Value,1> typeParams;
hlfir::genLengthParameters(loc, builder, array, typeParams);
93

I think we probably do not want to attempt the rewrite for polymorphic arrays.

This is a corner case, probably unlikely outside of test suits, but we currently would have a hard time dealing with things like CLASS(*) to create a temporary inline from the hlfir.elemental (I may need to extend the hlfir.elemental to carry some dynamic type information so that we can allocate a storage for it if needed). Calling the TRANSPOSE runtime for those case may make more sense until there is a clear optimization use case.

I would suggest using the following here to be safe for now:

// Only inline transpose for arrays where a temporary can easily be created inline if needed.
target.addDynamicallyLegalOp<hlfir::TransposeOp>([](hlfir::TransposeOp transpose) {
      return transpose.getType().cast<hlfir::ExprType>().isPolymorphic();
    });
tblah updated this revision to Diff 516717.Apr 25 2023, 2:33 AM
tblah marked 4 inline comments as done.

Thanks for the review

Changes:

  • Pass typeParams to hlfir::genElementalOp
  • Skip transformation of polymorphic arrays
  • Spelling and formatting nits
jeanPerier accepted this revision.Apr 25 2023, 4:21 AM

Looks great, thanks for addressing all the comments!

This revision was automatically updated to reflect the committed changes.