9737 Commits

Author SHA1 Message Date
Michael Kruse
02fa340711
[Flang] Promote FortranEvaluateTesting library (#124417)
The non-GTest library will be shared by unittests of Flang and Flang-RT.
Promote it as a regular library for use by both projects.

In the long term, we may want to convert these to regular GTest checks
to avoid having multiple testing frameworks.
2025-02-06 21:45:51 +01:00
Renaud Kauffmann
6dc41a6393
[flang][NFC] Moving alias analysis utilities utilities together. Adding new utility. (#125925)
1. Our static functions are a bit spread out in this file. I am
gathering them in an anonymous namespace
2. Moving the code to get the `target` attribute on a `fir.global` into
its own utility.
2025-02-06 12:18:51 -08:00
Kareem Ergawy
dcb124e820
[flang][OpenMP] Enable delayed privatization by default omp.wsloop (#125732)
Reapplies #122471

This is based on https://github.com/llvm/llvm-project/pull/125699, only
the latest commit is relevant.

With changes in this PR and the parent one, the previously reported
failures in the Fujitsu(*) test suite should hopefully be resolved (I
verified all the 14 reported failures and they pass now).

(*) https://linaro.atlassian.net/browse/LLVM-1521
2025-02-06 19:11:04 +01:00
Renaud Kauffmann
8d5f280559
[flang] Adding a couple of tests to the alias analysis (#125917)
To establish a baseline for new tests mentioned in
https://github.com/llvm/llvm-project/pull/117785, adding them here
independently.
2025-02-06 09:55:49 -08:00
Michael Kruse
b815a3942a
[Flang] Move non-common headers to FortranSupport (#124416)
Move non-common files from FortranCommon to FortranSupport (analogous to
LLVMSupport) such that

* declarations and definitions that are only used by the Flang compiler,
but not by the runtime, are moved to FortranSupport

* declarations and definitions that are used by both ("common"), the
compiler and the runtime, remain in FortranCommon

* generic STL-like/ADT/utility classes and algorithms remain in
FortranCommon

This allows a for cleaner separation between compiler and runtime
components, which are compiled differently. For instance, runtime
sources must not use STL's `<optional>` which causes problems with CUDA
support. Instead, the surrogate header `flang/Common/optional.h` must be
used. This PR fixes this for `fast-int-sel.h`.

Declarations in include/Runtime are also used by both, but are
header-only. `ISO_Fortran_binding_wrapper.h`, a header used by compiler
and runtime, is also moved into FortranCommon.
2025-02-06 15:29:10 +01:00
Tom Eccles
d5c60724be
[flang][Lower][OpenMP] try to avoid using init mold argument (#125900)
Unfortunately we still have a lot of cases like
!fir.box<!fir.array<10xi32>> where we read dimensions from the mold in
case there are non-default lower bounds stored inside the box. I will
address this in the next patch.
2025-02-06 14:27:41 +00:00
Nikita Popov
7c695e4906
[flang] Use clang_target_link_libraries() for clang dependency (#126037)
This dependency is part of libclang-cpp, so it should use
clang_target_link_libraries.
2025-02-06 14:11:22 +01:00
Kareem Ergawy
84c3b05e5e
[OpenMP][flang][MLIR] Decouple alloc, init, and copy regions for omp.private|declare_reduction ops (#125699)
This PR changes the emitted block structure of alloc, init, and copy
regions for `omp.private` and `omp.declare_reduction` ops a little bit.
In particular, this decouples init and copy regions from the alloca
insertion-point. The main motivation is fix "Instruction does not
dominate all uses!" errors that happen specially when an init region
uses a value from the OpenMP region it is being inlined into. The issue
happens because, previous to this PR, we inline the init region right
after the latest alloc block (since we used the alloca IP); which in
some cases (see exmaple below), is too early and causes the use
dominance issue.

Example that would break without this PR (when delayed privatization is
enabled for `omp.wsloop`s):
```fortran
subroutine test2 (xyz)
  integer :: i
  integer :: xyz(:)

  !$omp target map(from:xyz)
    !$omp do private(xyz)
      do i = 1, 10
        xyz(i) = i
      end do
  !$omp end target
end subroutine
```
2025-02-06 11:45:40 +01:00
Tom Eccles
39be2d0266
[flang][OpenMP][Semantics] Don't allow reduction of derived type components (#125480)
Before this patch, reduction of derived type components crashed the
compiler when trying to create the omp.declare_reduction.

In OpenMP 3.1 the standard says "a list item that appears in a reduction
clause must be a named variable of intrinsic type" (page 106). As I
understand it, a derived type component is not a variable.

OpenMP 4.0 added declare reduction, partly so that users could define
their own reductions on derived types. The above wording was removed
from the standard but derived type components were never explicitly
allowed.

OpenMP 5.0 added "A variable that is part of another variable, with the
exception of array elements, cannot appear in17 a reduction clause".

All standard versions also require the reduction argument to be
"definable", which roughly means that it is a variable. A
derived type component is more like an expression.

Fixes #125445
2025-02-06 10:44:16 +00:00
Tom Eccles
4daf307099
[flang][Lower][OpenMP][NFC] tidy up PrivateReductionUtils (#125867)
First part of a series of patches to improve private/reduction init and
cleanup region generation.

This commit is NFC. I factored out processing for each datatype into its
own method so that it is easier to keep track of what is being handled
where (I found the old gigantic init region generation function
difficult to navigate). The methods all share context in a helper class
to avoid having to pass a very large number of arguments.

I also removed the conflation between the mold argument and the mold
argument after loading. This should make it easier to avoid generating
dead uses of the mold argument in a later non-nfc patch.
2025-02-06 10:25:15 +00:00
Brad Smith
d1de75acea
[flang][Driver] When linking with the Fortran runtime also link with libexecinfo (#125998)
Also link with libexecinfo on FreeBSD, NetBSD, OpenBSD and DragonFly
for the backtrace functions.
2025-02-06 04:36:47 -05:00
Paul Carabas
df1bee03de
[mlir] Add math to LLVM lowering support for missing trigonometric & hyperbolic ops (#125753)
The patch adds support for math -> LLVM dialect lowering for TanOp,
Sinh, Cosh, Tanh
2025-02-05 16:02:29 -08:00
Valentin Clement (バレンタイン クレメン)
69ccb1357f
[flang][cuda] Make argument passed by value for sync functions (#125909)
`syncthreads_and`, `syncthreads_count`, `syncthreads_or`, `synwrap` must
take their argument by value. This patch updates the interfaces and
makes sure these functions can be called inside a cuff kernel as well.
2025-02-05 13:47:53 -08:00
klensy
c491cbfe75
[flang][test] Fix filecheck annotation typos (#92387) 2025-02-05 18:24:47 +00:00
Valentin Clement (バレンタイン クレメン)
d3e2459da8
[flang][cuda][NFC] Add tests for device functions in cuf kernels (#125882) 2025-02-05 09:52:59 -08:00
jeanPerier
925d347c5a
[flang] fix IsSimplyContiguous with expressions (#125708)
IsSymplyContiguous was visiting expressions and returning false on
expressions like `x(::2) + y`, which triggered an assert in lowering
when preparing arguments for copy-in/out.

Update it to return false for everything that is not a variable, except
when provided a flag to treat PARAMETER bases as variables. This flags
is required for internal usages in lowering where lowering needs to now
if the read-only memory is being addressed contiguously or not.

Update call lowering to always copy parameter array section into
contiguous writable memory when passing them. The rational here is that
copy-out generated in nested calls using the dummy arguments will cause
a segfault.
2025-02-05 17:20:35 +01:00
Anchu Rajendran S
ccd92ec4c6
[flang][openmp] Changes for invoking scan Op (#123254) 2025-02-05 06:55:32 -08:00
Nikita Popov
f9af5c145f
[flang][cmake] Fix bcc dependencies (#125822)
The Fortran libraries are not part of MLIR, so they should use
target_link_libraries() rather than mlir_target_link_libraries().

This fixes an issue introduced in
https://github.com/llvm/llvm-project/pull/120966.
2025-02-05 11:58:44 +01:00
Nikita Popov
ee76bdac19
[flang] Move FIRSupport dependency to correct place (#125697)
This library is provided by flang, not MLIR, so it should not be part of
MLIR_LIBS.

Fixes an issue introduced in https://github.com/llvm/llvm-project/pull/120966.
2025-02-05 09:48:23 +01:00
Eugene Epshteyn
642288247d
[flang] Add support for -fimplicit-none-ext option (#125248)
When -fimplicit-none-ext is passed, flang behaves as if "implicit
none(external)" was specified for all relevant constructs in Fortran
source file.

Note: implicit17.f90 was based on implicit07.f90 with `implicit
none(external)` removed and `-fimplicit-none-ext` added.
2025-02-04 20:50:01 -05:00
Renaud Kauffmann
8cc7f747cc
[flang][cuda][NFC] Adding missing tests (#125755)
I thought I had added tests together with
https://github.com/llvm/llvm-project/pull/125276
But there are still in my sandbox. These are the tests that were meant
for this PR.
2025-02-04 15:44:18 -08:00
Valentin Clement (バレンタイン クレメン)
bbc90f899a
[flang][cuda] Relax semanctic check in cuf kernel and openacc compute constructs (#125750)
Previous patch was too restrictive and didn't take into account cuf
kernels and openacc compute constructs as being device context.
2025-02-04 13:10:47 -08:00
Razvan Lupusoru
bd30838422
[flang][acc] Improve acc lowering around fir.box and arrays (#125600)
The current implementation of OpenACC lowering includes explicit
expansion of following cases:
- Creation of `acc.bounds` operations for all arrays, including those
whose dimensions are captured in the type (eg `!fir.array<100xf32>`)
- Expansion of box types by only putting the box's address in the data
clause. The address was extracted with a `fir.box_addr` operation and
the bounds were filled with `fir.box_dims` operation.

However, with the creation of the new type interface `MappableType`, the
idea is that specific type-based semantics can now be used. This also
really simplifies representation in the IR. Consider the following
example:
```
subroutine sub(arr)
  real :: arr(:)
  !$acc enter data copyin(arr)
end subroutine
```

Before the current PR, the relevant acc dialect IR looked like:
```
func.func @_QPsub(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name =
"arr"}) {
  ...
  %1:2 = hlfir.declare %arg0 dummy_scope %0 {uniq_name = "_QFsubEarr"} :
(!fir.box<!fir.array<?xf32>>, !fir.dscope) ->
(!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>)
  %c1 = arith.constant 1 : index
  %c0 = arith.constant 0 : index
  %2:3 = fir.box_dims %1#0, %c0 : (!fir.box<!fir.array<?xf32>>, index)
-> (index, index, index)
  %c0_0 = arith.constant 0 : index
  %3 = arith.subi %2#1, %c1 : index
  %4 = acc.bounds lowerbound(%c0_0 : index) upperbound(%3 : index)
extent(%2#1 : index) stride(%2#2 : index) startIdx(%c1 : index)
{strideInBytes = true}
  %5 = fir.box_addr %1#0 : (!fir.box<!fir.array<?xf32>>) ->
!fir.ref<!fir.array<?xf32>>
  %6 = acc.copyin varPtr(%5 : !fir.ref<!fir.array<?xf32>>) bounds(%4) ->
!fir.ref<!fir.array<?xf32>> {name = "arr", structured = false}
  acc.enter_data dataOperands(%6 : !fir.ref<!fir.array<?xf32>>)
```

After the current change, it looks like:
```
func.func @_QPsub(%arg0: !fir.box<!fir.array<?xf32>> {fir.bindc_name =
"arr"}) {
  ...
  %1:2 = hlfir.declare %arg0 dummy_scope %0 {uniq_name = "_QFsubEarr"} :
(!fir.box<!fir.array<?xf32>>, !fir.dscope) ->
(!fir.box<!fir.array<?xf32>>, !fir.box<!fir.array<?xf32>>)
  %2 = acc.copyin var(%1#0 : !fir.box<!fir.array<?xf32>>) ->
!fir.box<!fir.array<?xf32>> {name = "arr", structured = false}
  acc.enter_data dataOperands(%2 : !fir.box<!fir.array<?xf32>>)
```

Restoring the old behavior can be done with following command line
options:
`--openacc-unwrap-fir-box=true --openacc-generate-default-bounds=true`
2025-02-04 08:08:16 -08:00
vdonaldson
e73a64bbd1
[flang][NFC] Document Arm exception raising behavior (#125579) 2025-02-04 09:54:33 -05:00
Kareem Ergawy
25f29ee377
[flang][OpenMP] Update all lastprivate symbols, not just in clauses (#125628)
Fixes a bug in updating `lastprivate` variables. Previously, we only
iterated over the symbols collected from `lastprivate` clauses. This
meants that for pre-determined symbols, we did not implement the update
correctly (e.g. for loop iteration variables of `simd` constructs).
2025-02-04 15:52:46 +01:00
Leandro Lupori
6fc66d322b
[flang][OpenMP] Fix sections lastprivate for common blocks (#125504)
Common block handling was missing in sections' lastprivate lowering.

Fixes #121719
2025-02-04 10:28:14 -03:00
Abid Qadeer
7ece824b6f
[flang][debug] Improve check for global variable detection. (#118326)
When a global variable is used in the OpenMP target region, it is passed
as an argument to the function that implements target region. But the
`DeclareOp` for this incarnation still have the original name of the
variable. As some of our checks to decide if a variable is global or nor
are based on the name, this can result in a local variable being treated
as global. This PR hardens the check a bit. We now also check that
memory ref is actually an `AddrOfOp` before looking at the name.
2025-02-04 13:17:14 +00:00
Valentin Clement (バレンタイン クレメン)
7e1437ba4a
[flang][cuda] Detect illegal data transfer in semantic (#125591)
When the LHS is a device variable and the RHS has implicit transfer,
this is considered as an illegal transfer according to
https://docs.nvidia.com/hpc-sdk/compilers/cuda-fortran-prog-guide/index.html#implicit-data-transfer-in-expressions.

Detect this is semantic .
2025-02-03 15:00:43 -08:00
Krzysztof Parzyszek
e8100c399b
[flang][OpenMP] Handle directive arguments in OmpDirectiveSpecifier (#124278)
Implement parsing and symbol resolution for directives that take
arguments. There are a few, and most of them take objects. Special
handling is needed for two that take more specialized arguments: DECLARE
MAPPER and DECLARE REDUCTION.

This only affects directives in METADIRECTIVE's WHEN and OTHERWISE
clauses. Parsing and semantic checks of other cases is unaffected.
2025-02-03 12:58:42 -06:00
Krzysztof Parzyszek
6dfe20dbbd
[flang][OpenMP] Parse METADIRECTIVE in specification part (#123397)
Add METADIRECTIVE to the OpenMP declarative constructs as well. Emit a
TODO error for both declarative and executable cases.
2025-02-03 11:13:44 -06:00
jeanPerier
22d9726593
[flang] do not finalize or initialize unused entry dummy (#125482)
Dummy arguments from other entry statement that are not live in the current entry have no backing storage, user code referring to them is not allowed to be reached. The compiler was generating initialization/destruction code for them when INTENT(OUT), causing undefined behaviors.
2025-02-03 18:09:01 +01:00
Krzysztof Parzyszek
ab77db03ce
[flang][Lower] Move getHashValue and isEqual implementations to Utils… (#125513)
….cpp

This is intended to reduce the memory usage while compiling flang
sources.

There were over 7500 instantiations of function templates defined in the
Utils.h file. Most of them were not referenced anywhere outside, except
for specialized implementations of getHashValue and isEqual in
IterationSpace.cpp. These function were also moved to Utils.cpp.
2025-02-03 10:54:40 -06:00
Krzysztof Parzyszek
119e9d5a93 [flang][OpenMP] Fix build break after fe8b323f59
Replace structured bindings with references where they are captured in
a lambda.
2025-02-03 10:17:10 -06:00
Krzysztof Parzyszek
fe8b323f59
[flang][OpenMP] Semantic checks for context selectors (#123243)
This implements checks of the validity of context set selectors and
trait selectors, plus the types of trait properties. Clause properties
are also validated, but not name or extension properties.

---------

Co-authored-by: Tom Eccles <tom.eccles@arm.com>
2025-02-03 09:48:04 -06:00
Brad Smith
cb2598dda1
[flang][runtime] Make sure to link libexecinfo if it exists (#125344)
Fixes building the backtrace support on FreeBSD/NetBSD/OpenBSD/DragonFly and musl
libc with libexecinfo.
2025-02-03 10:03:59 -05:00
Slava Zakharin
d03b5de71c [flang] Fixed Windows build.
`llvm::enumerate(llvm::reverse(ValueRange))` added in #124683 does not work
on Windows: https://lab.llvm.org/buildbot/#/builders/124/builds/322
2025-02-03 17:48:33 +05:00
jeanPerier
2dc17fd173
[flang] fix isSimplyContiguous and isOptional hlfir::Entity methods (#125215)
Fix isSimplyContiguous:

- It ignored scalars, causing scalar fir.box to not be opened when
possible in `translateToExtendedValue`

Fix isOptional:

It is not reliable when the memory SSA value cannot be linked to a
declare. This is exposed by the `isSimplyContiguous` fix,
This is wrong because declare operation should not always assumed to be
visible (e.g., value may travel through a select, or the optional be
generated by the compiler like genOptionalBox in
lib/Lower/ConvertCall.cpp).

- Turn `isOptional` into a safer `mayBeOptional`
- Update translateToExtendedValue to open scalar fir.box for such values
in a fir.if.
- It turned out some `translateToExtendedValue` usage relied on fir.box
of optional scalars to be left untouched (mainly because they want to
forward those fir.box to the runtime), add an option to allow that.
2025-02-03 11:47:30 +01:00
jeanPerier
327d627066
[mlir] share argument attributes interface between calls and callables (#123176)
This patch shares core interface methods dealing with argument and
result attributes from CallableOpInterface with the CallOpInterface and
makes them mandatory to gives more consistent guarantees about concrete
operations using these interfaces.

This allows adding argument attributes on call like operations, which is
sometimes required to get proper ABI, like with  llvm.call (and llvm.invoke).


The patch adds optional `arg_attrs` and `res_attrs` attributes to operations using
these interfaces that did not have that already.
They can then re-use the common "rich function signature"
printing/parsing helpers if they want (for the LLVM dialect, this is
done in the next patch).

Part of RFC: https://discourse.llvm.org/t/mlir-rfc-adding-argument-and-result-attributes-to-llvm-call/84107
2025-02-03 11:27:14 +01:00
Valentin Clement (バレンタイン クレメン)
f1b075df2e
[flang][cuda] Pass the pinned variable in allocate calls (#125310) 2025-02-02 18:05:59 -08:00
Renaud Kauffmann
9ad153a696
[flang][cuda] Adding more bindings to libcudadevice (#125276) 2025-01-31 14:41:34 -08:00
Peter Klausler
2bfb3bae69
[flang] Make REAL/COMPLEX(10) a hard error for non-x86 targets (#124655)
Currently the use of REAL/COMPLEX(KIND=10) as a type or literal constant
suffix elicits an optional warning message only. This leads to compiler
internal errors during lowering when these types appear in code being
compiled to non-x86_64 targets. For better error messaging, make the use
of these types a hard error in semantics instead when they are not
supported by the target architecture.
2025-01-31 11:43:29 -08:00
Peter Klausler
f8300f1c2a
[flang] Refine "same type" testing for intrinsic arguments (#125133)
Some errors aren't being caught, such as the case in the linked bug
where the PAD= argument to RESHAPE() didn't have the same declared type
as the ARRAY=; this led to a crash in lowering. Refine the "same type"
testing logic for intrinsic procedures, and add a better test.

Fixes https://github.com/llvm/llvm-project/issues/124976.
2025-01-31 10:55:08 -08:00
Peter Klausler
56c468474d
[flang] Suppress USEs of non-USE'able names in module files (#124980)
When harvesting and formatting symbols USE'd from other modules, don't
emit USE statements to module files for names unless they come from the
topmost scope of the module. There was a check to prevent names from
derived type scopes from escaping in this way, but it must be made more
general to prevent other cases like dummy arguments in interfaces.

Fixes https://github.com/llvm/llvm-project/issues/124716.
2025-01-31 10:54:48 -08:00
Peter Klausler
c82db773f4
[flang] Handle indirect USE of ancestor module into submodule (#124969)
A USE statement within a submodule (possibly in a nested scope) is not
allowed to USE the submodule's ancestor module directly, but it is
permissible to USE that ancestor module indirectly via another unrelated
module. Don't emit "already present in scope" errors for this case.

Fixes https://github.com/llvm/llvm-project/issues/124731.
2025-01-31 10:54:25 -08:00
Peter Klausler
cadc70c1f0
[flang] Prefer non-elemental to elemental defined operator resolution (#124941)
A non-elemental specific procedure must take precedence over an
elemental specific procedure in defined operator generic resolution.

Fixes https://github.com/llvm/llvm-project/issues/124777.
2025-01-31 10:54:00 -08:00
Peter Klausler
4927a5ed4a
[flang] Allow defined assignment to CLASS(*) (#124817)
An unlimited polymorphic left-hand side variable is acceptable in the
definition of a defined assignment subroutine.

Fixes https://github.com/llvm/llvm-project/issues/124621.
2025-01-31 10:53:40 -08:00
Peter Klausler
10b0a07e11
[flang] Fold KIND= arguments in intrinsic function references (#124666)
KIND= arguments in e.g. ACHAR(..., KIND=...) intrinsic function
references must be compilation-time constant expressions. The compiler
was failing to evaluate those expressions if they were not actually
literaly constant values.

Fixes https://github.com/llvm/llvm-project/issues/124618.
2025-01-31 10:53:14 -08:00
Peter Klausler
36caa8f9e2
[flang] Fix crash on SMP with dummy procedure (#124663)
When a separate module procedure is defined with MODULE PROCEDURE, the
compiler crashes if there is a dummy procedure in the interface defined
with only a result type. This is due to the type already having been
defined on the ProcEntityDetails symbol as part of earlier wholesale
symbol duplication. Adjust the code to not define the result type of the
ProcEntityDetails if it is already present, but to verify that it is the
same type instead.

Fixes https://github.com/llvm/llvm-project/issues/124487.
2025-01-31 10:52:34 -08: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
Brad Smith
e31c6c97b7
[flang] Fix building on aarch64 *BSD and musl libc after 9d8dc45d17088300e9e2086594ca581b119193c8 (#125183)
The fpu_control.h header appears to be GLIBC specific.
2025-01-31 11:49:28 -05:00