7031 Commits

Author SHA1 Message Date
Andrew Gozillon
171d8c4028 [Flang][OpenMP][MLIR] Fix memory leak caused by D149368 causing sanitizer error and fix iterator invalidation error
This patch fixes two issues introduced by the D149368 patch, one is
a memory leak from using the removeFromParent rather
than eraseFromParent (the erase also had to be moved to not create
use after deletes).

And the other is a possible iterator invalidation bug, better to be safe
than sorry.
2023-09-20 22:28:11 -05:00
Andrew Gozillon
76916669b9 [MLIR][OpenMP] Initial Lowering of Declare Target for Data
This patch adds initial lowering for DeclareTargetAttr on
GlobalOp's utilising registerTargetGlobalVariable
and getAddrOfDeclareTargetVar from the
OMPIRBuilder.

It also adds initial processing of declare target map
operands, populating the combinedInfo that the
OMPIRBuilder requires to generate kernels and
it's kernel argument structure.

The combination of these additions allows simple mapping
of declare target globals to Target regions, as such a simple
runtime test showcasing this and testing it has been added.

The patch currently does not factor in filtering
based on device_type clauses (e.g. no emission of
globals for device if host specified), this will come in
a future iteration. And for the moment it's only been
tested with 1-D arrays and basic fortran data types,
more complex types (such as user defined derived
types from Fortran, allocatables or Fortran pointers)
may need further work.

reviewers: kiranchandramohan, skatrak

Differential Revision: https://reviews.llvm.org/D149368
2023-09-20 13:31:15 -05:00
Slava Zakharin
69d9ad1cee
[flang][hlfir] Fixed cleanup code placement indeterminism in OrderedAssignments. (#66811)
I had to remove test3() case in 73086dab9e9870f49f3a9cc2763a1e8b1a20aa20
to fix the buildbots. This patch brings it back with proper fix.
2023-09-20 08:34:11 -07:00
Kiran Chandramohan
c366e61b59
[Flang][OpenMP] Move FIR lowering tests to a separate directory (#66779)
As part of preparing for the switch to HLFIR lowering, all OpenMP FIR
lowering tests are moved to a subdirectory in the OpenMP directory.
Copies of these tests that work with HLFIR will be created during this
week. After the switch the FIR lowering tests will be removed.

All new tests should be added with the HLFIR flow.
2023-09-20 11:11:07 +01:00
jeanPerier
1062c140f8
[flang] Prevent IR name clashes between BIND(C) and external procedures (#66777)
Defining a procedure with a BIND(C, NAME="...") where the binding label
matches the assembly name of a non BIND(C) external procedure in the
same file causes a failure when generating the LLVM IR because of the
assembly symbol name clash.

Prevent this crash with a clearer semantic error.
2023-09-20 10:00:28 +02:00
Peter Klausler
682270877d
[flang] Correct handling of assumed-rank allocatables in ALLOCATE (#66718)
Construct entities that are associations from selectors in ASSOCIATE,
CHANGE TEAMS, and SELECT TYPE constructs do not have the ALLOCATABLE or
POINTER attributes, even when associating with allocatables or pointers;
associations from selectors in SELECT RANK constructs do have those
attributes.
2023-09-19 12:27:35 -07:00
Slava Zakharin
73086dab9e Revert "Revert "[flang][hlfir] Fixed assignment/finalization order for user-defined assignments. (#66736)""
This reverts commit 775754e32840c6b6ca64c8bc0b7ae2c778b97d1e.

Relanding with removing part of the LIT test. There seems to be operations
ordering indeterminism that is unrelated to my change. I will address this
issue separately.
2023-09-19 11:40:58 -07:00
Slava Zakharin
775754e328 Revert "[flang][hlfir] Fixed assignment/finalization order for user-defined assignments. (#66736)"
This reverts commit a9a1f849a99ea1d7c44fe0419f6b11c0e5b13495.
2023-09-19 11:31:58 -07:00
Renaud Kauffmann
575a648306
[flang] Follow memory source through more operations (#66713)
Add support for fir.box_addr, fir.array_corr, fir.coordinate, fir.embox,
fir.rebox and fir.load.

1) Through the use of boolean `followBoxAddr` determine whether the
analysis should apply to the address of the box or the address wrapped
by the box.
2) Some asserts have been removed to allow for more SourceKinds though
the flow, in a particular SourceKind::Direct
3) getSource was a public method but the returned type (SourceKind) was
not public making it impossible to be called publicly
4) About 12 tests have been added to check for real Fortran scenarios 
5) More tests will be added with HLFIR
6) A few TODOs have been identified and will need to be addressed in
follow-up patches. I felt that more changes would increase the
complexity of the patch.
2023-09-19 11:10:49 -07:00
Slava Zakharin
a9a1f849a9
[flang][hlfir] Fixed assignment/finalization order for user-defined assignments. (#66736)
This patch places the finalization code for the RHS of a user-defined
assignment after the assignment code. The change only affects
standalone RegionAssignOp operations.
2023-09-19 10:57:40 -07:00
Matthias Springer
9b5ef2bea8
[mlir][Interfaces] LoopLikeOpInterface: Support ops with multiple regions (#66754)
This commit implements `LoopLikeOpInterface` on `scf.while`. This
enables LICM (and potentially other transforms) on `scf.while`.

`LoopLikeOpInterface::getLoopBody()` is renamed to `getLoopRegions` and
can now return multiple regions.

Also fix a bug in the default implementation of
`LoopLikeOpInterface::isDefinedOutsideOfLoop()`, which returned "false"
for some values that are defined outside of the loop (in a nested op, in
such a way that the value does not dominate the loop). This interface is
currently only used for LICM and there is no way to trigger this bug, so
no test is added.
2023-09-19 17:35:38 +02:00
David Truby
5f476b80e3
[flang] Add comdats to functions with linkonce linkage (#66516)
This fixes a bug where functions generated by the MLIR Math dialect, for
example ipowi, would fail to link with link.exe on Windows due to having
linkonce linkage but no associated comdat. Adding the comdat on ELF also
allows linkers to perform better garbage collection in the binary.

Simply adding comdats to all functions with this linkage type should
also cover future cases where linkonce or linkonce_odr functions might
be necessary.
2023-09-19 15:00:04 +01:00
Andrew Gozillon
eaa0d281b6 [Flang][MLIR][OpenMP] Update OMPEarlyOutlining to support Bounds, MapEntry and declare target globals
This patch is a required change for the device side IR to
maintain apporpiate links for declare target variables to
their global variables for later lowering.

It is also a requirement to clone over map bounds and
entry operations to maintain the correct information for
later lowering of the IR.

It simply tries to clone over the relevant information
maintaining the appropriate links they would have
maintained prior to the pass, rather than redirecting
them to new function arguments which causes a
loss of information in the case of Declare Target
and map information.

Depends on D158734

reviewers: TIFitis, razvanlupusoru

Differential Revision: https://reviews.llvm.org/D158735
2023-09-19 08:26:46 -05:00
Andrew Gozillon
8fde6f41a0 [Flang][OpenMP] Add lowering from PFT to new MapEntry and Bounds operations and tie them to relevant Target operations
This patch builds on top of a prior patch in review which adds a new map
and bounds operation by modifying the OpenMP PFT lowering to support
these operations and generate them from the PFT.

A significant amount of the support for the Bounds operation is borrowed
from OpenACC's own current implementation and lowering, just ported
over to OpenMP.

The patch also adds very preliminary/initial support for lowering to
a new Capture attribute, which is stored on the new Map Operation,
which helps the later lowering from OpenMP -> LLVM IR by indicating
how a map argument should be handled. This capture type will
influence how a map argument is accessed on device and passed by
the host (different load/store handling etc.). It is reflective of a
similar piece of information stored in the Clang AST which performs a
similar role.

As well as some minor adjustments to how the map type (map bitshift
which dictates to the runtime how it should handle an argument) is
generated to further support more use-cases for future patches that
build on this work.

Finally it adds the map entry operation creation and tying it to the relevant
target operations as well as the addition of some new tests and alteration
of previous tests to support the new changes.

Depends on D158732

reviewers: kiranchandramohan, TIFitis, clementval, razvanlupusoru

Differential Revision: https://reviews.llvm.org/D158734
2023-09-19 08:26:46 -05:00
Peter Klausler
fba474d3d6
[flang] Fix CFI_CDESC_T(rank) for C (#66260)
The CFI_CDESC_T(rank) macro defined for C (not C++) in
ISO_Fortran_binding.h incorporates a cdesc_t structure as a member,
which works for data layout but doesn't allow for direct access to its
members. (The C++ definition can use inheritance.)

Restructure the definitions in that header file so that
CFI_CDESC_T(rank) for C defines a struct with the expected members.
2023-09-18 13:00:42 -07:00
Peter Klausler
f89d2beb23
[flang] Prioritize DATA object error messages a little better (#66258)
When a DATA statement object is not valid, there's a number of possible
reasons. Emit an error message for the most egregious violation, so that
an unlucky user doesn't fix something easy (due to a less-severe error
message masking one that is worse) and then run into something that
might be more serious.
2023-09-18 12:49:33 -07:00
Peter Klausler
61d361d69b
[flang] Extension: accept NULL([MOLD=]) for ALLOCATABLE INTENT(IN) du… (#66256)
…mmy argument

Several compilers accept a null pointer (with or without a MOLD=) as an
actual argument for association with an INTENT(IN) allocatable dummy
argument. At runtime, the allocatable dummy argument appears to be in
the unallocated state. This seems useful, unambiguous, unlikely to
invalidate conforming code, and works with Intel, NAG, & XLF, so it
should be supported with an optional portability warning in this
compiler as well.
2023-09-18 12:37:46 -07:00
Peter Klausler
94b4a98681
[flang] Fix bogus error w/ COMMON & EQUIVALENCE (#66254)
Semantic checking of COMMON blocks and EQUIVALENCE sets has an
assumption that the base storage sequence object of each COMMON block
object will also be in that COMMON block's list of objects, and emits an
error message when this is not the case. This assumption is faulty; it
is possible for a base object to have its COMMON block set during offset
assignment.

Fixes https://github.com/llvm/llvm-project/issues/65922.
2023-09-18 12:24:19 -07:00
Peter Klausler
7991078632
[flang] Allow empty SEQUENCE types (#66252)
The Fortran standards require (F'2023 C745) that a derived type with the
SEQUENCE attribute have at least one component. No Fortran compiler
actually enforces this constraint. Accept this usage with a warning.
2023-09-18 12:11:35 -07:00
Peter Klausler
b76e08db9c
[flang] Accept NULL() actual for optional allocatable dummy argument (#66251)
A NULL() pointer is an acceptable actual argument for association with
an (absent) optional allocatable dummy argument. Semantics was
unconditionally emitting an error that the actual argument is not
allocatable.
2023-09-18 11:54:27 -07:00
Peter Klausler
338e312503
[flang] Fix bogus warning about missing FINAL on assumed-rank (#66250)
The warning message about a derived type not having a FINAL subroutine
for a particular object's rank should not issue for an assumed-rank
dummy argument.
2023-09-18 11:40:55 -07:00
Peter Klausler
15c93766a5
[flang] Fix characterization of result of function returning procedur… (#66248)
…e pointer

The procedure characterization package correctly characterizes the
result of a reference to a function that returns a procedure pointer. In
the event of a result that is a pointer to a function that itself is a
procedure pointer, the code in pointer assignment semantics checking was
mistakenly using that result's procedure characteristics as the
characteristics of the original function reference. This is just wrong;
delete it.
2023-09-18 11:24:51 -07:00
Slava Zakharin
10388c74e2
[flang] Fixed -Werror build after #65919. (#66679) 2023-09-18 11:10:07 -07:00
Kazu Hirata
1645b5d321 [flang] Remove unused function IsListDirectedFieldComplete
This patch fixes a warning:

  flang/runtime/edit-input.cpp:27:20: error: unused function
  'IsListDirectedFieldComplete' [-Werror,-Wunused-function]
2023-09-18 10:45:25 -07:00
Peter Klausler
e18fa6eea0
[flang] Fix compile-time infinite loop (#66246)
When a COMMON block object has a derived type that is part of a set of
mutually-dependent types with other members, the compiler loops.

Fixes https://github.com/llvm/llvm-project/issues/65572.
2023-09-18 10:20:05 -07:00
Slava Zakharin
47025af639
[flang][hlfir] Alias analysis for host associated accesses. (#65919)
This patch adds `host_assoc` attribute for operations that implement
FortranVariableInterface (e.g. `hlfir.declare`). The attribute is used
by the alias analysis to make better conclusions about memory overlap.
For example, a dummy argument of an inner subroutine and a host's
variable used inside the inner subroutine cannot refer to the same
object (if the dummy argument does not satisify exceptions in F2018
15.5.2.13).
This closes a performance gap between HLFIR optimization pipeline
and FIR ArrayValueCopy for Polyhedron/nf.
2023-09-18 09:59:06 -07:00
Peter Klausler
37ea42b22c
[flang][runtime] Enforce proper termination of list-directed input va… (#66244)
…lues

Emit an error at runtime when a list-directed input value is not
followed by a value separator or end of record. Previously, the runtime
I/O library was consuming as many input characters that were valid for
the type of the value, and leaving any remaining characters for the next
input edit, if any.
2023-09-18 09:57:28 -07:00
Peter Klausler
fa44ec72d6
[flang] Allow continuation of D lines (#66242)
When the old extension of D debug lines in fixed form source is enabled,
recognize continuation lines that begin with a D.

(No test is added, since the community's driver doesn't support the GNU
Fortran option -fd-lines-as-code.)
2023-09-18 09:37:01 -07:00
Peter Klausler
9bbec0ad42
[flang] Fix SIZEOF() expression rewriting (#66241)
The rewriting of the extension intrinsic function SIZEOF was producing
results that would reference symbols that were not available in the
current scope, leading to crashes in lowering. The symbols could be
function result variables, for SIZEOF(func()), or bare derived type
component names, for SIZEOF(array(n)%component). Fixing this without
regressing on a current test case involved careful threading of some
state through the TypeAndShape characterization code and the
shape/bounds analyzer, and some clean-up was done along the way.
2023-09-18 09:20:50 -07:00
Peter Klausler
39f1860dcc
[flang] Fold NORM2() (#66240)
Fold references to the (relatively new) intrinsic function NORM2 at
compilation time when the argument(s) are all constants. (Getting this
done right involved some changes to the API of the accumulator function
objects used by the DoReduction<> template, which rippled through some
other reduction function folding code.)
2023-09-18 08:58:19 -07:00
Peter Klausler
79c430787f
[flang][runtime] INQUIRE(UNIT=-666, EXIST=x) should be .FALSE. (#66239)
The runtime implementation for INQUIRE(EXIST=x) is returning .TRUE. for
all non-existent unit, which is incorrect for valid unit numbers.
2023-09-18 08:38:55 -07:00
Peter Klausler
f025e41174
[flang] Accept pointer-valued function results as ASSOCIATED() arguments (#66238)
The POINTER= and TARGET= arguments to the intrinsic function
ASSOCIATED() can be the results of references to functions that return
object pointers or procedure pointers. NULL() was working well but not
program-defined pointer-valued functions. Correct the validation of
ASSOCIATED() and extend the infrastructure used to detect and
characterize procedures and pointers.
2023-09-18 08:22:18 -07:00
Kiran Chandramohan
e2733a6767
[Flang][OpenMP] Add trivial conversion pattern for omp.ordered_region (#66085)
Fixes #65570
2023-09-18 16:05:17 +01:00
Sergio Afonso
fb4bdf361f
[Flang][OpenMP] Run Flang-specific OpenMP MLIR passes in bbc
This patch moves the group of OpenMP MLIR passes using after lowering of
Fortran to MLIR into a pipeline to be shared by `flang-new` and `bbc`.
Currently, the `bbc` tool does not produce the expected FIR for offloading-
enabled OpenMP codes due to not running these passes.

Unit tests exercising these passes are updated to check `bbc` output as well.
2023-09-18 14:10:04 +01:00
jeanPerier
99a54b839a
[flang] Lower PRIVATE component names safely (#66076)
It is possible for a derived type extending a type with private
components to define components with the same name as the private
components.

This was not properly handled by lowering where several fir.record type
component names could end-up being the same, leading to bad generated
code (only the first component was accessed via fir.field_index, leading
to bad generated code).

This patch handles the situation by adding the derived type mangled name
to private component.
2023-09-18 14:59:56 +02:00
jeanPerier
79508db494
[flang][runtime] zero size allocation in source allocation (#66124)
Source allocation with a zero sized array is legal, and the resulting
allocatable/pointer should be allocated/associated.

The current code skipped the actual allocation, leading the allocatable
or pointer to look unallocated/disassociated.
2023-09-18 08:51:07 +02:00
Yusuke MINATO
4eafb5f57c
[flang][hlfir] Add hlfir.minval intrinsic (#66306)
Adds a new HLFIR operation for the MINVAL intrinsic according to the
design set out in flang/docs/HighLevelFIR.md.
2023-09-15 18:30:06 +09:00
Arthur Eubanks
0a1aa6cda2
[NFC][CodeGen] Change CodeGenOpt::Level/CodeGenFileType into enum classes (#66295)
This will make it easy for callers to see issues with and fix up calls
to createTargetMachine after a future change to the params of
TargetMachine.

This matches other nearby enums.

For downstream users, this should be a fairly straightforward
replacement,
e.g. s/CodeGenOpt::Aggressive/CodeGenOptLevel::Aggressive
or s/CGFT_/CodeGenFileType::
2023-09-14 14:10:14 -07:00
Slava Zakharin
b7d02d7e12
[flang] Select proper library APIs for derived type io. (#66327)
This patch syncs the logic inside `getInputFunc` that selects
the library API and the logic in `createIoRuntimeCallForItem`
that creates the input arguments for the library call.
There were cases where we selected `InputDerivedType` API
and passed only two arguments, and also we selected `InputDescriptor`
and passed three arguments.
It turns out we also were incorrectly selecting `OutputDescriptor`
in `getOutputFunc` (`test4` case in the new LIT test),
which caused runtime issues for output of a derived type
with descriptor components (due to the missing non-type-bound table).
2023-09-14 07:58:26 -07:00
Slava Zakharin
8730fe95d8
[flang] Do not finalize main program variables. (#66326) 2023-09-14 07:58:01 -07:00
Sergio Afonso
29aa749087
[OpenMP][Flang][MLIR] Lowering of OpenMP requires directive from parse tree to MLIR
This patch implements the lowering of the OpenMP 'requires' directive
from Flang parse tree to MLIR attributes attached to the top-level
module.

Target-related 'requires' clauses are gathered and combined for each top-level
unit during semantics. Lastly, a single module-level `omp.requires` attribute
is attached to the MLIR module with that information at the end of the process.

The `atomic_default_mem_order` clause is not addressed by this patch, but
rather it will come as a separate patch and follow a different approach.

Depends on D147214, D150328, D150329 and D157983.

Differential Revision: https://reviews.llvm.org/D147218
2023-09-14 10:34:54 +01:00
Valentin Clement (バレンタイン クレメン)
b171849afe
[flang][openacc] Use OpenACC terminator instead of fir.unreachable after Stop stmt (#66325)
This follow an update made on OpenMP https://reviews.llvm.org/D129969
and was not possible on OpenACC until the unstructured construct was
supported.
2023-09-13 22:28:02 -07:00
Valentin Clement (バレンタイン クレメン)
16cf9c9af0
[flang][openacc] Support labeled DO loop after acc combined directive (#66296)
This patch adds support for labeled do loop after combined directive. It
relaxes the initial parser and canonicalize labeled do loop into the
OpenACCCombinedConstruct.
2023-09-13 20:50:42 -07:00
Valentin Clement (バレンタイン クレメン)
bc32346082
[flang][openacc] Support labeled DO loop after acc loop directive (#66294)
Make the `DoConstruct` in `OpenACCLoopConstruct` optional and move the
labeled do construct in in the canonicalization step.
2023-09-13 20:50:20 -07:00
Valentin Clement (バレンタイン クレメン)
b1341e2863
[flang][openacc] Fix unstructured code in OpenACC region ops (#66284)
For unstructured construct, the blocks are created in advance inside the
function body. This causes issues when the unstructured construct is
inside an OpenACC region operations. This patch adds the same fix than
OpenMP lowering and re-create the blocks inside the op region.

Initial OpenMP fix: 29f167abcf7d871d17dd3f38f361916de1a12470
2023-09-13 20:49:54 -07:00
Peter Klausler
f82ee15554
[flang] Don't check dummy vs. actual result rank for assumed-rank poi… (#66237)
…nters

When associating a function result pointer as an actual argument with a
dummy pointer that is assumed-rank, don't emit a bogus error.
2023-09-13 16:52:15 -07:00
Peter Klausler
daa5da063a
[flang] Don't blow up when combining mixed COMPLEX operations (#66235)
Expression processing applies some straightforward rewriting of mixed
complex/real and complex/integer operations to avoid having to promote
the real/integer operand to complex and then perform a complex
operation; for example, (a,b)+x becomes (a+x,b) rather than (a,b)+(x,0).
But this can blow up the expression representation when the complex
operand cannot be duplicated cheaply. So apply this technique only to
complex operands that are appropriate to duplicate.

Fixes https://github.com/llvm/llvm-project/issues/65142.
2023-09-13 16:34:23 -07:00
Peter Klausler
4fed595997
[flang] Correct semantic representation & handling of RANK(*) (#66234)
A RANK(*) case in a SELECT RANK construct selects the case of an
assumed-rank dummy argument whose effective actual argument is an
assumed-size array. In this case, the attributes of the selector are
those of a rank-1 assumed-size array, and the selector cannot be
allocatable or a pointer.

Ensure that the representation of a SELECT RANK construct's per-case
AssocEntityDetails can distinguish RANK(n), RANK(*), and RANK DEFAULT,
and clean up various code sites and tests where the distinctions matter.
2023-09-13 16:13:31 -07:00
Peter Klausler
d34f5dd07a
[flang] Accept ALLOCATED(ARRAY=assumedRank) (#66233)
The definitions of the ALLOCATED intrinsic in the intrinsics table did
not allow for an assumed-rank array.
2023-09-13 15:55:40 -07:00
Peter Klausler
8ddedbf2f8
[flang] Assume unknown target of procedure pointer assignment is a pr… (#66232)
…ocedure

When an previously unknown name appears as the target of an assignment
to a known procedure pointer, create an external symbol for it rather
than an implicitly-typed object symbol.
2023-09-13 15:47:31 -07:00