Commit Graph

9879 Commits

Author SHA1 Message Date
Mats Petersson
9925359fee [flang][llvm][openmp]Add Initializer clause to OMP.td (#129540)
Then use this in the Flang compiler for parsing the OpenMP declare
reduction.

This has no real functional change to the existing code, it's only
moving the declaration itself around.

A few tests has been updated, to reflect the new type names.
2025-03-05 15:41:24 +00:00
NimishMishra
0ae1f0a310 [flang] Rely on global initialization for simpler derived types (#114002)
Currently, all derived types are initialized through `_FortranAInitialize`, which is functionally correct, but bears poor runtime performance. This patch falls back on global initialization for "simpler" derived types to speed up the initialization.
2025-03-05 05:44:51 -08:00
jeanPerier
7302e1b94e [flang] implement simple pointer assignments inside FORALL (#129522)
The semantic of pointer assignments inside FORALL requires evaluating
the targets (RHS) and pointer variables (LHS) of all iterations before
evaluating the assignments.

In practice, if the compiler can prove that the RHS and LHS evaluations
are not impacted by the assignments, the evaluation of the FORALL
assignment statement can be done in a single loop. However, if the
compiler cannot prove this, it needs to "save" the addresses of the
targets and/or the pointer descriptors of each iterations before doing
the assignments.

This patch implements the most common cases where there is no lower bound
spec, no bounds remapping, the LHS is not polymorphic, and the RHS is
not NULL.

The HLFIR operation used to represent assignments inside FORALL can be
used for pointer assignments to (the only difference being that the LHS
is a descriptor address).

The analysis for intrinsic assignment can be reused, with the
distinction that the RHS data is not read during the assignment.

The logic that is used to save LHS in intrinsic assignments inside
FORALL is extracted to be used for the RHS of pointer assignments when
needed (saving a descriptor value).
Pointer assignment LHS are just descriptor addresses and are saved as
int_ptr values.
2025-03-05 11:24:04 +01:00
Iñaki Amatria Barral
6eefadd8ef [flang][Semantics] Ensure deterministic mod file output (#129669)
This PR is a follow-up to #128655.

It adds another test to ensure deterministic ordering in `.mod` files
and includes related changes to prevent non-deterministic ordering
caused by iterating over a set ordered by heap pointers. This issue is
particularly noticeable when using Flang as a library and compiling the
same files multiple times.

The reduced test case is as minimal as possible. We were unable to
reproduce the issue with a smaller set of files.
2025-03-05 08:27:17 +01:00
Eugene Epshteyn
ab6cc6b7b3 [flang] Allow nested scopes for implied DO loops with DATA statements (#129410)
Previously, nested scopes for implied DO loops with DATA statements were
disallowed, which meant that the following code couldn't compile due to
re-use of `j` loop variable name:
    
    DATA (a(i),(b(i,j),j=1,3),(c(i,j),j=1,3),i=0,4)/
    
This change allows nested scopes implied DO loops, which allows the code
above to compile.

Tests modified to in accordance with this change:
Semantics/resolve40.f90, Semantics/symbol09.f90
2025-03-04 20:41:01 -05:00
jeanPerier
9a659fac2f [flang] fix MAXVAL(x%array_comp_with_custom_lower_bounds) (#129684)
The HLFIR inlining of MAXVAL kicks in at O1 and more when the argument
is an array component reference but the implementation did not account
for the rare cases where the array components have non default lower
bounds.

This patch fixes the issue by using `getElementAt` to compute the
element address.
Rename `indices` to `oneBasedIndices` for more clarity.
2025-03-04 17:52:05 +01:00
Abid Qadeer
e27b8b2eda [flang][debug] Improve handling of cyclic derived types with classes. (#129588)
While checking if a type should be cached or not, we use
`getDerivedType` to peel outer layers and get to the base type. This
function did not peel the `fir.class` which caused the algorithm to
fail.

Fixes #128606.
2025-03-04 10:27:24 +00:00
Slava Zakharin
f57756a640 [flang-rt] Use RT_API_ATTRS for ErfcScaled. (#129598)
As long as it is a host-only function, it cannot be referenced
by the flang-rt's ErfcScaled entry points. With the markup in place,
it is compiling properly by a CUDA compiler.
2025-03-03 17:10:50 -08:00
Peter Klausler
f6e83664e0 [flang] Improve two coarray error messages (#129597)
Two messages that complain about local variables mention that they don't
have the SAVE attribute; in both cases, it would be okay if they were
ALLOCATABLE instead. Clarify the messages.
2025-03-03 14:47:02 -08:00
Peter Klausler
79a25e11fe [flang] Further work on NULL(MOLD=allocatable) (#129345)
Refine handling of NULL(...) in semantics to properly distinguish
NULL(), NULL(objectPointer), NULL(procPointer), and NULL(allocatable)
from each other in relevant contexts.

Add IsNullAllocatable() and IsNullPointerOrAllocatable() utility
functions. IsNullAllocatable() is true only for NULL(allocatable); it is
false for a bare NULL(), which can be detected independently with
IsBareNullPointer().

IsNullPointer() now returns false for NULL(allocatable).

ALLOCATED(NULL(allocatable)) now works, and folds to .FALSE.

These utilities were modified to accept const pointer arguments rather
than const references; I usually prefer this style when the result
should clearly be false for a null argument (in the C sense), and it
helped me find all of their use sites in the code.
2025-03-03 14:46:35 -08:00
Peter Klausler
b2ba43a9c1 [flang] Refine checking of type-bound generics (#129292)
I merged a patch yesterday
(https://github.com/llvm/llvm-project/pull/128980) that strengthened
error detection of indistinguishable specific procedures in a type-bound
generic procedure, and broke a couple of tests. Refine the check so that
it doesn't flag valid cases of overridden bindings, and add a thorough
test with all of the boundary cases that I can think of.
2025-03-03 14:46:08 -08:00
Krzysztof Parzyszek
8f971ca1d9 [flang] Move DumpEvaluateExpr from Lower to Semantics (#128723)
Since evaluate::Expr can show up in the parse tree in the semantic
analysis step, make it possible to dump its structure in the Semantics
module.

The Lower module depends on Semantics, so the code is still accessible
in it.
2025-03-03 15:38:42 -06:00
Jean-Didier PAILLEUX
a9b2e31fb0 [flang] Define CO_REDUCE intrinsic procedure (#125115)
Define the intrinsic `CO_REDUCE` and add semantic checks.
A test was already present but was at `XFAIL`. It has been modified to
take new messages into the output.
2025-03-03 20:50:02 +01:00
Kelvin Li
83f8721201 [flang] handle passing bind(c) derived type by value for ppc64le and powerpc64-aix (#128780) 2025-03-03 14:43:43 -05:00
Slava Zakharin
a704e6587b [flang] Added alternative inlining code for hlfir.cshift. (#129176)
Flang generates slower code for `CSHIFT(CSHIFT(PTR(:,:,I),sh1,1),sh2,2)`
pattern in facerec than other compilers. The first CSHIFT can be done
as two memcpy's wrapped in a loop for the second dimension.
This does require creating a temporary array, but it seems to be faster,
than the current hlfir.elemental inlining.

I started with modifying the new index computation in
hlfir.elemental inlining: the new arith.select approach does enable
some vectorization in LLVM, but on x86 it is using gathers/scatters
and does not give much speed-up.

I also experimented with LoopBoundSplitPass
and InductiveRangeCheckElimination for a simple (not chained) CSHIFT
case, but I could not adjust them to split the loop with a condition
on the value of the IV into two loops with disjoint iteration spaces.
I thought if I could do it, I would be able to keep the hlfir.elemental
inlining mostly untouched, and then adjust the hlfir.elemental inlining
heuristics for the facerec case.

Since I was not able to make these pass work for me, I added a special
case inlining for CSHIFT(ARRAY,SH,DIM=1) via hlfir.eval_in_mem.
If ARRAY is not statically known to have the contiguous leading
dimension, there is a dynamic check for contiguity, which allows
exposing it to LLVM and enabling the rewrite of the copy loops
into memcpys. This approach is stepping on the toes of LoopVersioning,
but it is helpful in facerec case.

I measured ~6% speed-up on grace, and ~4% on zen4.
2025-03-03 09:58:20 -08:00
Slava Zakharin
0735cece68 [flang] Fixed fir.coordinate_of access in AddDebugInfo. (#129423)
The issue came up after #127231, when fir.coordinate_of, fed into
a declare, only has the field attribute and no coordinates.
2025-03-03 07:53:17 -08:00
Krzysztof Parzyszek
9573c62114 [flang][OpenMP] Accept modern syntax of FLUSH construct (#128975)
The syntax with the object list following the memory-order clause has
been removed in OpenMP 5.2. Still, accept that syntax with versions >=
5.2, but treat it as deprecated (and emit a warning).
2025-03-03 07:59:19 -06:00
Mats Petersson
50301052e9 [flang][OpenMP]Support for subroutine call for DECLARE REDUCTION init (#127889)
The DECLARE REDUCTION allows the initialization part to be either an
expression or a call to a subroutine.

This modifies the parsing and semantic analysis to allow the use of the
subroutine, in addition to the simple expression that was already
supported.

New tests in parser and semantics sections check that the generated
structure is as expected.

DECLARE REDUCTION lowering is not yet implemented, so will end in a
TODO. A new test with an init subroutine is added, that checks that this
variant also ends with a "Not yet implemented" message.
2025-03-03 13:49:51 +00:00
Jean-Didier PAILLEUX
370d34fe40 [flang][Driver] Add support of -fd-lines-as-comments and -fd-lines-as-code flags (#127605)
`-fd-lines-as-code` and `-fd-lines-as-comments` enables treatment for
lines beginning with `d` or `D` in fixed form sources.
Using these options in free form has no effect.
If the `-fd-lines-as-code` option is given they are treated as if the
first column contained a blank.
If the `-fd-lines-as-comments` option is given, they are treated as
comment lines.
2025-03-03 11:55:36 +00:00
jeanPerier
9805854699 [flang][NFC] clean-up fir.field_index legacy usages in tests (#129219)
After #127231, fir.coordinate_of should directly carry the field.

I updated the lowering and codegen tests in #12731, but not the FIR to
FIR tests, which is what this patch is cleaning up.
2025-03-03 10:01:54 +01:00
Valentin Clement (バレンタイン クレメン)
d1fd3698a9 [flang][cuda] Allow unsupported data transfer to be done on the host (#129160)
Some data transfer marked as unsupported can actually be deferred to an
assignment on the host when the variables involved are unified or
managed.
2025-03-02 16:12:01 -08:00
klensy
62f15a042b [flang][test] Fix filecheck annotation typos [2/n] (#126099)
Few more fixes, previous: #92387

Co-authored-by: klensy <nightouser@gmail.com>
2025-02-28 10:04:16 +00:00
jeanPerier
a8db1fb9b5 [flang] update fir.coordinate_of to carry the fields (#127231)
This patch updates fir.coordinate_op to carry the field index as
attributes instead of relying on getting it from the fir.field_index
operations defining its operands.

The rational is that FIR currently has a few operations that require
DAGs to be preserved in order to be able to do code generation. This is
the case of fir.coordinate_op, which requires its fir.field operand
producer to be visible.
This makes IR transformation harder/brittle, so I want to update FIR to
get rid if this.

Codegen/printer/parser of fir.coordinate_of and many tests need to be
updated after this change.
2025-02-28 09:50:05 +01:00
Kareem Ergawy
e0c690990d [flang][OpenMP] Add reduction clause support to loop directive (#128849)
Extends `loop` directive transformation by adding support for the
`reduction` clause.
2025-02-28 05:46:03 +01:00
KAWASHIMA Takahiro
0e56f6dc3e [flang][docs][NFC] Fix Markdown /*comments*/ (#129018)
`*` in `/*comments*/` were interpreted as emphasis marks and were not
displayed in https://flang.llvm.org/docs/Extensions.html.
2025-02-28 10:18:37 +09:00
Peter Klausler
51dc52631c [flang] Catch more defined I/O conflicts (#129115)
The code that checks for conflicts between type-bound defined I/O
generic procedures and non-type-bound defined I/O interfaces only works
when then procedures are defined in the same module as subroutines. It
doesn't catch conflicts when either are external procedures, procedure
pointers, dummy procedures, &c. Extend the checking to cover those cases
as well.

Fixes https://github.com/llvm/llvm-project/issues/128752.
2025-02-27 16:16:34 -08:00
Kazu Hirata
44c6616a4a [flang] Fix a warning
This patch fixes:

  flang/lib/Semantics/check-declarations.cpp:2009:15: error: unused
  variable 'kind' [-Werror,-Wunused-variable]
2025-02-27 14:48:13 -08:00
Peter Klausler
cbef629838 [flang] Catch type-bound generic with inherited indistinguishable spe… (#128980)
…cific

When checking generic procedures for indistinguishable specific
procedures, don't neglect to include specific procedures from any
accessible instance of the generic procedure inherited from its parent
type..

Fixes https://github.com/llvm/llvm-project/issues/128760.
2025-02-27 14:33:11 -08:00
Peter Klausler
c6dd9f4278 [flang] Catch usage of : and * lengths in array c'tors (#128974)
The definition of an array constructor doesn't preclude the use of
[character(:)::] or [character(*)::] directly, but there is language
elsewhere in the standard that restricts their use to specific contexts,
neither of which include explicitly typed array constructors.

Fixes https://github.com/llvm/llvm-project/issues/128755.
2025-02-27 14:32:50 -08:00
Peter Klausler
78acf7bb0a [flang] Enforce C1503 (#128962)
Enforce an obscure constraint from the standard: an abstract interface
is not allowed to have the same name as an intrinsic type keyword. I
suspect this is meant to prevent a declaration like "PROCEDURE(REAL),
POINTER :: P" from being ambiguous.

Fixes https://github.com/llvm/llvm-project/issues/128744.
2025-02-27 14:32:30 -08:00
Peter Klausler
e843d514b1 [flang] Refine handling of SELECT TYPE associations in analyses (#128935)
A few bits of semantic checking need a variant of the
ResolveAssociations utility function that stops when hitting a construct
entity for a type or class guard. This is necessary for cases like the
bug below where the analysis is concerned with the type of the name in
context, rather than its shape or storage or whatever. So add a flag to
ResolveAssociations and GetAssociationRoot to make this happen, and use
it at the appropriate call sites.

Fixes https://github.com/llvm/llvm-project/issues/128608.
2025-02-27 14:32:12 -08:00
Peter Klausler
523537f0c9 [flang] Silence spurious error (#128777)
When checking for conflicts between type-bound generic defined I/O
procedures and non-type-bound defined I/O generic interfaces, don't
worry about conflicts where the type-bound generic interface is
inaccessible in the scope around the non-type-bound interface.

Fixes https://github.com/llvm/llvm-project/issues/126797.
2025-02-27 14:31:50 -08:00
Peter Klausler
8b7a90b84b [flang] Accept proc ptr function result as actual argument without IN… (#128771)
…TENT

A dummy procedure pointer with no INTENT attribute may associate with an
actual argument that is the result of a reference to a function that
returns a procedure pointer, we think.

Fixes https://github.com/llvm/llvm-project/issues/126950.
2025-02-27 14:31:24 -08:00
Peter Klausler
e1ba1be787 [flang] Account for accessibility in extensibility check (#128765)
A derived type with a component of the same name as the type is not
extensible... unless the extension occurs in another module where the
conflicting component is inaccessible.

Fixes https://github.com/llvm/llvm-project/issues/126114.
2025-02-27 14:30:55 -08:00
Peter Klausler
161d002a09 [flang] Silence warnings from hermetic module files (#128763)
Modules read from module files must have their symbols tagged with the
ModFile flag to suppress all warnings messages that might be emitted for
their contents. (Actionable warnings will have been emitted when the
modules were originally compiled, so we don't want to repeat them later
when the modules are USE'd.) The module symbols of the additional
modules in hermetic module files were not being tagged with that flag;
fix.
2025-02-27 14:29:35 -08:00
Peter Klausler
fce29486ac [flang] Fix bogus error on defined I/O procedure. (#125898)
The check that "v_list" be deferred shape is just wrong; there are no
deferred shape non-pointer non-allocatable dummy arguments in Fortran.
Correct to check for an assumed shape dummy argument. And de-split the
error messages that were split across multiple source lines, making them
much harder to find with grep.

Fixes https://github.com/llvm/llvm-project/issues/125878.
2025-02-27 14:29:00 -08:00
Peter Klausler
3e3855b0e5 [flang] Don't flag CLASS(*) ASSOCIATED() pointer or target as error (#125890)
As I read the standard, an unlimited polymorphic pointer or target
should be viewed as compatible with any data target or data pointer when
used in the two-argument form of the intrinsic function ASSOCIATED().

Fixes https://github.com/llvm/llvm-project/issues/125774.
2025-02-27 14:28:34 -08:00
Peter Klausler
29025a0600 [flang] Catch more semantic errors with coarrays (#125536)
Detect and report a bunch of uncaught semantic errors with coarray
declarations. Add more tests, and clean up bad usage in existing tests.
2025-02-27 14:28:08 -08:00
Peter Klausler
a21089a24b [flang] Support COSHAPE() intrinsic function (#125286)
Enable COSHAPE in the intrinsics table and enable its test.
2025-02-27 14:27:46 -08:00
Peter Klausler
9a49a03dc9 [flang] Refine handling of NULL() actual to non-optional allocatable … (#116126)
…dummy

We presently allow a NULL() actual argument to associate with a
non-optional dummy allocatable argument only under INTENT(IN). This is
too strict, as it precludes the case of a dummy argument with default
intent. Continue to require that the actual argument be definable under
INTENT(OUT) and INTENT(IN OUT), and (contra XLF) interpret NULL() as
being an expression, not a definable variable, even when it is given an
allocatable MOLD.

Fixes https://github.com/llvm/llvm-project/issues/115984.
2025-02-27 14:27:19 -08:00
vdonaldson
62d4cc811a [flang] Modifications to ieee_support_standard (#128895)
Some Arm processors support exception halting control and some do not.
An Arm executable will run on either type of processor, so it is
effectively unknown at compile time whether or not this support will be
available. ieee_support_halting is therefore implemented with a runtime
check.

The result of a call to ieee_support_standard depends in part on support
for halting control. Update the ieee_support_standard implementation to
check for support for halting control at runtime.
2025-02-27 14:57:10 -05:00
Jan Leyonberg
c3b3352f73 [MLIR][ROCDL] Add conversion of math.erfc to AMD GPU library calls (#128899)
This patch adds a pattern to convert the math.erfc operation to AMD GPU
library calls.

Depends on: #128897 for the flang test
2025-02-27 14:32:25 -05:00
Jan Leyonberg
326638bac1 [Flang] Generate math.erfc op for non-precise erfc interinsic calls (#128897)
This patch changes the codegen for non-precise erfc calls to generate
math.erfc ops. This wasn't done before because the math dialect did not
have a erfc operation at the time.
2025-02-27 11:57:56 -05:00
Kareem Ergawy
f6262fa035 [flang] Extend omp loop semantic checks for reduction (#128823)
Extend semantic checks for `omp loop` directive to report errors when a
`reduction` clause is specified on a standalone `loop` directive with
`teams` binding.

This is similar to how clang behaves.
2025-02-27 12:08:04 +01:00
Tom Eccles
db48d49311 [mlir][OpenMP] Pack task private variables into a heap-allocated context struct (#125307)
See RFC:

https://discourse.llvm.org/t/rfc-openmp-supporting-delayed-task-execution-with-firstprivate-variables/83084

The aim here is to ensure that tasks which are not executed for a while
after they are created do not try to reference any data which are now
out of scope. This is done by packing the data referred to by the task
into a heap allocated structure (freed at the end of the task).

I decided to create the task context structure in
OpenMPToLLVMIRTranslation instead of adapting how it is done
CodeExtractor (via OpenMPIRBuilder] because CodeExtractor is (at least
in theory) generic code which could have other unrelated uses.
2025-02-27 09:22:44 +00:00
Valentin Clement (バレンタイン クレメン)
110b77f328 [flang][cuda] Handle floats in atomiccas (#128970) 2025-02-26 20:13:10 -08:00
Valentin Clement (バレンタイン クレメン)
eb84c1181e [flang][cuda] Add more math intrinsic interfaces in cudadevice (#128931) 2025-02-26 13:32:54 -08:00
Iñaki Amatria Barral
722c7c0b0f [flang][Semantics] Ensure deterministic mod file output (#128655)
This PR adds a test to ensure deterministic ordering in `.mod` files. It
also includes related changes to prevent non-deterministic symbol
ordering caused by pointers outside the cooked source. This issue is
particularly noticeable when using Flang as a library and compiling the
same files multiple times.
2025-02-26 18:19:02 +01:00
Valentin Clement (バレンタイン クレメン)
e350485595 [flang][cuda] Set alloca block in cuf kernel (#128776)
Temporary created during lowering in a cuf kernel must be set in the cuf
kernel itself otherwise they will be allocated on the host.
2025-02-25 17:25:23 -08:00
Valentin Clement (バレンタイン クレメン)
f3000d7d27 [flang][cuda] Do not trigger automatic deallocation in main (#128789)
Similar to host flow, do not trigger automatic deallocation at then end
of the main program since anything could happen like a
cudaDevcieReset().
2025-02-25 17:25:04 -08:00