9773 Commits

Author SHA1 Message Date
Joel E. Denny
eb8ffd617a
[flang] AliasAnalysis: Handle fir.load on fir.alloca (#117785)
For example, determine that the address in p below cannot alias the
address of v:

```
subroutine test()
  real, pointer :: p
  real, target :: t
  real :: v
  p => t
  v = p
end subroutine test
```
2025-02-13 12:40:03 -05:00
Slava Zakharin
660cdace55
[flang] Fixed write past allocated descriptor in PointerAssociateRemapping. (#127000)
The pointer descriptor might be smaller than the target descriptor,
so `operator=` would write beyound the pointer descriptor.
2025-02-13 09:39:36 -08:00
Craig Topper
8da8ff8768
[flang][RISCV] Add target-abi ModuleFlag. (#126188)
This is needed to generate proper ABI flags in the ELF header for LTO
builds. If these flags aren't set correctly, we can't link with objects
that were built with the correct flags.

For non-LTO builds the mcpu/mattr in the TargetMachine will cause the
backend to infer an ABI. For LTO builds the mcpu/mattr aren't set.

I've only added lp64, lp64f, and lp64d ABIs. ilp32* requires riscv32
which is not yet supported in flang. lp64e requires a different
DataLayout string and would need additional plumbing.

Fixes #115679
2025-02-13 08:08:09 -08:00
Joseph Huber
c4ed95c85b [Flang] Fix leftover use of 'OPT_nogpulib'
Summary:
This didn't show up as a failure in precommit and I don't build flang so
this slipped by.
2025-02-13 08:14:12 -06:00
Jan Leyonberg
f63e3b15f9
[Flang] Generate math ops for non-precise calls to acosh, asin, asinh and atanh intrinsic calls (#126932)
This patch changes the codegen for non-precise acosh, asin, asinh and
atanh calls to generate math ops instead. This wasn't done before
because the math dialect did not have the corresponding operations at
the time.
2025-02-13 09:10:46 -05:00
Leandro Lupori
46ffacc2dd
[flang][OpenMP] Skip invalid conditional compilation sentinels (#126282)
In fixed form, an initial line can have an OpenMP conditional
compilation sentinel only if columns 3 through 5 have only
white space or numbers.

Fixes https://github.com/llvm/llvm-project/issues/89560
2025-02-13 09:36:04 -03:00
Abid Qadeer
bfd3e25fdd
[flang][debug] Allow variable from non-entry blocks. (#125692)
We currently drop the `DeclareOp` which are not in the entry block of
the function. This was done to side step a problem with the variables in
OpenMP target region. Now that issue has been addressed in #118314 so we
can lift this restriction as well.
2025-02-13 11:20:45 +00:00
Nikita Popov
3bf425764e [flang] Update f128 tests
Update test that require flang-supports-f128-math after #126929.
2025-02-13 09:24:38 +01:00
Nikita Popov
75c356c488
[MLIR][LLVMIR] Always use TargetFolder in IRBuilder (#126929)
This is a followup to https://github.com/llvm/llvm-project/pull/126745,
generalizing it to always use TargetFolder, including inside function
bodies.

This avoids generating non-canonical constant expressions that can be
folded away.
2025-02-13 08:53:59 +01:00
Razvan Lupusoru
7b473dfe84
[flang][acc] Implement type categorization for FIR types (#126964)
The OpenACC type interfaces have been updated to require that a type
self-identify which type category it belongs to. Ensure that FIR types
are able to provide this self identification.

In addition to implementing the new API, the PointerLikeType interface
attachment was moved to FIROpenACCSupport library like MappableType to
ensure all type interfaces and their implementation are now in the same
spot.
2025-02-12 21:09:59 -08:00
Michał Górny
c665480694
[flang] [runtime] Explicitly disable EH & RTTI (#126920)
Explicitly disable EH & RTTI when building Flang runtime library. This
fixes the runtime built when Flang is built standalone against system
LLVM that was compiled with EH & RTTI enabled.

I think this change may be sufficient to lift the top-level
`LLVM_ENABLE_EH` restriction from Flang. However, I'd prefer if somebody
more knowledgeable decided on that.
2025-02-12 18:03:04 +00:00
jeanPerier
5836d91845
[flang] add ABI argument attributes in indirect calls (#126896)
Last piece that implements the TODO for sret and byval setting on
indirect calls.

This includes a fix to the codegen last patch. I thought types in in
type attributes were automatically converted in dialect conversion
passes, but that is not the case. The sret and byval type needs to be
converted to llvm types in codegen (mlir FuncOp conversion is doing a
similar conversion).
2025-02-12 17:31:34 +01:00
Sergio Afonso
f13aea1d87
[Flang][OpenMP] Update semantics checks for 'teams' nesting (#126922)
This patch introduces a directive set for combined constructs where
`teams` is the last leaf. This is used in a couple places to simplify
checks, which is NFC, but it also replaces two incorrect uses of
`topTeamsSet`.

Before, these checks would incorrectly skip combined constructs where
`teams` was the last leaf construct when checking for allowed nested
constructs inside of a `teams` region. Similarly, it would also
incorrectly perform these checks whenever a compound `teams` construct
where `teams` was the first leaf construct was found.
2025-02-12 15:24:31 +00:00
Scott Manley
830a2911ee
[Flang] fix initializer with empty string to fix aarch64 build (#126918)
After tuneCPU was changed to std::string in
c8376a93bb9853cbcedeb22d80a9b200060eaf85 the flang builds broke, due to
a missing initializer.

If we want to add tuneCPU to the MLIRToLLVMPassPipelineConfig, we might
want to tackle that separately after the build is restored. This should
be no different than the previous behaviour.
2025-02-12 08:56:02 -06:00
Scott Manley
cbe879d685
[Flang] Fix options for FunctionAttr pass (#126819)
For non-basic types GenericOptionParser::findArgStrForValue will return
null, ultimately an llvm_unreachable, when the specific values are not
found. Add the enum, much like the debug-level option in AddDebugInfo to
resolve this problem. Also change tuneCPU to be std::string or it will
also fail.
2025-02-12 08:17:07 -06:00
Kareem Ergawy
32faf43878
[flang][OpenMP] Handle fixed length charaters in delayed privatization (#126704)
We currently handle sequences of fixed-length arrays properly by **not**
emitting length parameters for `embox` ops inside the `omp.private` op.
However, we do not handle the scalar case. This PR extends
`getLengthParameters` defined in `PrivateReductionUtils.cpp` to handle
such cases.

Fixes issue reported in #125732.
2025-02-12 11:04:26 +01:00
Nikita Popov
c03325cead
[MLIR][LLVMIR] Use TargetFolder when creating globals (#126745)
The LLVM dialect lowers globals using IRBuilder, relying on it creating
constant expressions where possible. As we remove support for more
constant expressions (per
https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179),
this can cause issues for cases where the constant expression is no
longer supported, and the operation cannot be constant folded without
DataLayout being available. In particular, I ran into this issue with
flang and the removal of mul constant expressions.

Address this by using TargetFolder when creating globals, which will
perform DL-aware constant folding. I think it would make sense to also
do this in general, but I'm starting with globals where not doing this
can result in translation failures.

Ideally, globals with these problematic expressions would never be
generated in the first place, but there has been little movement on
fixing this (https://github.com/llvm/llvm-project/issues/96047).
2025-02-12 10:14:00 +01:00
jeanPerier
65075a863b
[flang][FIR] handle argument attributes in fir.call (#126711)
Add pretty printer/parser for fir.call argument/result attributes and
propagate them to llvm.call.

This will allow implementing the TODO about ABI relevant argument
attribute in indirect calls.
2025-02-12 09:49:52 +01:00
Valentin Clement (バレンタイン クレメン)
401f4b828c
[flang][rt] Add decimal files to device runtime (#126778)
The library FortranDecimal is not used anymore with the runtime but its
files are now integrated. Add the files for the device build as well.
2025-02-11 12:03:19 -08:00
Renaud Kauffmann
9d7177a2d7
[flang][NFCI] Stop tracking memory source after a load in a more explicit manner. (#126156)
Typically, we do not track memory sources after a load because of the
dynamic nature of the load and the fact that the alias analysis is a
simple static analysis.

However, the code is written in a way that makes it seem like we are
continuing to track memory but in reality we are only doing so when we
know that the tracked memory is a leaf and therefore when there will
only be one more iteration through the switch statement. In other words,
we are iterating one more time, to gather data about a box, anticipating
that this will be the last time. This is a hack that helped avoid
cut-and-paste from other case statements but gives the wrong impression
about the intention of the code and makes it confusing.

To make it clear that there is no more tracking, we gather all the
necessary data from the memref of the load, in the case statement for
the load, and exit the loop. I am also limiting this data gathering for
the case when we load a box reference while we were actually following
data, as tests have shows, is the only case when we need it for. Other
cases will be handled conservatively, but this can change in the future,
on a case-by-case basis.

---------

Co-authored-by: Joel E. Denny <jdenny.ornl@gmail.com>
2025-02-11 10:47:38 -08:00
Razvan Lupusoru
c3d8c625af
[flang][acc] Fill-in name for privatized loop iv (#126601)
When the loop induction variable implicit private clause was being
generated, the name was left empty. The intent is that the data clause
operation holds the source language variable name. Thus, add the missing
name now.
2025-02-11 09:16:59 -08:00
Nikita Popov
82bd148a3f
[mlir][cmake] Do not export MLIR_MAIN_SRC_DIR and MLIR_INCLUDE_DIR (#125842)
MLIR_MAIN_SRC_DIR and MLIR_INCLUDE_DIR point to the source directory,
which is not installed. As such, the installed MLIRConfig.cmake also
should not reference it.

The comment indicates that these are needed for mlir_tablegen(), but I
don't see any related uses.

The motivation for this is the use in flang, where we end up inheriting
a meaningless MLIR_MAIN_SRC_DIR from a previous MLIR build, whose source
directory doesn't exist anymore, and that cannot be overridden with the
correct path, because it's not a cached variable.

Instead do what all the other projects do for LLVM_MAIN_SRC_DIR and
initialize MLIR_MAIN_SRC_DIR to CMAKE_CURRENT_SOURCE_DIR/../mlir.

For MLIR_INCLUDE_DIR there already is an exported MLIR_INCLUDE_DIRS,
which can be used instead.
2025-02-11 14:32:30 +01:00
jeanPerier
06f4fe3317
[flang][NFC] fix rewrite-out_of_range.F90 tests (#126699)
There was a typo in the target, and REAL(16) parts needed to be
protected too.
2025-02-11 11:49:32 +01:00
Slava Zakharin
754d896ca7
[flang] Propagate fast-math flags to FIROpBuilder. (#126316)
One constructor was missing to propagate fast-math flags
from an operation to the builder. It is fixed now.
And the builder creation in one opt-bufferization case should take
the rewriter, I think.
2025-02-10 15:23:34 -08:00
Razvan Lupusoru
83edbd4958
[flang][acc] Ensure data exit action is generated for present & nocreate (#126560)
The acc.delete operation has semantics of decrementing present counter
and deleting the data when the counter reaches zero. Since both
acc.present and acc.nocreate are both intended to increment present
counter, this matching exit action must be inserted.

This is also what was specified in OpenACC dialect documentation:
https://mlir.llvm.org/docs/Dialects/OpenACCDialect/#operation-categories
2025-02-10 13:04:10 -08: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
e6e8ac59ba
[Flang] Optionally do not compile the runtime in-tree (#122336)
Introduce the CMake switch FLANG_INCLUDE_RUNTIME. When set to off, do
not add build instructions for the runtime.

This is required for Flang-RT (#110217) and the current runtime CMake
code to co-exist. When using `LLVM_ENABLE_RUNTIME=flang-rt`, the in-tree
build instructions are in conflict and must be disabled.
2025-02-08 01:06:27 +01:00
Kelvin Li
4c7cbb9478
[flang] update PPC vector tests (NFC) (#126256)
Replace 'undef' with 'poison' based on commit f4e3b87
2025-02-07 11:23:09 -05:00
vdonaldson
79e9887a0f
[flang] test fix (#126251) 2025-02-07 09:55:10 -05:00
vdonaldson
b51fc2ac60
[flang] Modifications to ieee_support_standard (#125967)
The result of a call to ieee_support_halting is one of the components
that affect the result of a call to ieee_support_standard.
2025-02-07 08:49:12 -05: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
Valentin Clement (バレンタイン クレメン)
070c888292
[flang][cuda] Lower syncwarp to NVVM intrinsic (#126164) 2025-02-06 19:43:21 -08:00
Valentin Clement (バレンタイン クレメン)
b00b193728
[flang][cuda] Allow POINTER component to have device attribute (#126116) 2025-02-06 19:43:04 -08:00
David Pagan
8fb1b3f4b2
[flang][OpenMP] 'no_openmp_constructs' added to clang broke flang build (#126145)
Adding 'no_openmp_constructs' assumption clause to clang broke the flang
build. Adding to flang so it builds.

Testing
- Build
- Testing: check-all
2025-02-06 15:00:47 -08:00
Valentin Clement (バレンタイン クレメン)
98752ef893
[flang][cuda] Add interface for sinpi, cospi and sincospi (#126123)
Add interface for `sinpi`, `cospi` and `sincospi` and also expose
`sincosf`
2025-02-06 13:12:41 -08:00
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