25 Commits

Author SHA1 Message Date
Peter Klausler
5fc77c1666
[flang] Add new warnings for unused & undefined locals (#173504)
Add a requested warning for completely unused local variables. The
implementation runs a scan over typed expressions during the existing
expression semantics pass to detect variable uses, and a routine at the
end of semantics to take a pass over the symbol tables to find unused
locals.

The new infrastructure needed to detect variable uses, and the existing
infrastructure that detects potential variable definitions, then makes
it easy to detect variables that are used without any possible
initialization or definition, so I did that too.

The warning for unused locals is off by default -- they might indicate a
misspelling (that IMPLICIT NONE would have caught), but seem otherwise
generally benign. The warning for uses of completely uninitialized and
undefined variables, however, is enabled by default, since that's likely
to indicate a program bug that should be investigated.

This patch touches a lot of files lightly. Many of these files are tests
that would have produced needless warning noise; one new test was added.

Fixes https://github.com/llvm/llvm-project/issues/173276.
2025-12-31 12:42:06 -08:00
Krish Gupta
87ca51a7d2
[Flang][OpenMP] Fix crash on invalid atomic variable expressions (#173068)
Fixes #169484

The compiler crashed with an assertion failure when processing OpenMP
ATOMIC constructs containing invalid variable expressions like function
references or undeclared variables.

## Root Cause: 
`CheckAtomicVariable()` assumed `GetAllDesignators()` always returns
exactly one designator, but it returns an empty vector for function
references.

## Fix: 
Replaced the assertion with proper validation that emits diagnostic
errors instead of crashing.

## Testing: 
Added regression test [atomic-invalid-variable.f90]
2025-12-19 13:35:09 -06:00
Krzysztof Parzyszek
c88ae6eb21
[flang][OpenMP] Move two utilities from Semantics to Parser, NFC (#168549)
Move `GetInnermostExecPart` and `IsStrictlyStructuredBlock` from
Semantics/openmp-utils.* to Parser/openmp-utils.*. These two only depend
on the AST contents and properties.
2025-11-18 11:44:03 -06:00
Krzysztof Parzyszek
89577e910e
[flang][OpenMP] Detect complex part designators in atomic variables (#166612)
Complex part designators do not have their own symbols. A symbol
obtained for the expression `x%re` will be the symbol for `x`, and in
this case x is allowed to be allocatable.

Fixes https://github.com/llvm/llvm-project/issues/166278.
2025-11-10 13:22:19 -06:00
Krzysztof Parzyszek
34cf8bb413
[flang][OpenMP] Use parser::UnwrapRef instead of thing/value, NFC (#164087) 2025-10-21 06:17:48 -05:00
Krish Gupta
59ab4d4bbc
[flang][OpenMP] Add diagnostic for ATOMIC WRITE with pointer to non-intrinsic type (#162364)
Fixes https://github.com/llvm/llvm-project/issues/161932

Added a check for non-intrinsic types in non-pointer assignments.

Added bool checkTypeOnPointer = true to both CheckAtomicVariable and
CheckAtomicType. All atomic assignment functions now pass
!IsPointerAssignment(...) to control the check. The pointer-to-non-intrinsic
check lives in CheckAtomicType, guarded by the checkTypeOnPointer flag.
The check now applies uniformly to all atomic operations while properly
excluding pointer assignments (=>).

---------

Co-authored-by: Krish Gupta <krishgupta@Krishs-MacBook-Air.local>
2025-10-15 13:42:33 -05:00
Krzysztof Parzyszek
5fc7e43012
[flang][OpenMP] Fix build break with gcc 7.5.0 after e58de06414 (#157115)
Example: https://lab.llvm.org/buildbot/#/builders/140/builds/30179
2025-09-05 11:20:13 -05:00
Krzysztof Parzyszek
e58de06414
[flang][OpenMP] Reassociate logical ATOMIC update expressions (#156961)
This is a follow-up to PR153488 and PR155840, this time for expressions
of logical type. The handling of logical operations in Expr<T> differs
slightly from regular arithmetic operations. The difference is that the
specific operation (e.g. and, or, etc.) is not a part of the type, but
stored as a data member.
Both the matching code and the reconstruction code needed to be extended
to correctly handle the data member.

This fixes https://github.com/llvm/llvm-project/issues/144944
2025-09-05 08:41:01 -05:00
Krzysztof Parzyszek
21e1ab3340
[flang][OpenMP] Reassociate floating-point ATOMIC update expressions (#155840)
This is a follow-up to PR153488, this time the reassociation is enabled
for floating-point expressions, but only when associative-nath is
enabled in the language options. This can be done via -ffast-math on the
command line.
2025-08-28 11:18:57 -05:00
Michael Kruse
38853a0146 [flang][OpenMP] MSVC buildbot fix
PR #153488 caused the msvc build (https://lab.llvm.org/buildbot/#/builders/166/builds/1397) to fail:
```
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): error C2668: 'Fortran::evaluate::rewrite::Identity::operator ()': ambiguous call to overloaded function
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(43): note: could be 'Fortran::evaluate::Expr<Fortran::evaluate::SomeType> Fortran::evaluate::rewrite::Identity::operator ()<Fortran::evaluate::SomeType,S>(Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &&,const U &)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>,
            U=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\lib\Semantics\check-omp-atomic.cpp(174): note: or       'Fortran::evaluate::Expr<Fortran::evaluate::SomeType> Fortran::semantics::ReassocRewriter::operator ()<Fortran::evaluate::SomeType,S,void>(Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &&,const U &,Fortran::semantics::ReassocRewriter::NonIntegralTag)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>,
            U=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): note: while trying to match the argument list '(Fortran::evaluate::Expr<Fortran::evaluate::SomeType>, const S)'
        with
        [
            S=Fortran::evaluate::value::Integer<128,true,32,unsigned int,unsigned __int64,128>
        ]
..\llvm-project\flang\include\flang/Evaluate/rewrite.h(78): note: the template instantiation context (the oldest one first) is
..\llvm-project\flang\lib\Semantics\check-omp-atomic.cpp(814): note: see reference to function template instantiation 'U Fortran::evaluate::rewrite::Mutator<Fortran::semantics::ReassocRewriter>::operator ()<const Fortran::evaluate::Expr<Fortran::evaluate::SomeType>&,Fortran::evaluate::Expr<Fortran::evaluate::SomeType>>(T)' being compiled
        with
        [
            U=Fortran::evaluate::Expr<Fortran::evaluate::SomeType>,
            T=const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &
        ]
```

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

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

Since the overloads of ReassocRewriter cover all cases (integral and
non-integral), removing the using declaration to avoid the ambiguity.
2025-08-14 12:30:59 +02:00
Krzysztof Parzyszek
1e7772abcb
[flang][OpenMP] Reassociate ATOMIC update expressions (#153488)
An atomic update expression of form
  x = x + a + b
is technically illegal, since the right-hand side is parsed as (x+a)+b,
and the atomic variable x should be an argument to the top-level +. When
the type of x is integer, the result of (x+a)+b is guaranteed to be the
same as x+(a+b), so instead of reporting an error, the compiler can
treat (x+a)+b as x+(a+b).

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

Reinstate PR153098 one more time with fixes for the issues that came up:
- unused variable "lsrc",
- use of ‘outer1’ before deduction of ‘auto’.
2025-08-13 15:53:59 -05:00
Krzysztof Parzyszek
bb6f16ecd7 Revert "[flang][OpenMP] Reassociate ATOMIC update expressions (#153450)"
This reverts commit 7e125b9892f26d2028c3570f537e5a26f8c94447.

The fixes helped gcc-8, but not 7.5.
2025-08-13 12:17:13 -05:00
Krzysztof Parzyszek
7e125b9892
[flang][OpenMP] Reassociate ATOMIC update expressions (#153450)
An atomic update expression of form
  x = x + a + b
is technically illegal, since the right-hand side is parsed as (x+a)+b,
and the atomic variable x should be an argument to the top-level +. When
the type of x is integer, the result of (x+a)+b is guaranteed to be the
same as x+(a+b), so instead of reporting an error, the compiler can
treat (x+a)+b as x+(a+b).

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

Reinstate PR153098 with fixes for the issues that came up:
- unused variable "lsrc",
- use of ‘outer1’ before deduction of ‘auto’.
2025-08-13 12:00:05 -05:00
Krzysztof Parzyszek
6b5c38dbbb Revert "[flang][OpenMP] Reassociate ATOMIC update expressions (#153098)"
This reverts commit 4f6ae2af3563a7eefbe4179eabe10ef5898a5963.

This PR causes build breaks with older versions of GCC.
2025-08-13 10:27:53 -05:00
Krzysztof Parzyszek
ed2fa6fd89 Revert "[flang][OpenMP] Fix unused variable warning (#153409)"
This reverts commit 6b20b16b2f796b01bc5c3d5d8013a8e65b40080a.

This patch depends on PR153098 which is the one being reverted.
2025-08-13 10:27:53 -05:00
Krzysztof Parzyszek
6b20b16b2f
[flang][OpenMP] Fix unused variable warning (#153409) 2025-08-13 08:59:20 -05:00
Krzysztof Parzyszek
4f6ae2af35
[flang][OpenMP] Reassociate ATOMIC update expressions (#153098)
An atomic update expression of form
  x = x + a + b
is technically illegal, since the right-hand side is parsed as (x+a)+b,
and the atomic variable x should be an argument to the top-level +. When
the type of x is integer, the result of (x+a)+b is guaranteed to be the
same as x+(a+b), so instead of reporting an error, the compiler can
treat (x+a)+b as x+(a+b).

This PR implements this kind of reassociation for integral types, and
for the two arithmetic associative/commutative operators: + and *.
2025-08-13 08:07:11 -05:00
Krzysztof Parzyszek
4b7f3806f6
[flang][OpenMP] Move rewriting of min/max from Lower to Semantics (#153038)
There semantic analysis of the ATOMIC construct will require additional
rewriting (reassociation of certain expressions for user convenience),
and that will be driven by diagnoses made in the semantic checks.

While the rewriting of min/max is not required to be done in semantic
analysis, moving it there will make all rewriting for ATOMIC construct
be located in a single location.
2025-08-12 12:13:50 -05:00
Krzysztof Parzyszek
ea14ee4464
[flang][OpenMP] Refactor creating atomic analysis, NFC (#153036)
Turn it into a class that combines the information and generates the
analysis instead of having independent functions do it.
2025-08-11 12:20:51 -05:00
Krzysztof Parzyszek
e368b5343d
[flang][OpenMP] Make OpenMPCriticalConstruct follow block structure (#152007)
This allows not having the END CRITICAL directive in certain situations.
Update semantic checks and symbol resolution.
2025-08-07 08:10:25 -05:00
Connector Switch
8b7f81f2de
[NFC] Fix assignment typo. (#151864) 2025-08-03 22:32:00 +08:00
Krzysztof Parzyszek
6533ad04ed
[flang][OpenMP] Make all block constructs share the same structure (#150956)
The structure is
- OmpBeginDirective (aka OmpDirectiveSpecification)
- Block
- optional<OmpEndDirective> (aka optional<OmpDirectiveSpecification>)

The OmpBeginDirective and OmpEndDirective are effectively different
names for OmpDirectiveSpecification. They exist to allow the semantic
analyses to distinguish between the beginning and the ending of a block
construct without maintaining additional context.

The actual changes are in the parser: parse-tree.h and openmp-parser.cpp
in particular. The rest is simply changing the way the directive/clause
information is accessed (typically for the simpler).

All standalone and block constructs now use OmpDirectiveSpecification to
store the directive/clause information.
2025-08-01 07:52:59 -05:00
Andre Kuhlenschmidt
062b22e462
[flang][openacc] Add semantic checks for atomic constructs (#149579)
An error report of the following code generating non-atomic code led us
to realize there are missing checks in the OpenACC atomics code. Add
some of those checks for atomic and sketch how the rest of the code
should proceed in checking the rest of the properties. The following
cases are all reported as errors.
```fortran
! Originally reported error!
!$acc atomic capture
a = b
c = b
!$acc end atomic capture
! Other ambiguous, but related errors!
!$acc atomic capture
x = i
i = x
!$acc end atomic capture
!$acc atomic capture
a = b
b = b
!$acc end atomic capture
!$acc atomic capture
a = b
a = c
!$acc end atomic capture
```
2025-07-30 08:13:07 -07:00
Krzysztof Parzyszek
638943b27e
[flang][OpenMP] Convert AST node for ALLOCATORS to use Block as body (#148005)
The ALLOCATORS construct is one of the few constructs that require a
special form of the associated block.
Convert the AST node to use OmpDirectiveSpecification for the directive
and the optional end directive, and to use parser::Block as the body:
the form of the block is checked in the semantic checks (with a more
meaningful message).
2025-07-11 06:45:11 -05: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