2081 Commits

Author SHA1 Message Date
Tom Eccles
f84070b2b0 [flang][OpenMP] move omp end sections validation to semantics
See #90452. The old parse tree errors exploded to thousands of unhelpful
lines when there were multiple missing end directives.

Instead, allow a missing end directive in the parse tree then validate
that it is present during semantics (where the error messages are a lot
easier to control).
2025-08-21 15:51:10 +00:00
Kazu Hirata
8a5b6b302e
[flang] Use SmallPtrSet directly instead of SmallSet (NFC) (#154471)
I'm trying to remove the redirection in SmallSet.h:

template <typename PointeeType, unsigned N>
class SmallSet<PointeeType*, N> : public SmallPtrSet<PointeeType*, N>
{};

to make it clear that we are using SmallPtrSet.  There are only
handful places that rely on this redirection.

This patch replaces SmallSet to SmallPtrSet where the element type is
a pointer.
2025-08-20 16:30:24 -07:00
Slava Zakharin
6f489fb5e5
Reapply "[flang] Lower EOSHIFT into hlfir.eoshift." (#153907) (#154241)
This reverts commit 5178aeff7b96e86b066f8407b9d9732ec660dd2e.

In addition:
  * Scalar constant UNSIGNED BOUNDARY is explicitly casted
    to the result type so that the generated hlfir.eoshift
    operation is valid. The lowering produces signless constants
    by default. It might be a bigger issue in lowering, so I just
    want to "fix" it for EOSHIFT in this patch.
  * Since we have to create unsigned integer constant during
    HLFIR inlining, I added code in createIntegerConstant
    to make it possible.
2025-08-19 08:36:14 -07:00
Krzysztof Parzyszek
42350f428d
[flang][OpenMP] Parse GROUPPRIVATE directive (#153807)
No semantic checks or lowering yet.
2025-08-19 08:32:43 -05:00
Leandro Lupori
ddb36a8102
[flang] Preserve dynamic length of characters in ALLOCATE (#152564)
Fixes #151895
2025-08-19 09:25:08 -03:00
Kareem Ergawy
0e93dbc6b1
[flang] do concurrent: Enable delayed localization by default (#154303)
Enables delayed localization by default for `do concurrent`. Tested both
gfortran and Fujitsu test suites.

All tests pass for gfortran tests. For Fujitsu, enabled delayed
localization passes more tests:

Delayed localization disabled:
Testing Time: 7251.76s
  Passed            : 88520
  Failed            :   162
  Executable Missing:   408

Delayed localization enabled:
Testing Time: 7216.73s
  Passed            : 88522
  Failed            :   160
  Executable Missing:   408
2025-08-19 12:07:17 +02:00
Krzysztof Parzyszek
8429f7faaa
[flang][OpenMP] Parsing support for DYN_GROUPPRIVATE (#153615)
This does not perform semantic checks or lowering.
2025-08-18 13:35:02 -05:00
Kareem Ergawy
c1e2a9c66d
[flang][OpenMP] Only privaize pre-determined symbols when defined the evaluation. (#154070)
Fixes a regression uncovered by Fujitsu test 0686_0024.f90. In
particular, verifies that a pre-determined symbol is only privatized by
its defining evaluation (e.g. the loop for which the symbol was marked
as pre-determined).
2025-08-18 13:36:08 +02:00
Slava Zakharin
5178aeff7b
Revert "[flang] Lower EOSHIFT into hlfir.eoshift." (#153907)
Reverts llvm/llvm-project#153106

Buildbots failing:
* https://lab.llvm.org/buildbot/#/builders/199/builds/5188
* https://lab.llvm.org/buildbot/#/builders/41/builds/8329
2025-08-15 17:48:40 -07:00
Jean-Didier PAILLEUX
acdbb00af5
[flang] Adding support of -fcoarray flang and init PRIF (#151675)
In relation to the approval and merge of the
[PRIF](https://github.com/llvm/llvm-project/pull/76088) specification
about multi-image features in Flang, here is a first PR to add support
for the `-fcoarray` compilation flag and the initialization of the PRIF
environment.
Other PRs will follow for adding support of lowering to PRIF.
2025-08-15 16:04:49 -07:00
Slava Zakharin
25285b3476
[flang] Lower EOSHIFT into hlfir.eoshift. (#153106)
Straightforward lowering of EOSHIFT intrinsic into the new hlfir.eoshift
operation.
2025-08-15 13:55:05 -07:00
Kareem Ergawy
b9e33fd493
[flang] Do not re-localize loop ivs when nested inside blocks (#153350)
Consider the following example:
```fortran
  implicit none
  integer :: i, j

  do concurrent (i=1:10) local(j)
    block
      do j=1,20
      end do
    end block
  end do
```

Without the fix introduced in this PR, the compiler would "re-localize"
the `j` variable inside the `fir.do_concurrent` loop:
```mlir
    fir.do_concurrent {
      %7 = fir.alloca i32 {bindc_name = "j"}
      %8:2 = hlfir.declare %7 {uniq_name = "_QFloop_in_nested_blockEj"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
      ...
      fir.do_concurrent.loop (%arg0) = (%5) to (%6) step (%c1) local(@_QFloop_in_nested_blockEj_private_i32 %4#0 -> %arg1 : !fir.ref<i32>) {
        %12:2 = hlfir.declare %arg1 {uniq_name = "_QFloop_in_nested_blockEj"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
        ...
        %17:2 = fir.do_loop %arg2 = %14 to %15 step %c1_1 iter_args(%arg3 = %16) -> (index, i32) {
          fir.store %arg3 to %8#0 : !fir.ref<i32>
          ...
        }
      }
    }
```

This happened because we did a shallow look-up of `j` and since the loop
is nested inside a `block`, the look-up failed and we re-created a local
allocation for `j` inside the parent `fir.do_concurrent` loop. This
means that we ended up not using the actual localized symbol which is
passed as a region argument to the `fir.do_concurrent.loop` op.

In case of `j`, we do not need to do a shallow look-up. The shallow
look-up is only needed if a symbol is an OpenMP private one or an
iteration variable of a `do concurrent` loop. Neither of which applies
to `j`.

With the fix, `j` is properly resolved to the `local` region argument:
```mlir
    fir.do_concurrent {
      ...
      fir.do_concurrent.loop (%arg0) = (%5) to (%6) step (%c1) local(@_QFloop_in_nested_blockEj_private_i32 %4#0 -> %arg1 : !fir.ref<i32>) {
        ...
        %10:2 = hlfir.declare %arg1 {uniq_name = "_QFloop_in_nested_blockEj"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
        ...
        %15:2 = fir.do_loop %arg2 = %12 to %13 step %c1_1 iter_args(%arg3 = %14) -> (index, i32) {
          fir.store %arg3 to %10#0 : !fir.ref<i32>
          ...
        }
      }
    }
```
2025-08-15 08:45:02 +02:00
Kajetan Puchalski
d3d96e2057
[flang][OpenMP] Add -f[no]-openmp-simd (#150269)
Both clang and gfortran support the -fopenmp-simd flag, which enables
OpenMP support only for simd constructs, while disabling the rest of
OpenMP.

Implement the appropriate parse tree rewriting to remove non-SIMD OpenMP
constructs at the parsing stage.

Add a new SimdOnly flang OpenMP IR pass which rewrites generated OpenMP
FIR to handle untangling composite simd constructs, and clean up OpenMP
operations leftover after the parse tree rewriting stage.
With this approach, the two parts of the logic required to make the flag
work can be self-contained within the parse tree rewriter and the MLIR
pass, respectively. It does not need to be implemented within the core
lowering logic itself.

The flag is expected to have no effect if -fopenmp is passed explicitly,
and is only expected to remove OpenMP constructs, not things like OpenMP
library functions calls. This matches the behaviour of other compilers.

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-08-14 14:20:15 +01:00
Valentin Clement (バレンタイン クレメン)
f9b9e9b7d5
[flang][cuda] Fix buildbot failure after #153242 (#153500) 2025-08-13 14:54:33 -07:00
Valentin Clement (バレンタイン クレメン)
a2899c457e
[flang][cuda] Support data transfer with conversion (#153242)
When the rhs of the data transfer is from a different type, allocate a
new temp on the host and first transfer the rhs to it. Then, use the
elemental op created to do the conversion.
2025-08-13 10:55:15 -07:00
Tom Eccles
d02ad56015
[flang][OpenMP] Catch assumed-rank/size variables for privatization a… (#152764)
…nd reduction

Fixes #152312

Assumed size is valid for privatization and simple examples generate
sensible looking HLFIR.
2025-08-13 16:59:17 +01:00
Krzysztof Parzyszek
4b7f3806f6
[flang][OpenMP] Move rewriting of min/max from Lower to Semantics (#153038)
There semantic analysis of the ATOMIC construct will require additional
rewriting (reassociation of certain expressions for user convenience),
and that will be driven by diagnoses made in the semantic checks.

While the rewriting of min/max is not required to be done in semantic
analysis, moving it there will make all rewriting for ATOMIC construct
be located in a single location.
2025-08-12 12:13:50 -05:00
Kareem Ergawy
31387d69e2
[flang][OpenMP] Don't privatize implicit symbols declare by nested BLOCKs (#152973)
Fixes a bug when a block variable is marked as implicit private. In such
case, we can simply ignore privatizing that symbol within the context of
the currrent OpenMP construct since the "private" allocation for the
symbol will be emitted within the nested block anyway.
2025-08-12 06:27:42 +02:00
Akash Banerjee
a9f9c7db4f Fix build error from #151513. 2025-08-11 13:16:39 +01:00
Akash Banerjee
3b10b9a2b0
[MLIR][OpenMP] Add lowering support for AUTOMAP modifier (#151513)
Add Automap modifier to the MLIR op definition for the DeclareTarget
directive's Enter clause. Also add lowering support in Flang.

Automap Ref: OpenMP 6.0 section 7.9.7.
2025-08-11 12:45:22 +01:00
Kareem Ergawy
98ffdf3958
[flang][OpenMP] Don't privatize pre-determined symbols declare by nested BLOCKs (#152652)
Fixes a bug when a block variable is marked as pre-determined private.
In such case, we can simply ignore privatizing that symbol within the
context of the currrent OpenMP construct since the "private" allocation
for the symbol will be emitted within the nested block anyway.
2025-08-11 10:21:07 +02:00
Kareem Ergawy
d7d0d7a80f
[flang] Skip processing reductions for unstructured do concurrent loops (#150188)
Fixes #149563

When emitting unstructured `do concurrent` loops, reduction processing
should be skipped since we are not emitting `fir.do_concurrent` loop in
the first place.
2025-08-08 06:22:53 +02:00
Krzysztof Parzyszek
e368b5343d
[flang][OpenMP] Make OpenMPCriticalConstruct follow block structure (#152007)
This allows not having the END CRITICAL directive in certain situations.
Update semantic checks and symbol resolution.
2025-08-07 08:10:25 -05:00
Valentin Clement (バレンタイン クレメン)
35f003d13b
[flang][cuda] Fix buildbot after #152418 (#152437) 2025-08-06 22:24:35 -07:00
Valentin Clement (バレンタイン クレメン)
eb0ddba26b
Reland "[flang][cuda] Set the allocator of derived type component after allocation" (#152418)
Reviewed in #152379
- Move the allocator index set up after the allocate statement otherwise
the derived type descriptor is not allocated.
- Support array of derived-type with device component
2025-08-06 21:49:55 -07:00
Valentin Clement (バレンタイン クレメン)
2696e8c149
[flang][cuda] Remove too restrictive assert for data transfer (#152398)
When the rhs is a an array element, the assert was triggered but this is
still a valid transfer. Remove the assert. The operation has a verifier
to check its validity.
2025-08-06 18:49:52 -07:00
Valentin Clement (バレンタイン クレメン)
7d3134f6cc
Revert "[flang][cuda] Set the allocator of derived type component after allocation" (#152402)
Reverts llvm/llvm-project#152379

Buildbot failure
https://lab.llvm.org/buildbot/#/builders/207/builds/4905
2025-08-06 15:55:53 -07:00
Valentin Clement (バレンタイン クレメン)
d897355876
[flang][cuda] Set the allocator of derived type component after allocation (#152379)
- Move the allocator index set up after the allocate statement otherwise
the derived type descriptor is not allocated.
- Support array of derived-type with device component
2025-08-06 15:14:00 -07:00
agozillon
255be51f3f [Flang][OpenMP] Fix missing dereference in isConstructWithTopLevelTarget
This was causing false negatives, yielding incorrect programs.
2025-08-05 23:46:31 -05:00
agozillon
88283a6e46 [Flang][OpenMP] Fix MSVC missing include caused by #147442
Windows/MSVC build bots are having issues with a missing header where
linux is not. So, attempt to fix by adding missing header explicitly to file.
2025-08-05 10:13:32 -05:00
Valentin Clement (バレンタイン クレメン)
3847620ba9
[flang][NFC] Move the rest of ops creation to new APIs (#152079) 2025-08-05 07:27:43 -07:00
Carlos Seo
47ef3d069b
[Flang] Avoid crash when a function return is undefined (#151577)
Properly terminate the StatementContext cleanup when a function return
value is undefined.

Fixes #126452
2025-08-05 10:53:18 -03:00
agozillon
cc2a385e65
[Flang][OpenMP] Make implicitly captured scalars fully firstprivatized (#147442)
Currently, we indicate to the runtime that implicit scalar captures are
firstprivate (via map and
capture types), enough for the runtime trace to treat it as such, but we
do not CodeGen the IR
in such a way that we can take full advantage of this aspect of the
OpenMP specification.

This patch seeks to change that by applying the correct symbol flags
(firstprivate/implicit) to the
implicitly captured scalars within target regions, which then triggers
the delayed privitization code
generation for these symbols, bringing the code generation in-line with
the explicit firstpriviate
clause. Currently, similarly to the delayed privitization I have
sheltered this segment of code
behind the EnabledDelayedPrivitization flag, as without it, we'll
trigger an compiler error for
firstprivate not being supported any time we implicitly capture a scalar
and try to firstprivitize
it, in future when this flag is removed it can also be removed here. So,
for now, you need to
enable this via providing the compiler the flag on compilation of any
programs.
2025-08-05 15:48:37 +02:00
Valentin Clement (バレンタイン クレメン)
e4d3dc6359
[flang][NFC] Update HLFIR ops creation to the new APIs (#152075)
See #147168
2025-08-04 22:09:08 -07:00
Valentin Clement (バレンタイン クレメン)
3b23fdb35d
[flang][NFC] Update more FIR op creation to the new APIs (#152060) 2025-08-04 17:53:44 -07:00
Valentin Clement (バレンタイン クレメン)
9b195dc3ef
[flang][cuda] Generate cuf.allocate for descriptor with CUDA components (#152041)
The descriptor for derived-type with CUDA components are allocated in
managed memory. The lowering was calling the standard runtime on
allocate statement where it should be a `cuf.allocate` operation.
2025-08-04 16:51:11 -07:00
Valentin Clement (バレンタイン クレメン)
05b52ef909
[flang][cuda][NFC] Update to the new create APIs (#152050)
Some operation creations were updated in flang directory but not all.
Migrate the CUF ops to the new create APIs introduce in #147168
2025-08-04 16:09:24 -07:00
Carlos Seo
9bb31e8f88
[Flang] Fix crash when a derived type with private attribute is specified in extends (#151051)
While lowering to HLFIR, when a parent type is private, its name is
mangled, so we need to get it from the parent symbol.

Fixes #120922
2025-08-04 10:38:15 -03:00
Tom Eccles
8cc4c6d78f
[flang][Lower] Make reduction processing failure a hard error (#150233)
See #150178

This may regress some test cases which only ever passed by accident.

I've tested SPEC2017 and a sample of applications to check that this
doesn't break anything too obvious. Presumably this was not a widely
used feature or we would have noticed the bug sooner.

I'm unsure whether this should be backported to LLVM 21 or not: I think
it is much better to refuse to compile than to silently produce the
wrong result, but there is a chance this could regress something which
previously worked by accident. Opinions welcome.
2025-08-04 12:01:27 +01:00
Connector Switch
8b7f81f2de
[NFC] Fix assignment typo. (#151864) 2025-08-03 22:32:00 +08:00
Razvan Lupusoru
2f33b01651
[flang] Ensure lowering diagnostic handler does not outlive lowering (#151608)
When the LoweringBridge is created, it registers an MLIR Diagnostics
handler with the MLIRContext. However, it never deregisters it once
lowering is finished.

This fixes this particular scenario. It also makes it so that the
Diagnostics handler is optional.
2025-08-01 09:27:36 -07:00
Carlos Seo
89e4d9f905
[Flang] Fix crash with parametrized derived types usage (#150289)
The current mangleName implementation doesn't take a FoldingContext,
which prevents the proper evaluation of expressions containing parameter
references to an integer constant. Since parametrized derived types are
not yet implemented, the compiler will crash there in some cases (see
example in issue #127424).

This is a workaround so that doesn't happen until the feature is
properly implemented.

Fixes #127424
2025-08-01 11:20:09 -03:00
Krzysztof Parzyszek
6533ad04ed
[flang][OpenMP] Make all block constructs share the same structure (#150956)
The structure is
- OmpBeginDirective (aka OmpDirectiveSpecification)
- Block
- optional<OmpEndDirective> (aka optional<OmpDirectiveSpecification>)

The OmpBeginDirective and OmpEndDirective are effectively different
names for OmpDirectiveSpecification. They exist to allow the semantic
analyses to distinguish between the beginning and the ending of a block
construct without maintaining additional context.

The actual changes are in the parser: parse-tree.h and openmp-parser.cpp
in particular. The rest is simply changing the way the directive/clause
information is accessed (typically for the simpler).

All standalone and block constructs now use OmpDirectiveSpecification to
store the directive/clause information.
2025-08-01 07:52:59 -05:00
Kajetan Puchalski
a361cde442
[flang][OpenMP] Support delayed privatisation for composite distribute simd (#151169)
Implement the lowering for delayed privatisation for composite
"distibute simd"constructs. Fixes new crashes previously masked by simd
information on composite constructs being ignored.

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-08-01 13:12:57 +01:00
Kajetan Puchalski
c0591477ac
[flang][OpenMP] Support delayed privatisation for composite do simd (#150979)
Implement the lowering for delayed privatisation for composite "do simd"
constructs. Fixes new crashes previously masked by simd information on
composite constructs being ignored, such as llvm#150975.

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-08-01 13:12:21 +01:00
Akash Banerjee
9fdd1d3d46
[Flang] Add parser support for AUTOMAP modifier (#151511)
Add parser support for the new AUTOMAP modifier for OpenMP Declare
Target Enter clause introduced in OpenMP 6.0 section 7.9.7.
2025-07-31 15:56:16 +01:00
Krzysztof Parzyszek
f4972a2add
[flang][OpenMP] Use GetOmpDirectiveName to find directive source location (#150955) 2025-07-31 08:20:03 -05:00
Krzysztof Parzyszek
6984922905
[flang][OpenMP] Store directive information in OpenMPSectionConstruct (#150804)
The OpenMPSectionConstruct corresponds to the `!$omp section` directive,
but there is nothing in the AST node that stores the directive
information. Even though the only possibility (at the moment) is
"section" without any clauses, for improved generality it is helpful to
have that information anyway.
2025-07-31 07:51:22 -05:00
Andre Kuhlenschmidt
062b22e462
[flang][openacc] Add semantic checks for atomic constructs (#149579)
An error report of the following code generating non-atomic code led us
to realize there are missing checks in the OpenACC atomics code. Add
some of those checks for atomic and sketch how the rest of the code
should proceed in checking the rest of the properties. The following
cases are all reported as errors.
```fortran
! Originally reported error!
!$acc atomic capture
a = b
c = b
!$acc end atomic capture
! Other ambiguous, but related errors!
!$acc atomic capture
x = i
i = x
!$acc end atomic capture
!$acc atomic capture
a = b
b = b
!$acc end atomic capture
!$acc atomic capture
a = b
a = c
!$acc end atomic capture
```
2025-07-30 08:13:07 -07:00
Michael Kruse
27f777e9c0
[Flang][OpenMP] Skip DSA for canonical loops (#150593)
OpenMP loop transformations to not have data-sharing attributes and do
not explicitly privatize the loop variable. The DataSharingProcessor was
still used in #144785 because `createAndSetPrivatizedLoopVar` expected
it.

We skip that function and directly write to the loop variable. If the
loop variable is implicitly or explicitly privatized, it will be due to
surrounding OpenMP constructs such as `parallel`.
2025-07-30 09:20:42 +02:00