10863 Commits

Author SHA1 Message Date
Slava Zakharin
b8e4232bd2
[flang] Cast fir.select[_rank] selector to i64. (#153239)
Properly cast the selector to `i64` regardless of its integer type.
We used to generate llvm.trunc always.

We have to use `i64` as long as the case values may exceed INT_MAX.

Fixes #153050.
2025-08-12 16:43:44 -07:00
Slava Zakharin
252efeba90
[flang] Change traversal order for OptimizeArrayRepackingPass. (#153136)
A long chain of fir.pack_arrays might require multiple iterations
of the greedy rewriter, if we use down-top traversal. The rewriter
may not converge in 10 (default) iterations. It is not an error,
but it was reported as such.

This patch changes the traversal to top-down and also disabled
the hard error, if the rewriter does not converge soon enough.
2025-08-12 13:13:10 -07:00
Akash Banerjee
1c7720ef78 Revert "[MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (#153048)"
This reverts commit 4e6d510eb3ec5b5e5ea234756ea1f0b283feee4a.
2025-08-12 20:19:45 +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
Akash Banerjee
4e6d510eb3
[MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (#153048)
Add a new AutomapToTargetData pass. This gathers the declare target
enter variables which have the AUTOMAP modifier. And adds
omp.declare_target_enter/exit mapping directives for fir.alloca and
fir.free oeprations on the AUTOMAP enabled variables.

Automap Ref: OpenMP 6.0 section 7.9.7.
2025-08-12 15:18:15 +01: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
Valentin Clement (バレンタイン クレメン)
12400a9ebb
[flang][cuda] Add bind name for __brev interface (#152806) 2025-08-11 20:44:00 -07:00
Valentin Clement (バレンタイン クレメン)
6b09e95542
[flang][cuda] Add bind name for __fss interface (#152803) 2025-08-11 19:32:52 -07:00
Krzysztof Parzyszek
f0471bca0b
[flang][Evaluate] Implement rewriting framework for evaluate::Expr (#153037)
The structure of evaluate::Expr is highly customized for the specific
operation or entity that it represents. The different cases are
expressed with different types, which makes the traversal and
modifications somewhat complicated. There exists a framework for
read-only traversal (traverse.h), but there is nothing that helps with
modifying evaluate::Expr.

It's rare that evaluate::Expr needs to be modified, but for the cases
where it needs to be, this code will make it easier.

---------

Co-authored-by: Tom Eccles <tom.eccles@arm.com>
2025-08-11 13:55:25 -05:00
Krzysztof Parzyszek
ea14ee4464
[flang][OpenMP] Refactor creating atomic analysis, NFC (#153036)
Turn it into a class that combines the information and generates the
analysis instead of having independent functions do it.
2025-08-11 12:20:51 -05:00
Connector Switch
7b41c2f824
[flang] Optimize acospi precision (#152869)
This patch implements the solution for `acospi` proposed in #150452,
improving precision for r16 and removing a redundant cast for r4.
2025-08-12 00:28:42 +08:00
Akash Banerjee
0998da27e9 Revert "[MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (#151989)"
This reverts commit 5a5e8ba0c388d57aecb359ed67919cda429fc7b1.
2025-08-11 13:52:39 +01:00
Akash Banerjee
5a5e8ba0c3
[MLIR][OpenMP] Add a new AutomapToTargetData conversion pass in FIR (#151989)
Add a new `AutomapToTargetData` pass. This gathers the declare target
enter variables which have the `AUTOMAP` modifier. And adds
`omp.declare_target_enter/exit` mapping directives for `fir.allocmem`
and `fir.freemem` oeprations on the `AUTOMAP` enabled variables.

Automap Ref: OpenMP 6.0 section 7.9.7.
2025-08-11 13:18:38 +01: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
parabola94
dea50a1797
[flang][Driver] Enable FLANG_DEFAULT_LINKER (#149786)
The default linker can be changed by a CMake variable
CLANG_DEFAULT_LINKER. However, it also changes the default linker
invoked by clang. In fact, there already exists FLANG_DEFAULT_LINKER,
but it does not work. This patch fixes it.

Note that FLANG_DEFAULT_LINKER will have the same value as
CLANG_DEFAULT_LINKER unless it is defined explicitly. That means this
patch does not affect the current behavior.

Fixes #73153

---------

Co-authored-by: Michael Kruse <github@meinersbur.de>
2025-08-10 05:21:14 +09:00
Valentin Clement (バレンタイン クレメン)
1acb1018d2
[flang][cuda] Set correct bind(c) name for __popc (#152795) 2025-08-08 16:49:45 -07:00
Nikita Popov
c23b4fbdbb
[IR] Remove size argument from lifetime intrinsics (#150248)
Now that #149310 has restricted lifetime intrinsics to only work on
allocas, we can also drop the explicit size argument. Instead, the size
is implied by the alloca.

This removes the ability to only mark a prefix of an alloca alive/dead.
We never used that capability, so we should remove the need to handle
that possibility everywhere (though many key places, including stack
coloring, did not actually respect this).
2025-08-08 11:09:34 +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
Valentin Clement (バレンタイン クレメン)
4394a0ca4a
[flang][cuda] Fix detection of implicit data transfer with a global (#152604) 2025-08-07 17:39:22 -07:00
Krzysztof Parzyszek
bc814348ec
[flang][OpenMP] Break up ResolveOmpObject for readability, NFC (#151957)
The function ResolveOmpObject had a lot of highly-indented code in two
variant visitors. Extract the visitors into their own functions, and
reformat the code. Replace !(||) with !&&! in a couple of places to make
the formatting a bit nicer. Use llvm::enumerate instead of manually
maintaining iteration index.
2025-08-07 15:29:51 -05:00
Anchu Rajendran S
38542efcba
[flang][OMPIRBuilder][MLIR][llvm] Backend support for atomic control options (#151579)
Adding mlir to llvm support for atomic control options.

Atomic Control Options are used to specify architectural characteristics
to help lowering of atomic operations. The options used are:
`-f[no-]atomic-remote-memory`, `-f[no-]atomic-fine-grained-memory`,
 `-f[no-]atomic-ignore-denormal-mode`.
Legacy option `-m[no-]unsafe-fp-atomics` is aliased to
`-f[no-]ignore-denormal-mode`.
More details can be found in
https://github.com/llvm/llvm-project/pull/102569. This PR implements the
MLIR to LLVM lowering support of atomic control attributes specified
with OpenMP `atomicUpdateOp`.

Initial support can be found in PR:
https://github.com/llvm/llvm-project/pull/150860
2025-08-07 12:23:38 -07: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 (バレンタイン クレメン)
a196281896
[flang][cuda] Remove meaningless warning on CUDA shared arguments (#152404)
The warning in issued during the compatibility check makes little sense.
Just remove it as it is confusing.
2025-08-06 18:50:07 -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
Eugene Epshteyn
cae7bebcaa
[flang-rt] Runtime implementation of extended intrinsic function SECNDS() (#152021)
Until the compiler part is fully hooked up via
https://github.com/llvm/llvm-project/pull/151878, tested this using
`external`:
```
external secnds
real s1, s2
s1 = secnds(0.0)
print *, "Seconds from midnight:", s1
call sleep(2)
s2 = secnds(s1)
print *, "Seconds from s1", s2
print *, "Seconds from midnight:", secnds(0.0)
end
```
2025-08-06 16:02:27 -04:00
Krzysztof Parzyszek
1d23005b8e
[flang][OpenMP] Insert CRITICAL construct names into global scope (#152004)
They were inserted in the current scope.

OpenMP spec (all versions):
The names of critical constructs are global entities of the program. If
a name conflicts with any other entity, the behavior of the program is
unspecified.
2025-08-06 10:16:43 -05:00
parabola94
a9dacb10fe
[flang] Add -O flag to tco (#151869)
At the moment, there is no established way to emit LLVM IR before
optimization in LLVM. tco can partially does, but the optimization level
is fixed to 2. This patch adds -O flag to tco.

Note that this is not completely equivalent to the frontend option. tco
does not accept -O flag without numbers, and the default optimization
level is not 0 but 2.
2025-08-06 16:04:32 +01:00
Michael Kruse
2bb23d4447
[flang][cmake] Fix bbc dependencies (#152306)
Re-apply "[flang][cmake] Fix bcc dependencies (#125822)"

It was overwritten due to an automatic merge gone wrong for #124416.

`git cherry-pick f9af5c145f40480d46874b643ca2b1237e9fbb2a` applied in
97cf061e83a0d0cada38d8a2d75c8a840b01ba26 ignored the renaming of
FortranCommon into FortranSupport after #125822.

Original commit message:

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 #20966.
2025-08-06 16:39:03 +02:00
Michael Kruse
2c2a368276 Revert "[flang][cmake] Re-apply "Fix bcc dependencies (#125822)""
This reverts commit 97cf061e83a0d0cada38d8a2d75c8a840b01ba26.
2025-08-06 15:10:52 +02:00
Michael Kruse
97cf061e83 [flang][cmake] Re-apply "Fix bcc dependencies (#125822)"
It was overwritten due to an automatic merge gone wrong for #124416
2025-08-06 14:26:24 +02: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
khaki3
8470027f25
[flang][acc] Add a semantic check for the validity of nested parallelism (#152225)
This PR implements a semantic checker to ensure the legality of nested
OpenACC parallelism. The following are quotes from Spec 3.3. We need to
disallow loops from having parallelism at the same level as or at a
sub-level of child loops.

>**2.9.2 gang clause**
>[2064] <ins>When the parent compute construct is a parallel
construct</ins>, or on an orphaned loop construct, the gang clause
behaves as follows. (...) The associated dimension is the value of the
dim argument, if it appears, or is dimension one. The dim argument must
be a constant positive integer with value 1, 2, or 3.
>[2112] The region of a loop with a gang(dim:d) clause may not contain a
loop construct with a gang(dim:e) clause where e >= d unless it appears
within a nested compute region.

>[2074] <ins>When the parent compute construct is a kernels
construct</ins>, the gang clause behaves as follows. (...)
>[2148] The region of a loop with the gang clause may not contain
another loop with a gang clause unless within a nested compute region.

>**2.9.3 worker clause**
>[2122]/[2129] The region of a loop with the worker clause may not
contain a loop with the gang or worker clause unless within a nested
compute region.

>**2.9.4 vector clause**
>[2141]/[2148] The region of a loop with the vector clause may not
contain a loop with a gang, worker, or vector clause unless within a
nested compute region.

https://openacc.org/sites/default/files/inline-images/Specification/OpenACC-3.3-final.pdf
2025-08-05 18:21:31 -07:00
Peter Klausler
867602742c
[flang] Minor speed-up to module file parsing (#152178)
Module files shouldn't ever produce parsing errors, and if they did in
the case of a badly-generated module file, the compiler will notice and
crash. So we can run the parser on module files with message deferral
enabled, and that saves time that would otherwise be spent generating
messages on failed parsing alternatives that are discarded anyway when
backtracking. It's not a big savings (single digit percentage on overall
compilation time for a big application with lots of modules), but worth
doing.
2025-08-05 13:40:30 -07:00
Peter Klausler
effa35d240
[flang][runtime] Don't always accept a bare exponent letter (#151597)
For more accurate compatibility with other compilers' extensions, accept
a bare exponent letter as valid real input to a formatted READ statement
only in a fixed-width input field. So it works with (G1.0) editing, but
not (G)/(D)/(E)/(F) or list-directed input.

Fixes https://github.com/llvm/llvm-project/issues/151465.
2025-08-05 13:39:33 -07: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
Krzysztof Parzyszek
7587a32d49
[flang][OpenMP] Fix crash in unparse-with-symbols for CRITICAL (#151962) 2025-08-05 07:52:50 -05: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 (バレンタイン クレメン)
951f40ac38
[flang][NFC] Update flang/unittests to the new create APIs (#152056)
While I was updating the creation of CUF operation in #152050, I noticed
some other places in flang that were not updated. This patch updates the
FIR operation creations in `flang/unittests`
2025-08-04 16:49:43 -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