287 Commits

Author SHA1 Message Date
Yi Wu
a2d4a4c0b2 Apply kind code check on exitstat and cmdstat (#78286)
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: https://github.com/llvm/llvm-project/issues/77990

(cherry picked from commit 14a15103cc9dbdb3e95c04627e0b96b5e3aa4944)
2024-01-30 16:45:35 +00:00
Tom Eccles
50e2581a19
[flang] Allow assumed-shape element pass to dummy arg with ignore_tkr (#78196)
This is allowed by gfortran and ifort with `![GCC|DEC]$ ATTRIBUTES
NO_ARG_CHECK`
2024-01-22 23:16:22 +00:00
Peter Klausler
8f3bdea9b4
[flang][NFC] Restore documentation (#78211)
Add a paragraph to Extensions.md that fell out of a recent patch.
2024-01-15 13:06:47 -08:00
Peter Klausler
691770ca67
[flang] Add portability warning for F'2008 feature (#77526)
Using the VALUE attribute for assumed-length CHARACTER dummy arguments
became standard in F'2008 but still lacks widespread implementation;
emit a portability warning when they are enabled.

Resolves llvm-test-suite/Fortran/gfortran/regression/value_5.f90.
2024-01-15 12:31:52 -08:00
Peter Klausler
f089691157
[flang][runtime] Better real MOD/MODULO results (#77167)
The Fortran standard defines real MOD and MODULO with expressions like
MOD(a,p) = a - AINT(a/p)*p. Unfortunately, these definitions have poor
accuracy when a is much larger in magnitude than p, and every Fortran
compiler uses better algorithms instead.

Fixes llvm-test-suite/Fortran/gfortran/regression/mod_large_1.f90.
2024-01-15 11:48:36 -08:00
Peter Klausler
90828d67ea
[flang] Weird restrictions on index variables (#77019)
There are some very odd (even for Fortran) rules in F'2023 subclause
19.4 (paras 6 & 8) pertaining to the index variables of FORALL and DO
CONCURRENT constructs/statements, and they are not currently implemented
correctly.

Although these index variables are construct entities, they have
restrictions in the standard that would essentially allow them to also
be variables in their enclosing scopes. If their names are present in
the enclosing scope, and the construct does not have an explicit type
specification for its indices, then the names in the enclosing scope
must either be scalar variables or COMMON blocks, and their type must be
integer.

Reimplement these restrictions largely with portability warnings rather
than hard errors. Retain the semantic interpretation that the type of an
untyped index variable be taken from the type of a variable of the same
name in the enclosing scope, if it exists, although that bit of the
standard could be interpreted otherwise.

Fixes https://github.com/llvm/llvm-project/issues/76978.
2024-01-15 10:50:40 -08:00
Peter Klausler
add189cdf0
[flang][runtime] Resume rounding hexadecimal floating-point input (#77006)
This reverts commit cab156c4129e5948a6322054480e66d3ca17b919, and adds
language to Extensions.md documenting the discrepancy with the letter of
the language standard.
2024-01-15 10:39:22 -08:00
Peter Klausler
82e1e412e3
[flang][runtime] Treatment of NaN in MAXVAL/MAXLOC/MINVAL/MINLOC (#76999)
Detect NaN elements in data and handle them like gfortran does (at
runtime); namely, NaN can be returned if all the data are NaNs, but any
non-NaN value is preferable. Ensure that folding returns the same
results as runtime computation.

Fixes llvm-test-suite/Fortran/gfortran/regression/maxloc_2.f90 (and
probably others).
2024-01-15 10:29:26 -08:00
Peter Klausler
2e08e821b7
[flang][runtime] Extension: allow a comma to terminate a fixed input … (#76768)
…field

When a comma appears in a fixed-width input field for integer editing,
many compilers accept it without error and interpret the comma as
terminating the field early.
2024-01-15 09:47:52 -08:00
Peter Klausler
c6dfb62d4d
[flang] Support \u Unicode escape sequences (#76757)
Support \uNNNN and \uNNNNNNNN escape sequences for CHARACTER(KIND=2) and
CHARACTER(KIND=4) literal constants for better GNU Fortran
compatibility.

Fixes llvm-test-suite/Fortran/gfortran/regression/achar_6.F90 and
.../widechar_1.f90.
2024-01-15 09:35:17 -08:00
Yi Wu
7dd4d28e41
[flang] Fix const cast issue in FreeMemory function call in execute_command_line (#77906)
The FreeMemory function only accepts a void pointer, but it was being
called with a const char pointer, resulting in a type-casting issue.
To address this, the const was removed, use char * instead.
2024-01-13 01:22:40 +00:00
Leandro Lupori
9edcf7a28e
[flang][driver] Add support for -isysroot in the frontend (#77365)
If DEFAULT_SYSROOT is not specfied when building flang, then the
-isysroot flag is needed to link binaries against system libraries
on Darwin. It's also needed when linking against a non-default
sysroot.
2024-01-11 10:37:58 -03:00
Yi Wu
959a430a8d
[flang] FDATE extension implementation: get date and time in ctime format (#71222)
reference to gfortran fdate
https://gcc.gnu.org/onlinedocs/gfortran/FDATE.html
usage:
```fortran
CHARACTER(32) :: time
CALL fdate(time)
WRITE(*,*) time
```

fdate is used in the ECP proxy application
https://proxyapps.exascaleproject.org/app/minismac2d/

f904467142/ref/smac2d.f (L1570)

`fdate` now produce the same result on flang, compare to gfortran, where
If the length is too short to fit completely, blank return.
```fortran
  character(20) :: string
  call fdate(string)
  write(*, *) string, "X"
```
```bash
$ ../build-release/bin/flang-new test.f90 
$ ./a.out 
                      X
```
If length if larger than it requires(24), fill the rest of buffer space.
```fortran
  character(30) :: string
  call fdate(string)
  write(*, *) string, "X"
```
```bash
$ ../build-release/bin/flang-new test.f90 
$ ./a.out 
 Wed Nov 15 16:59:13 2023      X
```
The length value is hardcoded, because:
```c++
  // Day Mon dd hh:mm:ss yyyy\n\0 is 26 characters, e.g.
  // Tue May 26 21:51:03 2015\n\0
```
---------

Co-authored-by: Yi Wu <yiwu02@wdev-yiwu02.arm.com>
2024-01-11 12:15:48 +00:00
Leandro Lupori
7775375003 [flang][doc] Correct spelling of CMake 2024-01-10 09:28:11 -03:00
Leandro Lupori
205aa3fb89
[flang] Document DEFAULT_SYSROOT usage on Darwin (#77353) 2024-01-10 09:15:33 -03:00
Yi Wu
e2b896aa64
[flang] Add EXECUTE_COMMAND_LINE runtime and lowering intrinsics implementation (#74077)
This patch add support of intrinsics Fortran 2008 EXECUTE_COMMAND_LINE.
The patch contains both the lowering and the runtime code and works on
both Windows and Linux. The patch contains a list of commits, to convey
the authorship and the history of changes. Some implementation specifics
or status has been added to `flang/docs/Intrinsics.md`.

I have provided a summary of the usage and the options required for the
`EXECUTE_COMMAND_LINE intrinsic`. The intrinsic supports both a
synchronous
(by default) and an asynchronous option.

| System  | Mode  | Implemention              |
|---------|-------|---------------------------|
| Linux   | Sync  | std::system()             |
| Windows | Sync  | std::system()             |
| Linux   | Async | fork()  |
| Windows | Async | CreateProcess             |

Support for the SYSTEM GNU extension will be added in a separate PR.

Co-authored with @jeffhammond

---------

Signed-off-by: Jeff Hammond <jeff.science@gmail.com>
Co-authored-by: Jeff Hammond <jeff.science@gmail.com>
Co-authored-by: Yi Wu <yiwu02@wdev-yiwu02.arm.com>
2024-01-10 10:02:48 +00:00
Peter Klausler
cab156c412
[flang][runtime] Don't round hexadecimal floating-point input (#76586)
Fortran 2023 subclause 13.7.2.3.8 discusses input rounding only in the
context of decimal-to-binary conversion. There is no mention of rounding
for hexadecimal floating-point input conversion. At least one Fortran
compiler seems to have interpreted this silence as implying no rounding.
(Note that this is not the same thing as rounding to zero (RZ), which
would return +/-HUGE() for overflow.)
2024-01-02 09:32:54 -08:00
Peter Klausler
dea30aca3a
[flang][runtime] NAMELIST input into storage sequence (#76584)
Nearly every Fortran compiler supports the extension of NAMELIST input
into a storage sequence identified by its initial scalar array element.
For example,
  &GROUP A(1) = 1. 2. 3. /
should be processed as if the input had been
  &GROUP A(1:) = 1. 2. 3. /

Fixes llvm-test-suite/Fortran/gfortran/regression/namelist_24.f90.
2024-01-02 09:25:49 -08:00
Peter Klausler
120ad2508a
[flang][runtime] Extension: NAMELIST input may omit terminal '/' (#76476)
... when it is followed eventually by the '&' that begins the next
NAMELIST input group. This is a gfortran extension.
2024-01-02 08:42:10 -08:00
Peter Klausler
5a402c5622
[flang] USE-associated explicit INTRINSIC names (#76199)
The compiler doesn't USE-associate names of intrinsic procedures from
modules (in the absence of ONLY:), so that the associating scope doesn't
get populated with names of intrinsics that were used only in
declarations (e.g., SELECTED_REAL_KIND). A recent bug report (below)
shows that we should modify that policy in the case of names that appear
in explicit INTRINSIC attribute statements. The behaviors of other
Fortran compilers are not consistent and the requirements of the
standard are not clear; this fix follows the precedent set by gfortran
and nvfortran.

Fixes https://github.com/llvm/llvm-project/issues/72084.
2023-12-26 16:03:03 -08:00
Michael Klemm
9d6837d595
[flang][driver] Remove Fortain_main static library from linking stages (#75816)
At present, when building static or shared libraries, Flang adds
`-lFortran_main.a` (or `/WHOLEARCHIVE:Fortran.*.lib` pon Windows) to the
link line. This leads to the problem that `_QQmain` and
`_QQEnvironmentDefaults` (as of the time of this PR) are symbols marked
as used, while `main` is being defined. This should not happen and this
PR fixes this by detecting if `-shared` or `-static` is used on the
Flang command line and removing the static `Fortran_main` library.

---------

Co-authored-by: kkwli <kkwli@users.noreply.github.com>
2023-12-25 19:15:00 +01:00
Yi Wu
18af032c0e
[flang] add GETLOG runtime and extension implementation: get login username (#74628)
Get login username, ussage:
```
CHARACTER(32) :: login
CALL getlog(login)
WRITE(*,*) login
```
getlog is required for an exascale proxyapp.
https://proxyapps.exascaleproject.org/app/minismac2d/

f904467142/ref/smac2d.f (L615)

f904467142/ref/smac2d.f (L1570)

---------

Co-authored-by: Yi Wu <43659785+PAX-12-WU@users.noreply.github.com>
Co-authored-by: Yi Wu <yiwu02@wdev-yiwu02.arm.com>
Co-authored-by: Kiran Chandramohan <kiranchandramohan@gmail.com>
2023-12-21 10:35:28 +00:00
Slava Zakharin
002c54a2e6
[flang][openacc] Representing Fortran descriptor management with OpenACC. (#72725)
This document describes the aspects of Fortran descriptor management
in the offload data environment and how this semantics can be
represented using MLIR OpenACC dialect.

In the document I tried to clarify some parts of the OpenACC
specification that seemed unclear to me, e.g. where the spec allowed different
interpretations (in my opinion) or did not specify the actual runtime
behavior. I tried to demonstrate how this ambiguity can affect user
programs with examples.

The document proposes using `attachRecipe` on the data operations
to represent the non-trivial pointer attachment semantics for variables
represented by descriptors. The recipe provides the actual
implementation of the pointer attachment, e.g. we can call an F18 offload runtime
function that will perform all necessary checks and actions.

It is unclear at this point if we want to expose the implementation
details in FIR, otherwise, the recipe could have contained more low
level acc operations such as is_present lookups, host to device
memory copies, etc.
2023-11-29 11:42:29 -08:00
Slava Zakharin
0d77978e75
[flang][doc] Added remark about array element references in data clauses. (#72332) 2023-11-20 18:01:38 -08:00
jeanPerier
915f6c3d6a
[flang][RFC] Adding a design document for assumed-rank objects (#71959)
This patch adds a document describing assumed-rank objects and the
related features as well as how they will be implemented in Flang.
2023-11-17 11:10:15 +01:00
Kiran Chandramohan
aa54851502 [Flang][Docs] NFC: Change cross-reference to href link
This change avoids the following warning:
'myst' cross-reference target not found: 'standards' [myst.xref_missing]
2023-11-15 17:04:23 +00:00
Kiran Chandramohan
f0fbae5041 [Flang][Docs] NFC:Minor fix to section reference 2023-11-15 16:19:59 +00:00
Kiran Chandramohan
6d858e2ad3
[Flang][Doc] Trivial fixes for PDT doc (#72306)
Adjust the depth (number of #) for some sections.
Move an underscore prefix to a suffix in an example to let the Fortran
parser in the documentation generator pass.
2023-11-15 13:09:35 +00:00
Kiran Chandramohan
73f1753a95
[Flang][Doc] Fix trivial issues in HighLevelFIR.md (#72278)
Fixes section depth and removes annotation of source as HLFIR since this
is not currently understood by the lexer. Fixing these issues help
reduce warnings in the documentation CI for LLVM.
2023-11-15 09:54:15 +00:00
Kiran Chandramohan
3f743fd3a3
[Flang][Docs] Fix lexer issue in Driver and Trampoline doc (#72322)
Allowed lexers are documented in https://pygments.org/docs/lexers/.
2023-11-15 04:53:42 +00:00
Kiran Chandramohan
d7b8ea63b3
[Flang][Docs] Fix GettingStarted.md with correct depth for in-tree build section (#72268)
Just a minor fix for the section depth for the patch in
https://reviews.llvm.org/D155029.
2023-11-15 04:53:21 +00:00
Kiran Chandramohan
d32df98954
[Flang][Docs] Add or exclude documents to the toc (#72302)
This fixes all the warnings of the following type in the CI
(https://lab.llvm.org/buildbot/#/builders/89/builds/50248/steps/5/logs/stdio)
-> WARNING: document isn't included in any toctree.
2023-11-14 22:42:26 +00:00
Kiran Chandramohan
70150d5d06
[Flang][Doc] Correct section depth in IntrinsicTypes.md (#72271)
Fixes flang documentation CI build.
2023-11-14 22:41:36 +00:00
Kiran Chandramohan
1d60d54a88
[Flang][Doc] Add a FIROperations section (#72252)
Adding this section ensures that the documentation generated by
mlir-tblgen for all the FIR operations are at the correct depth.
FIROperations are at depth 3, the new section is at depth 2. This fixes
the "Non-consecutive header level increase; H1 to H3" warning in
FIRLangRef.md.
2023-11-14 22:41:20 +00:00
Peter Klausler
8b5eacb4a6
[flang][NFC] Document an extension (#71609)
Fortran free form line continuation with '&' works with this compiler
even across the end of an included source file, as it does with most
other Fortran compilers. This extension should be documented.
2023-11-13 15:50:49 -08:00
Peter Klausler
36ad3431bb
[flang] Don't accept NULL() actual for assumed-rank dummy (#71574)
A NULL() pointer without MOLD= cannot be allowed to be associated with
an assumed-rank dummy argument, as its rank is not well-defined and
neither the RANK() intrinsic function or the SELECT RANK construct will
work in the callee.
2023-11-13 15:14:05 -08:00
Peter Klausler
3de9aa6b36
[flang] Catch READ(... SIZE=) with NML= or FMT=* (#71235)
The SIZE= specifier may not appear on a list-directed or namelist READ
statement.
2023-11-13 14:20:43 -08:00
Yi Wu
de58aa8372
[flang] GETPID runtime and lower intrinsic implementation (#70442)
Runtime function GetPID calls the function getpid
from unistd.h or process.h base on the OS.
2023-11-13 10:31:36 +00:00
Fabian Mora
fd389f46de
[flang] Change uniqueCGIdent separator from . to X (#71338)
Change the separator in the `uniqueCGIdent` method to `X`. This change
is required to enable OpenMP offloading for the NVPTX target, as dots
are not valid identifiers in PTX and `uniqueCGIdent` is used to mangle
some literals. Follow up patches will change the remainder of `.`
appearances in names to `X` and add support for the NVPTX target.
2023-11-08 15:04:00 -05:00
Peter Klausler
9191738764
[flang] Accept CONTIGUOUS attribute when redundant (#70853)
As an extension, accept the redundant use of the CONTIGUOUS attribute
when applied to scalars and to simply contiguous objects, with a
portability warning.
2023-10-31 16:04:35 -07:00
Peter Klausler
f706411f71
[flang][preprocessor] Handle compiler directives with continuations a… (#70128)
…fter macro expansion

When compiler directives (!$omp) and/or their continuations (!$omp &)
are produced by macro expansion, handle those continuations. Also allow
a continuation marker (&) to appear in a macro actual argument.
2023-10-31 11:38:25 -07:00
Peter Klausler
1c3445052b
[flang] Accept distinct kinds of arguments to IAND/IEOR/IOR (#69782)
As is already supported as a common extension for intrinsic functions
like DIM, allow distinct kinds of integer actual arguments to the
MIL-STD bit intrinsic functions IAND, IEOR, and IOR, with the kind of
the result being the largest of the kinds of the operands. (Though one
could make a case that IAND should return the smallest kind of its
operands, that's not what other compilers do.)
2023-10-31 11:03:02 -07:00
Valentin Clement (バレンタイン クレメン)
72d8e47a2c
[flang][openacc] Allow scalar in acc cache directive (#70713) 2023-10-31 08:54:33 -07:00
Valentin Clement (バレンタイン クレメン)
828674395b
[flang][openacc] Allow acc routine at the top level (#69936)
Some compilers allow the `$acc routine(<name>)` to be placed at the
program unit level. To be compatible, this patch enables the use of acc
routine at this level. These acc routine directives must have a name.
2023-10-24 09:17:48 -07:00
Slava Zakharin
e5944c980b
[RFC][flang] Trampolines for internal procedures. (#66157)
I would like to start a discussion about the ways for modifying
the current trampolines approach for Fortran internal procedures
used as actual arguments or pointer targets.

As Peter Klausler noted before the current approach implies security
risks due to writeable and executable stack requirement. We may need
to agree on a new scheme that does not have this issue.
2023-10-23 14:07:18 -07:00
Aiden Fox Ivey
7df9b0c5b3
Fix typos and formatting in GettingStarted.md (#68537)
Not too much to change here. Just found some issues with the flang
GettingStarted.md file and wanted to sort it out.
2023-10-21 10:48:33 +05:30
Valentin Clement (バレンタイン クレメン)
cf670d5e56
[flang][openacc] Accept scalar integer expression in the if clause (#69381)
Relax the parser to accept scalar integer expression in addition to
scalar logical expression. The parser now accepts scalar expression and
the semantic checks its type.
2023-10-17 14:41:03 -07:00
Peter Klausler
39f4ec5854
[flang] Catch a dangerous ambiguity in standard Fortran (#67483)
Fortran allows forward references to type names, which can lead to
ambiguity when coupled with host association, as in:

  module m
    type ambiguous; integer n; end type
   contains
    subroutine s
      type(ambiguous), pointer :: variable
      type t
        type(ambiguous), pointer :: component
      end type
      type ambiguous; real x; end type
    end
  end

Some other compilers resolve to a host association, some resolve to a
forward reference. This compiler will now emit an error.
2023-10-16 15:40:13 -07:00
Corentin Jabot
c226d6c88f [Documentation][NFC] Remove invalid language specifiers in markdown code blocks 2023-10-07 16:05:56 +02:00
Pete Steinfeld
d0663005a8
[flang] Update instructions for a standalone flang build (#68361)
I added instructions for setting the environment variable ROOT_DIR.
2023-10-06 06:49:48 -07:00