10838 Commits

Author SHA1 Message Date
Peter Klausler
50a40738d6
[flang] Catch semantic error with LBOUND/UBOUND (#154184)
The "ARRAY=" argument to these intrinsics cannot be scalar, whether
"DIM=" is present or not. (Allowing the "ARRAY=" argument to be scalar
when "DIM=" is absent would be a conceivable extension returning an
empty result array, like SHAPE() does with extents, but it doesn't seem
useful in a programming language without compilation-time rank
polymorphism apart from assumed-rank dummy arguments, and those are
supported.)

Fixes https://github.com/llvm/llvm-project/issues/154044.
2025-08-18 14:45:38 -07:00
Peter Klausler
2cf982c0f5
[flang] Don't duplicate impure function call for UBOUND() (#153648)
Because the per-dimension information in a descriptor holds an extent
and a lower bound, but not an upper bound, the calculation of the upper
bound sometimes requires that the extent and lower bound be extracted
from a descriptor and added together, minus 1. This shouldn't be
attempted when the NamedEntity of the descriptor is something that
shouldn't be duplicated and used twice; specifically, it shouldn't apply
to NamedEntities containing references to impure functions as parts of
subscript expressions.

Fixes https://github.com/llvm/llvm-project/issues/153031.
2025-08-18 14:43:13 -07: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
Connector Switch
b368e7f6a5
[flang] optimize acosd precision (#154118)
Part of https://github.com/llvm/llvm-project/issues/150452.
2025-08-18 14:15:52 +00:00
Krzysztof Parzyszek
ae75884130
[Frontend][OpenMP] Add 6.1 as a valid OpenMP version (#153628)
Co-authored-by: Michael Klemm <michael.klemm@amd.com>
2025-08-18 09:13:27 -05:00
Chaitanya
4a3bf27c69
[OpenMP] Introduce omp.target_allocmem and omp.target_freemem omp dialect ops. (#145464)
This PR introduces two new ops in omp dialect, omp.target_allocmem and
omp.target_freemem.
omp.target_allocmem: Allocates heap memory on device. Will be lowered to
omp_target_alloc call in llvm.
omp.target_freemem: Deallocates heap memory on device. Will be lowered
to omp+target_free call in llvm.


Example:
  %1 = omp.target_allocmem %device : i32, i64
  omp.target_freemem %device, %1 : i32, i64

The work in this PR is C-P/inspired from @ivanradanov commit from
coexecute implementation:
[Add fir omp target alloc and free
ops](be860ac8ba)
[Lower omp_target_{alloc,free} to
llvm](6e2d584dc9)
2025-08-18 18:15:11 +05:30
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
9f302ed0cf
[flang] Inline hlfir.eoshift during HLFIR intrinsics simplication. (#153108)
This patch generalizes the code for hlfir.cshift to be applicable
for hlfir.eoshift. The major difference is the selection
of the boundary value that might be statically/dynamically absent,
in which case the default scalar value has to be used.
The scalar value of the boundary is always computed before
the hlfir.elemental or the assignment loop.
Contrary to hlfir.cshift simplication, the SHIFT value is not
normalized,
because the original value (and its sign) participate in the EOSHIFT
index computation for addressing the input array and selecting
which elements of the results are assigned from the boundary operand.
2025-08-15 15:22:06 -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
Slava Zakharin
4c6afc7993
[flang] Lower hlfir.eoshift to the runtime call. (#153107)
Straightforward lowering of hlfir.eoshift to the runtime call
in LowerHLFIRIntrinsics pass.
2025-08-15 13:54:49 -07:00
Slava Zakharin
95d4362521
[flang] Added hlfir.eoshift operation definition. (#153105)
This is a basic definition of the operation corresponding to
the Fortran's EOSHIFT transformational intrinsic.
2025-08-15 13:15:35 -07:00
Valentin Clement (バレンタイン クレメン)
3720d8b52d
[flang][cuda] Update some bind name to fast version and add __sincosf (#153744)
Use the fast version in the bind name and reorder these fast math
functions. Add missing __sincosf interface.
2025-08-15 11:07:15 -07:00
Valentin Clement (バレンタイン クレメン)
115f816069
[flang][cuda] Add missing bind name for __int2double_rn (#153720) 2025-08-15 10:27:19 -07:00
Valentin Clement (バレンタイン クレメン)
0e4af726cb
[flang][cuda] Add interface for __fdividef (#153742) 2025-08-15 10:26:40 -07:00
Valentin Clement (バレンタイン クレメン)
0e8c964c21
[flang][cuda] Add interfaces for double_as_longlong and longlong_as_double (#153719) 2025-08-15 17:26:11 +00:00
Valentin Clement (バレンタイン クレメン)
fd3f052aeb
[flang][cuda] Add interfaces for int_as_float and float_as_int (#153716) 2025-08-15 10:00:53 -07:00
Valentin Clement (バレンタイン クレメン)
583499a8cf
[flang][cuda] Add missing bind name for __hiloint2double, __double2loint and __double2hiint (#153713) 2025-08-15 09:32:59 -07:00
Akash Banerjee
1fd1d63463 [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-15 15:41:41 +01:00
Yanzuo Liu
3b27d50cc7
[LLVM][utils] Add script which clears release notes (#153593)
The script copies `ReleaseNotesTemplate.txt` to corresponding
`ReleaseNotes.rst`/`.md` to clear release notes.

The suffix of `ReleaseNotesTemplate.txt` must be `.txt`. If it is
`.rst`/`.md`, it will be treated as a documentation source file when
building documentation.
2025-08-15 19:00:08 +08:00
Markus Böck
8582025f1f
[mlir][Transforms] Turn 1:N -> 1:1 dispatch fatal error into match failure (#153605)
Prior to this PR, the default behaviour of a conversion pattern which
receives operands of a 1:N is to abort the compilation. This has
historically been useful when the 1:N type conversion got merged into
the dialect conversion as it allowed us to easily find patterns that
should be capable of handling 1:N type conversions but didn't.

However, this behaviour has the disadvantage of being non-composable:
While the pattern in question cannot handle the 1:N type conversion,
another pattern part of the set might, but doesn't get the chance as
compilation is aborted.

This PR fixes this behaviour by failing to match and instead of
aborting, giving other patterns the chance to legalize an op. The
implementation uses a reusable function called `dispatchTo1To1` to allow
derived conversion patterns to also implement the behaviour.
2025-08-15 11:45:25 +02: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
Valentin Clement (バレンタイン クレメン)
3bc4d66082
[flang][cuda] Add interfaces for __int2float_rX (#153708) 2025-08-14 16:45:44 -07:00
Valentin Clement (バレンタイン クレメン)
ffe4870472
[flang][cuda] Add interfaces for __float2int_rX and __float2unit_rX (#153691) 2025-08-14 23:11:45 +00:00
Valentin Clement (バレンタイン クレメン)
602f308d4f
[flang][cuda] Add interface for __saturatef (#153705) 2025-08-14 15:55:17 -07:00
Valentin Clement (バレンタイン クレメン)
2775c79c4f
[flang][cuda] Add interfaces for __float2ll_rX (#153702) 2025-08-14 15:44:52 -07:00
Valentin Clement (バレンタイン クレメン)
ca9ddd54b7
[flang][cuda] Add interfaces for __ll2float_rX (#153694) 2025-08-14 15:35:02 -07:00
Valentin Clement (バレンタイン クレメン)
df15c0d716
[flang][cuda] Add interfaces for __dsqrt_rn and __dsqrt_rz (#153624) 2025-08-14 22:08:33 +00:00
Valentin Clement (バレンタイン クレメン)
b989c7c2e0
[flang][cuda] Add interfaces for __drcp_rX (#153681) 2025-08-14 21:44:47 +00:00
Valentin Clement (バレンタイン クレメン)
06590444f5
[flang][cuda] Add bind names for __double2ull_rX interfaces (#153678) 2025-08-14 21:10:20 +00:00
Valentin Clement (バレンタイン クレメン)
bad3df4764
[flang][cuda] Add bind names for __double2ll_rX interfaces (#153660) 2025-08-14 13:34:25 -07:00
Valentin Clement (バレンタイン クレメン)
20a829937c
[flang][cuda] Add interfaces for __expf and __exp10f (#153633) 2025-08-14 11:36:55 -07:00
Valentin Clement (バレンタイン クレメン)
e27e4f3a99
[flang][cuda] Add interfaces for __uint2float_rX functions (#153620)
Also add bind name for __uint2double_rn
2025-08-14 18:05:37 +00:00
Valentin Clement (バレンタイン クレメン)
efce767a88
[flang][cuda] Add interfaces for __ull2float_rX functions (#153613) 2025-08-14 10:28:17 -07:00
Valentin Clement (バレンタイン クレメン)
a8f1f1b41f
[flang][cuda] Add interfaces for __logf, __log2f and __log10f (#153611) 2025-08-14 17:17:52 +00:00
Valentin Clement (バレンタイン クレメン)
6961139ce9
[flang][cuda] Add interfaces for __sinf and __tanf (#153609) 2025-08-14 09:50:03 -07:00
Ian McInerney
28d5bc5649
[Flang][Driver] Predefine pic/pie macros based on configured level (#153449)
Predefine the `__pic__/__pie__/__PIC__/__PIE__` macros based on the
configured relocation level. This logic mirrors that of the clang
driver, where `__pic__/__PIC__` are defined for both PIC and PIE modes,
but `__pie__/__PIE__` are only defined for PIE mode.

Fixes https://github.com/llvm/llvm-project/issues/135275
2025-08-14 17:48:20 +01:00
parabola94
23d0cc7ab3
[flang] Fix buildbot failure after #152914 (#153578)
Buildbot failure:
https://lab.llvm.org/buildbot/#/builders/201/builds/5894
2025-08-14 23:31:28 +09: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
Michael Kruse
38853a0146 [flang][OpenMP] MSVC buildbot fix
PR #153488 caused the msvc build (https://lab.llvm.org/buildbot/#/builders/166/builds/1397) to fail:
```
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): error C2668: 'Fortran::evaluate::rewrite::Identity::operator ()': ambiguous call to overloaded function
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(43): note: could be 'Fortran::evaluate::Expr<Fortran::evaluate::SomeType> Fortran::evaluate::rewrite::Identity::operator ()<Fortran::evaluate::SomeType,S>(Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &&,const U &)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>,
            U=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\lib\Semantics\check-omp-atomic.cpp(174): note: or       'Fortran::evaluate::Expr<Fortran::evaluate::SomeType> Fortran::semantics::ReassocRewriter::operator ()<Fortran::evaluate::SomeType,S,void>(Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &&,const U &,Fortran::semantics::ReassocRewriter::NonIntegralTag)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>,
            U=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): note: while trying to match the argument list '(Fortran::evaluate::Expr<Fortran::evaluate::SomeType>, const S)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): note: the template instantiation context (the oldest one first) is
..\llvm-project\flang\lib\Semantics\check-omp-atomic.cpp(814): note: see reference to function template instantiation 'U Fortran::evaluate::rewrite::Mutator<Fortran::semantics::ReassocRewriter>::operator ()<const Fortran::evaluate::Expr<Fortran::evaluate::SomeType>&,Fortran::evaluate::Expr<Fortran::evaluate::SomeType>>(T)' being compiled
        with
        [
            U=Fortran::evaluate::Expr<Fortran::evaluate::SomeType>,
            T=const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &
        ]
```

The reason is that there is an ambiguity between operator() of
ReassocRewriter itself and operator() of the base class `Identity` through
`using Id::operator();`. By the C++ specification, method declarations
in ReassocRewriter hide methods with the same signature from a using
declaration, but this does not apply to
```
evaluate::Expr<T> operator()(..., NonIntegralTag = {})
```
which has a different signature due to an additional tag parameter.
Since it has a default value, it is ambiguous with operator() without
tag parameter.

GCC and Clang both accept this, but in my understanding MSVC is correct
here.

Since the overloads of ReassocRewriter cover all cases (integral and
non-integral), removing the using declaration to avoid the ambiguity.
2025-08-14 12:30:59 +02:00
Terapines MLIR
c164e6309b
[flang][fir] Add conversion of fir.iterate_while to scf.while. (#152439)
This commmit is a supplement for
https://github.com/llvm/llvm-project/pull/140374.

RFC:https://discourse.llvm.org/t/rfc-add-fir-affine-optimization-fir-pass-pipeline/86190/6
2025-08-14 13:39:55 +08:00
Valentin Clement (バレンタイン クレメン)
8d0668c38a
[flang][cuda] Add bind name for __ll2double_rX interfaces (#153462) 2025-08-13 22:15:57 +00:00
Valentin Clement (バレンタイン クレメン)
f9b9e9b7d5
[flang][cuda] Fix buildbot failure after #153242 (#153500) 2025-08-13 14:54:33 -07:00
parabola94
cc58ca5370
[flang/flang-rt] Add -isysroot flag only to tests really requiring (#152914)
-isysroot flag was added to all tests, but it makes
Driver/darwin-version.f90 failed.

In fact, only a few tests regarding interoperability with C need
-isysroot flag to search for headers and libraries. So, -isysroot flag
is now eliminated from the substitution `%flang`, and a new substitution
`%isysroot` has been introduced.

Moreover, Integration/iso-fortran-binding.cpp invokes clang++ via a
shell script, which makes it hard to add -isysroot flag. So, it is
refactored.

Fixes #150765
2025-08-13 21:43:53 +00:00
Valentin Clement (バレンタイン クレメン)
0425cc95bd
[flang][cuda] Add bind name for __ull2double_rX interfaces (#153465) 2025-08-13 21:42:57 +00:00
Peter Klausler
7a13a756d6
[flang][NFC] Document ALLOCATE/DEALLOCATE error semantics (#153027)
This compiler stops an ALLOCATE/DEALLOCATE statement with multiple
variables after encountering a recoverable error to avoid the risk of
ambiguity in the case of multiple errors. Document.
2025-08-13 14:36:51 -07:00
Peter Klausler
442ae603c5
[flang] Warn about inexact real literal implicit widening pitfall (#152799)
When a REAL or COMPLEX literal appears without an explicit kind suffix
or a kind-determining exponent letter, and the conversion of that
literal from decimal to binary is inexact, emit a warning if that
constant is later implicitly widened to a more precise kind, since it
will have a different value than was probably intended.

Values that convert exactly from decimal to default real, e.g. 1.0 and
0.125, do not elicit this warning.

There are many contexts in which Fortran implicitly converts constants.
This patch covers name constant values, variable and component
initialization, constants in expressions, structure constructor
components, and array constructors.

For example, "real(8) :: tenth = 0.1" is a common Fortran bug that's
hard to find, and is one that often trips up even experienced Fortran
programmers. Unlike C and C++, the literal constant 0.1 is *not* double
precision by default, and it does not have the same value as 0.1d0 or
0.1_8 do when it is converted from decimal to real(4) and then to
real(8).
2025-08-13 14:36:13 -07:00
Valentin Clement (バレンタイン クレメン)
8061cae756
[flang][cuda] Add bind name for __float2half_rn and __half2float (#153298) 2025-08-13 21:12:27 +00:00
Krzysztof Parzyszek
1e7772abcb
[flang][OpenMP] Reassociate ATOMIC update expressions (#153488)
An atomic update expression of form
  x = x + a + b
is technically illegal, since the right-hand side is parsed as (x+a)+b,
and the atomic variable x should be an argument to the top-level +. When
the type of x is integer, the result of (x+a)+b is guaranteed to be the
same as x+(a+b), so instead of reporting an error, the compiler can
treat (x+a)+b as x+(a+b).

This PR implements this kind of reassociation for integral types, and
for the two arithmetic associative/commutative operators: + and *.

Reinstate PR153098 one more time with fixes for the issues that came up:
- unused variable "lsrc",
- use of ‘outer1’ before deduction of ‘auto’.
2025-08-13 15:53:59 -05:00