This is an archive of the discontinued LLVM Phabricator instance.

[flang][hlfir] Separate -emit-fir and -emit-hlfir for flang-new
ClosedPublic

Authored by tblah on May 22 2023, 6:05 AM.

Details

Summary

In review for https://reviews.llvm.org/D146278, @vzakhari asked to
separate -emit-fir and -emit-hlfir. This will allow FIR to be easily
outputted after the HLFIR passes have been run.

The new semantics are as follows:

Action-flang-experimental-hlfir?Result
-emit-hlfirNOutputs HLFIR
-emit-hlfirYOutputs HLFIR
-emit-firNOutputs FIR, using old lowering
-emit-firYOutputs FIR, lowering via HLFIR

Patch for bbc: https://reviews.llvm.org/D151108

Diff Detail

Event Timeline

tblah created this revision.May 22 2023, 6:05 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a subscriber: sunshaoce. · View Herald Transcript
tblah requested review of this revision.May 22 2023, 6:05 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 22 2023, 6:05 AM
jeanPerier accepted this revision.May 22 2023, 7:39 AM

Not the driver expert here, so please wait for @awarzynski input, but the new flag logic LGTM from a user point of view.

This revision is now accepted and ready to land.May 22 2023, 7:39 AM
tblah retitled this revision from [flang][hlfir] Separate -emit-fir and -emit-hlfir to [flang][hlfir] Separate -emit-fir and -emit-hlfir for flang-new.May 22 2023, 8:00 AM
tblah edited the summary of this revision. (Show Details)
vzakhari accepted this revision.May 22 2023, 12:18 PM

Thank you for the follow-up!

Thanks Tom, mostly makes sense, but I suggest the following:

  • EmitMLIR --> EmitFIR (nfc, just clarifying the name),
  • EmitHLFIRToFIR --> EmitHLFIR (functional change).

More comments inline.

flang/include/flang/Frontend/FrontendOptions.h
45

To me having EmitFIR and EmitHLFIR would make more sense. With 2 dialects, EmitMLIR becomes rather confusing (and, I suspect, rarely used).

flang/lib/Frontend/FrontendActions.cpp
651

I wouldn't really consider this hook as a separate action. Instead, I'd use it here: https://github.com/llvm/llvm-project/blob/6130c9df99a7a7eb9c6adc118a48f8f2acc534ab/flang/lib/Frontend/FrontendActions.cpp#L917-L920. As in, it basically "tweaks" EmitMLIR (which I would rename as EmitFIR).

652

This mlirModule comes from here: https://github.com/llvm/llvm-project/blob/6130c9df99a7a7eb9c6adc118a48f8f2acc534ab/flang/lib/Frontend/FrontendActions.cpp#L277. That will either be FIR or HLFIR, depending on whether -flang-experimental-hlfir was used or not, right?

673

"Lowering to LLVM IR"? ;-)

tblah updated this revision to Diff 526681.May 30 2023, 9:46 AM
tblah marked an inline comment as done.

Thanks for review!

Changes:

  • Fix assertion comment
  • Change the -emit-mlir flag to be an alias for -emit-fir (previously they were the other way around). This does not address review comments but hopefully makes the intent clearer. It is a shame that the -emit-mlir flag has so little to do with the EmitMLIR action.
tblah added inline comments.May 31 2023, 2:29 AM
flang/include/flang/Frontend/FrontendOptions.h
45

EmitMLIR emits whichever one Lowering was configured for (depending on the -flang-experimental-hlfir flag).

EmitHLFIRToFIR always emits FIR after lowering via HLFIR and running all of the passes to convert HLFIR into FIR.

I didn't add EmitFIR and EmitHLFIR because the frontend action is exactly the same for these two (run lowering and print out the MLIR it generates).

flang/lib/Frontend/FrontendActions.cpp
651

This is very different to EmitMLIR.

EmitMLIR will print the MLIR produced by lowering (HLFIR or FIR depending upon the -flang-experimental-hlfir flag).

This action will run lowering, always generating HLFIR. Then it will run the HLFIR pass pipeline to lower the HLFIR into FIR, and output that FIR (which will be very different to FIR generated directly from lowering without going through HLFIR).

652

Yes. In this case, lowering will have always produced HLFIR because we don't enter this action unless -flang-experimental-hlfir has been specified.

673

Thanks for spotting this

awarzynski added inline comments.May 31 2023, 8:22 AM
flang/lib/Frontend/FrontendActions.cpp
651

So, at the moment:

  • EmitMLIR will generate either FIR of HLFIR,
  • lowerHLFIRToFIR will also generate FIR via HFLIR.

When -flang-experimental-hlfir is used, both actions do the same thing (what) - generate FIR. The difference becomes in how this is achieved. However, actions represent "what", not "how" and hence my suggestion. In fact, this would be totally valid:

/// (with `-flang-experimental-hlfir`) Lower to FIR and print
EmitMLIR,

/// Lower to FIR and print, go via HLFIR
EmitHLFIRToFIR,

With EmitFIR and EmitHLFIR (instead of EmitMLIR and EmitHLFIRToFIR), you'd preserve all the functionality and also make sure that there's never overlap between actions, right? Additionally, you'd still have -flang-experimental-hlfir that would tweak EmitFIR).

tblah updated this revision to Diff 527386.Jun 1 2023, 6:07 AM

Thanks again for review!

Now there are two actions:

  • EmitFIR
  • EmitHLFIR

EmitHLFIR will assert that lowering was set up to emit HLFIR. EmitFIR will
use the HLFIR to FIR pipeline if lowering was set up to emit HLFIR.

awarzynski accepted this revision.Jun 1 2023, 6:18 AM

Thanks for the updates, LGTM!

flang/test/HLFIR/flang-experimental-hlfir-flag.f90
7–12

This comment is pure gold!