392 Commits

Author SHA1 Message Date
Slava Zakharin
2c91f10362
[flang] Fixed repacking for TARGET and INTENT(OUT) (#131972)
TARGET dummy arrays can be accessed indirectly, so it is unsafe
to repack them.
INTENT(OUT) dummy arrays that require finalization on entry
to their subroutine must be copied-in by `fir.pack_arrays`.

In addition, based on my testing results, I think it will be useful
to document that `LOC` and `IS_CONTIGUOUS` will have different values
for the repacked arrays. I still need to decide where to document
this, so just added a note in the design doc for the time being.
2025-03-19 17:12:32 -07:00
jeanPerier
cd0a2a3f1b
[flang] add QSORT extension intrinsic to the runtime (#132033)
Add support for legacy Fortran intrinsic QSORT from lib 3f.
This is a thin Fortran wrapper over libc qsort.
2025-03-19 16:14:37 +01:00
Slava Zakharin
7d7b58bc5d
[flang-rt] Added ShallowCopy API. (#131702)
This API will be used for copying non-contiguous arrays
into contiguous temporaries to support `-frepack-arrays`.
The builder factory API will be used in the following commits.
2025-03-18 12:58:25 -07:00
Michael Kruse
d06937aea3
[Flang][NFC] Fix typo (#130960)
This was mainly a test of the pre-merge CI, but merging it since it fixes an actual typo.
2025-03-13 17:55:07 +01:00
Slava Zakharin
aa38754117
[RFC][flang] Add support for assumed-shape dummy arrays repacking. (#127147)
This is a document describing why and how to add support for repacking
of assumed-shape dummy arrrays to provide more efficient data cache.
It proposes adding new FIR operations and outlines the compiler flow
handling these operations.
I would like to hear feedback on all of it, but especially on:
  * The possibility of detecting safeness of the repacking
    in the context of OpenACC/OpenMP. If it is not possible
    to do the runtime checks to determine safety, then
    there is not need to add the `TempCopyIsSafe` attributes
    to the instruction.
  * Whether it is possible to preserve the debug information
    in cases where `fir.pack_array` is sunk after `[hl]fir.declare`,
    so that before the `fir.pack_array` a debugger will refer
    to the values in the original array, and after `fir.pack_array`
    it will refer to the copy.
2025-03-10 11:08:37 -07:00
KAWASHIMA Takahiro
0e56f6dc3e
[flang][docs][NFC] Fix Markdown /*comments*/ (#129018)
`*` in `/*comments*/` were interpreted as emphasis marks and were not
displayed in https://flang.llvm.org/docs/Extensions.html.
2025-02-28 10:18:37 +09:00
vdonaldson
62d4cc811a
[flang] Modifications to ieee_support_standard (#128895)
Some Arm processors support exception halting control and some do not.
An Arm executable will run on either type of processor, so it is
effectively unknown at compile time whether or not this support will be
available. ieee_support_halting is therefore implemented with a runtime
check.

The result of a call to ieee_support_standard depends in part on support
for halting control. Update the ieee_support_standard implementation to
check for support for halting control at runtime.
2025-02-27 14:57:10 -05:00
vdonaldson
0b52aa1bdb
[flang] Unsupported rounding modes (#128240)
Two new ieee_round_type values were added in f18 beyond the four values
defined in f03 and f08: ieee_away and ieee_other. Contemporary hardware
typically does not have support for these rounding modes, so flang does
not support them. ieee_support_rounding calls for these values return
false. Current generated code handles some attempts to set the rounding
mode to one of these unsupported values by setting the mode to
ieee_nearest. Update the code to explicitly do this in all cases.
2025-02-24 10:50:03 -05:00
Jean-Didier PAILLEUX
d6c6bde9db
[flang] Implement !DIR$ UNROLL_AND_JAM [N] (#125046)
This patch implements support for the UNROLL_AND_JAM directive to enable
or disable unrolling and jamming on a `DO LOOP`.
It must be placed immediately before a `DO LOOP` and applies only to the
loop that follows. N is an integer that specifying the unrolling factor.
This is done by adding an attribute to the branch into the loop in LLVM
to indicate that the loop should unrolled and jammed.
2025-02-19 15:00:09 +00:00
Michael Kruse
b55f7512a7
[Flang] LLVM_ENABLE_RUNTIMES=flang-rt (#110217)
Extract Flang's runtime library to use the LLVM_ENABLE_RUNTIME
mechanism. It will only become active when
`LLVM_ENABLE_RUNTIMES=flang-rt` is used, which also changes the
`FLANG_INCLUDE_RUNTIME` to `OFF` so the old runtime build rules do not
conflict. This also means that unless `LLVM_ENABLE_RUNTIMES=flang-rt` is
passed, nothing changes with the current build process.

Motivation:
* Consistency with LLVM's other runtime libraries (compiler-rt, libc,
libcxx, openmp offload, ...)
* Allows compiling the runtime for multiple targets at once using the
LLVM_RUNTIME_TARGETS configuration options
* Installs the runtime into the compiler's per-target resource directory
so it can be automatically found even when cross-compiling

Also see RFC discussion at
https://discourse.llvm.org/t/rfc-use-llvm-enable-runtimes-for-flangs-runtime/80826
2025-02-16 15:39:52 +01:00
Asher Mancinelli
6b52fb25b9
[flang] Correctly handle !dir$ unroll with unrolling factors of 0 and 1 (#126170)
https://github.com/llvm/llvm-project/pull/123331 added support for the
unrolling directive. In the presence of an explicit unrolling factor,
that unrolling factor would be unconditionally passed into the metadata
even when it was 1 or 0. These special cases should instead disable
unrolling. Adding an explicit unrolling factor of 0 triggered this
assertion which is fixed by this patch:

```
unsigned int unrollCountPragmaValue(const llvm::Loop*):
Assertion `Count >= 1 && "Unroll count must be positive."' failed.
```

Updated tests and documentation.
2025-02-10 08:21:22 -08:00
Michael Kruse
5c8c2b3db5
[Flang] Rename libFortranRuntime.a to libflang_rt.runtime.a (#122341)
Following the conclusion of the
[RFC](https://discourse.llvm.org/t/rfc-names-for-flang-rt-libraries/84321),
rename Flang's runtime libraries as follows:

 * libFortranRuntime.(a|so) to libflang_rt.runtime.(a|so)
 * libFortranFloat128Math.a to libflang_rt.quadmath.a
* libCufRuntime_cuda_${CUDAToolkit_VERSION_MAJOR}.(a|so) to
libflang_rt.cuda_${CUDAToolkit_VERSION_MAJOR}.(a|so)

This follows the same naming scheme as Compiler-RT libraries
(`libclang_rt.${component}.(a|so)`). It provides some consistency
between Flang's runtime libraries for current and potential future
library components.
2025-02-08 18:02:54 +01:00
Michael Kruse
98e118ca43
[Flang] Don't use FortranDecimal for runtime (#121997)
Avoid using the same library for runtime and compiler. `FortranDecimal`
was used in two ways:

1. As an auxiliary library needed for `libFortranRuntime.a`. This patch
adds the two source files of FortranDecimal directly into
FortranRuntime, so `FortranRuntime` is not used anymore.
 
2. As a library used by the Flang compiler. As the only remaining use of
the library, extra CMake code to make it compatible with the runtime can
be removed.

Before this PR, `enable_cuda_compilation` is applied to `FortranDecimal`
which causes everything that links to it, including flang (the
compiler), to depend on libcudart when CUDA support is enabled.

Having two runtime library just makes everything more complicated while
the user ideally should not be concerned with how the runtime is
structured internally. Some logic was copied for FortranDecimal because
of this, such as the ability to be compiled out-of tree
(b75a3c9f31c1ffdc9856aee32991d8129b372ee7) which is undocumented, the
logic to link against the various versions of Microsofts runtime library
(#70833), and avoiding dependency on the C++ runtime
(7783bba22c7add678d796741d30669c73159b3d8).
2025-02-07 11:43:31 +01:00
vdonaldson
e73a64bbd1
[flang][NFC] Document Arm exception raising behavior (#125579) 2025-02-04 09:54:33 -05:00
Peter Klausler
8d8a821b78
[flang] Support OPEN(..., FORM="BINARY") (#124657)
... as a legacy spelling for OPEN(..., FORM="UNFORMATTED",
ACCESS="STREAM").
2025-01-31 10:51:16 -08:00
Sirraide
c4a019747c
[Clang] Remove ARCMigrate (#119269)
In the discussion around #116792, @rjmccall mentioned that ARCMigrate
has been obsoleted and that we could go ahead and remove it from Clang,
so this patch does just that.
2025-01-30 05:32:25 +01:00
Jean-Didier PAILLEUX
ecc71de53f
[flang] Implement IERRNO intrinsic (#124281)
Add the implementation of the IERRNO intrinsic to get the last system
error number, as given by the C errno variable.
This intrinsic is also used in RAMSES
(https://github.com/ramses-organisation/ramses/).
2025-01-29 12:01:07 +01:00
Jean-Didier PAILLEUX
5a34e6fdce
[flang] Implement CHDIR intrinsic (#124280)
This intrinsic is a gnu extension
(https://gcc.gnu.org/onlinedocs/gfortran/CHDIR.html) and is used in
FLEUR (https://github.com/JuDFTteam/FLEUR).
2025-01-29 09:44:58 +01:00
Peter Klausler
038b42ba5b
[flang] Safer hermetic module file reading (#121002)
When a hermetic module file is read, use a new scope to hold its
dependent modules so that they don't conflict with any modules in the
global scope.
2025-01-27 08:43:41 -08:00
Valentin Clement (バレンタイン クレメン)
12ba74e181
[flang] Do not produce result for void runtime call (#123155)
Runtime function call to a void function are producing a ssa value
because the FunctionType result is set to NoneType with is later
translated to a empty struct. This is not an issue when going to LLVM IR
but it breaks when lowering a gpu module to PTX. This patch update the
RTModel to correctly set the FunctionType result type to nothing.

This is one runtime call before this patch at the LLVM IR dialect step.
```
%45 = llvm.call @_FortranAAssign(%arg0, %1, %44, %4) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i32) -> !llvm.struct<()>
```

After the patch the call would be correctly formed
```
llvm.call @_FortranAAssign(%arg0, %1, %44, %4) : (!llvm.ptr, !llvm.ptr, !llvm.ptr, i32) -> ()
```

Without the patch it would lead to error like:
```
ptxas /tmp/mlir-cuda_device_mod-nvptx64-nvidia-cuda-sm_60-e804b6.ptx, line 10; error   : Output parameter cannot be an incomplete array.
ptxas /tmp/mlir-cuda_device_mod-nvptx64-nvidia-cuda-sm_60-e804b6.ptx, line 125; error   : Call has wrong number of parameters
```

The change is pretty much mechanical.
2025-01-16 12:34:38 -08:00
Krzysztof Parzyszek
70e96dc3fb
[flang][OpenMP] Parsing context selectors for METADIRECTIVE (#121815)
This is just adding parsers for context selectors. There are no tests
because there is no way to execute these parsers yet.
2025-01-10 11:05:23 -06:00
Peter Klausler
eb77f442b3
[flang] Accept L0 (#121998)
Accept a zero field width for formatted logical output (L0),
interpreting it as if it had been L1.
2025-01-08 13:15:51 -08:00
jeanPerier
44e8ee7359
[flang][doc] refine zero initialization extension documentation (#121956)
Following-up on [comments
](https://github.com/llvm/llvm-project/issues/62432#issuecomment-2555316522)
in the issue that motivated this extension.
2025-01-08 10:56:03 +01:00
vdonaldson
c28a7c1efd
[flang] Modifications to ieee_support_halting (#120747)
The F23 standard requires that a call to intrinsic module procedure
ieee_support_halting be foldable to a constant at compile time in some
contexts. See for example F23 Clause 10.1.11 [Specification expression]
list item (13), Clause 1.1.12 [Constant expression] list item (11), and
references to specification and constant expressions elsewhere, such as
constraints C1012, C853, and C704.

Some Arm processors allow a user to control processor behavior when an
arithmetic exception is signaled, and some Arm processors do not have
this capability. An Arm executable will run on either type of processor,
so it is effectively unknown at compile time whether or not this support
will be available at runtime. This in conflict with the standard
requirement.

This patch addresses this conflict by implementing ieee_support_halting
calls on Arm processors to check if this capability is present at
runtime. A call to ieee_support_halting in a constant context, such as
in the specification part of a program unit, will generate a compile
time "cannot be computed as a constant value" error. The expectation is
that such calls are unlikely to appear in production code.

Code generation for other processors will continue to generate a compile
time constant result for ieee_support_halting calls.
2024-12-23 09:30:45 -05:00
Peter Klausler
fc97d2e68b
[flang] Add UNSIGNED (#113504)
Implement the UNSIGNED extension type and operations under control of a
language feature flag (-funsigned).

This is nearly identical to the UNSIGNED feature that has been available
in Sun Fortran for years, and now implemented in GNU Fortran for
gfortran 15, and proposed for ISO standardization in J3/24-116.txt.

See the new documentation for details; but in short, this is C's
unsigned type, with guaranteed modular arithmetic for +, -, and *, and
the related transformational intrinsic functions SUM & al.
2024-12-18 07:02:37 -08:00
执着
e8baa792e7
Backtrace support for flang (#118179)
Fixed build failures in old PRs due to missing files
2024-12-10 10:31:48 +00:00
Peter Klausler
8115454aa0
[flang] Extension: accept "var*length(bounds)" (#117399)
A character length specifier in an entity declaration or a component
declaration is required by the standard to follow any array bounds or
coarray bounds that are present. Several Fortran compilers allow the
character length specifier to follow the name and appear before the
bounds.

Fixes https://github.com/llvm/llvm-project/issues/117372.
2024-12-02 12:25:47 -08:00
Tom Eccles
ad7bb652d6
[flang] Implement non-standard LNBLNK intrinsic (#117589)
This is defined here
https://gcc.gnu.org/onlinedocs/gfortran/LNBLNK.html. It is just an alias
to LEN_TRIM.

This was requested by a user:

https://discourse.llvm.org/t/unresolved-externals-with-appendend-underscore/83305
2024-11-26 10:18:34 +00:00
Peter Klausler
be6bc6a1e5
[flang] Better recovery from errors in a loop control (#117025)
When there's an error in a DO statement loop control, error recovery
isn't great. A bare "DO" is a valid statement, so a failure to parse its
loop control doesn't fail on the whole statement. Its partial parse ends
after the keyword, and as some other statement parsers can get further
into the input before failing, errors in the loop control can lead to
confusing error messages about bad pointer assignment statements and
others. So just check that a bare "DO" is followed by the end of the
statement.
2024-11-21 10:47:42 -08:00
Jay Foad
922992a22f
Fix typo "instrinsic" (#112899) 2024-10-18 15:58:33 +01:00
David Spickett
ab208de34e
[flang][docs] Update description of how to contribute (#112369)
It's my understanding that all code review pre-commit takes place on
GitHub Pull Requests and that post-commit review is done either on the
closed PR or the commit on GitHub.
2024-10-17 16:30:28 +01:00
Peter Klausler
a70ffe784d
[flang] Support "PRINT namelistname" (#112024)
Nearly every Fortran compiler supports "PRINT namelistname" as a synonym
for "WRITE (*, NML=namelistname)". Implement this extension via parse
tree rewriting.

Fixes https://github.com/llvm/llvm-project/issues/111738.
2024-10-15 14:22:22 -07:00
Alexis Perry-Holby
86f78c0093
[flang] Add a link to the ICS file for the Biweekly Flang Community Call 2024-10-10 14:21:21 -06:00
Brad Richardson
06eb10dadf
[flang][driver] rename flang-new to flang (#110023)
This does a global rename from `flang-new` to `flang`. I also
removed/changed any TODOs that I found related to making this change.

---------

Co-authored-by: H. Vetinari <h.vetinari@gmx.com>
Co-authored-by: Andrzej Warzynski <andrzej.warzynski@arm.com>
2024-10-10 09:26:04 +01:00
Yusuke MINATO
b91a25ef58
[flang] add nsw to operations in subscripts (#110060)
This patch adds nsw to operations when lowering subscripts.

See also the discussion in the following discourse post.
https://discourse.llvm.org/t/rfc-add-nsw-flags-to-arithmetic-integer-operations-using-the-option-fno-wrapv/77584/9
2024-10-03 10:56:01 +09:00
David Truby
856c38d542
[flang] Implement GETUID and GETGID intrinsics (#110679)
GETUID and GETGID are non-standard intrinsics supported by a number of
other Fortran compilers. On supported platforms these intrinsics simply
call the POSIX getuid() and getgid() functions and return the result.
The only platform we support that does not have these is Windows.

Windows does not have the same concept of UIDs and GIDs, so on Windows
we issue a warning indicating this and return 1 from both functions.

Co-authored-by: Yi Wu <yi.wu2@arm.com>
2024-10-02 13:26:40 +01:00
David Truby
78ccffc053
[flang] Add MALLOC and FREE intrinsics for Cray pointers (#110018)
MALLOC and FREE are extensions provided by gfortran, Intel Fortran and
classic flang to allocate memory for Cray pointers. These are used in
some legacy codes such as libexodus.

All the above compilers accept using MALLOC and FREE with integers as
well, despite that this will often signify a bug in user code. We should
accept the same as the other compilers for compatibility.
2024-09-30 22:40:16 +01:00
David Truby
7a0a7947ee
Revert "[flang] Implement GETUID and GETGID intrinsics" (#110531)
Reverts llvm/llvm-project#108017
2024-09-30 17:35:27 +01:00
David Truby
054eadcb11
[flang] Implement GETUID and GETGID intrinsics (#108017)
GETUID and GETGID are non-standard intrinsics supported by a number of
other Fortran compilers. On supported platforms these intrinsics simply
call the POSIX getuid() and getgid() functions and return the result.
The only platform we support that does not have these is Windows.

Windows does not have the same concept of UIDs and GIDs, so on Windows
we issue a warning indicating this and return 1 from both functions.

Co-authored-by: Yi Wu <yi.wu2@arm.com>

---------

Co-authored-by: Yi Wu <yi.wu2@arm.com>
2024-09-30 14:36:39 +01:00
Tarun Prabhu
c3201ddaea
[flang][NFC] Refactor to remove .inc file containing shared code (#109874)
Remove flang/include/flang/Tools/CLOptions.inc - which was included as
is in - several places. Move the code in it to header and source files
which are used used in the "standard" way. Some minor cleanup such as
removing trailing whitespace and excessive newlines and reordering
entries alphabetically for files that were modified along the way.
Update the documentation that referenced CLOptions.inc.
2024-09-25 14:04:38 -06:00
Peter Klausler
26ac30bcec
[flang] Accept initialized SAVE local in specification expression (#107656)
Specification expressions may contain references to dummy arguments,
host objects, module variables, and variables in COMMON blocks, since
they will have values on entry to the scope. A local variable with a
initializer and the SAVE attribute (which will always be implied by an
explicit initialization) will also always work, and is accepted by at
least one other compiler, so accept it with a warning.
2024-09-10 14:13:09 -07:00
Peter Klausler
5a229dbca1
[flang] Relax error into a warning (#107489)
The standard requires that a generic interface with the same name as a
derived type contain only functions. We generally allow a generic
interface to contain both functions and subroutines, since there's never
any ambiguity at the point of call; these is helpful when the specific
procedures of two generics are combined during USE association. Emit a
warning instead of a hard error when a generic interface with the same
name as a derived type contains a subroutine to improve portability of
code from compilers that don't check for this condition.
2024-09-10 14:10:40 -07:00
Ivan R. Ivanov
65f66d2c60 [flang][NFC] Move OpenMP related passes into a separate directory (#104732)
Reapplied with fixed library dependencies for shared lib build
2024-08-22 11:23:21 +09:00
Ivan Radanov Ivanov
bccb227093 Revert "[flang][NFC] Move OpenMP related passes into a separate directory (#104732)"
This reverts commit 87eeed1f0ebe57abffde560c25dd9829dc6038f3.
2024-08-21 23:16:52 +09:00
Ivan R. Ivanov
87eeed1f0e
[flang][NFC] Move OpenMP related passes into a separate directory (#104732) 2024-08-21 22:59:11 +09:00
Peter Klausler
e83c5b25f3
[flang] Warn about automatic data in main program, disallow in BLOCK … (#102045)
…DATA

We allow automatic data objects in the specification part of the main
program; add an optional portability warning and documentation. Don't
allow them in BLOCK DATA. They're already disallowed as module
variables.
2024-08-08 11:06:32 -07:00
Carlos Seo
c6e69b041a
[Flang][Docs] Update information about AArch64 trampolines (#100391)
Commits c4b66bf and 7647174 add support for AArch64 trampolines. Updated
documentation to reflect the changes.
2024-07-24 18:14:05 -03:00
Peter Klausler
40ed6ba016
[flang][NFC] Document an intentional violation of the standard (#99073)
The Fortran standard committees passed an "interp" request at their June
2024 meetings that is distinct from nearly every other Fortran compiler
that I tried (6) in an an ambiguous case (parent component naming when
the base type has been renamed via USE association). Document this case
in flang/docs/Extensions.md as an intentional instance of
non-conformance chosen for portability and better usability.
2024-07-18 15:59:24 -07:00
Tom Eccles
60ec6868ea
[flang] Implement SECOND intrinsic (#98881)
The SECOND intrinsic is a gnu extension providing an alias for CPU_TIME:
https://gcc.gnu.org/onlinedocs/gfortran/SECOND.html

This cannot be implemented as a straightforward alias because there is
both a function and a subroutine form.
2024-07-16 11:29:36 +01:00
Michael Klemm
a5a29a26aa
[Flang] Implement RENAME intrinsic (code-gen + runtime entry point) (#98359)
This PR implements the RENAME intrinsic, which is a GFortran extension
(see
https://gcc.gnu.org/onlinedocs/gfortran/intrinsic-procedures/rename.html).
2024-07-15 08:39:19 +02:00