Commit Graph

10524 Commits

Author SHA1 Message Date
Jack Styles
9ba332f996 [Flang][OpenMP] Deprecate Allocate Directive (#142378)
As part of OpenMP 5.2, the allocate directive has been deprecated in
favour of the allocators construct for Fortran when an ALLOCATE
statement follows the OpenMP allocate directive.

To enable this in flang, a warning has been added informing the user of
this. Tests to ensure this behaviour is continued are also included.

See also: #110008
2025-06-04 10:36:44 +01:00
Leandro Lupori
aac1f85393 [flang][OpenMP] Explicitly set Shared DSA in symbols (#142154)
Before this change, OmpShared was not always set in shared symbols.
Instead, absence of private flags was interpreted as shared DSA.
The problem was that symbols with no flags, with only a host
association, could also mean "has same DSA as in the enclosing
context". Now shared symbols behave the same as private and can be
treated the same way.

Because of the host association symbols with no flags mentioned
above, it was also incorrect to simply test the flags of a given
symbol to find out if it was private or shared. The function
GetSymbolDSA() was added to fix this. It would be better to avoid
the need of these special symbols, but this would require changes
to how symbols are collected in lowering.

Besides that, some semantic checks need to know if a DSA clause
was used or not. To avoid confusing implicit symbols with DSA
clauses a new flag was added: OmpExplicit. It is now set for all
symbols with explicitly determined data-sharing attributes.

With the changes above, AddToContextObjectWithDSA() and the symbol
to DSA map could probably be removed and the DSA could be obtained
directly from the symbol, but this was not attempted.

Some debug messages were also added, with the "omp" DEBUG_TYPE, to
make it easier to debug the creation of implicit symbols and to
visualize all associations of a given symbol.

Fixes #130533
Fixes #140882
2025-06-03 10:58:23 -03:00
Abid Qadeer
3374263a08 [flang] Harden testcase by changing variable name. (#142363)
I noticed that this testcase was failing in a CI build because it has a
`CHECK-NOT: bbb` while `bbb` could appear in the hashed global variable
name. Improved the test by changing this name to `ggg` which can't
appear in a hex string.
2025-06-03 10:21:21 +01:00
Kajetan Puchalski
01d4b16406 [flang][OpenMP] Resolve names for declare simd uniform clause (#142160)
Add a visitor for OmpClause::Uniform to resolve its parameter names.
Add Symbol::Flag::OmpUniform to attach it to the resolved symbols.
Fixes issue #140741.

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-06-02 16:37:02 +01:00
Asher Mancinelli
f50fe748b3 [flang] Propagate volatile on openmp reduction vars (#142182)
In the openmp reduction clause lowering, reduction variables' reference
types were not propagating the volatility of the original variable's
type. Add a test and address cases of this in ReductionProcessor.
2025-06-02 08:04:11 -07:00
Kareem Ergawy
fd66fa56c4 [flang] Retrieve shape from selector when generating assoc sym type (#137117)
This PR extends `genSymbolType` so that the type of an associating
symbol carries the shape of the selector expression, if any. This is a
fix for a bug that triggered when an associating symbol is used in a
locality specifier. For example, given the following input:

```fortran
  associate(a => aa(4:))
    do concurrent (i = 4:11) local(a)
      a(i) = 0
    end do
  end associate
```
before the changes in the PR, flang would assert that we are casting
between incompatible types. The issue happened since for the associating
symbol (`a`), flang generated its type as `f32` rather than
`!fir.array<8xf32>` as it should be in this case.
2025-06-02 15:49:58 +02:00
Cameron McInally
c5a56f74fe [flang] Fix ppc64le-flang-rhel-test buildbot failure (#142269)
PR#142073 introduced a new test that checks the
prefer-vector-width function attribute. This test was not accounting for
target triples that include default function attributes.

This patch updates prefer-vector-width.f90 to ignore extra function
attributes.

---------

Co-authored-by: Cameron McInally <cmcinally@nvidia.com>
2025-06-02 05:12:18 -06:00
Abid Qadeer
de38c4eaf5 [flang][driver] Introduce FCC_OVERRIDE_OPTIONS. (#140556)
This PR add functionality to change `flang` command line using
environment variable `FCC_OVERRIDE_OPTIONS`. It is quite similar to what
`CCC_OVERRIDE_OPTIONS` does for clang.

The `FCC_OVERRIDE_OPTIONS` seemed like the most obvious name to me but I
am open to other ideas. The `applyOverrideOptions` now takes an extra
argument that is the name of the environment variable. Previously
`CCC_OVERRIDE_OPTIONS` was hardcoded.
2025-06-02 11:37:26 +01:00
Hubert Tong
b68565b8c7 [NFC][tests] Relax matching for prefer-vector-width.f90
The attributes matching was too strict and caused failures:
https://lab.llvm.org/buildbot/#/builders/157/builds/29494/steps/6/logs/FAIL__Flang__prefer-vector-width_f90
2025-06-01 16:55:40 -04:00
Tom Eccles
d16ecad968 [flang] Disable noalias by default (#142128)
With these enabled we see a 70% performance regression for exchange2_r
on neoverse-v1 (aws graviton 3) using `-mcpu=native -Ofast -flto`. There
is also a smaller regression on neoverse-v2.

This appears to be because function specialization is no longer kicking
in during LTO for digits_2. This can be seen in the output executable:
previously it contained specialized copies of the function with names
like `_QMbrute_forcePdigits_2.specialized.4`. Now there are no names
like this.

The bug is not in flang - instead in the function specialization pass -
but due to the size of the regression I would like to request that this
is disabled until function specialization has been fixed.
2025-05-30 17:35:41 +01:00
Tarun Prabhu
597340b5b6 Revert "Add IR Profile-Guided Optimization (IR PGO) support to the Flang compiler" (#142159)
Reverts llvm/llvm-project#136098
2025-05-30 08:27:08 -06:00
FYK
d27a210a77 Add IR Profile-Guided Optimization (IR PGO) support to the Flang compiler (#136098)
This patch implements IR-based Profile-Guided Optimization support in
Flang through the following flags:

- `-fprofile-generate` for instrumentation-based profile generation

- `-fprofile-use=<dir>/file` for profile-guided optimization

Resolves #74216 (implements IR PGO support phase)

**Key changes:**

- Frontend flag handling aligned with Clang/GCC semantics

- Instrumentation hooks into LLVM PGO infrastructure

- LIT tests verifying:

    - Instrumentation metadata generation

    - Profile loading from specified path

    - Branch weight attribution (IR checks)

**Tests:**

- Added gcc-flag-compatibility.f90 test module verifying:

    -  Flag parsing boundary conditions

    -  IR-level profile annotation consistency

    -  Profile input path normalization rules

- SPEC2006 benchmark results will be shared in comments

For details on LLVM's PGO framework, refer to [Clang PGO
Documentation](https://clang.llvm.org/docs/UsersManual.html#profile-guided-optimization).

This implementation was developed by [XSCC Compiler
Team](https://github.com/orgs/OpenXiangShan/teams/xscc).

---------

Co-authored-by: ict-ql <168183727+ict-ql@users.noreply.github.com>
Co-authored-by: Tom Eccles <t@freedommail.info>
2025-05-30 08:13:53 -06:00
Cameron McInally
ce9cef79ea [flang] Add support for -mprefer-vector-width=<value> (#142073)
This patch adds support for the -mprefer-vector-width= command line
option. The parsing of this options is equivalent to Clang's and it is
implemented by setting the "prefer-vector-width" function attribute.

Co-authored-by: Cameron McInally <cmcinally@nvidia.com>
2025-05-30 07:50:18 -06:00
Akash Banerjee
99ae675fb7 [NFC][OpenMP] Move the default declare mapper name suffix to OMPConstants.h (#141964)
This patch moves the default declare mapper name suffix
".omp.default.mapper" to the OMPConstants.h file to be used everywhere
for lowering.
2025-05-30 14:39:03 +01:00
Yussur Mustafa Oraji
5c3bf36c99 [flang] Add __COUNTER__ preprocessor macro (#136827)
This commit adds support for the `__COUNTER__` preprocessor macro, which
works the same as the one found in clang.
It is useful to generate unique names at compile-time.
2025-05-30 08:33:53 -04:00
Kareem Ergawy
f5d3470d42 [flang][OpenMP] Allow structure component in task depend clauses (#141923)
Even though the spec (version 5.2) prohibits strcuture components from
being specified in `depend` clauses, this restriction is not sensible.

This PR rectifies the issue by lifting that restriction and allowing
structure components in `depend` clauses (which is allowed by OpenMP
6.0).
2025-05-30 06:22:29 +02:00
Slava Zakharin
a0d699a8e6 Reland "[flang] Added noalias attribute to function arguments. (#140803)"
This helps to disambiguate accesses in the caller and the callee
after LLVM inlining in some apps. I did not see any performance
changes, but this is one step towards enabling other optimizations
in the apps that I am looking at.

The definition of llvm.noalias says:
```
... indicates that memory locations accessed via pointer values based on the argument or return value are not also accessed, during the execution of the function, via pointer values not based on the argument or return value. This guarantee only holds for memory locations that are modified, by any means, during the execution of the function.
```

I believe this exactly matches Fortran rules for the dummy arguments
that are modified during their subprogram execution.

I also set llvm.noalias and llvm.nocapture on the !fir.box<> arguments,
because the corresponding descriptors cannot be captured and cannot
alias anything (not based on them) during the execution of the
subprogram.
2025-05-29 13:42:57 -07:00
Krzysztof Parzyszek
798ae82399 [flang][OpenMP] Verify that arguments to COPYPRIVATE are variables (#141823)
The check if the arguments are variable list items was missing, leading
to a crash in lowering in some invalid situations.

This fixes the first testcase reported in
https://github.com/llvm/llvm-project/issues/141481
2025-05-29 15:05:05 -05:00
Krzysztof Parzyszek
4811c67d62 [flang][OpenMP] Treat ClassType as BoxType in COPYPRIVATE (#141844)
This fixes the second problem reported in
https://github.com/llvm/llvm-project/issues/141481
2025-05-29 14:32:49 -05:00
Valentin Clement (バレンタイン クレメン)
ef11bdc7fe [flang][cuda] Allow compiler directive in cuda code (#141991) 2025-05-29 10:46:44 -07:00
Kareem Ergawy
11c7a0c3f7 [NFC][flang] Remove unused variable from privatizeSymbol (#141938) 2025-05-29 16:44:01 +02:00
Kareem Ergawy
f8dcb059ae [flang][fir][OpenMP] Refactor privtization code into shared location (#141767)
Refactors the utils needed to create privtization/locatization ops for
both the fir and OpenMP dialects into a shared location isolating OpenMP
stuff out of it as much as possible.
2025-05-29 13:13:44 +02:00
Kareem Ergawy
7e9887a50d [flang] Generlize names of delayed privatization CLI flags (#138816)
Remove the `openmp` prefix from delayed privatization/localization flags
since they are now used for `do concurrent` as well.

PR stack:
- https://github.com/llvm/llvm-project/pull/137928
- https://github.com/llvm/llvm-project/pull/138505
- 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 (this PR)
2025-05-29 12:27:03 +02:00
Kareem Ergawy
e33cd9690f [flang][fir] Basic PFT to MLIR lowering for do concurrent locality specifiers (#138534)
Extends support for `fir.do_concurrent` locality specifiers to the PFT
to MLIR level. This adds code-gen for generating the newly added
`fir.local` ops and referencing these ops from `fir.do_concurrent.loop`
ops that have locality specifiers attached to them. This reuses the
`DataSharingProcessor` component and generalizes it a bit more to allow
for handling `omp.private` ops and `fir.local` ops as well.


PR stack:
- https://github.com/llvm/llvm-project/pull/137928
- https://github.com/llvm/llvm-project/pull/138505
- https://github.com/llvm/llvm-project/pull/138506
- https://github.com/llvm/llvm-project/pull/138512
- https://github.com/llvm/llvm-project/pull/138534 (this PR)
- https://github.com/llvm/llvm-project/pull/138816
2025-05-29 11:04:27 +02:00
Valentin Clement (バレンタイン クレメン)
7b074fc936 [flang][openacc] Allow polymorphic entity in data clauses (#141856)
- Attach the mappable interface to ClassType
- Remove the TODO since derived type in descriptor can be handled as
other descriptors
2025-05-28 18:20:02 -07:00
Slava Zakharin
6ee2453360 Revert "[flang] Added noalias attribute to function arguments." (#141884)
Reverts llvm/llvm-project#140803

Buildbot failure:
https://lab.llvm.org/buildbot/#/builders/143/builds/8041
2025-05-28 18:06:11 -07:00
Slava Zakharin
2426ac6865 [flang] Added noalias attribute to function arguments. (#140803)
This helps to disambiguate accesses in the caller and the callee
after LLVM inlining in some apps. I did not see any performance
changes, but this is one step towards enabling other optimizations
in the apps that I am looking at.

The definition of llvm.noalias says:
```
... indicates that memory locations accessed via pointer values based on the argument or return value are not also accessed, during the execution of the function, via pointer values not based on the argument or return value. This guarantee only holds for memory locations that are modified, by any means, during the execution of the function.
```

I believe this exactly matches Fortran rules for the dummy arguments
that are modified during their subprogram execution.

I also set llvm.noalias and llvm.nocapture on the !fir.box<> arguments,
because the corresponding descriptors cannot be captured and cannot
alias anything (not based on them) during the execution of the
subprogram.
2025-05-28 17:18:04 -07:00
Peter Klausler
f48f844f3c [flang] Fix prescanner bug w/ empty macros in line continuation (#141274)
When processing free form source line continuation, the prescanner
treats empty keyword macros as if they were spaces or tabs. After
skipping over them, however, there's code that only works if the skipped
characters ended with an actual space or tab. If the last skipped item
was an empty keyword macro's name, the last character of that name would
end up being the first character of the continuation line. Fix.
2025-05-28 14:01:55 -07:00
Peter Klausler
145712555f [flang] Allow forward reference to non-default INTEGER dummy (#141254)
A dummy argument with an explicit INTEGER type of non-default kind can
be forward-referenced from a specification expression in many Fortran
compilers. Handle by adding type declaration statements to the initial
pass over a specification part's declaration constructs. Emit an
optional warning under -pedantic.

Fixes https://github.com/llvm/llvm-project/issues/140941.
2025-05-28 14:01:28 -07:00
Peter Klausler
a6432b971a [flang] Fix folding of SHAPE(SPREAD(source,dim,ncopies=-1)) (#141146)
The number of copies on the new dimension must be clamped via MAX(0,
ncopies) so that it is no less than zero.

Fixes https://github.com/llvm/llvm-project/issues/141119.
2025-05-28 14:01:10 -07:00
Peter Klausler
ff8288442d [flang] Fix crash in error recovery (#140768)
When a TYPE(*) dummy argument is erroneously used as a component value
in a structure constructor, semantics crashes if the structure
constructor had been initially parsed as a potential function reference.
Clean out stale typed expressions when reanalyzing the reconstructed
parse subtree to ensure that errors are caught the next time around.
2025-05-28 13:59:48 -07:00
Peter Klausler
4c6b60a639 [flang] Extension: allow char string edit descriptors in input formats (#140624)
FORMAT("J=",I3) is accepted by a few other Fortran compilers as a valid
format for input as well as for output. The character string edit
descriptor "J=" is interpreted as if it had been 2X on input, causing
two characters to be skipped over. The skipped characters don't have to
match the characters in the literal string. An optional warning is
emitted under control of the -pedantic option.
2025-05-28 13:58:22 -07:00
Akash Banerjee
59b7b5b6b5 [OpenMP][Flang] Fix semantic check and scoping for declare mappers (#140560)
The current semantic check in place is incorrect, this patch fixes this.

Up to 1 **'default'** named mapper should be allowed for each derived
type.
The current semantic check only allows up to 1 **'default'** named
mapper across all derived types.

This also makes sure that declare mappers follow proper scoping rules
for both default and named mappers.

Co-authored-by: Raghu Maddhipatla <Raghu.Maddhipatla@amd.com>
2025-05-28 14:32:17 +01:00
Eugene Epshteyn
66a2d4b1e7 [flang] Ensure that the integer for Cray pointer is sized correctly (#140822)
The integer used for Cray pointers should have the size equivalent to
platform's pointer size.
2025-05-27 13:08:35 -04:00
Asher Mancinelli
42b1df43e7 [mlir][math] Add missing trig math-to-llvm conversion patterns (#141069)
asin, acos, atan, and atan2 were being lowered to libm calls instead of
llvm intrinsics. Add the conversion patterns to handle these intrinsics
and update tests to expect this.
2025-05-27 08:09:48 -07:00
MingYan
953302eb98 [flang][fir] Add FIR structured control flow ops to SCF dialect pass. (#140374)
This patch only supports the conversion from `fir.do_loop` to `scf.for`.
This pass is still experimental, and future work will focus on gradually
improving this conversion pass.

Co-authored-by: yanming <ming.yan@terapines.com>
2025-05-25 14:28:47 +08:00
Andre Kuhlenschmidt
df03f7ed4c [flang][openacc] use location of end directive for exit operations (#140763)
Make sure to preserve the location of the end statement on data
declarations for use in debugging OpenACC runtime.
2025-05-23 13:39:11 -07:00
Yang Zaizhou
5530474e3e [Flang][OpenMP] fix crash on sematic error in atomic capture clause (#140710)
Fix a crash caused by an invalid expression in the atomic capture
clause, due to the `checkForSymbolMatch` function not accounting for
`GetExpr` potentially returning null.

Fix https://github.com/llvm/llvm-project/issues/139884
2025-05-23 07:15:10 -05:00
Jan Svoboda
3ea2cec732 [flang] Fix build after 9e306ad4 (#141134) 2025-05-22 13:51:20 -07:00
Valentin Clement (バレンタイン クレメン)
d6d52a4abc [flang][cuda] Do not generate cuf.alloc/cuf.free in device context (#141117)
`cuf.alloc` and `cuf.free` are converted to `fir.alloca` or deleted when
in device context during the CUFOpConversion pass. Do not generate them
in lowering to avoid confusion.
2025-05-22 13:30:34 -07:00
Tom Eccles
e9cba3c8ed [flang][OpenMP] use attribute for delayed privatization barrier (#140092)
Fixes #136357

The barrier needs to go between the copying into firstprivate variables
and the initialization call for the OpenMP construct (e.g. wsloop).
There is no way of expressing this in MLIR because for delayed
privatization that is all implicit (added in MLIR->LLVMIR conversion).

The previous approach put the barrier immediately before the wsloop (or
similar). For delayed privatization, the firstprivate copy code would
then be inserted after that, opening the possibility for the race
observed in the bug report.

This patch solves the issue by instead setting an attribute on the mlir
operation, which will instruct openmp dialect to llvm ir conversion to
insert a barrier in the correct place.
2025-05-22 15:25:53 +01:00
Asher Mancinelli
898df4b8ed [flang] Skip opt-bufferization when memory effect does not have an associated value (#140781)
Memory effects on the volatile memory resource may not be attached to a
particular source, in which case the value of an effect will be null.
This caused this test case to crash in the optimized bufferization
pass's safety analysis because it assumes it can get the SSA value
modified by the memory effect. This is because memory effects on the
volatile resource indicate that the operation must not be reordered with
respect to other volatile operations, but there is not a material ssa
value that can be pointed to.

This patch changes the safety checks such that memory effects which do
not have associated values are not safe for optimized bufferization.
2025-05-22 06:50:25 -07:00
jeanPerier
1f5b6ae89f [flang] optionally add lifetime markers to alloca created in stack-arrays (#140901)
Flang at Ofast usually produces executables that consume more stack that
other Fortran compilers.
This is in part because the alloca created from temporary heap
allocation by the StackArray pass are created at the function scope
level without lifetimes, and LLVM does not/is not able to merge alloca
that do not have overlapping lifetimes.

This patch adds an option to generate LLVM lifetime in the StackArray
pass at the previous heap allocation/free using the LLVM dialect
operation for it.
2025-05-22 09:26:14 +02:00
jeremyd2019
ba4bd3f46e [CMake] respect LLVMConfig.cmake's LLVM_DEFINITIONS in standalone builds (#138587)
In #138329, _GNU_SOURCE was added for Cygwin, but when building Clang
standalone against an installed LLVM this definition was not picked up,
resulting in undefined strnlen. Follow the documentation in
https://llvm.org/docs/CMake.html#embedding-llvm-in-your-project and add
the LLVM_DEFINITIONS in standalone projects' cmakes.
2025-05-22 08:14:13 +03:00
Akash Banerjee
fbb11b4c4e [OpenMP][Flang] Fix OOB access for derived type mapping (#140948) 2025-05-22 01:34:40 +01:00
Valentin Clement (バレンタイン クレメン)
89d9a83b70 [flang][cuda] Use NVVM op for barrier0 intrinsic (#140947)
The simple form of `Barrier0Op` is available in the NVVM dialect. It is
needed to use it instead of the string version since
https://github.com/llvm/llvm-project/pull/140615
2025-05-21 13:05:14 -07:00
Zhen Wang
4042a002ce [flang] [cuda] implicitly set DEVICE attribute to scalars in device routines (#140834)
Scalars inside device routines also need to implicitly set the DEVICE
attribute, except for function results.
2025-05-21 10:55:52 -07:00
jeanPerier
2d956d2d4e [flang] fix ICE with ignore_tkr(tk) character in explicit interface (#140885)
Some MPI libraries use character dummies + ignore(TKR) to allow passing
any kind of buffer.

This was meant to already be handled by #108168
However, when the library interface also had an argument requiring an
explicit interface, `builder.convertWithSemantics` was not allowed to properly deal
with the actual/dummy type mismatch and generated bad IR causing errors like:
`'fir.convert' op invalid type conversion'!fir.ref' / '!fir.boxchar\<1\>'`.

This restriction was artificial, lowering should just handle any cases
allowed by semantics. Just remove it.
2025-05-21 15:31:18 +02:00
Sebastian Pop
d36028120a [flang] add -floop-interchange and enable it with opt levels (#140182)
Enable the use of -floop-interchange from the flang driver.
Enable in flang LLVM's loop interchange at levels -O2, -O3, -Ofast, and -Os.
2025-05-21 08:27:39 -05:00
Scott Manley
651db24a9c [OpenACC] rename private/firstprivate recipe attributes (#140719)
Make private and firstprivate recipe attribute names consistent with
reductionRecipes attribute
2025-05-21 07:38:08 -05:00