This is an archive of the discontinued LLVM Phabricator instance.

[Flang] Add support for fsave-optimization-record
ClosedPublic

Authored by victorkingi on Jul 17 2023, 5:48 AM.

Details

Summary

Add support for generating and saving the optimization record.
Optimization record lists the optimizations performed by LLVM.

This patch enables the flag in Flang. Clang handles this functionality
using the BackendConsumer which Flang doesn't have, hence, was
implemented in CodeGenAction::executeAction

FlangOption added to all variants of fsave-optimization-record in
clang/include/clang/Driver/Options.td . Clang handles it the
same way.

opt_record_file, opt_record_passes and opt_record_format flags
in Options.td were moved out of the group [CC1Option, NoDriverOption]
to allow flang -fc1 support.

The renderRemarksOptions and willEmitRemarks functions in
clang/lib/Driver/ToolChains/Flang.cpp follow same syntax as clang.
In flang/lib/Frontend/CompilerInvocation.cpp we update the field
OptRecordFile with the provided optimization file value. Clang
doesn't do this as it processes the Options.td, mapping the
OptRecordFile earlier on.

Diff Detail

Event Timeline

victorkingi created this revision.Jul 17 2023, 5:48 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a subscriber: sunshaoce. · View Herald Transcript
victorkingi requested review of this revision.Jul 17 2023, 5:48 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 17 2023, 5:48 AM
tblah added a subscriber: tblah.Jul 17 2023, 5:51 AM

Couple of quick comments.

You probably need another frontend-forwarding test to check fsave-optimization-record=<file_name>.

clang/lib/Driver/ToolChains/Flang.cpp
392–395

JA and Triple do not seem to be used.

If the functionalities are similar, you could move the renderRemarksOptions from Clang to a file in llvm/Frontend/ and then use that in Flang.

flang/test/Driver/fsave-optimization-record.f90
9–12

Check for the existence of the optimization report in all these cases.

tblah added inline comments.Jul 17 2023, 6:34 AM
clang/lib/Driver/ToolChains/Flang.cpp
398

What happens if an invalid or unsupported format is specified. Is that caught somewhere and a good error message printed, or does something crash?

flang/lib/Frontend/FrontendActions.cpp
1047

The other error checking in this function outputs using the DiagnosticsEngine. This ensures that the errors always have the correct format.

See reportOptRecordError in clang/lib/CodeGen/CodeGenAction.cpp.

Addressed comments

victorkingi marked 4 inline comments as done.Jul 18 2023, 8:50 AM
victorkingi added inline comments.
clang/lib/Driver/ToolChains/Flang.cpp
398

It has the same behavior as clang, producing the error "unknown remark serializer format"

flang/lib/Frontend/FrontendActions.cpp
966–970

Thought this refactoring might help making the function clearer. Let me know if its unnecessary

victorkingi marked an inline comment as done.

Fixed failing test

victorkingi edited the summary of this revision. (Show Details)Jul 19 2023, 6:20 AM

Please check the failing test.

flang/test/Driver/fsave-optimization-record.f90
15
20

Attempt to fix failing test

victorkingi marked 2 inline comments as done.Jul 20 2023, 3:12 AM

Addressed kiranchandramohan comment on fsave-optimization-record.f90 test file

No changes rebasing

no changes rebasing

tblah accepted this revision.Jul 24 2023, 3:07 AM

This looks good to me. Thanks for your efforts on this!

This revision is now accepted and ready to land.Jul 24 2023, 3:07 AM

Thanks for working on this, LG! I've left a few minor comments inline.

clang/include/clang/Driver/Options.td
6454–6460

Currently, these options have the following flags set:

  • CC1Option and NoDriverOption.

See:

IIUC, you'd like this to be CC1Option` and NoDriverOption _and_ FC1Option. You can do that by moving these definitions here:

flang/lib/Frontend/CompilerInvocation.cpp
194–196

Drop braces: https://llvm.org/docs/CodingStandards.html#don-t-use-braces-on-simple-single-statement-bodies-of-if-else-loop-statements. Same for the other options below.

Btw, I'd be OK with moving this to a dedicated hook for parsing e.g. "opt remark options".

flang/lib/Frontend/FrontendActions.cpp
966–970

It's a nice clean-up!

I'd move it to a separate patch, but the noise level is low, so I am not too concerned.

flang/test/Driver/frontend-forwarding.f90
26 ↗(On Diff #543424)

Is a dedicated driver invocation needed for this test?

victorkingi marked 3 inline comments as done.

minor cleanup

  • dropped braces on single line if statements
  • moved opt_record options into [CC1Option, FC1Option, NoDriverOption] Group in clang/include/clang/Driver/Options.td
victorkingi added inline comments.Jul 24 2023, 8:41 AM
flang/lib/Frontend/CompilerInvocation.cpp
166–172

dropped braces on single line if statements

flang/test/Driver/frontend-forwarding.f90
26 ↗(On Diff #543424)

since -fsave-optimization-record and foptimization-record-file both produce opt-record-file and opt-record-format flags, I couldn't find an easier way to test for both calls without having to use 2 separate flang-new invocations.

We could ignore the foptimization-record-file test and assume if fsave-optimization-record passes, then the former passes as well

awarzynski accepted this revision.Jul 24 2023, 11:32 AM

LGTM, thanks for the updates! (please address my comment in "frontend-forwarding.f90" before landing this)

flang/test/Driver/frontend-forwarding.f90
26 ↗(On Diff #543424)

Ah, I see what's happening here. Neither -fsave-optimization-record nor -foptimization-record-file are "forwarded" from flang-new to flang-new -fc1 (*). So, shouldn't really be tested in this file. Please remove this change and I'll update the comment in this file to clarify this for our future selves. Apologies for the confusion.

(*) Forwarding in this context means "passing verbatim from flang-new to flang-new -fc1"

moved opt_record flags generation test from frontend-forwarding.f90
to fsave-optimization-record.f90

victorkingi marked 2 inline comments as done.Jul 25 2023, 6:16 AM
victorkingi added inline comments.
flang/test/Driver/frontend-forwarding.f90
26 ↗(On Diff #543424)

moved the test to flang/test/Driver/fsave-optimization-record.f90

rebasing no changes