This is an archive of the discontinued LLVM Phabricator instance.

[flang] Add -fstack-arrays flag
ClosedPublic

Authored by tblah on Jan 4 2023, 5:12 AM.

Details

Summary

The implementation of -fstack-arrays was added in https://reviews.llvm.org/D140415

I stored the option in CodeGenOpts because this seemed like the most relevant of what is available and creating a new options structure for a single boolean flag seemed like overkill.

-fstack-arrays is implied by -Ofast to match behavior by other fortran compilers.

The MemoryAllocationOpt pass is disabled when stack arrays is enabled because its purpose is the opposite: to move array allocations to the heap (although by default it won't move any allocations).

Diff Detail

Event Timeline

tblah created this revision.Jan 4 2023, 5:12 AM
Herald added a project: Restricted Project. · View Herald Transcript
tblah requested review of this revision.Jan 4 2023, 5:12 AM
tblah updated this revision to Diff 488686.Jan 12 2023, 9:05 AM

Updated to construct StackArrays as a ModuleOp pass

Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJan 12 2023, 9:05 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
awarzynski added inline comments.Jan 16 2023, 12:28 AM
clang/include/clang/Driver/Options.td
5102–5106

We should avoid duplicating options like this and use multiclasses instead. For example, see how debug_pass_manager is defined.

flang/docs/FlangDriver.md
572–573

[nit] "Clang" is a sub-project. It's not clear what "clang" would be - also a sub-project or the binary?

flang/include/flang/Tools/CLOptions.inc
169–171

CLANG-FORMAT-ME :) Same for other long lines here.

228–230

[nit] To me, it would make more sense to put stackArrays at the end. optLevelis a more powerful flag.

flang/test/Transforms/stack-arrays.f90
3

Also, I don't quite follow this comment:

We have to check llvm ir here to force flang to run the whole mlir pipeline

Why is checking LLVM IR going to force Flang to run anything?

6
flang/tools/bbc/bbc.cpp
276 ↗(On Diff #488686)

Similar suggestion below.

tblah updated this revision to Diff 489484.Jan 16 2023, 3:26 AM
tblah marked 6 inline comments as done.

Update to address review comments

flang/include/flang/Tools/CLOptions.inc
169–171

It seems clang-format is not run on this file. I have fixed the lines changed in my patch. Should I clang-format the whole file in a separate patch?

flang/test/Transforms/stack-arrays.f90
3

Just running flang-new -fc1 -emit-fir outputs the FIR before all of the transformation passes run (which is why I have to pipe the result through fir-opt to run the array value copy and stack arrays passes on the first line).

Outputting LLVM IR requires all of the MLIR transformation passes to be run to transform all of the higher level dialects into the LLVM dialect. Stack arrays is run as part of that same pipeline (fir::createMLIRToLLVMPassPipeline). I want to test that -fstack-arrays does cause the stack arrays pass to run as part of that pipeline, which is why I am checking the LLVM-IR.

One alternative would be to print out which passes have run and check that, but I felt this way fits more with the spirit of the other tests.

6

Lots of other tests do not follow this convention. It would change the FileCheck comments to look like ! LLVM: array_value_copy_simple.

I think it is good to require CHECK-FEATURE: so that any mention of FEATURE in comments does not confuse FileCheck. Especially for a name like LLVM which is likely to be written in capitals.

awarzynski added inline comments.Jan 18 2023, 2:38 AM
flang/include/flang/Tools/CLOptions.inc
169–171

+1

flang/test/Transforms/stack-arrays.f90
3

Thanks for the explanation. So you want to verify this functionality by investigating two pipelines:

  • source --> FIR
  • source --> LLVM IR

You could convey that by using CHECK-FIR and CHECK-LLVM-IR (or just FIR and LLVM-IR). I "complained", because this is unclear:

We have to check llvm ir here to force flang to run the whole mlir pipeline

Perhaps:

In order to verify the whole MLIR pipeline, make the driver generate LLVM IR.

6

Lots of other tests do not follow this convention.

I think that we would easily find examples for either approach.

I think it is good to require CHECK-FEATURE:

Well, LLVM is not a feature ;-) Also, most folks working with LLVM are familiar with the LIT syntax - the presence of CHECK in CHECK-FEATURE is just unnecessary noise to me. But I am happy either way. But I would appreciate replacing with LLVM with something a bit more meaningful - perhaps LLVM-IR?

tblah updated this revision to Diff 490100.Jan 18 2023, 3:37 AM
tblah marked 4 inline comments as done.

Clarified a comment in stack-arrays.f90 and clarified the choice of FileCheck
prefix.

awarzynski accepted this revision.Jan 18 2023, 5:23 AM

LGTM, thanks!

flang/test/Transforms/stack-arrays.f90
22–29

Hopefully I got this right, it's been a while :) https://llvm.org/docs/CommandGuide/FileCheck.html#the-check-dag-directive

This revision is now accepted and ready to land.Jan 18 2023, 5:23 AM
tblah updated this revision to Diff 490801.Jan 20 2023, 5:57 AM

Update to ensure that generated code for Options.td does not try to reference
clang::CodegenOpts::StackArrays.

awarzynski added inline comments.Jan 20 2023, 11:07 AM
clang/include/clang/Driver/Options.td
485–486

[nit] To me KeyPathAndMacro is just an implementation detail. The key part is that BoolOption does use the marshaling infra and BoolFlangOnlyOption does not. Perhaps rename as BoolOptionWithoutMarshalling? There isn't anything Flang specific here, is there?

tblah updated this revision to Diff 491267.Jan 23 2023, 2:15 AM
  • Rename option macro to make it clear that it doesn't do marshalling
tblah marked an inline comment as done.Jan 23 2023, 2:16 AM
awarzynski accepted this revision.Jan 24 2023, 6:23 AM

Thanks for the updates, LGTM!

This revision was landed with ongoing or failed builds.Feb 7 2023, 2:28 AM
This revision was automatically updated to reflect the committed changes.