[flang][Evaluate] Analyze contiguity and copies per consequent-arg (#238055)

The contiguity and copy-in/copy-out analysis that semantics and lowering
use treated a conditional argument (F2023 R1526) as a single expression.
It answered from the condition or from the first consequent-arg. The
actual argument is the consequent-arg chosen at run time (F2023 15.5.2.3
p2), so the analysis has to account for every consequent-arg. This is
also the base for lowering conditional arguments. Without it, an array
conditional argument passed to a CONTIGUOUS dummy argument wouldn't get
the copy-in it needs.

Contiguity:
- IsContiguous() had an overload for ActualArgument that no caller
  reached, because the header declared only the function template. The
template traversed the whole argument; it saw the scalar condition first
  and reported every conditional argument as contiguous. Declare the
  overload in check-expression.h with default arguments and remove the
  template's explicit instantiation for ActualArgument. Other actual
  arguments get the same answers as before.
- A conditional argument is simply contiguous if and only if all its
consequent-args are (F2023 15.5.2.3 p4). The overload returns true when
every consequent-arg is known to be contiguous, false when every one is
  known not to be, and std::nullopt otherwise. A .NIL. consequent takes
  no part.
- IS_CONTIGUOUS folds through the same overload, so it now folds a
  conditional argument whose consequent-args are all contiguous or all
discontiguous. The check that stops intrinsic folding for a conditional
  argument lets IS_CONTIGUOUS through for this reason. Such a reference
  can then appear in a constant expression; docs/Extensions.md now lists
  this extension.

ActualArgNeedsCopy():
- Apply the rules for a plain actual argument to each consequent-arg.
  IsVariable() of the whole argument visited the condition first, so a
  condition such as `n > 0` made it an expression that never needed
  copy-out, and a logical variable condition made it a variable, which
  could miss the copy-in of an expression consequent-arg. Now a
  conditional argument needs copy-in if any consequent-arg is an
  expression other than a named constant, and it can need copy-out only
  if a consequent-arg is a variable.
- A conditional argument is polymorphic if any consequent-arg is (F2023
  15.5.2.3 p4). Add ActualArgument::ConditionalArg::IsPolymorphic() and
  use it in place of the type of the first consequent-arg.
- Treat a conditional argument as coindexed when any consequent-arg is.

Semantics:
- CheckExplicitDataArg() runs once for each consequent-arg, but it took
  the copy-out decision from the whole argument. A contiguous VOLATILE
  consequent-arg next to a discontiguous one got a false warning that a
  temporary copy is required. When the whole argument needs copy-out,
decide again for the consequent-arg being checked. ActualArgNeedsCopy()
  keeps the whole-argument answer at least as conservative as the answer
  for each consequent-arg, so a consequent-arg never needs copy-out when
  the whole argument doesn't.

As a result, semantics reports F2023 C1547 and C1548 for conditional
arguments, and IS_CONTIGUOUS folds when the answer doesn't depend on the
condition.

Known limitations:
- The copy-out suppression for subobjects of an INTENT(IN) dummy
argument
  doesn't look into the consequent-args yet; a TODO marks it. Only
  lowering can observe this, and lowering doesn't support conditional
  arguments yet.
- When every consequent-arg violates C1548, flang reports the error
once,
  because a conditional argument has no source location of its own
  (#227805).

Tests: Semantics/conditional-arg-contiguity.f90 (new) covers C1547,
C1548,
and the warnings about a temporary copy. Its cases include a condition
that is an expression, an ASYNCHRONOUS dummy argument, polymorphic,
coindexed, and assumed-type consequent-args, and a consequent-arg that
is
a subobject of an INTENT(IN) dummy argument.
Semantics/conditional-arg-portability.f90 (new) checks that each
consequent-arg gets the portability or temporary-copy warning that it
would get on its own. Evaluate/fold-conditional-arg.f90 covers the
IS_CONTIGUOUS folding. Semantics/conditional-arg-inquiry.f90 checks
that IS_CONTIGUOUS doesn't fold when the consequent-args disagree or
when one of them is an assumed-shape array, whose contiguity is known
only at run time. No test covers the copy-in answer, because semantics
asks only about copy-out and lowering doesn't support conditional
arguments yet.

These changes were generated with the assistance of AI tooling and have
been
approved by the author.

PR description generated with AI assistance.

Part of llvm/llvm-project#180592

GitOrigin-RevId: b9526f6a68332b3f6d4cb64696c4f1a1789e2ce8
12 files changed
tree: 10c984b6374f08c7f795644f5ff8df3ec39bb417
  1. cmake/
  2. docs/
  3. examples/
  4. include/
  5. lib/
  6. test/
  7. tools/
  8. unittests/
  9. .clang-format
  10. .clang-tidy
  11. .drone.star
  12. .gitignore
  13. CMakeLists.txt
  14. LICENSE.TXT
  15. Maintainers.md
  16. README.md
README.md

Flang

Flang is a ground-up implementation of a Fortran front end written in modern C++. It started off as the f18 project (https://github.com/flang-compiler/f18) with an aim to replace the previous flang project (https://github.com/flang-compiler/flang) and address its various deficiencies. F18 was subsequently accepted into the LLVM project and rechristened as Flang.

Please note that flang is not ready yet for production usage.

Getting Started

Read more about flang in the docs directory. Start with the compiler overview.

To better understand Fortran as a language and the specific grammar accepted by flang, read Fortran For C Programmers and flang's specifications of the Fortran grammar and the OpenMP grammar.

Treatment of language extensions is covered in this document.

To understand the compilers handling of intrinsics, see the discussion of intrinsics.

To understand how a flang program communicates with libraries at runtime, see the discussion of runtime descriptors.

If you're interested in contributing to the compiler, read the style guide and also review how flang uses modern C++ features.

If you are interested in writing new documentation, follow LLVM's Markdown style guide.

Consult the Getting Started with Flang for information on building and running flang.