Commit Graph

7856 Commits

Author SHA1 Message Date
Kareem Ergawy
b1774222c7 [flang] Emit fir.global in the global address space (#146653)
Instead of emitting globals in the program/default address space, emit
them in the global address space. This also requires changes how address
of code-gen is handled, we need to cast to the default address space to
prevent code-gen issues.
2025-07-02 17:15:22 +02:00
Tom Eccles
1b7cbe1f87 [flang][OpenMP] Create unique reduction decls for different logical kinds (#146558)
Some Fujitsu tests showed incorrect results because we were sharing
reduction declarations for different kinds for logical variables.
2025-07-02 10:25:43 +01:00
Kazu Hirata
d79c284a2f [flang] Fix a warning
This patch fixes:

  flang/lib/Semantics/check-omp-structure.cpp:1747:41: error: lambda
  capture 'this' is not used [-Werror,-Wunused-lambda-capture]
2025-07-01 10:34:41 -07:00
Krzysztof Parzyszek
ba116a8bed [flang][OpenMP] Split check-omp-structure.cpp into smaller files, NFC (#146359)
Create these new files in flang/lib/Semantics:
  openmp-utils.cpp/.h         - Common utilities
  check-omp-atomic.cpp        - Atomic-related checks
  check-omp-loop.cpp          - Loop constructs/clauses
  check-omp-metadirective.cpp - Metadirective-related checks

Update lists of included headers, std in particular.

---------

Co-authored-by: Jack Styles <jack.styles@arm.com>
2025-07-01 11:12:00 -05:00
Krzysztof Parzyszek
86077c41a7 [flang][OpenMP] Rewrite min/max with more than 2 arguments (#146423)
Given an atomic operation `w = max(w, x1, x2, ...)` rewrite it as `w =
max(w, max(x1, x2, ...))`. This will avoid unnecessary non-atomic
comparisons inside of the atomic operation (min/max are expanded
inline).

In particular, if some of the x_i's are optional dummy parameters in the
containing function, this will avoid any presence tests within the
atomic operation.

Fixes https://github.com/llvm/llvm-project/issues/144838
2025-07-01 09:54:58 -05:00
Jack Styles
65cb0eae58 [Flang][OpenMP] Add Semantics support for Nested OpenMPLoopConstructs (#145917)
In OpenMP Version 5.1, the tile and unroll directives were added. When
using these directives, it is possible to nest them within other OpenMP
Loop Constructs. This patch enables the semantics to allow for this
behaviour on these specific directives. Any nested loops will be stored
within the initial Loop Construct until reaching the DoConstruct itself.

Relevant tests have been added, and previous behaviour has been retained
with no changes.

See also, #110008
2025-07-01 08:39:15 +01:00
Razvan Lupusoru
f16983f7d0 [flang][acc] Ensure fir.class is handled in type categorization (#146174)
fir.class is treated similarly as fir.box - but it has one key
distinction which is that it doesn't hold an element type. Thus the
categorization logic was mishandling this case for this reason (and also
the fact that it assumed that a base object is always a fir.ref).

This PR improves this handling and adds appropriate test exercising both
a class and a class field to ensure categorization works.
2025-06-30 15:04:14 -07:00
Valentin Clement (バレンタイン クレメン)
f4cecfe1bb [flang][cuda] Bring PARAMETER arrays into the GPU module (#146416) 2025-06-30 14:24:44 -07:00
Eugene Epshteyn
93849a39c4 [flang] Check for ultimate ALLOCATABLE component in LOCAL_INIT() (#145800)
Fortran 2023 constraint C1130 disallows variables of derived type with
ultimate allocatable component to appear in LOCAL_INIT().
2025-06-30 15:49:07 -04:00
Slava Zakharin
90da61634a [flang] Use outermost fir.dummy_scope for TBAA of local allocations. (#146006)
This change only matters when MLIR function inlining kicks in,
and I am still just experimenting with this.

The added LIT test shows the particular example where the current
TBAA assignment is incorrect:
```
  subroutine bar(this)
    ...
  end subroutine bar
  function foo() result(res)
    type(t) :: res
    call bar(res)
  end function foo
  subroutine test(arg)
    ! %0 = fir.alloca !fir.type<_QMmTt{x:f32}> {bindc_name = ".result"}
    type(t) :: var
    var = foo()
    ! fir.save_result foo's result to %0 (temp-alloca)
    ! fir.declare %0 {uniq_name = ".tmp.func_result"}
    ! load from %0 -> store into var
    arg = var%x
  end subroutine test
```

One way for FIR inlining to handle `foo()` call is to substitute
all uses of the `res` alloca (inside `foo`) with uses of the temp-alloca
(inside `test`). This means that the temp-alloca is then used
by the code inside bar (e.g. to store something into it).
After the inlining, the innermost dominating fir.dummy_scope
for `fir.declare %0` is the one from `bar`. If we use this scope
for assigning TBAA to the load from temp-alloca, then it will be
in the same TBAA tree as the stores into `this` inside `bar`.
The load and the stores will have different sub-trees, because
`this` is a dummy argument, and temp-alloca is a local allocation.
So they will appear as non-conflicting while they are accessing
the same memory.

This change makes sure that all local allocations always
use the outermost scope.
2025-06-30 12:37:30 -07:00
Peter Klausler
407542b3ec [flang] Process pointer component default initializers sooner (#145601)
Name resolution defers the analysis of all object pointer initializers
to the end of a specification part, including the default initializers
of derived type data pointer components. This deferment allows object
pointer initializers to contain forward references to objects whose
declarations appear later.

However, this deferment has the unfortunate effect of causing NULL
default initialization of such object pointer components when they do
not appear in structure constructors that are used as default
initializers, and their default initializers are required. So handle
object pointer default initializers of components as they appear, as
before.
2025-06-30 10:25:00 -07:00
Peter Klausler
f3d57590bf [flang] Skip over fixed form spaces when prescanning exponents & kind… (#145347)
… suffixes

When performing conditional tokenization of exponents and numeric kind
suffixes, be sure to skip over spaces in fixed form source.

Fixes https://github.com/llvm/llvm-project/issues/145333.
2025-06-30 10:22:50 -07:00
Peter Klausler
a93d843ab3 [flang] Don't warn on (0.,0.)**(nonzero noninteger) (#145179)
Folding hands complex exponentiations with constant arguments off to the
native libm, and on a least on host, this can produce spurious warnings
about division by zero and invalid arguments. Handle the case of a zero
base specially to avoid that, and also emit better warnings for the
undefined 0.**0 and (0.,0.)**0 cases. And add a test for these warnings
and the existing related ones.
2025-06-30 10:21:37 -07:00
Peter Klausler
348002e111 [flang] Check definability for logical INQUIRE specifiers (#144797)
check-io.cpp was missing checks for the definability of logical-valued
specifiers in INQUIRE statements (e.g. EXIST=), and therefore also not
noting the definitions of those variables. This could lead to bogus
warnings about undefined function result variables, and also to missed
errors about immutable objects appearing in those specifiers.

Fixes https://github.com/llvm/llvm-project/issues/144453.
2025-06-30 10:21:06 -07:00
Andre Kuhlenschmidt
83b462af17 [flang][CLI] Have the CLI hint the flag to disable a warning (#144767)
Adds a hint to the warning message to disable a warning and updates the
tests to expect this.

Also fixes a bug in the storage of canonical spelling of error flags so
that they are not used after free.
2025-06-30 10:17:05 -07:00
jeanPerier
faefe7cf7d [flang] add option to generate runtime type info as external (#146071)
Reland #145901 with a fix for shared library builds.

So far flang generates runtime derived type info global definitions (as
opposed to declarations) for all the types used in the current
compilation unit even when the derived types are defined in other
compilation units. It is using linkonce_odr to achieve derived type
descriptor address "uniqueness" aspect needed to match two derived type
inside the runtime.

This comes at a big compile time cost because of all the extra globals
and their definitions in apps with many and complex derived types.

This patch adds and experimental option to only generate the rtti
definition for the types defined in the current compilation unit and to
only generate external declaration for the derived type descriptor
object of types defined elsewhere.

Note that objects compiled with this option are not compatible with
object files compiled without because files compiled without it may drop
the rtti for type they defined if it is not used in the compilation unit
because of the linkonce_odr aspect.

I am adding the option so that we can better measure the extra cost of
the current approach on apps and allow speeding up some compilation
where devirtualization does not matter (and the build config links to
all module file object anyway).
2025-06-30 09:58:00 +02:00
Kazu Hirata
c57c5f53a3 [flang] Fix warnings
This patch fixes:

  flang/../mlir/include/mlir/IR/TypeRange.h:51:19: error: 'ArrayRef'
  is deprecated: Use {} or ArrayRef<T>() instead
  [-Werror,-Wdeprecated-declarations]

  flang/../mlir/include/mlir/IR/ValueRange.h:401:20: error: 'ArrayRef'
  is deprecated: Use {} or ArrayRef<T>() instead
  [-Werror,-Wdeprecated-declarations]
2025-06-28 12:55:22 -07:00
Krzysztof Parzyszek
344b5b7f9e [flang][OpenMP] Move lowering of ATOMIC to separate file, NFC (#146225)
Reinstate commits e5559ca4 and 925dbc79. Fix the issues with compilation
hangs by including DenseMapInfo specialization where the corresponding
instance of DenseMap was defined.

Ref: https://github.com/llvm/llvm-project/pull/144960
2025-06-28 13:38:00 -05:00
Valentin Clement (バレンタイン クレメン)
570b95218c [flang][cuda] Do not generate section checks in device context (#146170)
This check is only useful on the host side. Also the function is bot
built for the device.
2025-06-27 16:19:40 -07:00
Valentin Clement (バレンタイン クレメン)
75175e7230 [flang][cuda] Inline this_thread_block() calls (#146144) 2025-06-27 14:59:29 -07:00
Valentin Clement (バレンタイン クレメン)
b2f504ff15 [flang][cuda] Inline this_warp() calls (#146134) 2025-06-27 14:12:17 -07:00
Krzysztof Parzyszek
dc6d2b841f Revert "[flang][OpenMP] Move lowering of ATOMIC to separate file, NFC" (#146091)
Reverts llvm/llvm-project#146067

This still causes timeouts, e.g.

https://lab.llvm.org/buildbot/#/builders/207/builds/3023/steps/7/logs/stdio
2025-06-27 09:44:16 -05:00
Krzysztof Parzyszek
302ed97b58 [flang][OpenMP] Move lowering of ATOMIC to separate file, NFC (#146067)
Reinstate commits e5559ca4 and 925dbc79 with changes that avoid the
reported failures in Windows builds.

Ref: https://github.com/llvm/llvm-project/pull/144960
2025-06-27 08:19:16 -05:00
jeanPerier
37e2d10499 Revert "[flang] add option to generate runtime type info as external" (#146064)
Reverts llvm/llvm-project#145901

Broke shared library builds because of the usage of
`skipExternalRttiDefinition` in Lowering.
2025-06-27 14:05:59 +02:00
Akash Banerjee
91f10df794 [Flang][OpenMP] Skip implicit mapping of named constants (#145966)
Added early return when mapping named constants.

This prevents linking error in the following example:

```
program test
   use, intrinsic :: iso_c_binding, only: c_double
   implicit none

   real(c_double) :: x
   integer        :: i
   x = 0.0_c_double
   !$omp target teams distribute parallel do reduction(+:x)
   do i = 0, 9
      x = x + 1.0_c_double
   end do
   !$omp end target teams distribute parallel do
end program test
```
2025-06-27 13:05:22 +01:00
jeanPerier
e816817bbb [flang] add option to generate runtime type info as external (#145901)
So far flang generates runtime derived type info global definitions (as
opposed to declarations) for all the types used in the current
compilation unit even when the derived types are defined in other
compilation units. It is using linkonce_odr to achieve derived type
descriptor address "uniqueness" aspect needed to match two derived type
inside the runtime.

This comes at a big compile time cost because of all the extra globals
and their definitions in apps with many and complex derived types.

This patch adds and experimental option to only generate the rtti
definition for the types defined in the current compilation unit and to
only generate external declaration for the derived type descriptor
object of types defined elsewhere.

Note that objects compiled with this option are not compatible with
object files compiled without because files compiled without it may drop
the rtti for type they defined if it is not used in the compilation unit
because of the linkonce_odr aspect.

I am adding the option so that we can better measure the extra cost of
the current approach on apps and allow speeding up some compilation
where devirtualization does not matter (and the build config links to
all module file object anyway).
2025-06-27 13:00:29 +02:00
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
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
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
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
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
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
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