448 Commits

Author SHA1 Message Date
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
Peter Klausler
867602742c
[flang] Minor speed-up to module file parsing (#152178)
Module files shouldn't ever produce parsing errors, and if they did in
the case of a badly-generated module file, the compiler will notice and
crash. So we can run the parser on module files with message deferral
enabled, and that saves time that would otherwise be spent generating
messages on failed parsing alternatives that are discarded anyway when
backtracking. It's not a big savings (single digit percentage on overall
compilation time for a big application with lots of modules), but worth
doing.
2025-08-05 13:40:30 -07:00
Krzysztof Parzyszek
ca513ee1ec
[flang][OpenMP] Remove unused class OmpMemoryOrderClause, NFC (#151759) 2025-08-04 07:34:00 -05: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
Akash Banerjee
9fdd1d3d46
[Flang] Add parser support for AUTOMAP modifier (#151511)
Add parser support for the new AUTOMAP modifier for OpenMP Declare
Target Enter clause introduced in OpenMP 6.0 section 7.9.7.
2025-07-31 15:56:16 +01:00
Krzysztof Parzyszek
6984922905
[flang][OpenMP] Store directive information in OpenMPSectionConstruct (#150804)
The OpenMPSectionConstruct corresponds to the `!$omp section` directive,
but there is nothing in the AST node that stores the directive
information. Even though the only possibility (at the moment) is
"section" without any clauses, for improved generality it is helpful to
have that information anyway.
2025-07-31 07:51:22 -05:00
Krzysztof Parzyszek
6ffcfc5a8a
[flang][OpenMP] Make OmpDirectiveNameModifier a distrinct type (#150768)
It was an alias for OmpDirectiveName, which could cause confusion in
parse-tree visitors: a visitor for OmpDirectiveNameModifier could be
executed for an OmpDirectiveName node, leading to unexpected results.
2025-07-31 07:23:48 -05:00
Krzysztof Parzyszek
6b92a3bc21
[flang][OpenMP] Detect BLOCK construct through lookahead (#150617)
Avoid parsing the entire ExecutionPartConstruct in either the strictly-
or the loosely-structured block parser only to discard it when it's not
BLOCK (or is BLOCK) respectively. Doing so was not incorrct, but in
pathological cases (like Fujitsu 0981_0034) the recursive parsing can
take a very long time.

Instead, detect the presence of BLOCK first (via a simple lookahead),
and fail immediately if necessary.
2025-07-25 08:42:23 -05:00
Krzysztof Parzyszek
8f8b436c2b [flang][OpenMP] Silence warning in openmp-parsers.cpp
flang/lib/Parser/openmp-parsers.cpp:1655:43: warning
: logical not is only applied to the left hand side of comparison [-Wlogical-not
-parentheses]
 1655 | TYPE_PARSER(!StandaloneDirectiveLookahead >=
      |                                           ^~
2025-07-24 10:04:11 -05:00
Krzysztof Parzyszek
1ba3859cdb
[flang][OpenMP] Parse strictly- and loosely-structured blocks (#150298)
Block-associated constructs have, as their body, either a strictly- or a
loosely-structured block. In the former case the end-directive is
optional.

The existing parser required the end-directive to be present in all
cases.

Note:
The definitions of these blocks in the OpenMP spec exclude cases where
the block contains more than one construct, and the first one is
BLOCK/ENDBLOCK. For example, the following is invalid:
```
  !$omp target
  block         ! This cannot be a strictly-structured block, but
    continue    ! a loosely-structured block cannot start with
  endblock      ! BLOCK/ENDBLOCK
  continue      !
  !$omp end target
```
2025-07-24 08:59:13 -05:00
Krzysztof Parzyszek
2aa1e54fa1
[flang][OpenMP] Parse OpenMP 6.0 map modifiers (#149134)
OpenMP 6.0 has changed the modifiers on the MAP clause:
- map-type-modifier has been split into individual modifiers,
- map-type "delete" has become a modifier,
- new modifiers have been added.

This patch adds parsing support for all of the OpenMP 6.0 modifiers. The
old "map-type-modifier" is retained, but is no longer created in
parsing. It will remain to take advantage of the preexisting modifier
validation for older versions: when the OpenMP version is < 6.0, the
modifiers will be rewritten back as map-type-modifiers (or map- type in
case of "delete").

In this patch the modifiers will always be rewritten in the older format
to isolate these changes to parsing as much as possible.
2025-07-21 10:55:37 -05:00
Andre Kuhlenschmidt
b3c72a97c5
[flang][driver] -Werror promotes warnings to error and interopts with -Wfatal-errors (#148748)
This PR changes how `-Werror` promotes warnings to errors so that it
interoperates with `-Wfatal-error`. It maintains the property that
warnings and other messages promoted to errors are displayed as there
original message.
2025-07-16 12:26:38 -07:00
Tom Eccles
c2e41be50f
[flang][Parser] Add whitespace token after the sentinel in fixed form (#148825)
Fixes #148386

The first time the line was classified (using
`Prescanner::ClassifyLine(const char *)`) the line was correctly
classified as a compiler directive. But then later on the token form is
invoked (`Prescanner::ClassifyLine(TokenSequence, Provenance)`). This
one incorrectly classified the line as a comment because there was no
whitespace token right after the sentinel. This fixes the issue by
ensuring this whitespace is added.
2025-07-15 16:39:34 +01:00
Krzysztof Parzyszek
51b6f64b89
[flang][OpenMP] Avoid unnecessary parsing of OpenMP constructs (#148629)
When parsing a specification part, the parser will look ahead to see if
the next construct is an executable construct. In doing so it will
invoke OpenMPConstruct parser, whereas the only necessary thing to check
would be the directive alone.
2025-07-15 09:05:56 -05: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
Krzysztof Parzyszek
61a9d2c22d
[flang][OpenMP] Use OmpDirectiveSpecification in DISPATCH (#148008)
Dispatch is the last construct (after ATOMIC and ALLOCATORS) where the
associated block requires a specific form.
Using OmpDirectiveSpecification for the begin and the optional end
directives will make the structure of all block directives more uniform.
2025-07-11 07:28:54 -05: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
9b0ae6ccd6
[flang][OpenMP] Issue a warning when parsing future directive spelling (#147765)
OpenMP 6.0 introduced alternative spelling for some directives, with the
previous spellings still allowed.

Warn the user when a new spelling is encountered with OpenMP version set
to an older value.
2025-07-10 09:57:03 -05:00
Krzysztof Parzyszek
2546c6d3f7
[flang][OpenMP] Recognize remaining OpenMP 6.0 spellings in parser (#147723)
Parse OpenMP 6.0 spellings for directives that don't use
OmpDirectiveNameParser.
2025-07-09 16:02:24 -05:00
Krzysztof Parzyszek
d2adfcaa9e
[flang][OpenMP] Handle multiple spellings in OmpDirectiveNameParser (#147722)
Collect all spellings from all supported OpenMP versions before parsing.
Break up the list of spellings by the initial letter to speed up parsing
a little.
2025-07-09 16:02:01 -05:00
Andre Kuhlenschmidt
fc9dd58734
[flang][driver] add -Wfatal-errors (#147614)
Adds the flag `-Wfatal-errors` which truncates the error messages at 1 error.
2025-07-09 12:35:43 -07:00
Peter Klausler
dd3214d5a6
[flang] Fix handling of identifier in column 1 of free form continuat… (#146430)
…ion line

An obsolete flag ("insertASpace_") is being used to signal some cases in
the prescanner's implementation of continuation lines when a token
should be broken when it straddles a line break. It turns out that it's
sufficient to simply note these cases without ever actually inserting a
space, so don't do that (fixing the motivating bug). This leaves some
variables with obsolete names, so change them as well.

This patch handles the third of the three bugs reported in
https://github.com/llvm/llvm-project/issues/146362 .
2025-07-03 14:32:38 -07:00
Andre Kuhlenschmidt
bc89380179
[flang][preprocessor] fix use of bitwise-and for logical-and (#146758)
The preprocessor used bitwise and to implement logical, this is a bug.

towards #146362
2025-07-03 12:37:54 -07:00
Andre Kuhlenschmidt
67d6679c91
[flang][prescanner] fix invalid check (#146613)
`TokenSequence::pop_back()` had a check assumed that tokens are never
empty. Loosen this check since isn't true.

towards #146362
2025-07-03 12:36:34 -07:00
Akash Banerjee
ab42c4abe8 [NFC] Replace "omp.default.mapper" with llvm::omp::OmpDefaultMapperName. 2025-07-02 16:29:52 +01: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
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
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
Jack Styles
cb355def95
[Flang][OpenMP] Add Parsing support for Indirect Clause (#143505)
As part of OpenMP Version 5.1, support for the `indirect` clause was
added for the `declare target` directive. This clause should follow an
`enter` clause, and allows procedure calls to be done indirectly through
OpenMP.

This adds Parsing support for the clause, along with semantics checks.
Currently, lowering for the clause is not supported so a TODO message
will be outputted to the user. It also performs version checking as
`indirect` is only support in OpenMP 5.1 or greater.

See also: #110008
2025-06-17 09:05:36 +01: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
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
Tom Eccles
ce603a0f16
[flang][openmp]Add UserReductionDetails and use in DECLARE REDUCTION (#140066)
This adds another puzzle piece for the support of OpenMP DECLARE
REDUCTION functionality.

This adds support for operators with derived types, as well as declaring
multiple different types with the same name or operator.

A new detail class for UserReductionDetials is introduced to hold the
list of types supported for a given reduction declaration.

Tests for parsing and symbol generation added.

Declare reduction is still not supported to lowering, it will generate a
"Not yet implemented" fatal error.

Fixes #141306
Fixes #97241
Fixes #92832
Fixes #66453

---------

Co-authored-by: Mats Petersson <mats.petersson@arm.com>
2025-06-09 11:17:03 +01: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
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
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
Peter Klausler
c26e7520a9
[flang] Support -D for function-like macros (#139812)
Handle a command-line function-like macro definition like
"-Dfoo(a)=...".

TODO: error reporting for badly formed argument lists.
2025-05-15 11:26:03 -07:00
Krzysztof Parzyszek
e06363f80f
[flang][OpenMP] Verify uses of OmpCancellationConstructTypeClause (#139743)
Some directive names can be used as clauses, for example in "cancel". In
case where a directive name is misplaced, it could be interpreted as a
clause.

Verify that such uses are valid, and emit a diagnostic message if not.

Fixes https://github.com/llvm/llvm-project/issues/138224
2025-05-14 07:07:14 -05:00
Peter Klausler
58535e81dd
[flang] Further refinement of OpenMP !$ lines in -E mode (#138956)
Address failing Fujitsu test suite cases that were broken by the patch
to defer the handling of !$ lines in -fopenmp vs. normal compilation to
actual compilation rather than processing them immediately in -E mode.

Tested on the samples in the bug report as well as all of the Fujitsu
tests that I could find that use !$ lines.

Fixes https://github.com/llvm/llvm-project/issues/136845.
2025-05-12 12:16:05 -07:00
agozillon
f687ed9ff7
[Flang][OpenMP] Initial defaultmap implementation (#135226)
This aims to implement most of the initial arguments for defaultmap
aside from firstprivate and none, and some of the more recent OpenMP 6
additions which will come in subsequent updates (with the OpenMP 6
variants needing parsing/semantic support first).
2025-05-12 16:30:43 +02:00
Krzysztof Parzyszek
a68f35a17d [flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)
The OpenMP version is stored in LangOptions in SemanticsContext. Use the
fallback version where SemanticsContext is unavailable (mostly in case
of debug dumps).

RFC:
https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507

Reland with a fix for build break in f18-parse-demo.
2025-05-09 08:13:52 -05:00
Krzysztof Parzyszek
89822ff5a8 Revert "[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)"
This reverts commit 41aa67488c3ca33334ec79fb5216145c3644277c.

Breaks build: https://lab.llvm.org/buildbot/#/builders/140/builds/22826
2025-05-09 07:56:10 -05:00
Krzysztof Parzyszek
41aa67488c
[flang][OpenMP] Pass OpenMP version to getOpenMPDirectiveName (#139131)
The OpenMP version is stored in LangOptions in SemanticsContext. Use the
fallback version where SemanticsContext is unavailable (mostly in case
of debug dumps).

RFC:
https://discourse.llvm.org/t/rfc-alternative-spellings-of-openmp-directives/85507
2025-05-09 07:42:15 -05:00
Kiran Chandramohan
a13c0b6770
[Flang][OpenMP] Add frontend support for declare variant (#130578)
Support is added for parsing. Basic semantics support is added to
forward the code to Lowering. Lowering will emit a TODO error. Detailed
semantics checks and lowering is further work.
2025-05-07 09:56:45 +01:00
Eugene Epshteyn
36541ec3ca
[flang] Fix #else with trailing text (#138045)
Fixed the issue, where the extra text on #else line (' Z' in the example
below) caused the data from the "else" clause to be processed together
with the data of "then" clause.
```
#ifndef XYZ42 
      PARAMETER(A=2)
#else Z
      PARAMETER(A=3)
#endif
      end
```
2025-05-01 23:07:52 -04:00
Krzysztof Parzyszek
760bba4666
[flang][OpenMP] Allow UPDATE clause to not have any arguments (#137521)
The UPDATE clause can be specified on both ATOMIC and DEPOBJ directives.
Currently, the ATOMIC directive has its own handling of it, and the
definition of the UPDATE clause only supports its use in the DEPOBJ
directive, where it takes a dependence-type as an argument.

The UPDATE clause on the ATOMIC directive may not have any arguments.
Since the implementation of the ATOMIC construct will be modified to use
the standard handling of clauses, the definition of UPDATE should
reflect that.
2025-05-01 13:30:45 -05:00
Kajetan Puchalski
d68c732473
[flang] Use precompiled headers in Frontend, Lower, Parser, Semantics and Evaluate (#131137)
Precompiling larger headers can save a lot of compile time across
various compilation units.

For the time being, disable precompiled headers for ccache builds on Windows
due to issues with reliably passing the appropriate options to ccache.

Selected compile time & memory improvements are as follows:

flang/lib/Parser/Fortran-parsers.cpp:
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:47.31 -> 0:41.68
Maximum resident set size (kbytes): 2062140 -> 1745584

flang/lib/Lower/Bridge.cpp:
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:19.16 -> 0:45.86
Maximum resident set size (kbytes): 3849144 -> 2443476

flang/lib/Lower/PFTBuilder.cpp
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:29.24 -> 1:00.99
Maximum resident set size (kbytes): 4218368 -> 2923128

flang/lib/Lower/Allocatable.cpp
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:53.03 -> 0:22.50
Maximum resident set size (kbytes): 3092840 -> 2116908

flang/lib/Semantics/Semantics.cpp
Elapsed (wall clock) time (h:mm:ss or m:ss): 1:18.75 -> 1:00.20
Maximum resident set size (kbytes): 3527744 -> 2545308

While the newly added precompiled headers are as follows:

Parser:
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:09.62
Maximum resident set size (kbytes): 1034608

Lower:
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:41.33
Maximum resident set size (kbytes): 3615240

Semantics:
Elapsed (wall clock) time (h:mm:ss or m:ss): 0:26.69
Maximum resident set size (kbytes): 2403776

---------

Signed-off-by: Kajetan Puchalski <kajetan.puchalski@arm.com>
2025-04-30 11:46:57 +01:00
Krzysztof Parzyszek
386ff113f9
[flang][OpenMP] Use OmpMemoryOrderType enumeration in FAIL clause (#136313)
Make the FAIL clause contain OmpMemoryOrderType enumeration instead of
OmpClause. This simplifies the semantic checks of the FAIL clause.
2025-04-23 07:40:30 -05:00
Krzysztof Parzyszek
05b7e97c78
[flang][OpenMP] Extend common::AtomicDefaultMemOrderType enumeration (#136312)
Add "Acquire" and "Release", and rename it to OmpMemoryOrderType, since
memory order type is a concept extending beyond the
ATOMIC_DEFAULT_MEM_ORDER clause.

When processing a REQUIRES directive (in rewrite-directives.cpp), do not
add Acquire or Release to ATOMIC constructs, because handling of those
types depends on the OpenMP version, which is not available in that
file. This issue will be addressed later.
2025-04-23 05:57:52 -05:00
Krzysztof Parzyszek
b5eae19f64
[flang][OpenMP] Introduce OmpHintClause, simplify OmpAtomicClause (#136311)
The OmpAtomicClause is a variant of a few specific clauses that are used
on the ATOMIC construct. The HINT clause, however, was represented as a
generic OmpClause, which somewhat complicated the analysis of an
OmpAtomicClause.

Introduce OmpHintClause to represent the contents of the HINT clause,
and use it on OmpAtomicClause similarly to how OmpFailClause is used.
2025-04-22 14:05:32 -05:00