This is an archive of the discontinued LLVM Phabricator instance.

[Flang] Lower Unstructured do loops
ClosedPublic

Authored by kiranchandramohan on May 3 2022, 3:38 AM.

Details

Summary

The FIR do_loop is designed as a structured operation with a single
block inside it. Presence of unstructured constructs like jumps, exits
inside the loop will cause the loop to be marked as unstructured. These
loops are lowered using the control-flow dialect branch operations.

Fortran semantics do not allow the loop variable to be modified inside
the loop. To prevent accidental modification, the iteration of the
loop is modeled by two variables, trip-count and loop-variable.
-> The trip-count and loop-variable are initialized in the pre-header.
The trip-count is set as (end-start+step)/step where end, start and
step have the usual meanings. The loop-variable is initialized to start.
-> The header block contains a conditional branch instruction which
selects between branching to the body of the loop or the exit block
depending on the value of the trip-count.
-> Inside the body, the trip-count is decremented and the loop-variable
incremented by the step value. Finally it branches to the header of the
loop.

Part of the upstreaming effort to move LLVM Flang from fir-dev branch of
https://github.com/flang-compiler/f18-llvm-project to the LLVM Project.

Co-authored-by: Val Donaldson <vdonaldson@nvidia.com>
Co-authored-by: Eric Schweitz <eschweitz@nvidia.com>
Co-authored-by: Jean Perier <jperier@nvidia.com>
Co-authored-by: Peter Klausler <pklausler@nvidia.com>

Diff Detail

Event Timeline

Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
Herald added a subscriber: mehdi_amini. · View Herald Transcript
kiranchandramohan requested review of this revision.May 3 2022, 3:38 AM
awarzynski accepted this revision.May 4 2022, 7:46 AM

Makes sense, thank you!

I've left some minor suggestions for tests, but I would also be happy for this to be merged as is. Please wait at least one day before merging this - just in case there are comments from other reviewers.

flang/test/Lower/do_loop_unstructured.f90
89–91

[nit] This way it might be easier to match FIR with the input Fortran

133

You define COND_J, BODY_J and EXIT_J, but don't use it :)

148

You define COND_K, BODY_K and EXIT_K, but don't use it :)

This revision is now accepted and ready to land.May 4 2022, 7:46 AM

Addressed comments from @awarzynski. Sure will wait before merging.

kiranchandramohan marked 3 inline comments as done.May 4 2022, 10:29 AM
This revision was automatically updated to reflect the committed changes.