146 Commits

Author SHA1 Message Date
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
Peter Klausler
6e0b0ec66a
[flang] Fix crash in Semantics (#148706)
Allow for renaming in USE association of Cray pointers.

Fixes https://github.com/llvm/llvm-project/issues/148559.
2025-07-16 09:09:05 -07:00
Peter Klausler
4dceb25dd1
[flang] Don't create bogus tokens from token pasting (##) (#147596)
When blank tokens arise from macro replacement in token sequences with
token pasting (##), the preprocessor is producing some bogus tokens
(e.g., "name(") that can lead to subtle bugs later when macro names are
not recognized as such.

The fix is to not paste tokens together when the result would not be a
valid Fortran or C token in the preprocessing context.
2025-07-14 11:11:43 -07:00
Valentin Clement (バレンタイン クレメン)
659c8102f4
Reland [flang][cuda] Allocate derived-type with CUDA component in anaged memory (#147416) 2025-07-07 17:40:04 -07:00
Valentin Clement (バレンタイン クレメン)
07cc7ea7d5
Reland [flang][cuda] Do not create global for derived-type with allocatable device components (#147402)
Reviewed in #146780

derived type with CUDA device allocatable components will be handle via
CUDA allocation. Do not create global for them.
2025-07-07 15:19:41 -07:00
Valentin Clement
e718ce0037 Revert "[flang][cuda] Do not create global for derived-type with allocatable device components (#146780)"
This reverts commit e873ce31ae0e875081c8e5480c9c4925c97469ce.
2025-07-02 17:51:55 -07:00
Valentin Clement
a5350785db Revert "[flang][cuda] Allocate derived-type with CUDA componement in managed memory (#146797)"
This reverts commit 925588cd001a91d592b99e6e7c6bee9514f5a26e.
2025-07-02 17:51:45 -07:00
Valentin Clement (バレンタイン クレメン)
925588cd00
[flang][cuda] Allocate derived-type with CUDA componement in managed memory (#146797)
Similarly to descriptor for device data, put derived type holding device
descriptor in managed memory.
2025-07-02 16:02:08 -07:00
Valentin Clement (バレンタイン クレメン)
e873ce31ae
[flang][cuda] Do not create global for derived-type with allocatable device components (#146780)
derived type with CUDA device allocatable components will be handle via
CUDA allocation. Do not create global for them.
2025-07-02 15:43:09 -07: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
Peter Klausler
2bf3ccabfa
[flang] Restructure runtime to avoid recursion (relanding) (#143993)
Recursion, both direct and indirect, prevents accurate stack size
calculation at link time for GPU device code. Restructure these
recursive (often mutually so) routines in the Fortran runtime with new
implementations based on an iterative work queue with
suspendable/resumable work tickets: Assign, Initialize, initializeClone,
Finalize, and Destroy.

Default derived type I/O is also recursive, but already disabled. It can
be added to this new framework later if the overall approach succeeds.

Note that derived type FINAL subroutine calls, defined assignments, and
defined I/O procedures all perform callbacks into user code, which may
well reenter the runtime library. This kind of recursion is not handled
by this change, although it may be possible to do so in the future using
thread-local work queues.

(Relanding this patch after reverting initial attempt due to some test
failures that needed some time to analyze and fix.)

Fixes https://github.com/llvm/llvm-project/issues/142481.
2025-06-16 14:37:01 -07:00
Krzysztof Parzyszek
141d390dcb
[flang][OpenMP] Overhaul implementation of ATOMIC construct (#137852)
The parser will accept a wide variety of illegal attempts at forming an
ATOMIC construct, leaving it to the semantic analysis to diagnose any
issues. This consolidates the analysis into one place and allows us to
produce more informative diagnostics.

The parser's outcome will be parser::OpenMPAtomicConstruct object
holding the directive, parser::Body, and an optional end-directive. The
prior variety of OmpAtomicXyz classes, as well as OmpAtomicClause have
been removed. READ, WRITE, etc. are now proper clauses.

The semantic analysis consistently operates on "evaluation"
representations, mainly evaluate::Expr (as SomeExpr) and
evaluate::Assignment. The results of the semantic analysis are stored in
a mutable member of the OpenMPAtomicConstruct node. This follows a
precedent of having `typedExpr` member in parser::Expr, for example.
This allows the lowering code to avoid duplicated handling of AST nodes.

Using a BLOCK construct containing multiple statements for an ATOMIC
construct that requires multiple statements is now allowed. In fact, any
nesting of such BLOCK constructs is allowed.

This implementation will parse, and perform semantic checks for both
conditional-update and conditional-update-capture, although no MLIR will
be generated for those. Instead, a TODO error will be issues prior to
lowering.

The allowed forms of the ATOMIC construct were based on the OpenMP 6.0
spec.
2025-06-11 10:05:34 -05:00
Peter Klausler
10f512f7bb
Revert runtime work queue patch, it breaks some tests that need investigation (#143713)
Revert "[flang][runtime] Another try to fix build failure"

This reverts commit 13869cac2b5051e453aa96ad71220d9d33404620.

Revert "[flang][runtime] Fix build bot flang-runtime-cuda-gcc errors
(#143650)"

This reverts commit d75e28477af0baa063a4d4cc7b3cf657cfadd758.

Revert "[flang][runtime] Replace recursion with iterative work queue
(#137727)"

This reverts commit 163c67ad3d1bf7af6590930d8f18700d65ad4564.
2025-06-11 07:55:06 -07:00
Peter Klausler
b994a4c04f
[flang][NFC] Clean up code in two new functions (#142037)
Two recently-added functions in Semantics/tools.h need some cleaning up
to conform to the coding style of the project. One of them should
actually be in Parser/tools.{h,cpp}, the other doesn't need to be
defined in the header.
2025-06-10 14:44:41 -07:00
Peter Klausler
163c67ad3d
[flang][runtime] Replace recursion with iterative work queue (#137727)
Recursion, both direct and indirect, prevents accurate stack size
calculation at link time for GPU device code. Restructure these
recursive (often mutually so) routines in the Fortran runtime with new
implementations based on an iterative work queue with
suspendable/resumable work tickets: Assign, Initialize, initializeClone,
Finalize, and Destroy.

Default derived type I/O is also recursive, but already disabled. It can
be added to this new framework later if the overall approach succeeds.

Note that derived type FINAL subroutine calls, defined assignments, and
defined I/O procedures all perform callbacks into user code, which may
well reenter the runtime library. This kind of recursion is not handled
by this change, although it may be possible to do so in the future using
thread-local work queues.

The effects of this restructuring on CPU performance are yet to be
measured.
2025-06-10 14:44:19 -07:00
Peter Klausler
f600154ebf
[flang] PRIVATE statement in derived type applies to proc components (#139336)
A PRIVATE statement in a derived type definition is failing to set the
default accessibility of procedure pointer components; fix.

Fixes https://github.com/llvm/llvm-project/issues/138911.
2025-05-12 12:28:31 -07:00
Peter Klausler
3674a5f18e
[flang] Permit unused USE association of subprogram name (#134009)
A function or subroutine can allow an object of the same name to appear
in its scope, so long as the name is not used. This is similar to the
case of a name being imported from multiple distinct modules, and
implemented by the same representation.

It's not clear whether this is conforming behavior or a common
extension.
2025-04-04 08:41:32 -07:00
Peter Klausler
587f997db7
[flang] Catch C15104(4) violations when coindexing is present (#130677)
The value of a structure constructor component can't have a pointer
ultimate component if it is a coindexed designator.
2025-03-19 11:58:59 -07: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
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
Michael Kruse
b815a3942a
[Flang] Move non-common headers to FortranSupport (#124416)
Move non-common files from FortranCommon to FortranSupport (analogous to
LLVMSupport) such that

* declarations and definitions that are only used by the Flang compiler,
but not by the runtime, are moved to FortranSupport

* declarations and definitions that are used by both ("common"), the
compiler and the runtime, remain in FortranCommon

* generic STL-like/ADT/utility classes and algorithms remain in
FortranCommon

This allows a for cleaner separation between compiler and runtime
components, which are compiled differently. For instance, runtime
sources must not use STL's `<optional>` which causes problems with CUDA
support. Instead, the surrogate header `flang/Common/optional.h` must be
used. This PR fixes this for `fast-int-sel.h`.

Declarations in include/Runtime are also used by both, but are
header-only. `ISO_Fortran_binding_wrapper.h`, a header used by compiler
and runtime, is also moved into FortranCommon.
2025-02-06 15:29:10 +01:00
Peter Klausler
4927a5ed4a
[flang] Allow defined assignment to CLASS(*) (#124817)
An unlimited polymorphic left-hand side variable is acceptable in the
definition of a defined assignment subroutine.

Fixes https://github.com/llvm/llvm-project/issues/124621.
2025-01-31 10:53:40 -08:00
Peter Klausler
2625510ef8
[flang] Refine EVENT_TYPE/LOCK_TYPE usage checks (#123244)
The event variable in an EVENT POST/WAIT statement can be a coarray
reference, and need not be an entire coarray.

Variables and potential subobject components with EVENT_TYPE/LOCK_TYPE
must be coarrays, unless they are potential subobjects nested within
coarrays or pointers.
2025-01-27 08:45:11 -08:00
Peter Klausler
038b42ba5b
[flang] Safer hermetic module file reading (#121002)
When a hermetic module file is read, use a new scope to hold its
dependent modules so that they don't conflict with any modules in the
global scope.
2025-01-27 08:43:41 -08:00
Peter Klausler
ecf264d3b4
[flang] Fix spurious error message due to inaccessible generic binding (#122810)
Generic operator/assignment checks for distinguishable specific
procedures must ignore inaccessible generic bindings.

Fixes https://github.com/llvm/llvm-project/issues/122764.
2025-01-14 13:02:21 -08:00
Peter Klausler
9f0f54a629
[flang] Improve error messages for module self-USE (#122747)
A module can't USE itself, either directly within the top-level module
or from one of its submodules. Add a test for this case (which we
already caught), and improve the diagnostic for the more confusing case
involving a submodule.
2025-01-14 13:00:03 -08:00
Peter Klausler
9696355484
[flang] Better messages and error recovery for a bad RESHAPE (#122604)
Add tests for negative array extents where necessary, motivated by a
compiler crash exposed by yet another fuzzer test, and improve overall
error message quality for RESHAPE().

Fixes https://github.com/llvm/llvm-project/issues/122060.
2025-01-14 12:57:49 -08:00
Peter Klausler
fc97d2e68b
[flang] Add UNSIGNED (#113504)
Implement the UNSIGNED extension type and operations under control of a
language feature flag (-funsigned).

This is nearly identical to the UNSIGNED feature that has been available
in Sun Fortran for years, and now implemented in GNU Fortran for
gfortran 15, and proposed for ISO standardization in J3/24-116.txt.

See the new documentation for details; but in short, this is C's
unsigned type, with guaranteed modular arithmetic for +, -, and *, and
the related transformational intrinsic functions SUM & al.
2024-12-18 07:02:37 -08:00
Peter Klausler
2f22656db5
[flang] Minor cleanup (move function into /tools.cpp) (#111587)
The semantics utility GetAllNames has declarations in two header files
and a definition that really should be in the common utilities source
file. Remove the redudant declaration from resolve-names-utils.h and
move code from resolve-names-utils.cpp into Semantics/tools.cpp.
2024-10-10 10:25:42 -07:00
Peter Klausler
70cbedcd6e
[flang] Catch errors with INTENT(OUT) assumed rank dummy arguments (#111204)
Emit an error when an actual argument with potentially unknown size
(assumed size, or non-pointer non-allocatable assumed rank) with any
risk of needing initialization, finalization, or destruction is
associated with an INTENT(OUT) dummy argument with assumed rank.

Emit an optional portability warning for cases where the type is known
to be safe from needing initialization, finalization, or destruction,
since it's not conforming and might elicit an error from other
compilers.

Fixes https://github.com/llvm/llvm-project/issues/111120.
2024-10-07 13:17:45 -07:00
Peter Klausler
49016d53e8
[flang] Silence bogus error message (#111057)
Fortran doesn't permit the use of a polymorphic I/O list item for
intrinsic data transfers, so the compiler emits an error message for
polymorphic items whose types can't possibly be handled by a defined I/O
subroutine. This check didn't allow for the possibility that the defined
I/O subroutine might apply to the parent component of an extended type.

Fixes https://github.com/llvm/llvm-project/issues/111021.
2024-10-07 13:17:28 -07:00
Youngsuk Kim
d5dd7d230e [flang] Tidy uses of raw_string_ostream (NFC)
As specified in the docs,
1) raw_string_ostream is always unbuffered and
2) the underlying buffer may be used directly

( 65b13610a5226b84889b923bae884ba395ad084d for further reference )

Avoid unneeded calls to raw_string_ostream::str(), to avoid excess indirection.
2024-09-17 12:20:21 -05:00
Peter Klausler
9e53e77265
[flang] Fix warnings from more recent GCCs (#106567)
While experimenting with some more recent C++ features, I ran into
trouble with warnings from GCC 12.3.0 and 14.2.0. These warnings looked
legitimate, so I've tweaked the code to avoid them.
2024-09-04 10:52:51 -07:00
Peter Klausler
143be4eafc
[flang] Silence spurious error (#104821)
Don't complain about a local object with an impure final procedure in a
pure subprogram when the local object is a named constant.

Fixes https://github.com/llvm/llvm-project/issues/104796.
2024-08-20 12:04:32 -07:00
Peter Klausler
25822dc392
[flang] Fix searches for polymorphic components (#102212)
FindPolymorphicAllocatableUltimateComponent needs to be
FindPolymorphicAllocatablePotentialComponent. The current search is
missing cases where a derived type has an allocatable component whose
type has a polymorphic allocatable component.
2024-08-08 11:07:40 -07:00
Peter Klausler
539a6b500c
[flang] Detect use-before-decl errors on type parameters (#99947)
Ensure that type parameters are declared as such before being referenced
within the derived type definition. (Previously, such references would
resolve to symbols in the enclosing scope.)

This change causes the symbols for the type parameters to be created
when the TYPE statement is processed in name resolution. They are
TypeParamDetails symbols with no KIND/LEN attribute set, and they shadow
any symbols of the same name in the enclosing scope.

When the type parameter declarations are processed, the KIND/LEN
attributes are set. Any earlier reference to a type parameter with no
KIND/LEN attribute elicits an error.

Some members of TypeParamDetails have been retyped &/or renamed.
2024-07-30 09:42:15 -07:00
Peter Klausler
33c27f28d1
[flang] Warn about undefined function results (#99533)
When the result of a function never appears in a variable definition
context, emit a warning.

If the function has multiple result variables due to alternate ENTRY
statements, any definition will suffice.

The implementation of this check is tied to the general variable
definability checking utility in semantics. Every variable definition
context uses it to ensure that no undefinable variable is being defined.
A set of defined variables is maintained in the SemanticsContext and,
when the warning is enabled and no fatal error has been reported, the
scope tree is traversed and all the function subprograms' results are
tested for membership in that set.
2024-07-30 09:41:46 -07:00
Peter Klausler
a51d92a447
[flang] Fix crash in semantics on error case (#91482)
An erroneous statement function declaration exposed an unhandled
situation in a utility routine in semantics. Patch that hole and add a
test.

Fixes https://github.com/llvm/llvm-project/issues/91429.
2024-05-09 11:42:25 -07:00
Peter Klausler
505f6da196
[flang] Ensure all warning/portability messages are guarded by Should… (#90518)
…Warn()

Many warning messages were being emitted unconditionally. Ensure that
all warnings are conditional on a true result from a call to
common::LanguageFeatureControl::ShouldWarn() so that it is easy for a
driver to disable them all, or, in the future, to provide per-warning
control over them.
2024-05-01 14:33:14 -07:00
Peter Klausler
f4fc959c35
[flang] Catch impossible but necessary TBP override (#86558)
An apparent attempt to override a type-bound procedure is not allowed to
be interpreted as on override when the procedure is PRIVATE and the
override attempt appears in another module. However, if the TBP that
would have been overridden is a DEFERRED procedure in an abstract base
type, the override must take place. PRIVATE DEFERRED procedures must
therefore have all of their overrides appear in the same module as the
abstract base type.
2024-03-26 10:11:19 -07:00
Peter Klausler
f92f775212
[flang][NFC] Address reported "possible missing return" (#86523)
A function uses "if constexpr" to consider all possible types in a
variant, but looks as if it can fall out without returning an
expression. Add a final "else" with a crash to make things more clear
and to protect against unlikely future extensions of the type.

Fixes https://github.com/llvm/llvm-project/issues/86391.
2024-03-26 10:02:22 -07:00
jeanPerier
de7a50fb88
[flang] Fix lowering of host associated cray pointee symbols (#86121)
Cray pointee symbols can be host associated from a module or host
procedure while the related cray pointer is not explicitly associated.
This caused the "not yet implemented: lowering symbol to HLFIR" to fire
when lowering a reference to the cray pointee and fetching the cray
pointer.

This patch:
- Ensures cray pointers are always instantiated when instantiating a
cray pointee.
- Fix internal procedure lowering to deal with cray pointee host
association like it does for pointers (the lowering strategy for cray
pointee is to create a pointer that is updated with the cray pointer
value before being fetched).

This should fix the bug reported in
https://github.com/llvm/llvm-project/issues/85420.
2024-03-22 11:13:04 +01:00
Kelvin Li
0c21377aea
[flang] Diagnose the impure procedure reference in finalization according to the rank of the entity (#85475)
Use the rank of the array section to determine which final procedure
would be called in diagnosing whether that procedure is impure or not.
2024-03-18 10:59:47 -04:00
Peter Klausler
83ca78deb9
[flang] Emit "raw" name for procedure interface in module file (#83915)
Save both the raw procedure interface symbol as well as the result of
passing it through GetUltimate() and BypassGeneric() in symbol table
entries with ProcEntityDetails. The raw symbol of the interface needs to
be the one used for emitting procedure symbols to module files.

Fixes https://github.com/llvm/llvm-project/issues/83836.
2024-03-05 12:00:46 -08:00
Peter Klausler
2b7a928dd9
[flang] Improve USE merging of homonymous types, interfaces, and proc… (#79364)
…edures

Fortran allows a generic interface to have the same name as a derived
type in the same scope. It also allows a generic interface to have the
same name as one of its specific procedures.

When two modules define the same name, possibly more than once each,
things get exciting. The standard is not clear, and other compilers do
variously different things. We are currently emitting some errors
prematurely for some usage in pfUnit due to how it combines two versions
of a package together via USE association.

This patch handles combinations of derived types and generic interfaces
and their specific procedures in a more principled way. Errors due to
ambiguity are deferred to actual usage of derived types and specific
procedures -- and when they're not used, the program is unambiguous and
no error issues.
2024-01-25 16:51:30 -08:00
Peter Klausler
89165e8b1d
[flang][openacc] Disable CUDA argument checks in OpenACC regions (#72310)
Checks for CUDA Fortran data attribute compatibility don't need to be
applied in OpenACC regions.
2023-11-30 10:58:51 -08:00
Slava Zakharin
be66a2f66b
[flang] Deallocate components of local variables at the end of the scope. (#68064)
Call Destroy runtime for local variables of derived types with
allocatable components.
2023-10-03 13:10:26 -07:00
jeanPerier
710503fc1b
[flang] Finalize polymorphic alloctable component if needed (#67326)
The runtime skips finalization if the runtime type info
"nofinalizationneeded" is set, so it should not be set if the derived
type has polymorphic allocatable components since they may be allocated
to some type extension with a final methods.
IsFinalizable cannot be updated since polymorphic allocatable components
do not imply a final routine will actually be called (it depends of the
dynamic type, which semantics cannot know about), and this would not
match the "Finalizable" definition of the standard in 7.5.6.1. Hence,
this patch adds a MayRequireFinalization helper.

The component visitor change is to avoid crashing in
FindPolymorphicAllocatableUltimateComponent in the test
test/Driver/dump-all-bad.f90 that tries generating runtime type info
even after some semantic error is raised under debug-dump options.
2023-09-25 18:53:36 +02:00
jeanPerier
6ffea74f7c
[flang] Use BIND name, if any, when consolidating common blocks (#65613)
This patch changes how common blocks are aggregated and named in
lowering in order to:

* fix one obvious issue where BIND(C) and non BIND(C) with the same
Fortran name were "merged"

* go further and deal with a derivative where the BIND(C) C name matches
the assembly name of a Fortran common block. This is a bit unspecified
IMHO, but gfortran, ifort, and nvfortran "merge" the common block
without complaints as a linker would have done. This required getting
rid of all the common block mangling early in FIR (\_QC) instead of
leaving that to the phase that emits LLVM from FIR because BIND(C)
common blocks did not have mangled names. Care has to be taken to deal
with the underscoring option of flang-new.

See added flang/test/Lower/HLFIR/common-block-bindc-conflicts.f90 for an
illustration.
2023-09-08 10:43:55 +02:00
Peter Klausler
031b4e5e79
[flang] Support SELECT RANK on allocatables & pointers
Unlike other executable constructs with associating selectors, the
selector of a SELECT RANK construct can have the ALLOCATABLE or POINTER
attribute, and will work as an allocatable or object pointer within
each rank case, so long as there is no RANK(*) case.

Getting this right exposed a correctness risk with the popular
predicate IsAllocatableOrPointer() -- it will be true for procedure
pointers as well as object pointers, and in many contexts, a procedure
pointer should not be acceptable.  So this patch adds the new predicate
IsAllocatableOrObjectPointer(), and updates some call sites of the original
function to use the new one.

Differential Revision: https://reviews.llvm.org/D159043
2023-08-29 14:56:15 -07:00