10609 Commits

Author SHA1 Message Date
Krzysztof Parzyszek
9554152c8a [flang][OpenMP] Avoid analyzing assumed-size array bases (#150324)
A check for character substrings masquerading as array sections was
using expression analyzer on the array base. When this array happened to
be an assumed-size array, the analyzer emitted a semantic error that did
not correspond to any issue with the source code.

To avoid that, check whether the object is an assumed-size array before
using the expression analyzer on it.

While at it, replace the call to GetShape with a simple check for rank,
since that's the only information needed.

Fixes https://github.com/llvm/llvm-project/issues/150297

(cherry picked from commit 8fcbd06b25fd0c1b4bc4a4c8775129f7dab1affd)
2025-07-28 09:37:49 +02:00
Carlos Seo
3db29aaee5 [Flang] Fix a crash when equivalence and namelist statements are used (#150081)
Check for equivalence when generating namelist descriptors in IO.cpp.

Fixes #124489
2025-07-24 12:07:34 +02:00
Carlos Seo
e43496bacf [Flang] Fix ASSIGN statement (#149941)
Handle the case where the assigned variable also has a pointer
attribute.

Fixes #121721

(cherry picked from commit fc0a978327215aa8883ae6f18d1e316f3c04520a)
2025-07-24 12:03:42 +02:00
Kareem Ergawy
4d6604adf1 [flang][OpenMP] Restore reduction processor behavior broken by #145837 (#150178)
Fixes #149089 and #149700.

Before #145837, when processing a reduction symbol not yet supported by
OpenMP lowering, the reduction processor would simply skip filling in
the reduction symbols and variables. With #145837, this behvaior was
slightly changed because the reduction symbols are populated before
invoking the reduction processor (this is more convenient to shared the
code with `do concurrent`).

This PR restores the previous behavior.

(cherry picked from commit 36c37b019b5daae79785e8558d693e6ec42b0ebd)
2025-07-24 12:03:24 +02:00
Koakuma
5c69f70244 [SPARC][Driver] Move feature mode selection to Arch/Sparc.cpp (#149652)
This is so that it's performed also for flang and not just for clang.

This should fix https://github.com/llvm/llvm-project/issues/138494.

(cherry picked from commit 38fc453afdb6a4511b7c8e189f12a92559ecc396)
2025-07-22 10:38:31 +02:00
Kazu Hirata
769bd90f8b [flang] Fix a warning
This patch fixes:

  flang/lib/Lower/ConvertVariable.cpp:787:22: error: unused variable
  'fieldTy' [-Werror,-Wunused-variable]
2025-07-14 22:17:12 -07:00
Valentin Clement (バレンタイン クレメン)
90ef114a33
[flang][cuda] Add cuf.set_allocator_idx for device component (#148750) 2025-07-14 19:31:44 -07:00
Valentin Clement (バレンタイン クレメン)
2c6771889a
[flang][cuda] Introduce cuf.set_allocator_idx operation (#148717) 2025-07-14 17:23:18 -07:00
Valentin Clement (バレンタイン クレメン)
5eecec8e81
[flang] Fix use of __has_builtin and formatting (#148746)
`__has_builtin` is not available on all compilers. Make sure it works
when not defined.

Also fix some formatting issues: 
- Use brace initialization where possible
- Fix wrong capitalization of variables.
- Add `std::` for `unit64_t` and `int64_t` as it is mostly done in this
part of the codebase.
2025-07-14 17:21:09 -07:00
Susan Tan (ス-ザン タン)
ada514b0af
[flang][acc][NFC] Add deviation to the spec that declaring the same variable is permitted (#148288)
OpenACC spec says `A var may appear at most once in all the clauses of
declare directives for a function, subroutine, program, or module.` but
our implementation allows it with a warning generated. Add this to the
diviation list for record.
2025-07-14 16:55:40 -04:00
Razvan Lupusoru
c4fc358156
[flang][acc][nfc] Move FIROpenACCSupport to Support subfolder (#148710)
In order to prepare for adding FIROpenACCTransforms, move the FIR
OpenACC support library to its own subfolder.
2025-07-14 13:42:20 -07:00
Peter Klausler
69f38443e5
[flang] Extension: TRANSFER(boz, integer or real scalar) (#147604)
Interpret TRANSFER(SOURCE=BOZ literal, MOLD=integer or real scalar) as
if it had been a reference to the standard INT or REAL intrinsic
functions, for which a BOZ literal is an acceptable argument, with a
warning about non-conformance. It's a needless extension that has
somehow crept into some other compilers and real applications.
2025-07-14 11:12:50 -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
Razvan Lupusoru
b54cfa46a7
[flang][acc] Implement MappableType's generatePrivateInit (#148302)
The recipe body generation was moved from lowering into FIR's
implementation of MappableType API. And now since all Fortran variable
types implement this type, lowering of OpenACC was updated to use this
API directly. No test changes were needed - all of the private,
firstprivate, and recipe tests get the same body as before.
2025-07-14 10:53:54 -07:00
Tom Eccles
3d6aac31fc
[flang][Lower] fix warning (#148667)
GCC 9.3.0
```
.../flang/lib/Lower/Support/ReductionProcessor.cpp:137:1: error: control reaches end of non-void function [-Werror=return-type]
```
2025-07-14 17:59:50 +01:00
Slava Zakharin
4775b96898
[flang] Optimize redundant array repacking. (#147881)
This patch allows optimizing redundant array repacking, when
the source array is statically known to be contiguous.
This is part of the implementation plan for the array repacking
feature, though, it does not affect any real life use case
as long as FIR inlining is not a thing. I experimented with
simple cases of FIR inling using `-inline-all`, and I recorded
these cases in optimize-array-repacking.fir tests.
2025-07-14 09:41:42 -07:00
Slava Zakharin
9e56d0d2ab
[flang] Fixed TBAA tags for derived types with descriptors. (#148093)
We cannot attach any "data" or "descriptor" tag to accesses
of derived types that contain descriptors, because this
will make them non-aliasing with any generic "data" or "descriptor"
accesses, which is not correct. We have to skip TBAA tags attachment
for such accesses same way we do it for boxes.
2025-07-14 09:41:15 -07:00
Slava Zakharin
fc99ef7411
[flang] Allow embox's source_box to be a !fir.box. (#148305)
In order to create temporary copies of assumed-type arrays
(e.g. for `-frepack-arrays`), we have to allow the source_box
to be a !fir.box.

This patch replaces #147618.
2025-07-14 09:40:42 -07:00
Kareem Ergawy
a716cc0961
[flang] Support do concurrent ... reduce for associating names (#148597)
Extends reduction support for `do concurrent`, in particular, for
associating names. Consider the following input:
```fortran
subroutine dc_associate_reduce
  integer :: i
  real, allocatable, dimension(:) :: x

  associate(x_associate => x)
  do concurrent (i = 1:10) reduce(+: x_associate)
  end do
  end associate
end subroutine
```

The declaration of `x_associate` is emitted as follows:
```mlir
%13:2 = hlfir.declare %10(%12) {uniq_name = "...."} : (!fir.heap<!fir.array<?xf32>>, !fir.shapeshift<1>) -> (!fir.box<!fir.array<?xf32>>, !fir.heap<!fir.array<?xf32>>)
```
where the HLFIR base type is an array descriptor (i.e. the
allocatable/heap attribute is dropped as stipulated by the spec; section
11.1.3.3).

The problem here is that `declare_reduction` ops accept only reference
types. This restriction is already partially handled for
`fir::BaseBoxType`'s by allocating a stack slot for the descriptor and
storing the box in that stack allocation. We have to modify this a
littble bit for `associate` since the HLFIR and FIR base types are
different (unlike most scenarios).
2025-07-14 12:18:16 +02:00
Tom Eccles
51d3bc67d0
[flang][tco] Add more flags for generating codegen tests (#148171)
Built on top of https://github.com/llvm/llvm-project/pull/146533

Adding some more options I use to generate minimal testcases for
MLIR->LLVMIR conversion.
Hopefully this will save us all some time when writing these tests.

The following options are added
- `-simplify-mlir` runs CSE and canonicalization at the end of the MLIR
pipeline
- `-enable-aa` allows toggling whether TBAA is enabled
- `-test-gen` is shorthand for `-emit-final-mlir -simplify-mlir
-enable-aa=false`
2025-07-14 10:24:58 +01:00
David Spickett
29d8c346c5
Reland "[flang] Avoid undefined behaviour when parsing format expressions (#147539)" (#148169)
This reverts commit e8e5d07767c444913f837dd35846a92fcf520eab.

This previously failed because the flang-rt build could not find the 
llvm header file. It passed on some machines but only because they
had globally installed copies of older llvm.

To fix this, I've copied the required routines from llvm into flang.

With the following justification:
* Flang can, and does, use llvm headers.
* Some Flang headers are also used in Flang-rt.
* Flang-rt cannot use llvm headers.
* Therefore any Flang header using in Flang-rt cannot use llvm headers
either.

To support that conclusion,
https://flang.llvm.org/docs/IORuntimeInternals.html
states:
"The Fortran I/O runtime support library is written in C++17, and uses
some C++17 standard library facilities, but it is intended to not have
any link-time dependences on the C++ runtime support library or any LLVM
libraries."

This talks about libraries but I assume it applies to llvm in general.

Nothing in flang/include/flang/Common, or flang/include/flang/Common
includes any llvm header, and I see some very similar headers there
that duplicate llvm functionality. Like float128.h.

I can only assume this means these files must remain free of
dependencies
on LLVM.

I have copied the two routines literally and put them in the
flang::common
namespace, for lack of a better place for them. So they don't clash with
something.

I have specialised the function to the 1 type flang needs, as it might
save a bit of compile time.
2025-07-14 10:14:04 +01:00
Christian Ulmann
374d5da214
[MLIR][Interfaces] Remove negative branch weight verifier (#148234)
This commit removes the verifier that checked if branch weights are
negative. This check was too strict because weights are interpreted as
unsigned integers.

This showed up when running the verifier on LLVM dialect modules that
were imported from LLVM IR.
2025-07-14 07:34:29 +02:00
Valentin Clement (バレンタイン クレメン)
aa7bf60618
[flang][cuda] Fix false positive on unsupported CUDA data transfer (#148295)
The switch to `GetSymbolVector` introduced a regression on detecting
implicit data transfer when the rhs is a function call.
Make sure the symbol we are looking at are of interest to detect data
transfer.
2025-07-11 14:30:57 -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
Tom Eccles
6630cde4e6
[flang][docs] Update release notes for OpenMP (#147988)
Co-authored-by: Kiran Chandramohan <kiranchandramohan@gmail.com>
2025-07-11 12:18:35 +01:00
Tom Eccles
9a805ba169
[flang][NFC] Fix deprecation warning (#147932)
I started getting deprecation warnings from operations constructors
which seem to be doing implicit construction of mlir::ValueRange from a
std::nullopt by relying on implicit conversion from std::nullopt into
llvm::ArrayRef. ArrayRef{std::nullopt} is what has been deprecated.
2025-07-11 10:37:34 +01:00
Kareem Ergawy
ab1c4905f4
[flang][do concurrent] Extned getAllocaBlock() and emit yields correctly (#146853)
Handles some loose ends in `do concurrent` reduction declarations. This
PR extends `getAllocaBlock` to handle declare ops, and also emit
`fir.yield` in all regions.
2025-07-11 10:17:17 +02:00
Kareem Ergawy
0e9b7b054c
[flang][OpenMP] Basic mapping of do concurrent ... reduce to OpenMP (#146033)
Now that we have changes introduced by #145837, mapping reductions from
`do concurrent` to OpenMP is almost trivial. This PR adds such mapping.

PR stack:
- https://github.com/llvm/llvm-project/pull/145837
- https://github.com/llvm/llvm-project/pull/146025
- https://github.com/llvm/llvm-project/pull/146028
- https://github.com/llvm/llvm-project/pull/146033 (this one)
2025-07-11 09:19:16 +02:00
Kareem Ergawy
a510e75949
[flang][fir] Small clean-up in fir_DoConcurrentLoopOp's defintion (#146028)
Re-organizes the op definition a little bit and removes a method that
does not add much value to the API.

PR stack:
- https://github.com/llvm/llvm-project/pull/145837
- https://github.com/llvm/llvm-project/pull/146025
- https://github.com/llvm/llvm-project/pull/146028 (this one)
- https://github.com/llvm/llvm-project/pull/146033
2025-07-11 08:30:36 +02:00
Kareem Ergawy
7c8a197918
[NFC][flang] Move ReductionProcessor to Lower/Support. (#146025)
With #145837, the `ReductionProcessor` component is now used by both
OpenMP and `do concurrent`. Therefore, this PR moves it to a shared
location: `flang/Lower/Support`.

PR stack:
- https://github.com/llvm/llvm-project/pull/145837
- https://github.com/llvm/llvm-project/pull/146025 (this one)
- https://github.com/llvm/llvm-project/pull/146028
- https://github.com/llvm/llvm-project/pull/146033
2025-07-11 07:42:51 +02:00
Kareem Ergawy
eba35cc1c0
[flang][do concurrent] Re-model reduce to match reductions are modelled in OpenMP and OpenACC (#145837)
This PR proposes re-modelling `reduce` specifiers to match OpenMP and
OpenACC. In particular, this PR includes the following:

* A new `fir` op: `fir.delcare_reduction` which is identical to OpenMP's
`omp.declare_reduction` op.
* Updating the `reduce` clause on `fir.do_concurrent.loop` to use the
new op.
* Re-uses the `ReductionProcessor` component to emit reductions for `do
concurrent` just like we do for OpenMP. To do this, the
`ReductionProcessor` had to be refactored to be more generalized.
* Upates mapping `do concurrent` to `fir.loop ... unordered` nests using
the new reduction model.

Unfortunately, this is a big PR that would be difficult to divide up in
smaller parts because the bottom of the changes are the `fir` table-gen
changes to `do concurrent`. However, doing these MLIR changes cascades
to the other parts that have to be modified to not break things.

This PR goes in the same direction we went for `private/local`
speicifiers. Now the `do concurrent` and OpenMP (and OpenACC) dialects
are modelled in essentially the same way which makes mapping between
them more trivial, hopefully.

PR stack:
- https://github.com/llvm/llvm-project/pull/145837 (this one)
- https://github.com/llvm/llvm-project/pull/146025
- https://github.com/llvm/llvm-project/pull/146028
- https://github.com/llvm/llvm-project/pull/146033
2025-07-11 06:39:30 +02:00
Valentin Clement (バレンタイン クレメン)
c919221bbe
[flang][cuda][NFC] Remove TODO implemented in semantic (#148058) 2025-07-10 20:52:55 -07:00
Razvan Lupusoru
4859b92b7f
[flang][acc] Update FIR ref, heap, and pointer to be MappableType (#147834)
The MappableType OpenACC type interface is a richer interface that
allows OpenACC dialect to be capable to better interact with a source
dialect, FIR in this case. fir.box and fir.class types already
implemented this interface. Now the same is being done with the other
FIR types that represent variables.

One additional notable change is that fir.array no longer implements
this interface. This is because MappableType is primarily intended for
variables - and FIR variables of this type have storage associated and
thus there's a pointer-like type (fir.ref/heap/pointer) that holds the
array type.

The end goal of promoting these FIR types to MappableType is that we
will soon implement ability to generate recipes outside of the frontend
via this interface.
2025-07-10 15:23:57 -07:00
Valentin Clement (バレンタイン クレメン)
9a0e03f430
[flang][cuda] Update implicit data transfer for device component (#147882)
Update the detection of implicit data transfer when a device resident
allocatable derived-type component is involved and remove the TODOs.
2025-07-10 09:50:31 -07: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
Daniel Paoliello
154de3e1bd
[flang] Don't check the '-mframe-pointer' flag (#147837)
The `-mframe-pointer` flag is not explicitly set in the original `flang`
invocation and so the value passed to `flang -fc1` can vary depending on
the host machine, so don't verify it in the output.

`-mframe-pointer` forwarding is already verified by
`flang/test/Driver/frame-pointer-forwarding.f90`.
2025-07-10 07:27:33 -07:00
Daniel Chen
13ead00049
[Flang] Fix PowerPC build failure due to the deprecation of ArrayRef(std::nullopt_t) {}. (#147816)
Our local Flang build on PowerPC was broken as
```
llvm/flang/../mlir/include/mlir/IR/ValueRange.h:401:20: error: 'ArrayRef' is deprecated: Use {} or ArrayRef<T>() instead [-Werror,-Wdeprecated-declarations]
  401 |       : ValueRange(ArrayRef<Value>(std::forward<Arg>(arg))) {}
      |                    ^
llvm/flang/lib/Optimizer/CodeGen/CodeGen.cpp:2243:53: note: in instantiation of function template specialization 'mlir::ValueRange::ValueRange<const std::nullopt_t &, void>' requested here
 2243 |                              /*cstInteriorIndices=*/std::nullopt, fieldIndices,
      |                                                     ^
 llvm/include/llvm/ADT/ArrayRef.h:70:18: note: 'ArrayRef' has been explicitly marked deprecated here
   70 |     /*implicit*/ LLVM_DEPRECATED("Use {} or ArrayRef<T>() instead", "{}")
      |                  ^
llvm/include/llvm/Support/Compiler.h:244:50: note: expanded from macro 'LLVM_DEPRECATED'
  244 | #define LLVM_DEPRECATED(MSG, FIX) __attribute__((deprecated(MSG, FIX)))
      |                                                  ^
1 error generated.
```

This patch is to fix it.
2025-07-10 09:53:03 -04:00
agozillon
75f81ded8f
[Flang][FlangRT][Runtime] Add RT_OFFLOAD_API_GROUP_BEGIN to missing symbols on AMDGPU (#147612)
After the recent move to work queues, in certain cases when linking in
the fortran runtime built for offload on AMDGPU as required in certain
cases, we'll get missing symbols when linking. This PR tries to address
this issue by encompassing more of the library in
RT_OFFLOAD_API_GROUP_BEGIN, which has the affect of compiling these
functions for AMDGPU, resolving the missing symbols.

This PR should address the following issue:
https://github.com/llvm/llvm-project/issues/145888
2025-07-10 13:19:58 +02: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
Vijay Kandiah
c4138a24dc
[mlir][acc][flang] Lower nested ACC loops with tile clause as collapsed loops (#147801)
In the case of nested loops, `acc.loop` is meant to subsume all of the
loops that it applies to (when explicitly described as doing so in the
OpenACC specification). So when there is a `acc loop tile(...)` present
on nested Fortran DO loops, `acc.loop` should apply to the `n` loops
that `tile` applies to. This change lowers such nested Fortran loops
with tile clause into a collapsed `acc.loop` with `n` IVs, loop bounds,
and step, in a similar fashion to the current lowering for acc loops
with `collapse` clause.
2025-07-09 15:47:11 -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
Leandro Lupori
a63846b475
[flang] Fix array assignment regression introduced by #147371 (#147761)
In some cases fixed shape arrays can be fir.heap/fir.ptr, even
after hlfir::derefPointersAndAllocatables() is called.
2025-07-09 14:41:56 -03:00
David Spickett
e8e5d07767 Revert "[flang] Avoid undefined behaviour when parsing format expressions (#147539)"
This reverts commit d0caf0d4857c2b00ba988f86703663685ec8697f.

MathExtras.h is not found in some builds.
2025-07-09 14:49:58 +00:00
David Spickett
d0caf0d485
[flang] Avoid undefined behaviour when parsing format expressions (#147539)
The test flang/test/Semantics/io08.f90 was failing when UBSAN was
enabled:
```
/home/david.spickett/llvm-project/flang/include/flang/Common/format.h:224:26: runtime error: signed integer overflow: 10 * 987654321098765432 cannot be represented in type 'int64_t' (aka 'long')
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/david.spickett/llvm-project/flang/include/flang/Common/format.h:224:26
```
This is because the code was effectively:
* Take the risk of UB happening
* Check whether it happened or not

Which UBSAN is obviously not going to like. Instead of checking after
the fact, use llvm's helpers that catch overflow without actually doing
it.
2025-07-09 15:31:45 +01:00
Maksim Levental
1770e9b5c6
[mlir] remove dangling builders from td (#147619)
These are "dangling" builders (decls are emitted but there are no defns
anywhere).
2025-07-09 09:59:24 -04:00
Shunsuke Watanabe
c9900015a9
[flang] Add -fcomplex-arithmetic= option and select complex division algorithm (#146641)
This patch adds an option to select the method for computing complex
number division. It uses `LoweringOptions` to determine whether to lower
complex division to a runtime function call or to MLIR's `complex.div`,
and `CodeGenOptions` to select the computation algorithm for
`complex.div`. The available option values and their corresponding
algorithms are as follows:
- `full`: Lower to a runtime function call. (Default behavior)
- `improved`: Lower to `complex.div` and expand to Smith's algorithm.
- `basic`: Lower to `complex.div` and expand to the algebraic algorithm.

See also the discussion in the following discourse post:
https://discourse.llvm.org/t/optimization-of-complex-number-division/83468

---------

Co-authored-by: Tarun Prabhu <tarunprabhu@gmail.com>
2025-07-09 13:43:54 +09:00
Valentin Clement (バレンタイン クレメン)
46caad52ac
[flang][cuda] Do not produce data transfer in offloaded do concurrent (#147435)
If a `do concurrent` loop is offloaded then there should be no CUDA data
transfer in it. Update the semantic and lowering to take that into
account.

`AssignmentChecker` has to be put into a separate pass because the
checkers in `SemanticsVisitor` cannot have the same `Enter/Leave`
functions. The `DoForallChecker` already has `Eneter/Leave` functions
for the `DoConstruct`.
2025-07-08 10:52:15 -07:00
Leandro Lupori
e976eaf303
[flang] Fix optimization of array assignments after #146408 (#147371)
Host associated variables were not being handled properly.
For array references, get the fixed shape extents from the value
type instead, that works correctly in all cases.
2025-07-08 14:47:26 -03:00