Commit Graph

1286 Commits

Author SHA1 Message Date
Valentin Clement (バレンタイン クレメン)
45daa4fdc6 [flang][cuda] Move CUDA Fortran operations to a CUF dialect (#92317)
The number of operations dedicated to CUF grew and where all still in
FIR. In order to have a better organization, the CUF operations,
attributes and code is moved into their specific dialect and files. CUF
dialect is tightly coupled with HLFIR/FIR and their types.

The CUF attributes are bundled into their own library since some
HLFIR/FIR operations depend on them and the CUF dialect depends on the
FIR types. Without having the attributes into a separate library there
would be a dependency cycle.
2024-05-17 09:37:53 -07:00
Tom Eccles
7c16e7d9cd [flang][NFC] Use tablegen to reduce AddAliasTags boilerplate (#92373)
I will make this pass run properly on more top-level operations in a
follow up patch.
2024-05-17 10:53:29 +01:00
Renaud Kauffmann
ee407e17a3 [flang] AliasAnalysis: More formally define and distinguish between data and non-data (#91020)
This PR is an implementation for changes proposed in
https://discourse.llvm.org/t/rfc-distinguish-between-data-and-non-data-in-fir-alias-analysis/78759

Test updates were made when the query was on the wrong reference. So, it
is my hope that this will clear ambiguity on the nature of the queries
from here on.
There are also some TODOs that were addressed. 

It also partly implements what
https://github.com/llvm/llvm-project/pull/87723 is attempting to
accomplish. At least, on a point-to-point query between references, the
distinction is made. To apply it to TBAA, would be another PR.

Note that, the changes were minimal in the TBAA code to retain the
current results.
2024-05-16 08:49:44 -07:00
Abid Qadeer
cd5ee2715e [reland][flang] Initial debug info support for local variables (#92304)
This is same as #90905 with an added fix. The issue was that we
generated variable info even when user asked for line-tables-only. This
caused llvm dwarf generation code to fail an assertion as it expected an
empty variable list.

Fixed by not generating debug info for variables when user wants only
line table. I also updated a test check for this case.
2024-05-16 09:10:59 +01:00
Mehdi Amini
6706aebefa Revert "[flang] Add ETIME runtime and lowering intrinsics implementation" (#92354)
Reverts llvm/llvm-project#90578

This broke the premerge linux buildbot.
2024-05-15 23:26:07 -07:00
jiajie zhang
245b7b65cb [flang] Add ETIME runtime and lowering intrinsics implementation (#90578)
This patch add support of intrinsics GNU extension ETIME
https://github.com/llvm/llvm-project/issues/84205. Some usage info and
example has been added to `flang/docs/Intrinsics.md`. The patch contains
both the lowering and the runtime code and works on both Windows and
Linux.


|   System  |   Implmentation  |
|-----------|--------------------|
| Windows| GetProcessTimes |
| Linux      |times                     |
2024-05-16 12:42:01 +08:00
Yusuke MINATO
526553b251 [flang] Add nsw flag to do-variable increment with a new option (#91579)
This patch adds nsw flag to the increment of do-variables when a new
option is enabled.
NOTE 11.10 in the Fortran 2018 standard says they never overflow.

See also the discussion in #74709 and the following discourse post.
https://discourse.llvm.org/t/rfc-add-nsw-flags-to-arithmetic-integer-operations-using-the-option-fno-wrapv/77584/5
2024-05-16 13:16:07 +09:00
Pete Steinfeld
468357114c Revert "[flang] Initial debug info support for local variables. (#909… (#92302)
…05)"

This reverts commit 61da6366d0.

Update #90905 was causing many tests to fail.

See comments in #90905.
2024-05-15 11:30:30 -07:00
Abid Qadeer
61da6366d0 [flang] Initial debug info support for local variables. (#90905)
We need the information in the `DeclareOp` to generate debug information
for variables.  Currently, cg-rewrite removes the `DeclareOp`. As
`AddDebugInfo` runs after that, it cannot process the `DeclareOp`. My
initial plan was to make the `AddDebugInfo` pass run before the cg-rewrite
but that has few issues.
    
1. Initially I was thinking to use the memref op to carry the variable
attr. But as @tblah suggested in the #86939, it makes more sense to
carry that information on `DeclareOp`. It also makes it easy to handle
it in codegen and there is no special handling needed for arguments. For
this reason, we need to preserve the `DeclareOp` till the codegen.
    
2. Running earlier, we will miss the changes in passes that run between
cg-rewrite and codegen.
    
But not removing the DeclareOp in cg-rewrite has the issue that ShapeOp
remains and it causes errors during codegen. To solve this problem, I
convert DeclareOp to XDeclareOp in cg-rewrite instead of removing
it. This was mentioned as possible solution by @jeanPerier in
https://reviews.llvm.org/D136254
    
The conversion follows similar logic as used for other operators in that
file. The FortranAttr and CudaAttr are currently not converted but left
as TODO when the need arise.

Now `AddDebugInfo` pass can extracts information about local variables
from `XDeclareOp` and creates `DILocalVariableAttr`. These are attached
to `XDeclareOp` using `FusedLoc` approach. Codegen can use them to
create `DbgDeclareOp`.  I have added tests that checks the debug
information in mlir from and also in llvm ir.

Currently we only handle very limited types. Rest are given a place
holder type. The previous placeholder type was basic type with
`DW_ATE_address` encoding. When variables are added, it started
causing assertions in the llvm debug info generation logic for some
types. It has been changed to an interger type to prevent these issues
until we handle those types properly.
2024-05-15 15:20:27 +01:00
Tom Eccles
2f6c0e6e18 [flang][Alias Analysis] not all block arguments are dummy arguments (#92156)
Arguments to openmp regions should not be tagged as dummy arguments.
This is particularly unsafe because these openmp blocks will eventually
be inlined into the calling function, where they will trivially alias
with other values inside of the calling function.

This is probably a theoretical issue because the calls to openmp runtime
function calls would act as barriers, preventing optimizations that are
too aggressive. But a lot more thought would need to go into a bet like
that.

This came out of discussion on
https://github.com/llvm/llvm-project/pull/92036
2024-05-15 10:25:51 +01:00
jeanPerier
c7c5666aac [flang] Do not hoist all scalar sub-expressions from WHERE constructs (#91395)
The HLFIR pass lowering WHERE (hlfir.where op) was too aggressive in its
hoisting of scalar sub-expressions from LHS/RHS/MASKS outside of the
loops generated for the WHERE construct.
This violated F'2023 10.2.3.2 point 10 that stipulated that elemental
operations must be evaluated only for elements corresponding to true
values, because scalar operations are still elemental, and hoisting them
is invalid if they could have side effects (e.g, division by zero) and
if the MASK is always false (i.e., the loop body is never evaluated).

The difficulty is that 10.2.3.2 point 9 mandates that nonelemental
function must be evaluated before the loops. So it is not possible to
simply stop hoisting non hlfir.elemental operations.
Marking calls with an elemental/nonelemental attribute would not allow
the pass to be correct if inlining is run before and drops this
information, beside, extracting the argument tree that may have been
CSE-ed with the rest of the expression evaluation would be a bit
combursome.

Instead, lower nonelemental calls into a new hlfir.exactly_once
operation that will allow retaining the information that the operations
contained inside its region must be hoisted. This allows inlining to
operate before if desired in order to improve alias analysis.

The LowerHLFIROrderedAssignments pass is updated to only hoist the
operations contained inside hlfir.exactly_once bodies.
2024-05-14 13:34:46 +02:00
Kazu Hirata
f841ca0c35 Use StringRef::operator== instead of StringRef::equals (NFC) (#91864)
I'm planning to remove StringRef::equals in favor of
StringRef::operator==.

- StringRef::operator==/!= outnumber StringRef::equals by a factor of
  276 under llvm-project/ in terms of their usage.

- The elimination of StringRef::equals brings StringRef closer to
  std::string_view, which has operator== but not equals.

- S == "foo" is more readable than S.equals("foo"), especially for
  !Long.Expression.equals("str") vs Long.Expression != "str".
2024-05-12 23:08:40 -07:00
Andrew Gozillon
435e850ba9 [Flang][OpenMP][MLIR] Initial derived type member map support
This patch is one in a series of four patches that seeks to refactor
slightly and extend the current record type map support that was
put in place for Fortran's descriptor types to handle explicit
member mapping for record types at a single level of depth.

For example, the below case where two members of a Fortran
derived type are mapped explicitly:

''''
  type :: scalar_and_array
    real(4) :: real
    integer(4) :: array(10)
    integer(4) :: int
  end type scalar_and_array
  type(scalar_and_array) :: scalar_arr

  !$omp target map(tofrom: scalar_arr%int, scalar_arr%real)
''''

Current cases of derived type mapping left for future work are:
  > explicit member mapping of nested members (e.g. two layers of
     record types where we explicitly map a member from the internal
     record type)
  > Fortran's automagical mapping of all elements and nested elements
     of a derived type
  > explicit member mapping of a derived type and then constituient members
     (redundant in Fortran due to former case but still legal as far as I am aware)
  > explicit member mapping of a record type (may be handled reasonably, just
     not fully tested in this iteration)
  > explicit member mapping for Fortran allocatable types (a variation of nested
     record types)

This patch seeks to support this by extending the Flang-new OpenMP lowering to
support generation of this newly required information, creating the neccessary
parent <-to-> member map_info links, calculating the member indices and
setting if it's a partial map.

The OMPDescriptorMapInfoGen pass has also been generalized into a map
finalization phase, now named OMPMapInfoFinalization. This pass was extended
to support the insertion of member maps into the BlockArg and MapOperands of
relevant map carrying operations. Similar to the method in which descriptor types
are expanded and constituient members inserted.

Pull Request: https://github.com/llvm/llvm-project/pull/82853
2024-05-10 14:16:26 -05:00
Razvan Lupusoru
c5c8ae4354 [flang] Use i16 instead of i2 for cmdstat (#91647)
The code for preparing cmdstat was generating an i2 constant with value
0, casting it, and then storing it into i16 storage. Just generate i16
constant directly.
2024-05-10 07:45:35 -07:00
Slava Zakharin
1710c8cf0f [flang] Lowering changes for assigning dummy_scope to hlfir.declare. (#90989)
The lowering produces fir.dummy_scope operation if the current
function has dummy arguments. Each hlfir.declare generated
for a dummy argument is then using the result of fir.dummy_scope
as its dummy_scope operand. This is only done for HLFIR.

I was not able to find a reliable way to identify dummy symbols
in `genDeclareSymbol`, so I added a set of registered dummy symbols
that is alive during the variables instantiation for the current
function. The set is initialized during the mapping of the dummy
argument symbols to their MLIR values. It is reset right after
all variables are instantiated - this is done to avoid generating
hlfir.declare operations with dummy_scope for the clones of
the dummy symbols (e.g. this happens with OpenMP privatization).

If this can be done in a cleaner way, please advise.
2024-05-08 16:48:14 -07:00
Kareem Ergawy
24f5fc77d4 [flang][MLIR][OpenMP] Extend delayed privatization for arrays and characters (#85023) 2024-05-04 21:20:17 +02:00
Valentin Clement (バレンタイン クレメン)
f8a9973f8c [flang][cuda] Add verifier for cuda_alloc/cuda_free (#90983)
Adding a verifier to check the associated cuda attribute.
2024-05-03 11:25:34 -07:00
Valentin Clement (バレンタイン クレメン)
26060de063 [flang][cuda] Lower device/managed/unified allocation to cuda ops (#90623)
Lower locals allocation of cuda device, managed and unified variables to
fir.cuda_alloc. Add fir.cuda_free in the function context finalization.

@vzakhari For some reason the PR #90526 has been closed when I merged PR
#90525. Just reopening one.
2024-05-02 14:32:53 -07:00
Tom Eccles
d1b3648ed9 [flang] always run PolymorphicOpConversion sequentially (#90721)
It was pointed out in post commit review of
https://github.com/llvm/llvm-project/pull/90597 that the pass should
never have been run in parallel over all functions (and now other top
level operations) in the first place. The mutex used in the pass was
ineffective at preventing races since each instance of the pass would
have a different mutex.
2024-05-01 19:04:51 +01:00
Kareem Ergawy
0632cb38a6 [flang][MLIR] Outline deallocation logic to omp.private ops (#90592)
When delayed privatization is enabled, this PR emits the deallocation
logic to the newly introduced `dealloc` region on `omp.private` ops.
2024-05-01 06:21:30 +02:00
Slava Zakharin
986f832cff [flang] Added fir.dummy_scope operation to preserve dummy arguments association. (#90642)
The new operation is just an abstract attribute that is attached to
[hl]fir.declare operations of dummy arguments of a subroutine.
Dummy arguments of the same subroutine refer to the same
fir.dummy_scope, so they can be recognized as such during FIR AliasAnalysis.
Note that the fir.dummy_scope must be specific to the runtime
instantiation of a subroutine, so any MLIR inlining/cloning should duplicate and
unique it vs using the same fir.dummy_scope for different runtime instantiations.
This is why I made it an operation rather than an attribute.
The new operation uses a write effect on DebuggingResource, same as
[hl]fir.declare, to avoid optimizing it away.
2024-04-30 17:40:36 -07:00
Slava Zakharin
85f28cf431 [flang] Fixed -g compilation issues caused by #90484. (#90683) 2024-04-30 16:17:45 -07:00
David Truby
ecec1311fe [flang] Remove double pointer indirection for _QQEnvironmentDefaults (#90615)
A double pointer was being passed to the call to FortranStart rather than just a pointer to the EnvironmentDefaults.list. This now passes `null` directly when there's no EnvironmentDefaults.list and passes the list directly when there is, removing the original global variable which was a pointer to a pointer containing null or the EnvironmentDefaults.list global.

Fixes #90537
2024-04-30 22:38:36 +01:00
Kazu Hirata
52cb953789 [flang] Update calls to isa/dyn_cast/dyn_cast_or_null 2024-04-30 12:06:18 -07:00
Tom Eccles
df513f86da [flang] Adapt PolymorphicOpConversion to run on all top level ops (#90597)
We might use polymorphic ops in top-level operations other than
functions some time in the future. We need to ensure that these
operations can be lowered.

See RFC:

https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations

Some of the changes are from moving declaration and definition of the
constructor function into tablegen (as requested in code review when
altering another pass).
2024-04-30 17:23:46 +01:00
Abid Qadeer
91a8cb781d Reapply "[flang] Improve debug info for functions." with regression fixed. (#90484)
The original PR #90083 had to be reverted in PR #90444 as it caused one
of the gfortran tests to fail. The issue was using `isIntOrIndex` for
checking for integer type. It allowed index type which later caused
assertion when calling `getIntOrFloatBitWidth`. I have now replaced it
with `isInteger` which should fix this regression.
2024-04-30 08:25:54 +01:00
David Truby
8d5386669e [flang] Generate main only when a Fortran program statement is present (#89938)
This patch changes the behaviour for flang to only create and link to a
`main` entry point when the Fortran code has a program statement in it.

This means that flang-new can be used to link even when the program is
a mixed C/Fortran code with `main` present in C and no entry point
present in Fortran.

This also removes the `-fno-fortran-main` flag as this no longer has any
functionality.
2024-04-29 14:16:25 +01:00
David Spickett
0edb5c3be5 Revert "[flang] Improve debug info for functions." (#90444)
Reverts llvm/llvm-project#90083 due to a test suite failure:
https://lab.llvm.org/buildbot/#/builders/184/builds/11961

```
flang-new: ../llvm/mlir/lib/IR/Types.cpp:126: unsigned int mlir::Type::getIntOrFloatBitWidth() const: Assertion `isIntOrFloat() && "only integers and floats have a bitwidth"' failed.
```
2024-04-29 11:02:16 +01:00
Abid Qadeer
f029da5cfc [flang] Improve debug info for functions. (#90083)
This PR improves the debug information for functions in the following
ways:

1. Get line number information from FuncOp and remove hard-coded line
numbers.
2. Use proper type for function signature. I have a added a type
converter. Currently, it is very limited but will be enhanced with time.
3. Use de-constructed function name.
2024-04-29 09:44:19 +01:00
Christian Sigg
bd9fdce69b [flang] Use isa/dyn_cast/cast/... free functions. (#90432)
The corresponding member functions are deprecated.
2024-04-29 09:16:22 +02:00
Kareem Ergawy
3785d74246 [flang][OpenMP][LLVMIR] Support CFG and LLVM IR conversion for `omp.p… (#90164)
…rivate`

Adds support for CFG conversion and conversion to LLVM IR for
`omp.private` ops. This bridges a gap between FIR and LLVM to provide
more support for lowering `omp.private` ops for things like
allocatables.
2024-04-29 05:46:37 +02:00
Christian Sigg
fac349a169 Reapply "[mlir] Mark isa/dyn_cast/cast/... member functions depreca… (#90406)
…ted. (#89998)" (#90250)

This partially reverts commit 7aedd7dc75.

This change removes calls to the deprecated member functions. It does
not mark the functions deprecated yet and does not disable the
deprecation warning in TypeSwitch. This seems to cause problems with
MSVC.
2024-04-28 22:01:42 +02:00
dyung
7aedd7dc75 Revert "[mlir] Mark isa/dyn_cast/cast/... member functions deprecated. (#89998)" (#90250)
This reverts commit 950b7ce0b8.

This change is causing build failures on a bot
https://lab.llvm.org/buildbot/#/builders/216/builds/38157
2024-04-26 12:09:13 -07:00
Christian Sigg
950b7ce0b8 [mlir] Mark isa/dyn_cast/cast/... member functions deprecated. (#89998)
See https://mlir.llvm.org/deprecation and
https://discourse.llvm.org/t/preferred-casting-style-going-forward.
2024-04-26 16:28:30 +02:00
Tom Eccles
213ab9610c [flang][NFC] Use tablegen to reduce MemoryAllocationOpt boilerplate (#90062)
This is another one that runs on functions but isn't appropriate to also
run on other top level operations. It needs to find all paths that
return from the function to free heap allocated memory. There isn't a
generic concept for general top level operations which is equivalent to
looking for function returns.

I removed the manual definition of the options structure because there
is already an identical definition in tablegen and the options are
documented in Passes.td.
2024-04-26 10:56:56 +01:00
Tom Eccles
46b66dfd31 [flang][NFC] use tablegen to create StackArrays constructor (#90038)
Stack arrays needs to stay running only on func.func because it needs to
know which block terminators can end the function (rather than just
branch between unstructured control flow). A similar concept does not
exist at the more abstract level of "any top level mlir operation".

For example, it currently looks for func::ReturnOp and
fir::UnreachableOp as points when execution can end. If this were to be
run on omp.declare_reduction, it would also need to understand
omp.YieldOp (perhaps only when omp.declare_reduction is the parent).
There isn't a generic concept in MLIR for this.
2024-04-26 10:55:56 +01:00
Tom Eccles
08dc03c570 [flang][NFC] Use tablegen to create LoopVersioning constructor (#90037)
The pass is currently defined as only considering function arguments as
candidates for the optimization. I would prefer to generalise the pass
for other top level operations only when there is a concrete use case
before making too many assumptions about the current set of top level
operations. Therefore I have not adapted this pass to run on all top
level operations.
2024-04-26 10:54:46 +01:00
Tom Eccles
81442f8d97 [flang][NFC] Use tablegen to create SimplifyIntrinsics constructor (#89963)
This pass runs on ModuleOp, internally walking all func::CallOps so it
shouldn't need anything special to work on other top level operations.
2024-04-25 10:26:05 +01:00
Tom Eccles
92f4f0b061 [flang][NFC] Use tablegen to create simplifyRegionLite constructor (#89957)
This is a ModuleOp pass anyway so it doesn't need to be run on
particular top level operations.
2024-04-25 10:25:46 +01:00
Tom Eccles
7bc0177fc7 [flang] run character conversion pass on all top level ops (#89910)
See RFC:

https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations

Some of the changes are from moving declaration and definition of the
constructor function into tablegen (as requested in code review when
altering another pass).
2024-04-25 10:25:31 +01:00
jeanPerier
3abcd5f282 [flang][openmp] fix OMPFunctionFiltering pass after #87796 (#89776)
The pass assumed that all fun.func symbol usages could be safely
replaced by undef, that is not true after #87796 that added a back link
from internal procedure back to the parent procedure. This caused the
internal procedures to be erased and then processed (segfault).

Also set visibility of such internal procedures so that MLIR do not remove them before
the target function is generated for the target region.
2024-04-24 17:37:40 +02:00
jeanPerier
3328ccf689 [flang] lower SHAPE intrinsic (#89785)
Semantics usually fold SHAPE into an array constructor, but sometimes it
cannot (like when the source is a function result that cannot be
duplicated in expression analysis). Add lowering handling for shape.
2024-04-24 15:30:45 +02:00
Tom Eccles
ceca523581 [flang] de-duplicate CFGConversion pass (#89783)
See RFC at
https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations

I previously did the same for the AbstractResult pass
https://github.com/llvm/llvm-project/pull/88867
2024-04-24 10:29:53 +01:00
Abid Qadeer
5f3f9d1a90 [flang] Remove hardcoded bits from AddDebugInfo. (#89231)
This PR adds following options to the AddDebugInfo pass.

1. IsOptimized flag.
2. Level of debug info to generate.
3. Name of the source file

This enables us to remove the hard coded values from the code. It also
allows us to test the pass with different options. The tests have been
modified to take advantage of that.

The calling convention flag and producer name have also been improved.
2024-04-23 19:11:43 +01:00
Tom Eccles
8cc34fadec [flang][OpenMP] Support reduction of allocatable variables (#88392)
Both arrays and trivial scalars are supported. Both cases must use
by-ref reductions because both are boxed.

My understanding of the standards are that OpenMP says that this should
follow the rules of the intrinsic reduction operators in fortran, and
fortran says that unallocated allocatable variables can only be
referenced to allocate them or test if they are already allocated.
Therefore we do not need a null pointer check in the combiner region.
2024-04-23 10:34:28 +01:00
Iman Hosseini
7c20576cc3 [flang][cuda] fix parsing of cuda_kernel (#89613)
Fix parsing of cuda_kernel: it missed a mlir::succeeded check and it was
not setting up the `types` and causing mismatch between values and types
of the grid/block (CUFKernelValues). @clementval

---------

Co-authored-by: Iman Hosseini <imanh@nvidia.com>
Co-authored-by: Valentin Clement (バレンタイン クレメン) <clementval@gmail.com>
2024-04-22 10:16:59 -07:00
Tom Eccles
bfd19445c3 [flang] de-duplicate AbstractResult pass (#88867)
This is the first proof of concept of the modification of FIR codegen to
fully support a variety of top level operations (beyond just func.func)
proposed in
https://discourse.llvm.org/t/rfc-add-an-interface-for-top-level-container-operations
2024-04-22 10:11:09 +01:00
Christian Sigg
57b2679051 Mark mlir::Value::isa/dyn_cast/cast/... member functions deprecated. (#89238)
See https://mlir.llvm.org/deprecation and
https://discourse.llvm.org/t/preferred-casting-style-going-forward/68443/4
2024-04-22 10:05:37 +02:00
Jeff Niu
e553ac4d81 [mlir][llvm] Port overflowFlags to a native operation property (RELAND) (#89410)
This PR changes the LLVM dialect's IntegerOverflowFlags to be stored on
operations as native properties.

Reland to fix flang
2024-04-19 09:23:00 -07:00
abidh
6d9ee88599 [flang] Rename AddDebugFoundation to AddDebugInfo (NFC) (#89191)
These changes were missed in PR #88526.
2024-04-18 13:54:53 +01:00