491 Commits

Author SHA1 Message Date
khaki3
13cd7a29e8
[flang][OpenACC] Generalize cross-region GOTO exit handling for all ACC region ops (#187613)
When a `GoTo` inside an ACC region (`acc.loop`, `acc.data`,
`acc.parallel`, etc.) targets a label outside that region, the lowering
generated an illegal cross-region `cf.br`. This caused MLIR verification
failures or stack overflows in `runRegionDCE`'s recursive
`propagateLiveness`.

This patch addresses the issue with a generalized approach:

- Add `genOpenACCRegionExitBranch` helper that detects cross-region
branches from any ACC region op and generates the appropriate terminator
(`acc.yield` for compute/loop ops, `acc.terminator` for data ops). The
helper verifies that `parentOp` is an ACC operation, so it does not
interfere with branches inside `scf.execute_region` or other non-ACC
regions.
- In `genBranch`, when a cross-region exit from an ACC region is
detected, store a unique exit ID into a selector variable and generate
the region terminator. After the ACC op, a jump table dispatches to the
correct target based on the selector. This correctly handles GOTOs that
skip intermediate code between the loop end and the target label.
- Emit a TODO diagnostic for GOTOs that cross multiple nested ACC region
boundaries.
- Fix `acc.data` creation when the construct has no data clauses but
contains unstructured control flow: skip the early return in
`genACCDataOp` so the `acc.data` region is created and blocks are
properly managed.
2026-03-30 06:35:08 -07:00
jeanPerier
656604093d
[flang] get rid of descriptor in scalar type is (#188762)
Select type lowering was keeping scalar selector as descriptors inside
TYPE IS for derived type, leading to a declare using a fir.box.
This is not the canonical representation for such variables that can be
tracked with a simple pointer. This code that is remapping variables
that appear in data operation in lowering was not expecting a
fir.declare to be emitted with fir.box for such entity (an assert was
hit in the added OpenACC test).

Align the lowering of derived type scalar selector with the handling of
intrinsic selector. While doing this, simplify the logic by using and
adding fir::BaseBoxAddr helpers to ensure that attributes such as
VOLATILE are correctly propagated (they matter more than keeping the
fir.ptr/fir.heap type that is not relevant for the selector that does
not have the POINTER/ALLOCATABLE attributes).
2026-03-30 09:43:41 +02:00
Valentin Clement (バレンタイン クレメン)
6bd99efec1
[flang][cuda] Add hasManagedOrUnifedSymbols attribute to cuf.data_transfer op (#185106)
Add an attribute to signal the presence of managed or unified symbols in
the data transfer. In some case, the presence of such symbols require to
insert synchronization. Adding the attribute in the op during lowering
facilitate the recognition of such data transfer.
2026-03-06 13:55:03 -08:00
Slava Zakharin
5230955af7
[flang,acc] Support -ffp-maxmin-behavior option in lowering. (#184730)
This patch adds `flang -fc1` option `-ffp-maxmin-behavior` and
propagates it throughout Flang, so that semantics context,
lowering and the pass pipeline builder can use it.

MAX/MIN intrinsic and OpenACC max/min reduction lowering
are now controlled by the option.

I kept the `Legacy` mode, which is the default and matches the current
behavior. I am going to test and merge a follow-up patch that
replaces `Legacy` with `Portable`.

RFC:
https://discourse.llvm.org/t/flang-canonical-and-optimizable-representation-for-min-max/90037
2026-03-06 12:21:58 -08:00
Valentin Clement (バレンタイン クレメン)
a224ba0689
[flang][cuda] Support data transfer with parenthesis around rhs (#183201) 2026-02-25 10:44:31 -08:00
jeanPerier
7c7a4f1cf5
[flang] fix do while lowering to SCF after unstructured context (#181996)
`maybeStartBlock(preheaderBlock);` should not be called when lowering a
DO WHILE in a structured fashion, it is meant to insert branches in
unstructured contexts lowering.

This had the side effect of inserting a back edge to the block
containing the scf for DO WHILE after a construct lowered in an
unstructured fashion because blocks may be unterminated after
unstructured lowering, and `maybeStartBlock` created a `cf.br` to its
argument when the current block is unterminated (and here, the argument
and current block where the same, creating an infinite loop).
2026-02-18 18:09:48 +01:00
jeanPerier
63826ea33e
[flang] still apply vectorization cost model with IVDEP (#180760)
The current implementation of `!DIR IVDEP` leads flang to bypass LLVM
cost model and always vectorize the loop carrying `!DIR$ IVDEP`.

IVDEP is an extension and its documentation varies, and while it usually
leads to vectorization because it is added on loops where it is usually
profitable, its documentation only tells it is meant to tell the
compiler that there are no loop carried dependencies and that the loop
is safe to vectorize.

In some application, such directive may have been added to help the
compiler proving it is safe to vectorize, but vectorizing is not always
the best choice for all architectures. The cost model should still be
applied. This is at least the case for classic flang.

When users want vectorization to happen, they should use `!DIR$ VECTOR
ALWAYS`.

This patch updates flang to not emit `llvm.loop.vectorize.enable` just
because IVDEP was seen. Instead, IVDEP now only controls the emissions
of the access groups to translate the independence of the accesses and
leave the vectorization decision up to the cost model. `!DIR$ VECTOR
ALWAYS` can be used in combination with IVDEP to force vectorization (it
causes the emission of `llvm.loop.vectorize.enable`).
2026-02-12 19:04:02 +01:00
Susan Tan (ス-ザン タン)
28d621e752
[flang] Lowering fortran structured do-while loops to scf.while (#177476)
WIP. Implemented structured do-while loops (i.e., the only control-flow
edge that can leave the outer loop is the one taken when the DO WHILE
condition becomes false) lowering to `scf.while`.
2026-01-27 10:15:55 -05:00
jeanPerier
4848313824
[flang] propagate IVDEP in array expressions optimization (#178171)
Follow-up on https://github.com/llvm/llvm-project/pull/177940.
This propagates the access attribute in cases where hlfir.assign is
being transformed in array expression optimizations.
It also adds handling for the cases where there are WHERE/FORALL or user
defined assignments inside the loop and that an hlfir.region_assign is
first being generated.
2026-01-27 15:19:14 +01:00
jeanPerier
c2d510f5bd
[flang] fix DIR IVDEP for array assignments inside loops (#177940)
The access attribute set on hlfir.assign for arrays was lost in
InlineHLFIRAssign.cpp. This patch propagates it to the creates loads and
stores.
2026-01-27 10:18:41 +01:00
Tom Eccles
9be7c1037f
[flang][Lower] Fix UB in location handling (#177944)
Previously `prov` received the address of a variable allocated in stack
memory (the contents of `include`). `prov` would then access that memory
outside of the lifetime of that stack allocation: leading to UB.

This only manifested on thinLTO builds. No added test because
flang/test/Lower/location.f90 covers it (when thinLTO is enabled) and
there are bots guarding the thin-lto configuration.

Fixes #156629
Fixes #176404
2026-01-26 14:06:46 +00:00
Andre Kuhlenschmidt
1036d782ae
[flang][lowering] lowering assigned goto of allocatable variable (#175874)
towards [#173594](https://github.com/llvm/llvm-project/issues/173594)

This PR adds a lowering for `AssignedGoto`s that reference an
allocatable variable.
2026-01-22 16:58:10 +00:00
Krzysztof Parzyszek
d542fac6b1
[flang] Add traits to more AST nodes (#175578)
Follow-up to PR175211.

There are still a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.

The members of these nodes were frequently used, so instead of
extracting them by hand each time use helper member functions to access
them.
2026-01-20 09:57:35 -06:00
Krzysztof Parzyszek
b8fec8ebc6
[flang] Add traits to more AST nodes (#175566)
Follow-up to PR175211.

There are still a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.
2026-01-20 08:00:00 -06:00
Kelvin Li
b86c84ce63
[flang] Handle unused variable (NFC) (#176274) 2026-01-16 10:12:02 -05:00
Jean-Didier PAILLEUX
1d4f9ac37c
[flang] Fix crash with coarray teams #171048 (#172259)
This PR updates the `CHANGE TEAM` construct to fix the bug mentioned in
the issue #171048.
When a construct such as `IfConstruct` was present in the `CHANGE TEAM`
region, several BB were created but outside the region.
2026-01-15 18:02:07 +01:00
khaki3
ccbe36f16d
[flang][acc] Implement cache directive lowering (#174897)
The `acc.cache` operation is currently defined to be associated with a
loop. However, this implementation generates `acc.cache` as a standalone
data entry operation inside the loop body. The `acc.cache` operation
definition should be updated in a future change to reflect this usage.

Key implementation details:
- Add semantic checks to validate cache-specific constraints: at least
  one bound must be specified for array sections, and only unit stride
  is supported
- Use the shared `gatherDataOperandAddrAndBounds` infrastructure to
  generate `acc.bounds` for cache operands, handling single elements
  (`arr(i)`), full ranges (`arr(l:u)`), and partial ranges with missing
  bounds (`arr(l:)` or `arr(:u)`)
- Set the data clause to `acc_cache` with the `readonly` modifier via
  the `modifiers` attribute when the `readonly` clause is present
- Update the symbol map so subsequent lowering uses the cache result
- Insert cache operations after loop iterator setup
- Add symbol scope management for constructs inside `acc.loop`
2026-01-12 09:45:47 -08:00
Krzysztof Parzyszek
568b8e4873
[flang] Add traits to several AST nodes (#175065)
There are quite a few AST nodes that don't have any of the standard
traits (Wrapper/Tuple/etc). Because of that they require special
handling in the parse tree visitor.

Convert a subset of these nodes to the typical format, and remove the
special cases from the parse tree visitor.
2026-01-08 18:26:48 -06:00
Abid Qadeer
316a9c52f0
[flang] Ignore ambiguous use statement in use_stmt generation. (#174387)
The https://github.com/llvm/llvm-project/pull/168106 caused build
failures in testcases which have ambiguous use statements. This PR fixes
that by properly ignoring them in `emitUseStatementsFromFunit`.
2026-01-05 14:46:49 +00:00
Thirumalai Shaktivel
212527c00b
[Flang] Add FIR and LLVM lowering support for prefetch directive (#167272)
Implementation details:
* Add PrefetchOp in FirOps
* Handle PrefetchOp in FIR Lowering and also pass required default
values
* Handle PrefetchOp in CodeGen.cpp
* Add required tests
2026-01-05 13:24:10 +05:30
Victor Chernyakin
c438773432
[LLVM][ADT] Migrate users of make_scope_exit to CTAD (#174030)
This is a followup to #173131, which introduced the CTAD functionality.
2026-01-02 20:42:56 -08:00
Abid Qadeer
fc9e6e13fd
[flang] Represent use statement in fir. (#168106)
We have a longstanding issue in debug info that use statement is not
fully respected. The problem has been described in
https://github.com/llvm/llvm-project/issues/160923. This is first part
of the effort to address this issue. This PR adds infrastructure to emit
`use` statement information in FIR, which will be used by subsequent
patches to generate DWARF debug information.

The information about use statement is collected during semantic
analysis and stored in `PreservedUseStmt` objects. During lowering,
`fir.use_stmt` operations are emitted for each `PreservedUseStmt`
object. The `fir.use_stmt` operation captures the module name, `only`
list symbols, and any renames specified in the use statement. The
`fir.use_stmt` is removed during `CodeGen`.
2026-01-02 12:10:18 +00:00
Tom Eccles
5ccf8c90d1
[flang] implement VECTOR VECTORLENGTH directive (#170114)
This should match exactly the llvm attributes generated by classic
flang.
2025-12-03 09:36:22 +00:00
jeanPerier
a09571ed5b
[flang] represent ABSTRACT in fir.type_info (#170109)
This patch keeps information about ABSTRACT derived types and DEFERRED
type bound procedures inside fir.type_info dispatch tables.

This is part of the effort to delay generation of runtime type info
global by keeping the type information in a more condense fashion inside
fir.type_info (which is also easier to use for any potential
optimizations).
2025-12-02 10:13:23 +01:00
Eugene Epshteyn
8be4641024
[flang] Use hlfir.cmpchar for SELECT CASE of charsSelect case hlfir cmpchar (#168476)
For SELECT CASE with character selector, instead of allways calling
runtime comparison function, emit hlfir.cmpchar. This has different
behaviors at different optimization levels: at -O0, it still emits
flang-rt call, but at higher optimization levels it does inline
comparison. Modify test/Lower/select-case-statement.f90 to test both
comparison cases.
2025-11-21 12:03:51 -05:00
jeanPerier
364fe55c42
[flang] simplify pointer assignments (#168732)
Pointer assignment lowering was done in different ways depending on
contexts and types, sometimes still using runtime calls when this is not
needed and the complexity of doing this inline is very limited (the
pointer and target descriptors were already prepared inline, the runtime
is just doing the descriptor assignment and ensuring the pointer
descriptor keep its pointer flag).

Slightly extent the inline version that was used for Forall and use it
for all cases.
When lowering without HLFIR is removed, this will allow removing more
code.
2025-11-20 15:37:53 +01:00
Jean-Didier PAILLEUX
3b83e7fa4e
[flang] Implement !DIR$ IVDEP directive (#133728)
This directive tells the compiler to ignore vector dependencies in the
following loop and it must be placed before a `do loop`.

Sometimes the compiler may not have sufficient information to decide
whether a particular loop is vectorizable due to potential dependencies
between iterations and the directive is here to tell to the compiler
that vectorization is safe with `parallelAccesses` metadata.

This directive is also equivalent to `#pragma clang loop assume(safety)`
in C++
2025-11-14 14:06:46 +01:00
Eugene Epshteyn
056f744789
[flang] Removed old option -fdebug-dump-pre-fir (#168008)
This option has long been replaced by `-fc1 -fdebug-dump-pft`. Removed
the old option and updated one test that still used it.
2025-11-14 07:05:08 -05:00
Akash Banerjee
833ffa54f2
[Flang][OpenMP] Update declare mapper lookup via use-module (#167903) 2025-11-13 19:21:34 +00:00
Akash Banerjee
e1324a9377
Revert "[Flang][OpenMP] Update declare mapper lookup via use-module" (#167896)
Reverts llvm/llvm-project#163860
2025-11-13 16:05:33 +00:00
Akash Banerjee
bb5f3a08b6
[Flang][OpenMP] Update declare mapper lookup via use-module (#163860)
- Implemented semantic TODO to catch undeclared mappers.
- Fix mapper lookup to include modules imported through USE.
- Update and add tests.

Fixes #163385.
2025-11-13 15:07:46 +00:00
Abid Qadeer
cfc56c982f
[flang][debug] Track dummy argument positions explicitly. (#167489)
CHARACTER dummy arguments were treated as local variables in debug info.
This happened because our method to get the argument number was not
robust. It relied on `DeclareOp` having a direct reference to arguments
which was not the case for character arguments. This is fixed by storing
source-level argument positions in `DeclareOp`.

Fixes #112886
2025-11-12 10:21:32 +00:00
Jean-Didier PAILLEUX
d02a5ae10b
[flang] Adding lowering of TEAMs features to PRIF in MIF Dialect (#165573)
Support for multi-image features has begun to be integrated into LLVM
with the MIF dialect.
In this PR, you will find lowering and operations related to the TEAM
features (`SYNC TEAM`, `GET_TEAM`, `FORM TEAM`, `CHANGE TEAM`,
`TEAM_NUMBER`).

Note regarding the operation for `CHANGE TEAM` : This operation is
partial because it does not support the associated list of coarrays
because the allocation of a coarray and the lowering of PRIF's
`prif_alias_{create|destroy}` procedures are not yet supported in Flang.
This will be integrated later.

Any feedback is welcome.
2025-11-12 09:40:04 +01:00
Thirumalai Shaktivel
cf1f871023
[Flang] Add parser support for prefetch directive (#139702)
Implementation details:
* Recognize prefetch directive in the parser as `!dir$ prefetch ...`
* Unparse the prefetch directive
* Add required tests

Details on the prefetch directive:
`!dir$ prefetch designator[, designator]...`, where the designator list
can be a variable or an array reference. This directive is used to
insert a hint to the code generator to prefetch instructions for
memory references.
2025-11-10 09:44:22 +05:30
Daniel Chen
63e45ef1bf
To fix polymorphic pointer assignment in FORALL when LHS is unlimited polymorphic and RHS is intrinsic type target (#164999)
Fixes #143569.
2025-10-31 09:49:50 -04:00
Jean-Didier PAILLEUX
c1779f33bd
[flang] Implement !DIR$ [NO]INLINE and FORCEINLINE directives (#134350)
This patch adds the support of these two directives : `!dir$ inline` and
`!dir$ noinline`.
- `!dir$ noinline` tells to the compiler to not perform inlining on
specific function calls by adding the `noinline` metadata on the call.
- `!dir$ inline` tells to the compiler to attempt inlining on specific
function calls by adding the `inlinehint` metadata on the call.
- `!dir$ forceinline` tells to the compiler to always perfom inlining on
specific function calls by adding the `alwaysinline` metadata on the
call.

Currently, these directives can be placed before a `DO LOOP`, call
functions or assignments. Maybe other statements can be added in the
future if needed.

For the `inline` directive the correct name might be `forceinline` but
I'm not sure ?
2025-10-28 08:02:15 +01:00
Jakub Kuderski
23ead47655
[flang][mlir] Migrate to free create functions. NFC. (#164657)
See
https://discourse.llvm.org/t/psa-opty-create-now-with-100-more-tab-complete/87339.

I plan to mark these as deprecated in
https://github.com/llvm/llvm-project/pull/164649.
2025-10-22 12:47:48 -04:00
Daniel Chen
2dbe959266
Get the BoxType from the RHS instead of LHS for polymorphic pointer assignment inside FORALL. (#164279)
Fixes #153220
2025-10-22 10:24:39 -04:00
jeanPerier
c9fb37c75f
[flang][FIR] add fir.assumed_size_extent to abstract assumed-size extent encoding (#164452)
The purpose of this patch is to allow converting FIR array representation to
memref when possible without hitting memref verifier issue.

The issue was that FIR arrays may be assumed size, in which case the
last dimension will not be known at runtime. Flang uses -1 to encode
this to fulfill Fortran 2023 standard requirements in 18.5.3 point 5
about CFI_desc_t.

When arrays are converted to memeref, if this `-1` reaches memeref
operations, it triggers verifier errors (even if the conversion happened
in code that guards the code to be entered at runtime if the array is
assumed-size because folders/verifiers do not take into account
reachability).

This follows-up on discussions in #163505 merge requests
2025-10-22 11:46:18 +02:00
Peter Klausler
5cd9f0f655
[flang] Move parse tree tool to Parser/tools.h (#163998)
Move the parse tree utility function
semantics::getDesignatorNameIfDataRef to Parser/tools.h and rename it to
comply with the local style.
2025-10-20 13:20:33 -07:00
Kazu Hirata
7d25ba39c8
[flang] Replace LLVM_ATTRIBUTE_UNUSED with [[maybe_unused]] (NFC) (#163916)
This patch replaces LLVM_ATTRIBUTE_UNUSED with [[maybe_unused]],
introduced as part of C++17.
2025-10-17 07:27:28 -07:00
Valentin Clement (バレンタイン クレメン)
e55071b157
[flang][cuda] Extent detection of data transfer with conversion (#163852) 2025-10-16 20:33:50 +00:00
jeanPerier
de55329b6e
[flang][NFC] update createTempFromMold interface to return a bool (#162680)
Some createTempFromMold users are looking for a compile time constant
for the `mustFree`. Instead of having them retrieving it, update the
interface to return a bool. The only users that needs a value was
`packageBufferizedExpr` and it has an overload that accept bool too.

Tests are updated to reflect that this changes the place where the
boolean is created in BufferizeHLFIR, and just removes its creation in
contexts it is not needed.
2025-10-10 11:19:54 +02:00
Susan Tan (ス-ザン タン)
5873d6a371
[flang][openacc] Add support for force clause for loop collapse (#162534)
Currently the force clause `collapse (force:num_level)` is NYI. Added
support to sink any prologue and epilogue code to the inner most level
as specified.
2025-10-09 12:50:24 -04:00
jeanPerier
121026b186
[flang][openacc] map data operand results to symbols inside compute region (#162306)
Variable references inside OpenACC compute and loop region were
currently always lowered to usages of the same SSA values than in the
host thread, even for variables that appear in data clauses and for
which acc data operations are created.

This makes it a non-trivial task to identify implicit data usages vs
usage of data appearing in clauses because the SSA addresses used in the
region may have a non-trivial SSA relationship with the SSA addresses
used as inputs of the data operations, especially after CSE runs that
may merge component or array element addressing operations with similar
addressing on the host thread (fir.coordinate/hlfir.designate).

This patch updates OpenACC lowering to remap the Symbol that appear in
data clauses to the related acc data operation result for the scope of
the compute or loop region.

To allow FIR passes to reason about these addresses, a new hlfir.declare
operation is created with the acc data operation result. This gives
access to the shape, contiguity, attributes, and dummy argument
relationships inside the region without having FIR extended to
understand the data operations.
2025-10-09 14:26:41 +02:00
Valentin Clement (バレンタイン クレメン)
c242aff245
[flang][cuda][openacc] Create new symbol in host_data region for CUDA Fortran interop (#161613) 2025-10-02 04:43:45 -10:00
Slava Zakharin
d6e20c42c1
[flang] Clean-up for fir.do_loop generation in lowering. (#160630)
This patch changes two things:
  1. We do not need to use the loop counter's last value
     for regular do-loops in Lowering.
  2. The loop counter's increment is implied by fir.do_loop
     operation, so there is no need to increment it explicitly.

The last point has been especially confusing to me, because it was
unclear why we have an explicit increment if it is implied.
It looks like CFGConversion somehow still makes the final code
correct, i.e. the counter is not incremented twice.
Anyway, the new lowering should look more concise.
2025-09-26 09:40:04 -07:00
Jean-Didier PAILLEUX
5149e51cb2
[flang][Lower] Add lowering to SYNC ALL, SYNC MEMORY and SYNC IMAGES to PRIF (#154166)
In relation to the approval and merge of the
https://github.com/llvm/llvm-project/pull/76088 specification about
multi-image features in Flang.
Here is a PR on adding support for SYNC ALL, SYNC MEMORY and SYNC IMAGES
in conformance with the PRIF specification.

---------

Co-authored-by: Katherine Rasmussen <krasmussen@lbl.gov>
2025-09-12 15:29:21 +02:00
Razvan Lupusoru
2a2296b1aa
[flang][acc] Fix incorrect loop body nesting and IV value use (#157708)
Two issues are being resolved:
- Incorrect loop body nesting caused by insertion point not being
updated after the loop. The scenario is now being tested through
`nested_do_loops` function in the test.
- Incorrect IV ssa values due to incorrect handling of scoping.

Additionally, this also adds `--openacc-do-loop-to-acc-loop` flag so
that the implicit conversion can be disabled for testing.
2025-09-09 13:09:04 -07:00
Slava Zakharin
83da8d08ff
[flang] Attach proper storage to [hl]fir.declare in lowering. (#155742)
As described in
https://discourse.llvm.org/t/rfc-flang-representation-for-objects-inside-physical-storage/88026,
`[hl]fir.declare` should carry information about the layout
of COMMON/EQUIVALENCE variables within the physical storage.

This patch modifes Flang lowering to attach this information.
2025-09-04 15:49:11 -07:00