640 Commits

Author SHA1 Message Date
Philipp Rados
9914ee6ef4
[flang] Fix -debug crash from VScaleAttrPass (#180234)
This pass splits up the `vscaleRange` pass-option from the
`VScaleAttrPass` into `vscaleMin` and `vscaleMax` respectively, since a
`std::pair<>` cannot be used as a cli-option and crashes when running
`flang -march=rv64gcv -O3 file.f90 -mmlir -debug`.

Since the options can now be set individually I added some error
checking following the semantics described in the langref
https://llvm.org/docs/LangRef.html#function-attributes.

I also added tests since there were none for only this pass before.
2026-02-10 11:46:06 +01:00
paperchalice
5c5677d7b8
[llvm] Remove "no-infs-fp-math" attribute support (#180083)
One of global options in `TargetMachine::resetTargetOptions`, now all
backends no longer support it, remove it.
2026-02-09 08:43:33 +08:00
Valentin Clement (バレンタイン クレメン)
320c330989
[flang][cuda] Update visibility of declaration copied to in gpu.module (#179725)
https://github.com/llvm/llvm-project/pull/179362 changes which op is
checked for visibility during nested symbol resolution. This cause
issues in the CUDA Fortran pipeline and make some lookup fails. Update
the visibility of declaration copied to the gpu.module to nested.
2026-02-04 18:49:40 +00:00
Valentin Clement (バレンタイン クレメン)
b88d49ef12
[flang][cuda] Do not initialize shared variable (#178489) 2026-01-28 19:23:31 +00:00
Jean-Didier PAILLEUX
6f8cea3f90
[flang][MIF] Adding Stop and ErrorStop PRIF call procedures (#166787)
This PR proposes to add `Stop` and `ErrorStop` PRIF call procedures to the MIF
dialect. If the `-fcoarray` flag is passed, then all calls to `STOP` and `ERROR
STOP` will use those of PRIF in flang-rt. Thes procedure has been registered 
during the initialization (mif::InitOp).

---------

Co-authored-by: Dan Bonachea <dobonachea@lbl.gov>
2026-01-27 16:54:11 +01:00
jeanPerier
9a39c2ff75
Revert "[flang] Use outermost fir.dummy_scope for TBAA of local allocations. (#146006) (#177617)
This reverts commit 90da61634a4accc9869b4e1cb1ac3736158c33e6.

See https://github.com/llvm/llvm-project/pull/177615 for more context
about why this patch is and can now be reverted.
2026-01-27 15:25:21 +01:00
Keyi Zhang
e0c12486dc
[MLIR][LLVM] Add flags field to the DIDerivedType attribute (#177889)
This PR extends the `DIDerivedType` with the `flags` field.
Fixes https://github.com/llvm/llvm-project/issues/177725
2026-01-27 08:53:44 +01:00
Valentin Clement (バレンタイン クレメン)
b57dcfff1d
[flang][cuda][NFC] Fix typo in header (#177299) 2026-01-22 02:56:15 +00:00
Valentin Clement (バレンタイン クレメン)
1d5e0408c9
[flang][cuda] Remove CUFDeviceAddressOpConversion from CUFOpConversion (#177213)
The pattern has been moved to CUFOpConversionLate
2026-01-21 11:20:45 -08:00
Valentin Clement (バレンタイン クレメン)
864c6cc9a6
[flang][cuda] Remove option allocationConversion from pass (#177037)
The pass option was meant to be used during migration. This is not
needed anymore.
2026-01-21 11:01:17 -08:00
Valentin Clement (バレンタイン クレメン)
49834b745f
[flang][cuda][NFC] Fix filename in header (#177070) 2026-01-21 01:34:32 +00:00
Valentin Clement (バレンタイン クレメン)
39b74fd20f
[flang][cuda] Add cuf.device_address conversion into separate pass (#177035)
Some conversion are better done later in the pipeline. Add
cuf.device_address conversion in its own pass so it can be scheduled
late.

The pattern will be removed from CUFOpConversion once this pass has
landed.
2026-01-20 14:51:06 -08:00
Abid Qadeer
dc9c08e6e0
[flang][debug] Generate DWARF debug info using fir.use_stmt. (#168541)
This patch uses the fir.use_stmt operations to generate correct debug
metadata for use statement when `only` and `=>` are used. The debug flow
is changed a bit where we process the module globals first so that we
have the global variables when we start to process `fir.use_stmt`.
    
Fixes #160923.
2026-01-19 17:16:11 +00:00
Slava Zakharin
09ae1bf8b7
[flang] Added OperationMoveOpInterface for controlling LICM. (#175108)
In #173438 I added a FIR specific loop invariant code motion pass.

During the review, Tom pointed out certain limitations about OpenMP
dialect operations that should be taken into consideration during
transformations such as LICM:
https://github.com/llvm/llvm-project/pull/173438#discussion_r2657612148

I also found issues with hoisting operations out of `acc.loop`
operations in certain conditions (see the added test in `licm.fir`).

I am proposing a new operation interface that will allow to control
movement of operations during MLIR transformations. In particular, I
propose two methods (there might be more):
* op.canMoveOutOf(cand) - returns true, if it is allowed to move 'cand'
operation out of 'op'.
* op.canMoveFromDescendant(descendant, cand) - return true, if it is
allowed to move 'cand' out of 'descendant' and into 'op'.

I used the new interface to get rid of explicit OpenMP interfaces checks
in Flang's LICM, and I also used it for `acc.loop` operation (though, I
provided conservative initial implementation).

The new interface is part of FIR dialect, but I think it would better
fit into the core MLIR set of interfaces so that the checks that I make
in Flang's LICM are actually done in
`mlir::moveLoopInvariantCode`. Moreover, other code movement
transformations that may appear in MLIR may also need to use such an
interface.

I would like to get some feedback on whether it is reasonable to move
the interface to core MLIR.
2026-01-16 08:32:38 -08:00
Razvan Lupusoru
ab7217a089
[acc][flang] Add isDeviceData APIs for device data detection (#176219)
Add comprehensive APIs to detect device-resident data across OpenACC
type and operation interfaces. This enables passes to identify data that
is already on the device (e.g., CUF device/managed/constant memory, GPU
address spaces) and handle it appropriately.

New interface methods:
- PointerLikeType::isDeviceData(Value): Returns true if the pointer
points to device data.
- MappableType::isDeviceData(Value): Returns true if the variable
represents device data.
- GlobalVariableOpInterface::isDeviceData(): Returns true if the global
variable is device data.

New utilities in OpenACCUtils:
- acc::isDeviceValue(Value): Checks if a value represents device data by
querying type interfaces, PartialEntityAccessOpInterface for base
entities, and AddressOfGlobalOpInterface for global symbols.
- acc::isValidValueUse(Value, Region): Checks if a value is legal in an
OpenACC region by verifying it comes from a data operation, is only used
by private clauses, or is device data.

Updated isValidSymbolUse to check
GlobalVariableOpInterface::isDeviceData()
for symbols referencing device-resident globals.

FIR implementations check for CUF data attributes (device, managed,
constant, shared, unified) on operations, block arguments, and globals.
The implementation traces through fir.rebox, fir.embox, fir.declare,
hlfir.declare, and fir.address_of to find the underlying data source.

Memref implementations check for gpu::AddressSpaceAttr on the memref
type.

Updated ACCImplicitData to use acc::isDeviceValue for generating
acc.deviceptr clauses for device-resident data instead of
copyin/copyout.

Updated OpenACCSupport::isValidValueUse to fallback to the new
acc::isValidValueUse utility.
2026-01-15 20:56:26 +00:00
Susan Tan (ス-ザン タン)
2698d15664
[flang] Lowering FIR memory ops to MemRef dialect (#173507)
This patch introduces FIRToMemRef, a lowering pass that converts FIR
memory operations to the MemRef dialect, including support for slices,
shifts, and descriptor-style access patterns. To support partial
lowering, where FIR and MemRef types can coexist, we extend the handling
of fir.convert to correctly marshal between FIR reference-like types and
MemRef descriptors. The patch also factors the type conversion logic
into a reusable FIRToMemRefTypeConverter, which centralizes the rules
for converting FIR types (e.g. !fir.ref, !fir.box, sequences, logicals)
to their corresponding memref types, and is used throughout the new
pass.

---------

Co-authored-by: Scott Manley <rscottmanley@gmail.com>
Co-authored-by: jeanPerier <jean.perier.polytechnique@gmail.com>
2026-01-14 10:46:50 -05:00
Razvan Lupusoru
33c4e3e2bc
[flang][cuda] Avoid inserting GetDeviceAddress call in offload regions (#175225)
In regions destined for GPU offload, computing an address_of means
getting device address directly - no need (and actually incorrect) to
insert a runtime call to get the address. This was already working for
regions such as `gpu.launch` - but now it applies to acc regions as
well.
2026-01-09 12:20:18 -08:00
Slava Zakharin
0bf4df8b1e
[flang] Added LoopInvariantCodeMotion pass for [HL]FIR. (#173438)
The new pass allows hoisting some `fir.load` operations early
in MLIR. For example, many descriptor load might be hoisted
out of the loops, though it does not make much difference
in performance, because LLVM is able to optimize such loads
(which are lowered as `llvm.memcpy` into temporary descriptors),
given that proper TBAA information is generated by Flang.

Further hoisting improvements are possible in [HL]FIR LICM,
e.g. getting proper mod-ref results for Fortran runtime calls
may allow hoisting loads from global variables, which LLVM
cannot do due to lack of alias information.

This patch also contains improvements for FIR mod-ref analysis:
We may recurse into `HasRecursiveMemoryEffects` operations and
use `getModRef` recursively to get more precise results for
regions with `fir.call` operations.

This patch also modifies `AliasAnalysis` to set the instantiation
point for cases where the tracked data is accessed through a load
from `!fir.ref<!fir.box<>>`: without this change the mod-ref
analysis was not able to recognize user pointer/allocatable variables.
2026-01-07 16:16:52 -08:00
Valentin Clement (バレンタイン クレメン)
30fe41b90e
[flang][cuda][NFC] Move CUDA Fortran passes into CUDA directory (#174657)
Move CUDA Fortran related passes with others in CUDA directory.
2026-01-06 14:15:07 -08:00
Valentin Clement (バレンタイン クレメン)
a19b46450c
[flang][cuda] Add CUFFunctionRewrite pass (#174650)
This rewrite some CUDA Fortran specific like `on_device` function to
constant boolean values.
2026-01-06 13:30:03 -08:00
Valentin Clement (バレンタイン クレメン)
a42bb08e44
[flang][cuda] Add CUFDeviceFuncTransform pass (#174487)
This pass transform CUDA Fortran func.func to gpu.func and put them in
the gpu.module.
2026-01-05 15:12:19 -08:00
Valentin Clement (バレンタイン クレメン)
e826168a24
[flang][cuda] Add CUFLaunchAttachAttr pass (#174465)
CUF kernel are generated via gpu.launch and then outlined. The resulting
launch operation needs to hava a CUDA attribute attached so it will
be distinguishable from other launch.
2026-01-05 13:05:34 -08:00
Valentin Clement (バレンタイン クレメン)
5dfd277c38
[flang][cuda] Add pass to transform predefined variables (#174451)
Change the predefined variables injected in the device function to their
corresponding NVVM dialect operations.
2026-01-05 10:10:37 -08:00
Susan Tan (ス-ザン タン)
5cfd02f44a
[flang] change yielded iv value to be iv + step (#174124)
In cases where induction variables are used after the loop,  like 

```
write(*,*) (a(j),j=1,10) 
print *, j
```

the incremented value should be used. Updating the FIRToSCF pass to
support this.
2026-01-05 07:31:11 -05:00
Valentin Clement (バレンタイン クレメン)
a0dfe45036
Reland "[flang][cuda] Add support for derived-type initialization on device #172568" (#174107)
The build bots failure have been address in #174048
2025-12-31 11:26:17 -08:00
Slava Zakharin
fe0f366f6e
[flang] Fixed hoisting order in fir.do_concurrent simplification. (#174044)
The order has to be fixed after #173502. This results in
reversing the order of `fir.alloca`, but that should be
insignificant.
2025-12-31 10:08:32 -08:00
Valentin Clement (バレンタイン クレメン)
f43d683409
Revert "Reland "[flang][cuda] Add support for derived-type initialization on device #172568" (#174033)
This fails https://lab.llvm.org/staging/#/builders/65
This reverts commit 1ac1a547ee3b74b4d02bc94faf02ca0381196d11.
2025-12-30 15:15:44 -08:00
Slava Zakharin
91981a5736
[flang] Fixed operations hoisting out of fir.do_concurrent. (#173502)
LICM (#173438) may insert new operations at the beginning of
`fir.do_concurrent`'s block and they cannot be always hoisted
to the alloca-block of the parent operation. This patch
only moves `fir.alloca`s into the alloca-block, and moves
all other operations right before fir.do_concurrent.
2025-12-30 10:27:31 -08:00
Valentin Clement (バレンタイン クレメン)
1ac1a547ee
Reland "[flang][cuda] Add support for derived-type initialization on device #172568" (#172913)
#172568
2025-12-30 08:49:04 -08:00
Valentin Clement (バレンタイン クレメン)
c3403db779
Revert "Reland "[flang][cuda] Add support for derived-type initialization on device #172568"" (#172889)
Reverts llvm/llvm-project#172741

Failures in Fortran testsuite. 

https://lab.llvm.org/buildbot/#/builders/199/builds/7943
2025-12-18 11:11:47 -08:00
Valentin Clement (バレンタイン クレメン)
e81bae73fe
Reland "[flang][cuda] Add support for derived-type initialization on device #172568" (#172741)
https://github.com/llvm/llvm-project/pull/172568
2025-12-18 10:11:18 -08:00
Valentin Clement (バレンタイン クレメン)
b6d06de34d
Revert "[flang][cuda] Add support for derived-type initialization on device" (#172737)
Reverts llvm/llvm-project#172568 this cause some build bots failures.
2025-12-17 12:56:41 -08:00
Valentin Clement (バレンタイン クレメン)
ca2275b25f
[flang][cuda] Add support for derived-type initialization on device (#172568)
Add support for derived-type with default initialization that are
allocated on the device.
2025-12-17 10:16:15 -08:00
jeanPerier
c14c256170
[flang][TBAA] fix unsafe optional deref after #170908 (#172033)
PR #170908 introduced an unconditional dereference of the local target
variable name, but in rare cases (I am not sure this can be reproduced
without `-mllvm -inline-all` currently), such variable may not have a
uniq name on the alloca. For instance, this can happen after a call to a
function with TARGET character result is inlined. The alloca is a temp
on the caller side, that gets the TARGET attribute in the inlined scope
via the result name.
2025-12-12 18:26:11 +01:00
Tom Eccles
33fcfb37da
[flang][TBAA] refine TARGET/POINTER encoding (#170908)
Depends upon https://github.com/llvm/llvm-project/pull/170900

Re-land https://github.com/llvm/llvm-project/pull/169544

Previously we were less specific for POINTER/TARGET: encoding that they
could alias with (almost) anything.

In the new system, the "target data" tree is now a sibling of the other
trees (e.g. "global data"). POITNTER variables go at the root of the
"target data" tree, whereas TARGET variables get their own nodes under
that tree. For example,

```
integer, pointer :: ip
real, pointer :: rp
integer, target :: it
integer, target :: it2(:)
real, target :: rt
integer :: i
real :: r
```
- `ip` and `rp` may alias with any variable except `i` and `r`.
- `it`, `it2`, and `rt` may alias only with `ip` or `rp`.
- `i` and `r` cannot alias with any other variable.

Fortran 2023 15.5.2.14 gives restrictions on entities associated with
dummy arguments. These do not allow non-target globals to be modified
through dummy arguments and therefore I don't think we need to make all
globals alias with dummy arguments.

I haven't implemented it in this patch, but I wonder whether it is ever
possible for `ip` to alias with `rt`.

While I was updating the tests I fixed up some tests that still assumed
that local alloc tbaa wasn't the default.

Cray pointers/pointees are (optionally) modelled as aliasing with all
non-descriptor data. This is not enabled by default.

I found no functional regressions in the gfortran test suite.
2025-12-11 10:03:43 +00:00
Valentin Clement (バレンタイン クレメン)
1f07f7c75a
[flang][cuda] Add support for allocate with device source (#171743)
Add support for allocate statement with a source that is a device
variable.
2025-12-10 18:05:12 -08:00
Ming Yan
a1f948947d
[Flang][Fir] Fix the comparison when lowering fir.iterate_while to scf.while. (#171080)
The comparison depends on the sign of the `step`, and when `step == 0` it always returns `false`.
2025-12-09 08:40:35 +08:00
Valentin Clement (バレンタイン クレメン)
7ba71012ee
[flang][cuda] Add pointer attribute to allocate/deallocate ops (#170937)
Similar for the double descriptor information added in
https://github.com/llvm/llvm-project/pull/170901, we need to carry over
the pointer information until the op can be converted. The correct
detection would fail if the op is converted late.
2025-12-05 22:43:26 +00:00
Valentin Clement (バレンタイン クレメン)
f57f338313
[flang][cuda] Add double descriptor information in allocate/deallocate operations (#170901)
After https://github.com/llvm/llvm-project/pull/169740, the allocate and
deallocate cuf operation can be converted later. Update the way to
recognize double descriptor case by adding this information directly on
the operation itself.
2025-12-05 11:08:45 -08:00
Ming Yan
65b2793e8e
[Flang][Fir] Fix the error when lowering fir.iterate_while to scf while. (#170630)
The lowering fails when `fir.iterate_while` has no final value; this patch fixes the bug.
2025-12-04 20:15:18 +08:00
Valentin Clement (バレンタイン クレメン)
2fb2d7eb41
[flang][cuda] Change how to handle static shared memory variables (#170388)
Generate one global per static shared variable so the alignment can be
set separately. Dynamic shared memory is unchanged.
2025-12-03 13:05:28 -08:00
Valentin Clement (バレンタイン クレメン)
28ac6b36c1
[flang][cuda] Use the option to populate conversion patterns (#170190)
#169740 split the conversion patterns but the option was not use when
populating them.
2025-12-01 11:51:35 -08:00
Valentin Clement (バレンタイン クレメン)
21e64d1f5a
[flang][cuda][NFC] Split allocation related operation conversion from other cuf operations (#169740)
Split AllocOp, FreeOp, AllocateOp and DeallocateOp from other
conversion. Patterns are currently added to the base CUFOpConversion
when the option is enabled.
This split is a pre-requisite to be more flexible where we do the
allocation related operations conversion in the pipeline.
2025-12-01 10:19:52 -08:00
Tom Eccles
b60a84a46f
Revert "[flang][TBAA] refine TARGET/POINTER encoding" (#170105)
Reverts llvm/llvm-project#169544

[Regressed](https://lab.llvm.org/buildbot/#/builders/143/builds/12956)
gfortran test suite
2025-12-01 11:19:12 +00:00
Tom Eccles
34c44f21ae
[flang][TBAA] refine TARGET/POINTER encoding (#169544)
Previously we were less specific for POINTER/TARGET: encoding that they
could alias with (almost) anything.

In the new system, the "target data" tree is now a sibling of the other
trees (e.g. "global data"). POITNTER variables go at the root of the
"target data" tree, whereas TARGET variables get their own nodes under
that tree. For example,

```
integer, pointer :: ip
real, pointer :: rp
integer, target :: it
integer, target :: it2(:)
real, target :: rt
integer :: i
real :: r
```
- `ip` and `rp` may alias with any variable except `i` and `r`.
- `it`, `it2`, and `rt` may alias only with `ip` or `rp`.
- `i` and `r` cannot alias with any other variable.

Fortran 2023 15.5.2.14 gives restrictions on entities associated with
dummy arguments. These do not allow non-target globals to be modified
through dummy arguments and therefore I don't think we need to make all
globals alias with dummy arguments.

I haven't implemented it in this patch, but I wonder whether it is ever
possible for `ip` to alias with `rt` or even `it2`.

While I was updating the tests I fixed up some tests that still assumed
that local alloc tbaa wasn't the default.

I found no functional regressions in the gfortran test suite, fujitsu
test suite, spec2017, or a selection of HPC apps we test internally.
2025-12-01 10:05:56 +00:00
Valentin Clement (バレンタイン クレメン)
bd643bc141
[flang] Use default constructor for FIRToSCF pass (#169741) 2025-11-26 17:44:44 -08:00
Valentin Clement (バレンタイン クレメン)
0f941f6866
[flang][cuda] Add support to allocate scalar character types (#169550)
Add support for character declared like: 

```
subroutine sub1()
  character*4, device :: b
end subroutine
```
2025-11-25 11:41:35 -08:00
Ming Yan
25c95ebfa8
[flang][fir] Convert fir.do_loop with the unordered attribute to scf.parallel. (#168510)
Refines the existing conversion to allow `fir.do_loop` annotated with
`unordered` to be lowered to `scf.parallel`, while other loops retain
their original lowering.
2025-11-25 14:43:41 +00:00
Zhen Wang
1b8a4aa6a5
[flang][cuda] Extract element count computation into helper function (#168937)
This patch extracts the common logic for computing array element counts
from shape operands into a reusable helper function in CUFCommon.
2025-11-20 13:01:22 -08:00
Abid Qadeer
0e8222b84b
[flang][debug] Make common blocks data extraction more robust. (#168752)
Our current implementation for extracting information about common block
required traversal of FIR which was not ideal but previously there was
no other way to obtain that information. The `[hl]fir.declare` was
extended in commit https://github.com/llvm/llvm-project/pull/155325 to
include storage and storage_offset. This commit adds these operands in
`fircg.ext_declare` and then use them in `AddDebugInfoPass` to create
debug data for common blocks.
2025-11-20 14:28:56 +00:00