10266 Commits

Author SHA1 Message Date
Krzysztof Parzyszek
89822ff5a8 Revert "[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)"
This reverts commit 41aa67488c3ca33334ec79fb5216145c3644277c.

Breaks build: https://lab.llvm.org/buildbot/#/builders/140/builds/22826
2025-05-09 07:56:10 -05:00
Krzysztof Parzyszek
41aa67488c
[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)
The OpenMP version is stored in LangOptions in SemanticsContext. Use the
fallback version where SemanticsContext is unavailable (mostly in case
of debug dumps).

RFC:
https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507
2025-05-09 07:42:15 -05:00
agozillon
b291cfcad4
[Flang][OpenMP] Generate correct present checks for implicit maps of optional allocatables (#138210)
Currently, we do not generate the appropriate checks to check if an
optional
allocatable argument is present before accessing relevant components of
it,
in particular when creating bounds, we must generate a presence check
and we
must make sure we do not generate/keep an load external to the presence
check
by utilising the raw address rather than the regular address of the info
data structure.

Similarly in cases for optional allocatables we must treat them like
non-allocatable
arguments and generate an intermediate allocation that we can have as a
location
in memory that we can access later in the lowering without causing
segfaults when
we perform "mapping" on it, even if the end result is an empty
allocatable
(basically, we shouldn't explode if someone tries to map a non-present
optional,
similar to C++ when mapping null data).
2025-05-09 13:57:45 +02:00
Kaviya Rajendiran
dd42112c82
[Flang][OpenMP] MLIR lowering support for grainsize and num_tasks clause (#128490)
- Added MLIR lowering for grainsize and num_tasks clauses of taskloop construct.
2025-05-09 17:26:28 +05:30
Sergio Afonso
4923cffc1d
[Flang] Add missing dependency to AddDebugInfo pass (#139099)
The `AddDebugInfo` pass currently has a dependency on the `DLTI` MLIR
dialect caused by a call to the `fir::support::getOrSetMLIRDataLayout()`
utility function.

This dependency is not captured in the pass definition. This patch adds
the dependency and simplifies several unit tests that had to explicitly
use the `DLTI` dialect to prevent the missing dependency from causing
compiler failures.
2025-05-09 12:19:53 +01:00
jeanPerier
92d2e13b99
[flang][driver] do not crash when fc1 process multiple files (#138875)
This is a fix for the issue https://github.com/llvm/llvm-project/issues/137126
that turned out to be a driver issue.

FrontendActions has a loop to process multiple input files and `flang -fc1`
accept multiple files, but the semantic, lowering, and llvm codegen
actions were not re-entrant, and crash or weird behaviors occurred
when processing multiple files with `-fc1`. 

This patch makes the actions reentrant by cleaning-up the contexts/modules
if needed on entry.
2025-05-09 10:36:38 +02:00
Thirumalai Shaktivel
3b9b377f6d
[Flang] [OpenMP] Add semantic checks for detach clause in task (#119172)
Fixes:
- Add semantic checks along with the tests
- Move the detach clause to allowedOnceClauses list in Task construct

Restrictions:\

OpenMP 5.0: Task construct
- At most one detach clause can appear on the directive.
- If a detach clause appears on the directive, then a mergeable clause
cannot appear on the same directive.

OpenMP 5.2: Detach contruct
- If a detach clause appears on a directive, then the encountering task
must not be a final task.
- A variable that appears in a detach clause cannot appear as a list
item on a data-environment attribute clause on the same construct.
- A variable that is part of another variable (as an array element or a
structure element) cannot appear in a detach clause.
- event-handle must not have the POINTER attribute.
2025-05-09 10:55:54 +05:30
MingYan
db2d5762eb
[flang][fir] Support promoting fir.do_loop with results to affine.for. (#137790)
Co-authored-by: yanming <ming.yan@terapines.com>
2025-05-09 10:55:21 +08:00
Asher Mancinelli
02f61ab46b
[flang] Use box for components with non-default lower bounds (#138994)
When designating an array component that has non-default lower bounds
the bridge was producing hlfir designates yielding reference types,
which did not preserve the bounds information. Then, when creating
components, unadjusted indices were used when initializing the
structure.

We could look at the declaration to get the shape parameter, but this
would not be preserved if the component were passed as a block argument.
These results must be boxed, but we also must not lose the contiguity
information either. To address contiguity, annotate these boxes with the
`contiguous` attribute during designation.

Note that other designated entities are handled inside the
HlfirDesignatorBuilder while component designators are built in
HlfirBuilder. I am not sure if this handling should be moved into the
designator builder or left in the general builder, so feedback is
welcome.

Also, I wouldn't mind finding a test that demonstrates a box-designated
component with the contiguous attribute really is determined to be
contiguous by any passes down the line checking for that. I don't have a
test like that yet.
2025-05-08 13:08:08 -07:00
Kareem Ergawy
227e1ff73b
[flang][fir] Add locality specifiers modeling to fir.do_concurrent.loop (#138506) 2025-05-08 21:42:52 +02:00
Kareem Ergawy
5fe69fd95c
[flang][OpenMP] Update do concurrent mapping pass to use fir.do_concurrent op (#138489)
This PR updates the `do concurrent` to OpenMP mapping pass to use the
newly added `fir.do_concurrent` ops that were recently added upstream
instead of handling nests of `fir.do_loop ... unordered` ops.

Parent PR: https://github.com/llvm/llvm-project/pull/137928.
2025-05-08 20:22:29 +02:00
Tom Eccles
3ed158fab4
[flang][docs][OpenMP] array sections with DEPEND are supported (#139081)
This was added in
- https://github.com/llvm/llvm-project/pull/132230
- https://github.com/llvm/llvm-project/pull/132994
- https://github.com/llvm/llvm-project/pull/133892
2025-05-08 15:13:04 +01:00
Asher Mancinelli
30f7a6cc42
[flang] Correctly prepare allocatable runtime call arguments (#138727)
When lowering allocatables, the generated calls to runtime functions
were not using the runtime::createArguments utility which handles the
required conversions. createArguments is where I added the implicit
volatile casts to handle converting volatile variables to the
appropriate type based on their volatility in the callee. Because the
calls to allocatable runtime functions were not using this function,
their arguments were not casted to have the appropriate volatility.

Add a test to demonstrate that volatile and allocatable
class/box/reference types are appropriately casted before calling into
the runtime library.

Instead of using a recursive variadic template to perform the
conversions in createArguments, map over the arguments directly so that
createArguments can be called with an ArrayRef of arguments. Some cases
in Allocatable.cpp already had a vector of values at the point where
createArguments needed to be called - the new overload allows calling
with a vector of args or the variadic version with each argument spelled
out at the callsite.

This change resulted in the allocatable runtime calls having their
arguments converted left-to-right, which changed some of the test
results. I used CHECK-DAG to ignore the order.

Add some missing handling of volatile class entities, which I previously
missed because I had not yet enabled volatile class entities in Lower.
2025-05-08 06:36:39 -07:00
Tom Eccles
e40200901c
[mlir][OpenMP] cancel(lation point) taskgroup LLVMIR (#137841)
A cancel or cancellation point for taskgroup is always nested inside of
a task inside of the taskgroup. For the task which is cancelled, it is
that task which needs to be cleaned up: not the owning taskgroup.
Therefore the cancellation branch handler is done in the conversion of
the task not in conversion of taskgroup.

I added a firstprivate clause to the test for cancel taskgroup to
demonstrate that the block being branched to is the same block where
mandatory cleanup code is added. Cancellation point follows exactly the
same code path.
2025-05-08 11:15:58 +01:00
Tom Eccles
2a32d738bb
[flang][OpenMP] fix predetermined privatization inside section (#138159)
This now produces code equivalent to if there was an explicit private
clause on the SECTIONS construct.

The problem was that each SECTION construct got its own DSP, which tried
to privatize the same symbol for that SECTION. Privatization for
SECTION(S) happens on the outer SECTION construct and so the outer
construct's DSP should be shared.

Fixes #135108
2025-05-08 10:08:49 +01:00
Jan Patrick Lehr
b8461acc5e
[Flang] Fix driver test after #125643 (#138959) 2025-05-07 23:01:14 +02:00
Valentin Clement (バレンタイン クレメン)
1a7cd92c86
[flang][cuda] Update syncthreads interface (#138023) 2025-05-07 21:56:11 +02:00
Kelvin Li
9a2d602187
[flang][AIX] Predefine __64BIT__ and _AIX macros (#138591) 2025-05-07 15:33:23 -04:00
Zhen Wang
ce69a60bc2
Skip contiguous check when ignore_tkr(c) is used (#138762)
The point of ignore_tkr(c) is to ignore both contiguous warnings and
errors for arguments of all attribute types.
2025-05-07 09:23:43 -07:00
Kareem Ergawy
a83bb35e99
[flang][fir] Add fir.local op for locality specifiers (#138505)
Adds a new `fir.local` op to model `local` and `local_init` locality
specifiers. This op is a clone of `omp.private`. In particular, this new
op also models the privatization/localization logic of an SSA value in
the `fir` dialect just like `omp.private` does for OpenMP.

PR stack:
- https://github.com/llvm/llvm-project/pull/137928
- https://github.com/llvm/llvm-project/pull/138505 (this PR)
- https://github.com/llvm/llvm-project/pull/138506
- https://github.com/llvm/llvm-project/pull/138512
- https://github.com/llvm/llvm-project/pull/138534
- https://github.com/llvm/llvm-project/pull/138816
2025-05-07 14:00:06 +02:00
Kareem Ergawy
2fb288d4b8
[flang][fir] Lower do concurrent loop nests to fir.do_concurrent (#137928)
Adds support for lowering `do concurrent` nests from PFT to the new
`fir.do_concurrent` MLIR op as well as its special terminator
`fir.do_concurrent.loop` which models the actual loop nest.

To that end, this PR emits the allocations for the iteration variables
within the block of the `fir.do_concurrent` op and creates a region for
the `fir.do_concurrent.loop` op that accepts arguments equal in number
to the number of the input `do concurrent` iteration ranges.

For example, given the following input:
```fortran
   do concurrent(i=1:10, j=11:20)
   end do
```
the changes in this PR emit the following MLIR:
```mlir
    fir.do_concurrent {
      %22 = fir.alloca i32 {bindc_name = "i"}
      %23:2 = hlfir.declare %22 {uniq_name = "_QFsub1Ei"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
      %24 = fir.alloca i32 {bindc_name = "j"}
      %25:2 = hlfir.declare %24 {uniq_name = "_QFsub1Ej"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
      fir.do_concurrent.loop (%arg1, %arg2) = (%18, %20) to (%19, %21) step (%c1, %c1_0) {
        %26 = fir.convert %arg1 : (index) -> i32
        fir.store %26 to %23#0 : !fir.ref<i32>
        %27 = fir.convert %arg2 : (index) -> i32
        fir.store %27 to %25#0 : !fir.ref<i32>
      }
    }
```
2025-05-07 12:52:25 +02:00
Tom Eccles
75e5643abf
[flang][OpenMP] share global variable initialization code (#138672)
Fixes #108136

In #108136 (the new testcase), flang was missing the length parameter
required for the variable length string when boxing the global variable.
The code that is initializing global variables for OpenMP did not
support types with length parameters.

Instead of duplicating this initialization logic in OpenMP, I decided to
use the exact same initialization as is used in the base language
because this will already be well tested and will be updated for any new
types. The difference for OpenMP is that the global variables will be
zero initialized instead of left undefined.

Previously `Fortran::lower::createGlobalInitialization` was used to
share a smaller amount of the logic with the base language lowering. I
think this bug has demonstrated that helper was too low level to be
helpful, and it was only used in OpenMP so I have made it static inside
of ConvertVariable.cpp.
2025-05-07 10:18:13 +01:00
Kiran Chandramohan
a13c0b6770
[Flang][OpenMP] Add frontend support for declare variant (#130578)
Support is added for parsing. Basic semantics support is added to
forward the code to Lowering. Lowering will emit a TODO error. Detailed
semantics checks and lowering is further work.
2025-05-07 09:56:45 +01:00
NimishMishra
e1fed24034
[flang][OpenMP] Fix fir.convert in omp.atomic.update region (#138397)
Region generation in omp.atomic.update currently emits a direct
`fir.convert`. This crashes when the RHS expression involves complex
type but the LHS variable is primitive type (say `f32`), since a
`fir.convert` from `complex<f32>` to `f32` is emitted, which is illegal.
This PR adds a conditional check to emit an additional `ExtractValueOp`
in case RHS expression has a complex type.

Fixes https://github.com/llvm/llvm-project/issues/138396
2025-05-06 22:44:48 -07:00
Kaviya Rajendiran
9e7d529607
[Flang][OpenMP]Support for lowering task_reduction and in_reduction to MLIR (#111155)
This patch,
- Added support for lowering of task_reduction to MLIR
- Added support for lowering of in_reduction to MLIR
- Fixed incorrect DSA handling for variables in the presence of 'in_reduction' clause.
2025-05-07 10:25:56 +05:30
Krzysztof Parzyszek
304c7a87d0
[flang][Evaluate] Restrict ConstantBase constructor overload (#138456)
ConstantBase has a constructor that takes a value of any type as an
input: template <typename T> ConstantBase(const T &). A derived type
Constant<T> is a member of many Expr<T> classes (as an alternative in
the member variant).

When trying (erroneously) to create Expr<T> from a wrong input, if the
specific instance of Expr<T> contains Constant<T>, it's that constructor
that will be instantiated, leading to cryptic and confusing errors.

Eliminate the constructor from overload for invalid input values to help
produce more meaningful diagnostics.
2025-05-06 12:00:29 -05:00
Krzysztof Parzyszek
96e09302f9
[flang][Evaluate] Fix AsGenericExpr for Relational (#138455)
The variant in Expr<Type<TypeCategory::Logical, KIND>> only contains
Relational<SomeType>, not other, more specific Relational<T> types.

When calling AsGenericExpr for a value of type Relational<T>, the AsExpr
function will attempt to create Expr<> directly for Relational<T>, which
won't work for the above reason.

Implement an overload of AsExpr for Relational<T>, which will wrap the
Relational<T> in Relational<SomeType> before creating Expr<>.
2025-05-06 11:59:22 -05:00
Asher Mancinelli
368fbc2163
[flang] Lower volatile class types (#138607)
So far, only boxes and references have had their volatile attribute set
during lowering. This patch enables the volatility of classes to be
properly represented in the ir, same as box and ref.

For simple cases, not much needs to change in the codegen or conversion
patterns because the prior work on volatile refs/boxes propagates
volatility already. I am running further testing with the strict
verification enabled to find remaining cases of incorrect/missing
volatile propagation.
2025-05-06 07:38:00 -07:00
Kaviya Rajendiran
e356893551
[Flang][OpenMP] Support for lowering of taskloop construct to MLIR (#138646)
Added support for lowering of taskloop construct and its clauses(Private
and Firstprivate) to MLIR.
2025-05-06 19:47:22 +05:30
Paul Walker
cb9683fad1
[Clang][Flang][Driver] Fix target parsing for -fveclib=libmvec option. (#138288)
There are various places where the -fveclib option is parsed to
determine whether its value is correct for the target. Unfortunately
these places assume case-insensitivity and subsequently use "LIBMVEC"
where the driver mandates "libmvec", thus rendering the diagnosistic
useless.

This PR corrects the naming along with similar incorrect uses within the
test files.
2025-05-06 11:57:04 +01:00
Asher Mancinelli
a04ab7b81f
[flang][nfc] Fix test unneccesarily checking type layout (#138585)
Test added in #138339 unneccesarily had CHECK lines with the type
layout, which fails on aix.
2025-05-05 15:22:34 -07:00
Asher Mancinelli
c0e52f3ec7
[flang] Component references are volatile if their parent is (#138339)
Component references inherit volatility from their base derived types.
Moved the base type volatility check before the box type is built, and
merge it (instead of overwrite it) with the volatility of the base type.
2025-05-05 10:03:54 -07:00
Asher Mancinelli
8870ce1aa0
[flang][docs] Add note about Cray pointers and the TARGET attribute (#137993)
We found some tests checking for loops assigning between Cray pointer
handles and their pointees which produced "incorrect" results with
optimizations enabled; this is because the compiler expects Cray
pointers not to alias with any other entity.

[The HPE documentation for Cray Fortran extensions
specifies:](https://support.hpe.com/hpesc/public/docDisplay?docId=a00113911en_us&docLocale=en_US&page=Types.html#cray-poiter-type)

> the compiler assumes that the storage of a pointee is
> never overlaid on the storage of another variable

Jean pointed out that if a user's code uses entities that alias via Cray
pointers, they may add the TARGET attribute to inform Flang of this
aliasing, but that Flang's behavior is in line with Cray's own
documentation and we should not make any changes to our alias analysis
to try and detect this case.

Updating documentation so that users that encounter this situation have
a way to allow their code to compile as they intend.
2025-05-05 08:32:08 -07:00
Asher Mancinelli
7220fdad0c
[flang] Hide strict volatility checks behind flag (#138183)
Enabling volatility lowering by default revealed some issues in lowering
and op verification.

For example, given volatile variable of a nested type, accessing
structure members of a structure member would result in a volatility
mismatch when the inner structure member is designated (and thus a
verification error at compile time).

In other cases, I found correct codegen when the checks were disabled,
also related to allocatable types and how we handle volatile references
of boxes.

This hides the strict verification of fir and hlfir ops behind a flag so
I can iteratively improve lowering of volatile variables without causing
compile-time failures, keeping the strict verification on when running
tests.
2025-05-02 09:03:20 -07:00
Anchu Rajendran S
580da48a93
[flang][flang-driver] Support flag -finstrument-functions (#137996) 2025-05-02 07:38:44 -07:00
Andre Kuhlenschmidt
a18adb2358
[flang] fix scoping of cray pointer declarations and add check for initialization (#136776)
This PR:
- makes Cray pointer declarations shadow previous bindings instead of
modifying them,
- errors when the pointee of a cray pointee has the SAVE attribute, and
- adds a missing newline after dumping the list of cray pointers in a
scope.

Closes #135579
2025-05-02 07:35:24 -07:00
Reilly Brogan
1ab70fed62
[flang] Enable installing binding header and modules as part of distribution (#133962)
This allows the C/Fortran interop header (`ISO_Fortran_binding.h`) and
the Fortran module interfaces (`.mod` files) to be installed with
`LLVM_DISTRIBUTION_COMPONENTS`.

Signed-off-by: Reilly Brogan <reilly@reillybrogan.com>
2025-05-02 15:24:02 +01:00
Kelvin Li
8f06f5dca0
[flang] update LIT test for AIX (NFC) (#138228) 2025-05-02 01:01:30 -04:00
Eugene Epshteyn
36541ec3ca
[flang] Fix #else with trailing text (#138045)
Fixed the issue, where the extra text on #else line (' Z' in the example
below) caused the data from the "else" clause to be processed together
with the data of "then" clause.
```
#ifndef XYZ42 
      PARAMETER(A=2)
#else Z
      PARAMETER(A=3)
#endif
      end
```
2025-05-01 23:07:52 -04:00
Valentin Clement (バレンタイン クレメン)
9b6b144438
Revert "[flang][cuda] Use a reference for asyncObject" (#138221)
Reverts llvm/llvm-project#138186
2025-05-01 17:41:44 -07:00
Valentin Clement (バレンタイン クレメン)
7f922f1400
[flang][cuda] Use a reference for asyncObject (#138186)
Switch from `int64_t` to `int64_t*` to fit with the rest of the
implementation.

New tentative with some fix. The previous was reverted yesterday.
2025-05-01 17:04:12 -07:00
Andre Kuhlenschmidt
42f5d716cb
[flang][frontend] warn when a volatile target is pointer associated with an non-volatile pointer (#136778)
closes #135805
2025-05-01 13:45:13 -07:00
Krzysztof Parzyszek
760bba4666
[flang][OpenMP] Allow UPDATE clause to not have any arguments (#137521)
The UPDATE clause can be specified on both ATOMIC and DEPOBJ directives.
Currently, the ATOMIC directive has its own handling of it, and the
definition of the UPDATE clause only supports its use in the DEPOBJ
directive, where it takes a dependence-type as an argument.

The UPDATE clause on the ATOMIC directive may not have any arguments.
Since the implementation of the ATOMIC construct will be modified to use
the standard handling of clauses, the definition of UPDATE should
reflect that.
2025-05-01 13:30:45 -05:00
Krzysztof Parzyszek
35c76eb195
[flang][OpenMP] Always set "openmp_flags" (#138153)
Many OpenMP tests use "%openmp_flags" in the RUN line. In many OpenMP
lit tests this variable is expected to at least have "-fopenmp" in it.
However, in the lit config this variable was only given a value when the
OpenMP runtime build was enabled.

If the runtime build was not enabled, %openmp_flags would expand to an
empty string, and unless a lit test specifically used -fopenmp in the
RUN line, OpenMP would be disabled.

This patch sets %openmp_flags to start with "-fopenmp" regardless of the
build configuration.
2025-05-01 12:10:15 -05:00
NimishMishra
c617466501
[flang][llvm][OpenMP] Add implicit casts to omp.atomic (#131603)
Currently, implicit casts in Fortran are handled by the OMPIRBuilder.
This patch shifts that responsibility to FIR codegen.
2025-05-01 08:20:42 -07:00
Valentin Clement (バレンタイン クレメン)
01a18809ee
Revert "[flang][cuda] Use a reference for asyncObject (#138010)" (#138082)
This reverts commit 9b0eaf71e674a28ee55be3afa11b5f7d4da732c0.
2025-04-30 22:03:26 -07:00
Valentin Clement (バレンタイン クレメン)
16f01b3777
[flang][cuda] Fix signatures after argument change (#138081) 2025-04-30 21:40:12 -07:00
Valentin Clement (バレンタイン クレメン)
9b0eaf71e6
[flang][cuda] Use a reference for asyncObject (#138010)
Switch from `int64_t` to `int64_t*` to fit with the rest of the
implementation.
2025-04-30 14:02:29 -07:00
Slava Zakharin
7dad8b91bc
[flang] Fetch the initial reduction value from the input array. (#136790)
Instead of using loop-carried IsFirst predicate, we can fetch
the initial reduction values for MIN/MAX LOC/VAL reductions
from the array itself. This results in a little bit cleaner
loop nests, especially, generated for total reductions.
Otherwise, LLVM is able to peel the first iteration of the innermost
loop, but the surroudings of the peeled code are executed
multiple times withing the outer loop(s).

This patch does the manual peeling, which only works for
non-masked reductions where the input array is not empty.
2025-04-30 13:53:26 -07:00
Asher Mancinelli
8836bce842
[flang] Add lowering of volatile references (#132486)
[RFC on
discourse](https://discourse.llvm.org/t/rfc-volatile-representation-in-flang/85404/1)

Flang currently lacks support for volatile variables. For some cases,
the compiler produces TODO error messages and others are ignored. Some
of our tests are like the example from _C.4 Clause 8 notes: The VOLATILE
attribute (8.5.20)_ and require volatile variables.

Prior commits:
```
c9ec1bc753b0 [flang] Handle volatility in lowering and codegen (#135311)
e42f8609858f [flang][nfc] Support volatility in Fir ops (#134858)
b2711e1526f9 [flang][nfc] Support volatile on ref, box, and class types (#134386)
```
2025-04-30 08:46:33 -07:00