Commit Graph

10493 Commits

Author SHA1 Message Date
Kazu Hirata
938cdb30f1 [flang] Migrate away from std::nullopt (NFC) (#145928)
ArrayRef has a constructor that accepts std::nullopt.  This
constructor dates back to the days when we still had llvm::Optional.

Since the use of std::nullopt outside the context of std::optional is
kind of abuse and not intuitive to new comers, I would like to move
away from the constructor and eventually remove it.

This patch replaces std::nullopt with {}.  There are a couple of
places where std::nullopt is replaced with TypeRange() to accommodate
perfect forwarding.
2025-06-26 12:41:49 -07:00
Andre Kuhlenschmidt
283c2e8d7c [flang][semantics] fix issue with equality of min/max in module files (#145824)
Convert all binary calls of min/max to extremum operations, so that
extremums generated by the compiler compare equal, and user min/max
calls also compare equal.

Fixes #133646

Originally opened as #144162 but I accidentally pushed a merge in such a
way that a bunch of code owners got added to the review. This is just
rebasing the original work on main and fixing the failing tests.
2025-06-26 12:15:57 -07:00
Muhammad Omair Javaid
cfdc4c4a5b Revert "[flang][OpenMP] Move lowering of ATOMIC to separate file, NFC (#144960)"
PR#144960 broke check-flang tests on Windows (x64/ARM64).

This reverts commit e5559ca45f.
2025-06-26 18:33:21 +05:00
Muhammad Omair Javaid
597ffb1187 Revert "[flang][OpenMP] Fix namespace nesting after PR144960"
PR#144960 broke check-flang tests on Windows (x64/ARM64).

This reverts commit 925dbc7988.
2025-06-26 18:33:10 +05:00
Tom Eccles
cc1eae6ea2 [flang][OpenMP] Remove experimental warning (#144915)
RFC:
https://discourse.llvm.org/t/rfc-removing-the-openmp-experimental-warning-for-llvm-21/86455

Fixes: #110008
2025-06-26 14:09:21 +01:00
jeanPerier
13daf65656 [flang] handle common block used as BIND(C) module variables (#145669)
Support odd case where a static object is being declared both as a
common block and a BIND(C) module variable name in different modules,
and both modules are used in the same compilation unit.

This is not standard, but happens when using MPI and MPI_F08 in the same
compilation unit, and at least both gfortran and ifx support this.

See added test case for an illustration.
2025-06-26 12:00:23 +02:00
Valentin Clement (バレンタイン クレメン)
2b2bd51f3b [flang][cuda] Inline this_grid call for cooperative groups (#145796) 2025-06-25 16:40:47 -07:00
Krzysztof Parzyszek
b4f4af7ebb [flang][OpenMP] Verify that N in -fopenmp-version=N is valid (#145725)
For historical versions that are unsupported, emit a warning and assume
the currently default version.
For values of N that are not integers or that don't correspond to any
OpenMP version (old or newer), emit an error.
2025-06-25 11:54:14 -05:00
Krzysztof Parzyszek
77a3ae5845 [flang][OpenMP] Remove recognition of versions 3.0 and older (#145708)
The oldest supported version is now 3.1. In terms of semantic analysis
the compiler treats all versions <= 4.5 identically, and there is no
plan to add version-specific checks for older versions.

See discourse thread:

https://discourse.llvm.org/t/rfc-remove-openmp-versions-prior-to-3-1/86901
2025-06-25 10:20:52 -05:00
Krzysztof Parzyszek
8231dd71cb [flang][OpenMP] Skip runtime mapping with no offload targets (#145594)
When no offload targets are specified flang will avoid offloading for
"target" constructs, but not "target data" constructs. This patch makes
the behavior consistent across all offload-related operations.

While ignoring "target" may produce semantically incorrect code, it may
still be a useful debugging tool.

--
This reinstates commits 6ba1955 and 349f8d6, reverted due to compilation
failures in the gfortran test suite. These build problems were caused by
an unrelated issue (https://github.com/llvm/llvm-project/issues/145558)
which is now fixed.

Ref: https://github.com/llvm/llvm-project/pull/144534
2025-06-25 07:10:08 -05:00
David Truby
892a263106 [flang] Add release note for compiler-rt dependency on AArch64 (#145547) 2025-06-25 11:39:27 +01:00
jeanPerier
22ee837ec0 [flang][NFC] do not copy fields in fir::RecordType::getTypeList (#145530)
For historical reason, `fir::RecordType::getTypeList` was returning an
std::vector, causing the entire field list to be copied when called.

It is called a lot indirectly in all type helpers, which themselves are
called a lot in derived type heavy code like WRF.
The `fir::hasDynamicType` helper is also called a lot, and it can just
check for length parameters to avoid looping on all derived type
components in most cases.
2025-06-25 11:51:07 +02:00
Jacques Pienaar
d0469d1d3c [mlir] Move WalkResult to Support (#145649)
This also enables moving StateStack, both are relatively generic helper
structs not tied to IR.
2025-06-25 01:58:44 -07:00
jeanPerier
aeaf319b8c [flang] avoid useless rebox of polymorphic scalars (#145507)
Do not create new descriptor for polymorphic scalars when lowering
hlfir.declare.

hlfir.declare of box/class is lowered to a fir.rebox to ensure that
local lower bounds and descriptor attributes (Pointer/Allocatable/None)
are properly set-up in the descriptor associated to the symbol.

For polymorphic scalar, this created a useless temporary descriptor.
This was breaking invalid code #145256 that violates OPTIONAL usage
rules. I am not fixing it primarily to support this invalid code, but
rather because it is dumb to create a useless fir.rebox.
2025-06-25 09:41:33 +02:00
Lance Wang
77af8bff97 [mlir]Moves the StateStack to IR folder from Support folder. (#145598)
[MLIR] Fix circular dependency introduced in In
https://github.com/llvm/llvm-project/pull/144897. This PR is to break
the dependency. by moving StateStack to IR folder

This commit resolves a circular dependency issue between mlir/Support
and mlir/IR:

- Move StateStack.h and StateStack.cpp from Support to IR folder
- Update CMakeLists.txt files to reflect the new locations
- Update Bazel BUILD file to maintain correct dependencies
- Update includes in affected files (flang, Target/LLVMIR)

The circular dependency was caused by StateStack.h depending on
IR/Visitors.h
while other IR files depended on Support. Moving StateStack to IR
eliminates
this cycle while maintaining proper separation of concerns.
2025-06-25 00:00:13 -04:00
Andrew Rogers
a451fff1ad [llvm] fix extern cl::opt definitions for DLL export (#145374)
## Purpose
This patch is one in a series of code-mods that annotate LLVM’s public
interface for export. This patch ensures a few `cl::opt` declarations
are properly annotated with `LLVM_ABI`. The annotations currently have
no meaningful impact on the LLVM build; however, they are a prerequisite
to support an LLVM Windows DLL (shared library) build.

## Background
This effort is tracked in #109483. Additional context is provided in
[this
discourse](https://discourse.llvm.org/t/psa-annotating-llvm-public-interface/85307),
and documentation for `LLVM_ABI` and related annotations is found in the
LLVM repo
[here](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InterfaceExportAnnotations.rst).

## Overview
- Remove local `extern` declarations of `llvm::PrintPipelinePasses`
because it is already correctly declared with an `LLVM_ABI` annotation
in `llvm\Passes\PassBuilder.h`. Leaving these declarations results in a
gcc compile warning unless they are also annotated with `LLVM_ABI`.
- Similarly, remove local `extern` declarations of
`ProfileSummaryCutoffHot` and `UseContextLessSummary` from
`llvm/tools/llvm-profgen/ProfileGenerator.cpp` since they are declared
with `LLVM_ABI` in `llvm\ProfileData\ProfileCommon.h`.
- Explicitly annotate the extern declaration of `ProfileCorrelate` in
`clang/lib/CodeGen/BackendUtil.cpp` since it is not declared in a
header. The definition of `ProfileCorrelate` in
`llvm\lib\Transforms\Instrumentation\InstrProfiling.cpp` is already
annotated with `LLVM_ABI`.

## Validation
Local builds and tests to validate cross-platform compatibility. This
included llvm, clang, and lldb on the following configurations:

- Windows with MSVC
- Windows with Clang
- Linux with GCC
- Linux with Clang
- Darwin with Clang
2025-06-24 16:23:00 -07:00
Krzysztof Parzyszek
3a71884ab1 [flang][OpenMP] Map device pointers on host device as well (#145562)
Given a TARGET DATA construct with USE_DEVICE_PTR(x) and IF(FALSE), the
compiler will crash if `x` was used in the body. The cause of the crash
is that the MLIR->LLVM codegen tries to look up the translated value of
x, but one had not been mapped.

Given an IF clause, the translation will generate an if-then-else
construct, with the "else" block corresponding to the false condition,
i.e. the host device playing the role of the target device. In that
block, still process the USE_DEVICE_ADDR/USE_DEVICE_PTR clauses, which
will cause the translation mappings to be created.

Fixes https://github.com/llvm/llvm-project/issues/145558
2025-06-24 15:38:23 -05:00
Tom Eccles
8f7f48a97e [flang][OpenMP][NFC] remove globals with mlir::StateStack (#144898)
Idea suggested by @skatrak
2025-06-24 18:30:37 +01:00
Kareem Ergawy
9aebfde1e7 [flang] Allow cycle in target teams distribute [simd] (#145462)
flang incorrectly issues a semantic erorr when a `cycle` statement is
used inside a `target teams distribute [simd]` associated loop. This is
not prevented by the spec, therefore this PR allows such construct.
2025-06-24 14:21:06 +02:00
David Truby
049d61ad65 [flang][AArch64] Always link compiler-rt to flang after libgcc (#144710)
This patch fixes an issue where the __trampoline_setup symbol is missing
with some programs compiled with flang. This symbol is present only in
compiler-rt and not in libgcc. This patch adds compiler-rt to the link
line after libgcc if libgcc is being used, so that only this symbol will
be picked from compiler-rt.

Fixes #141147
2025-06-24 11:08:13 +01:00
antoine moynault
5fa55b2dfc Revert "[flang][OpenMP] Skip runtime mapping with no offload targets (#144534)" (#145478)
And also revert 6ba1955 "[flang][OpenMP] Fix ignore-target-data.f90 test"

As it causes several bot failures
https://github.com/llvm/llvm-project/pull/144534#issuecomment-2995303224
2025-06-24 10:51:26 +02:00
Paul Osmialowski
4b9f7cd856 [flang] flang manpage overhaul (#144948)
Make the flang man page look more like the one clang is having.
2025-06-24 00:07:10 +01:00
Lei Huang
d715ecba79 Revert "[flang][fir] Add fir.if -> scf.if and add filecheck test … (#142965)" (#145345)
This reverts commit 823750d873.

Test causes segfault on aix flang builder.
2025-06-23 16:46:47 -04:00
Sam James
117e51de8a [flang][cmake] Don't pass -fno-strict-aliasing for GCC either
This is the same as 4ed10db859 with the
same rationale, but for Flang, I strongly suspect it was just pulled in
from Clang, see https://github.com/flang-compiler/f18/pull/6#issuecomment-364155817.
2025-06-23 14:24:50 +01:00
Krzysztof Parzyszek
fb209929e1 [flang][OpenMP] Set isNewBlock directly on OpenMP constructs (#144593)
When the PFT builder decides that an evaluation needs a new block it
checks if the evaluation has nested evaluations. In such case it sets
the flag on the first nested evaluation. This works under the assuption
that such an evaluation only serves as a container, and does not, by
itself, generate any code.

This fails for OpenMP constructs that contain nested evaluations because
the top-level evaluation does generate code that wraps the code from the
nested evaluations. In such cases, the code for the top-level evaluation
may be emitted in a wrong place.

When setting the `isNewBlock` flag, recognize OpenMP directives, and
treat them accordingly.

This fixes https://github.com/llvm/llvm-project/issues/139071
2025-06-23 08:09:50 -05:00
Krzysztof Parzyszek
925dbc7988 [flang][OpenMP] Fix namespace nesting after PR144960
Newly introduced Atomic.cpp fails to compile on its own, but somehow
compiles fine in the build. Maybe it's because PCH, but it needs to be
fixed nevertheless.
2025-06-20 13:22:58 -05:00
Krzysztof Parzyszek
6ba1955ba2 [flang][OpenMP] Fix ignore-target-data.f90 test
Allow the function definition line to match with and without attrbute
set number.

This fixes build break after PR144534:
https://lab.llvm.org/buildbot/#/builders/157/builds/31331

Also move the test to the OpenMP subdirectory where it should have
been from the beginning.
2025-06-20 11:09:07 -05:00
Krzysztof Parzyszek
349f8d67d4 [flang][OpenMP] Skip runtime mapping with no offload targets (#144534)
When no offload targets are specified flang will ignore "target"
constructs, but not "target data" constructs. This patch makes the
behavior consistent across all offload-related operations.

While ignoring "target" may produce semantically incorrect code, it may
still be a useful debugging tool.
2025-06-20 08:09:36 -05:00
NimishMishra
e970f59e6b [flang][OpenMP] Reintroduce TODO for FIR lowering of linear clause (#144883)
Current design of the linear clause lowering and translation shifts all
responsibility for handling the clause (like privatisation, linear
stepping, finalisation, and emission of synchronisation barriers) to the
IRBuilder. However in certain corner cases (like associated loops in or
before OpenMP version 4.5), variables are are implicitly linear. This
currently causes a problem with the existing linear clause
implementation. Hence, re-introduce TODO on the linear clause until the
linear clause lowering/translation are robust enough to handle such
cases as well.

Fixes https://github.com/llvm/llvm-project/issues/142935
2025-06-20 04:53:00 -07:00
NimishMishra
8650c80138 [flang][OpenMP] Do not skip privatization of linear variable if it is OmpPreDetermined (#144315)
Current implementation of linear clause skips privatisation of all
linear variables during the FIR generation phase, since linear variables
are handled in their entirety by the OpenMP IRBuilder. However,
"implicit" linear variables (like OmpPreDetermined) cannot be skipped,
since FIR generation requires privatized symbols. This patch adds checks
to skip the same.


Fixes https://github.com/llvm/llvm-project/issues/142935
2025-06-20 04:52:40 -07:00
Krzysztof Parzyszek
e5559ca45f [flang][OpenMP] Move lowering of ATOMIC to separate file, NFC (#144960) 2025-06-20 06:44:14 -05:00
Peter Klausler
9fd22cb56d [flang][NFC] Move new code to right place (#144551)
Some new code was added to flang/Semantics that only depends on
facilities in flang/Evaluate. Move it into Evaluate and clean up some
minor stylistic problems.
2025-06-19 13:42:46 -07:00
Slava Zakharin
28808dda2c [flang] Fixed test added in #144830. 2025-06-19 13:02:30 -07:00
Slava Zakharin
8631b4f1b4 [flang] Set low probability for array repacking code. (#144830)
This allows LLVM to place the most probably cold blocks
that do the repacking out of the line of the potentially hot code.
2025-06-19 12:12:04 -07:00
Jack Styles
89efae916a [Flang][OpenMP] Update default MapType for Map Clauses and OpenMP 5.2 (#144715)
In OpenMP 5.2, the `target enter data` and `target exit data` constructs
now have default map types if the user does not define them in the Map
clause. For `target enter data`, this is `to` and `target exit data`
this is `from`. This behaviour is now enabled when OpenMP 5.2 or greater
is used when compiling. To enable this, the default value is now set in
the `processMap` clause, with any previous behaviour being maintained
for either older versions of OpenMP or other directives.

See also #110008
2025-06-19 15:32:27 +01:00
Krzysztof Parzyszek
936c5566db [flang][OpenMP] Handle REQUIRES ADMO in lowering (#144362)
The previous approach rewrote the atomic constructs in the AST based on
the REQUIRES ATOMIC_DEFAULT_MEM_ORDER directives. The new approach
checks for incorrect uses of REQUIRED ADMO in the semantic analysis, and
applies it in lowering, eliminating the need for a separate
tree-rewriting procedure.
2025-06-19 07:18:21 -05:00
Andre Kuhlenschmidt
17f5b8b52a [flang][driver] add ability to look up feature flags without setting them (#144559)
This just adds some convenience methods to feature control and rewrites
old code in terms of those methods. Also cleans up some names that I
just realize were overloads of another method.
2025-06-18 11:21:35 -07:00
Krzysztof Parzyszek
4084ffcf1e [flang] Show types in DumpEvExpr (#143743)
When dumping evaluate::Expr, show type names which contain a lot of
useful information.

For example show
```
expr <Fortran::evaluate::SomeType> {
  expr <Fortran::evaluate::SomeKind<Fortran::common::TypeCategory::Integer>> {
    expr <Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 4>> {
      ...
```
instead of
```
expr T {
  expr T {
    expr T {
      ...
```
2025-06-18 11:31:03 -05:00
Krzysztof Parzyszek
5d502aeddf [flang][OpenMP] Clarify confusing error message (#144707)
The message "The atomic variable x should occur exactly once among the
arguments of the top-level [...] operator" was intended to convey that
(1) an atomic variable should be an argument, and (2) it should be
exactly one of the arguments. However, the wording turned out to be
sowing confusion instead.

Rework the corresponding check, and emit an individual error message for
each problematic situation:
- "atomic variable cannot be a proper subexpression of an argument",
- "atomic variable should appear as an argument",
- "atomic variable should be exactly one of the arguments".

Fixes https://github.com/llvm/llvm-project/issues/144599
2025-06-18 10:42:39 -05:00
Jack Styles
671caef379 [Flang][OpenMP] Update relevant warnings to emit when OMP >= v5.2 (#144492)
There has been a number of deprecation warnings that have been added to
Flang, however these features are only deprecated when the OpenMP
Version being used is 5.2 or later. Previously, flang did not consider
the version with the warnings so would always be emitted.

Flang now ensures warnings are emitted for the appropriate version of
OpenMP, and tests are updated to reflect this change.
2025-06-18 14:35:53 +01:00
Tom Eccles
a83d3362f6 [flang][OpenMP] Don't allow DO CONCURRENT inside of a loop nest (#144506)
I don't think DO CONCURRENT fits the definition of a Canonical Loop Nest
(OpenMP 6.0 section 6.4.1).
It is however explicitly allowed for the LOOP construct (6.0 section
13.8).

There's some obscure language in OpenMP 6.0 for the LOOP construct:

> If the collapsed loop is a DO CONCURRENT loop, neither the
> data-sharing attribute clauses nor the collapse clause may be
specified.

From the surrounding context, I think "collapsed loop" just means the
loop that the LOOP construct applies to. So I will interpret this to
mean that DO CONCURRENT can only be used with the LOOP construct if it
does not contain the COLLAPSE clause.

This also fixes a bug where the associated clause was never cleared
after it was set.

Fixes #144178
2025-06-18 14:02:11 +01:00
Krzysztof Parzyszek
4b2ab1494b [flang][OpenMP] Don't crash on iterator modifier in declare mapper (#144359)
Both the declare mapper directive argument, and the iterator modifier
can contain declaration-type-spec, so make sure that the processing of
one ends before processing of the other begins in semantic analysis.
2025-06-18 07:46:49 -05:00
Kareem Ergawy
59d6fbb8ff [flang][fir] Provide allocation block for fir.local when required (#144521)
Extends `fir::FirOpBuilder::getAllocaBlock()` to support `fir.local`.
This allows us to retrieve an allocation block when needed for
`fir.local`.
2025-06-18 10:24:08 +02:00
Slava Zakharin
70343c8d44 [mlir][flang] Added Weighted[Region]BranchOpInterface's. (#142079)
The new interfaces provide getters and setters for the weight
information about the branches of BranchOpInterface and
RegionBranchOpInterface operations.

These interfaces are done the same way as LLVM dialect's
BranchWeightOpInterface.

The plan is to produce this information in Flang, e.g. mark
most probably "cold" code as such and allow LLVM to order
basic blocks accordingly. An example of such a code is
copy loops generated for arrays repacking - we can mark it
as "cold" assuming that the copy will not happen dynamically.
If the copy actually happens the overhead of the copy is probably high
enough so that we may not care about the little overhead
of jumping to the "cold" code and fetching it.
2025-06-17 16:14:13 -07:00
Tom Eccles
cf637b7e35 [flang][OpenMP] Fix goto within SECTION (#144502)
Previously we didn't push any context for SECTION and they are not
modelled with differing scopes and so goto detection couldn't tell that
GOTOs between two SECTIONs were between constructs rather than just
staying inside of the parent SECTIONS construct.

Fixes #143231
2025-06-17 16:57:32 +01:00
Slava Zakharin
cd4e384339 [flang][test] Removed temporary workaround for buildbots. 2025-06-17 08:55:18 -07:00
Kajetan Puchalski
4cfe0d7f4c [flang][OpenMP] Support using copyprivate with fir.boxchar arguments (#144092)
Implement the lowering for passing a fir.boxchar argument to the
copyprivate clause.

Resolves https://github.com/llvm/llvm-project/issues/142123.

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-06-17 15:32:23 +01:00
Krzysztof Parzyszek
5f841a6284 [flang][OpenMP] Set _OPENMP macro for version 6.0 (#144410) 2025-06-17 07:41:20 -05:00
Kareem Ergawy
97e17e1595 Revert "[flang] Enable delayed localization by default for do concurrent (#144074)" (#144476)
This reverts commit b5dbf8210a.

Reverting again due to gfortran failure:
https://lab.llvm.org/buildbot/#/builders/17/builds/8868
2025-06-17 11:34:05 +02:00
Jack Styles
cb355def95 [Flang][OpenMP] Add Parsing support for Indirect Clause (#143505)
As part of OpenMP Version 5.1, support for the `indirect` clause was
added for the `declare target` directive. This clause should follow an
`enter` clause, and allows procedure calls to be done indirectly through
OpenMP.

This adds Parsing support for the clause, along with semantics checks.
Currently, lowering for the clause is not supported so a TODO message
will be outputted to the user. It also performs version checking as
`indirect` is only support in OpenMP 5.1 or greater.

See also: #110008
2025-06-17 09:05:36 +01:00