Commit Graph

5091 Commits

Author SHA1 Message Date
Valentin Clement e657acd449
[flang] Handle NULL(mold) used in initializer region
NULL intrinsic with a MOLD argument can be used in a type constructor.
This patch handles this use case with a specific lowering that create
an unallocated box with the MOLD type.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D134554
2022-09-24 15:23:08 +02:00
Valentin Clement de3efd1b4c
[flang] Lower character result of bind(c) function by value
BIND(C) Function returning character must return it by value and
not as hidden argument like done currently. This patch update the
code to return it by value for both use cases.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D134530
2022-09-24 09:00:26 +02:00
Peter Klausler 3bc334b31a [flang] Fix crash in semantics in error recovery
When a FUNCTION statement has both an explicit type in its prefix
and a RESULT clause in its suffix, semantics crashes due to the
redundant type; emit a nice error message instead.

Differential Revision: https://reviews.llvm.org/D134504
2022-09-23 17:45:35 -07:00
Peter Klausler 3bbb2c2d99 [flang] Preserve component array lower bounds in folding
When a component array of a named constant is extracted as
a constant value, ensure that the lower bounds of the array
are properly acquired from the declaration of the component.

Differential Revision: https://reviews.llvm.org/D134499
2022-09-23 17:13:05 -07:00
Slava Zakharin a707675dbb [flang][runtime] Enabled HAS_FLOAT128 for builds with clang.
I am building with clang, and I noticed for quite a while that
REAL(16) runtimes functions are not available. I did not know why
until I saw this typo.

Differential Revision: https://reviews.llvm.org/D134503
2022-09-23 16:20:09 -07:00
Peter Klausler 166563fdc7 [flang] Fix spurious error with COMMON and EQUIVALENCE
f18 emits an error message when two objects related by EQUIVALENCE
to a third are specified as members of a COMMON block.  This is not
always a sign of an error, however; it is possible for multiple objects
in a COMMON block to all be equivalenced to distinct offsets in another
object in a way that is consistent.  So refine the check.

Differential Revision: https://reviews.llvm.org/D134485
2022-09-23 15:32:39 -07:00
Peter Klausler 19d11b4d9d [flang] Allow "non-expandable" scalars in single-element contexts
When a scalar expression is not expandable -- i.e., it would have to be
evaluated once and saved in a temporary to avoid changing the semantics
of the program if it were to be evaluated more than once -- it affects
some aspects of folding and expression semantics.  In cases where
scalar expansion would not cause multiple evaluations due to the shape
of the result having but a single element, however, these "non-expandable"
scalar expressions can be safely allowed.

Differential Revision: https://reviews.llvm.org/D134476
2022-09-23 15:26:33 -07:00
Peter Klausler dd41453f1b [flang] Allow a generic-spec on a PUBLIC/PRIVATE statement to declare a generic
A generic-spec can appear on a module accessibility control statement
even if it has not been declared as a generic interface, because there's
nothing else that it could be.

While here, simplify the parse tree and parser for AccessId, since
one of its alternatives is ambiguous with the other.

Differential Revision: https://reviews.llvm.org/D134471
2022-09-23 14:17:42 -07:00
Peter Klausler 9489699438 [flang] Fold IS_CONTIGUOUS() to .FALSE. when it is known to be
At present, IS_CONTIGUOUS() can only either fold to .TRUE. or
remain unknown.  The underlying analysis, however, is capable
of returning a tri-state result (true, false, or unknown).
Extend and expose it to folding so that IS_CONTIGUOUS() can
fold to .FALSE. as well as to .TRUE. when contiguity is
known.

Differential Revision: https://reviews.llvm.org/D134466
2022-09-23 14:16:26 -07:00
Peter Klausler c7285cd09f [flang] Don't emit portability warnings for things in module files
Any symbol in a module file will have been already shamed with
portability warnings when the module was compiled, so don't pile
on when compiling other program units that use the module.
This also silences warnings about some symbols whose names were
created or extended by the compiler to avoid clashes.

Differential Revision: https://reviews.llvm.org/D134455
2022-09-23 13:45:09 -07:00
Peter Klausler bfc3907369 [flang] Fix spurious errors from MODULE subprograms
When an explicit MODULE procedure is defined in the same (sub)module
as its interface, and the interface was defined in a generic
interface of the same name, bogus errors about symbols already
having been defined will ensue.  Cleaning up this aspect of name
resolution and symbol table management requires marking the
place-holding SubprogramNameDetails symbols of explicit MODULE
subprograms as such, ensuring that that attribute is not inherited
if the SubprogramNameDetails symbol is recycled as a SubprogramDetails,
and gathering some code that should have been common between
BeginSubprogram() and BeginMpSubprogram() together in one
new routine.

Differential Revision: https://reviews.llvm.org/D134446
2022-09-23 13:12:23 -07:00
Peter Klausler 0ba2a3c44f [flang] Replace negative known CHARACTER length with zero in type analysis
When a DynamicType for CHARACTER has a known length, correct a negative
length value to its effective length of zero so that all such types
compare equal in interface compatibility checking.

Differential Revision: https://reviews.llvm.org/D134405
2022-09-23 12:22:19 -07:00
Peter Klausler 086234927a [flang] A separate module procedure must inherit interface attributes
Attributes like PURE, ELEMENTAL, &c. are specified on the interface of
a separate module procedure, so when the MODULE PROCEDURE is defined in
a submodule, it must acquire those attributes from the interface.

Differential Revision: https://reviews.llvm.org/D134403
2022-09-23 11:43:38 -07:00
Peter Klausler 3b7b7fa713 [flang] Accept a separate module procedure interface as a specific procedure
The code snippet

  module m
    interface
      module subroutine specific
      end subroutine
    end interface
    interface generic
       module procedure specific
    end interface
  end module

elicits a bogus semantic error about "specific" not being an acceptable
module procedure for the generic interface; fix.

Differential Revision: https://reviews.llvm.org/D134402
2022-09-23 11:18:01 -07:00
Peter Klausler 4c9d54982c [flang] Special handling of ENTRY BIND(C,NAME=...)
We apply special symbol table scoping to top-level subroutine
and function names that have interoperable binding names, so
that it's possible to define the same subroutine/function name
more than once at the top level so long as their binding names
are distinct.  But we don't use those scoping techniques for
ENTRY statement symbols with interoperable binding names,
which can lead to bogus semantic errors when the same ENTRY
name is defined multiple times with distinct binding names.

Differential Revision: https://reviews.llvm.org/D134401
2022-09-23 11:01:17 -07:00
Peter Klausler b918498193 [flang][NFC] Document non-extension (user ELEMENTAL procedures as actual arguments)
Some Fortran compilers accept user-defined ELEMENTAL procedures as actual
arguments corresponding to (necessarily) non-ELEMENTAL dummy procedures;
most do not, and f18 is one of them.  Document the fact that this is not
a supported extension.

Differential Revision: https://reviews.llvm.org/D134399
2022-09-23 10:57:04 -07:00
Peter Klausler 811aafcbf5 [flang] Don't resolve names in derived type definitions to generics
There's code in name resolution that handles resolution of references
that appear in the definitions of derived types -- it checks for
existing components in the type being defined, as well as for
non-parent components in its ancestors.  Special case code prevents
resolution of a name to the symbol of a procedure binding.  This
code needs to be extended so that names of generic procedures
are similarly prevented from shadowing symbols in the scope around
the type.

Differential Revision: https://reviews.llvm.org/D134398
2022-09-23 10:56:24 -07:00
Peter Klausler 47c998cc2e [flang] Improve handling of NULL pointers when folding components of named constants
When a component reference to a named constant of derived type should
fold down to NULL() without a MOLD=, do so.

Differential Revision: https://reviews.llvm.org/D134395
2022-09-23 09:22:52 -07:00
Peter Klausler bd1d170164 [flang] Prevent a bad folding rewrite
When a subexpression does not have both constant elements and
a constant shape, folding is rewriting it into a vector of its
elements.  This is of course wrong when the shape shows that
the result has rank greater than 1.

Differential Revision: https://reviews.llvm.org/D134392
2022-09-23 08:11:16 -07:00
Valentin Clement 5531c96c46
[flang][NFC] Fix typo 2022-09-23 11:47:38 +02:00
Jean Perier 2502c3afe0 [flang] Add TODOs for some FORALL assignments with Pointers and Allocatables
Add TODO for whole array allocatable assignment inside FORALL
Whole allocatable array assignment inside FORALL are otherwise currently
hitting more cryptic asserts.

Add TODO in FORALL assignment when a designator appear with a part ref
that is an allocatable or pointer component (a(i)%pointer%k). The lowering
code does not handle this case well because of the pointer dereference.

Differential Revision: https://reviews.llvm.org/D134440
2022-09-23 10:11:00 +02:00
Valentin Clement c0185c8d29
[flang] Load allocatable or pointer box of namelist item from common block
If a namelist item is an allocatable or pointer and is also part of a common
block, the box should be loaded from the common block ref.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D134470
2022-09-23 08:30:02 +02:00
Peter Klausler 81d857d037 [flang][NFC] Document ambiguous case of DATA in BLOCK
Fortran is not clear about the semantics of

```
  subroutine subr
    integer n = 1
    block
      data n/2/
    end block
  end subroutine
```

which could be interpreted as having two variables, each
named 'n', or as having one variable 'n' with invalid double
initialization.  Precedents from existing compilers are also
in disagreement.

The most common interpretation, however, agrees with a subtle
reading of the standard: BLOCK constructs scope names that have
local specifications, and a DATA statement is a declaration
construct, not a specification construct.  So this example is
*not* acceptable.

Differential Revision: https://reviews.llvm.org/D134391
2022-09-22 18:24:52 -07:00
Slava Zakharin d37250c9db [flang][runtime] Fixes for element size calculation.
BytesFor() used to return KIND for the size, which is not always
correct, so I changed it to return the size of the actual CppType
corresponding to the given category and kind.

MinElemLen() used to calculate size incorrectly (e.g. CFI_type_extended_double
was sized 10, whereas it may occupy more bytes on a target), so I changed it
to call BytesFor().

Additional changes were needed to resolve new failures for transformational
intrinsics. These intrinsics used to work for not fully supported
data types (e.g. REAL(3)), but now stopped working because CppType
cannot be computed for those categories/kinds. The solution is to use
known element size from the source argument(s) for establishing
the destination descriptor - the element size is all that is needed
for transformational intrinsics to keep working.

Note that this does not help cases, where runtime still has to
compute the element size, e.g. when it creates descriptors for
components of derived types. If the component has unsupported
data type, BytesFor() will still fail. So these cases require
adding support for the missing types.

New regression unit test in Runtime/Transformational.cpp
demonstrates the case that will start working properly with
this commit.
2022-09-22 10:10:42 -07:00
Peter Steinfeld 76fd4bf675 [Flang] Finalize IO operations after calling EndIO
The process of passing arguments to IO calls can cause allocations that
get referenced during EndIO calls.  Calling "Finalize" causes these
allocations to be deallocated.  This means that references to them in
the code in EndIO will be invalid.  The fix is to delay the call to
"finalize" until after the call to EndIO.

This particularly causes problems with the IO items are strings that are
produced by calls to functions.

Differential Revision: https://reviews.llvm.org/D134383
2022-09-22 07:13:03 -07:00
River Riddle 986b5c56ea [mlir] Flip Async/GPU/OpenACC/OpenMP to use Both accessors
This allows for incrementally updating the old API usages without
needing to update everything at once. These will be left on Both
for a little bit and then flipped to prefixed when all APIs have been
updated.

Differential Revision: https://reviews.llvm.org/D134386
2022-09-21 17:36:13 -07:00
Katherine Rasmussen adb2ba5ca3 [flang] Write semantics test for atomic_xor
Write a semantics test for the atomic intrinsic subroutine,
atomic_xor.

Reviewed By: rouson

Differential Revision: https://reviews.llvm.org/D134298
2022-09-21 15:50:14 -07:00
Valentin Clement f3222be4fc
[flang] Avoid deallocation of intent(out) when dummy arg is not in entry stmt
In some case, the ENTRY statement in a procedure is including some
dummy argument. Until now, deallocation of intent(out) allocatable was
not checking for this and it could result in a segmentation fault.

This patch avoids deallocation when the value is not in the ENTRY stmt.

Reviewed By: jeanPerier, PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D134342
2022-09-21 18:48:09 +02:00
Valentin Clement ed642d2064
[flang] Handle parent component of extended derived-type
Parent component refers to the parent derived-type of an extended type.
The parent component is skipped when a specififc component is
referred to. This is fine since all the components in extended type
are available in the type itself. When the parent component is referred,
it need to be taken into account correctly.
This patch fixes the case when the parent component is referred. In a
box, an approriate slice is created or updated to point to the first
component of the parent component. For scalar, a simple conversion to
the parent component type is done.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D134170
2022-09-21 11:20:14 +02:00
Jean Perier c4a73957f6 [flang] Limit shape inquiries rewrite to associate construct entity
The previous code was rewriting all shape inquires on associate
construct entities to inquires on the associated expression or variable.

This is is incorrect because at the point of inquiry, some statement
between the association and the inquiry may have modified the expression
operands or variable in a way that changes its shapes or bounds.

For instance, in the example below, expression rewrites was previously
replacing `size(x, 1)` by `size(p, 1)` which is invalid if p is a
pointer.

```
associate(x => p + 1)
 call call_that_may_modify_p_shape()
 print *, size(x, 1)
end associate
```

This change restricts rewrites of shape inquiries on associate construct entity
to use the associated expression shape and bounds if and only if the
shape/bounds are compile time constant. Otherwise, this may be invalid.

Differential Revision: https://reviews.llvm.org/D133857
2022-09-21 10:25:06 +02:00
Naje George 9df4e00d4a [flang] Add semantics test for atomic_add subroutine
Reviewed By: ktras

Differential Revision: https://reviews.llvm.org/D131535
2022-09-20 14:16:06 -07:00
Katherine Rasmussen 2e58d3e334 [flang] Write semantics test for atomic_or
Write a semantics test for the atomic intrinsic subroutine,
atomic_or.

Reviewed By: rouson

Differential Revision: https://reviews.llvm.org/D134200
2022-09-20 11:15:53 -07:00
Jean Perier e6238ab525 [flang] Deallocate WHERE masks after all assignments
Allocatable assignments were triggering lowering to clean-up
any WHERE mask temporaries, even if some assignments where left
in the WHERE construct.

This is because allocatable assignments lowering was being passed the
wrong statement context. Fix this by selecting the where/forall statement
context instead of a local one when there is one.

Differential Revision: https://reviews.llvm.org/D134197
2022-09-20 10:40:30 +02:00
Jean Perier b668de2de2 [flang] fix optional pointer TARGET argument lowering in ASSOCIATED
The TARGET argument of ASSOCIATED has a special lowering to deal with
POINTER and ALLOCATABLE optional actual arguments because they may be
dynamically absent. The previous code was doing a ternary
(mlir::SelectOp) to deal with this case, but generated invalid
code for the unused argument (loading a nullptr fir.ref<fir.box>). This
was not detected until D133779 was merged and modified how fir.load are
lowered to LLVM for fir.box types.

Replace the select by a proper if to prevent the fir.load from being
reachable in context where it should not.

Differential Revision: https://reviews.llvm.org/D134174
2022-09-20 10:16:58 +02:00
Jonathon Penix bfcd536a8e [flang] Give explicit convert= specifiers precedence over FORT_CONVERT
Currently, the FORT_CONVERT environment variable has the highest priority when
setting the endianness conversion for unformatted files. In discussing the
appropriate priority for the fconvert option, convert specifiers were decided
to take highest priority.

This patch also initializes the open statement convert state to unknown
to disambiguate cases where the convert specifier was not provided from
cases where convert=native was set. This makes it possible to defer to the
environment setting where appropriate.

Differential Revision: https://reviews.llvm.org/D133237
2022-09-19 20:40:51 -07:00
Slava Zakharin 8bd76ac151 [flang] Support multidimensional reductions in SimplifyIntrinsicsPass.
Create simplified functions for each rank with "x<rank>" suffix
that implement multidimensional reductions. To enable this I had to fix
an issue with taking incorrect box shape in cases of sliced embox/rebox.

Differential Revision: https://reviews.llvm.org/D133820
2022-09-19 12:16:23 -07:00
Slava Zakharin 2b138567e0 [flang] Support more data types for reduction in SimplifyIntrinsicsPass.
All floating point (not complex) and integer types should be supported now.

Differential Revision: https://reviews.llvm.org/D133818
2022-09-19 12:16:22 -07:00
Katherine Rasmussen e13b273d63 [flang] Write semantics test for atomic_and
Write a semantics test for the atomic intrinsic subroutine,
atomic_and.

Reviewed By: rouson

Differential Revision: https://reviews.llvm.org/D133727
2022-09-19 08:47:06 -07:00
Valentin Clement 458598ccc5
[flang][NFC] Remove not polymorphic from assumed type 2022-09-19 09:51:11 +02:00
Kazu Hirata 7ee332c4af [flang] Use x.empty() instead of llvm::empty(x) (NFC)
I'm planning to deprecate and eventually remove llvm::empty.

Note that no use of llvm::empty requires the ability of llvm::empty to
determine the emptiness from begin/end only.
2022-09-18 11:10:11 -07:00
Sourabh Singh Tomar 396ed327bb [flang][OpenMP] Lower OpenMP `taskgroup` construct
Lower Fortran OpenMP `taskgroup` to FIR + OpenMP Dialect.

Reviewed By: kiranchandramohan, peixin

Differential Revision: https://reviews.llvm.org/D133918
2022-09-16 21:07:47 +05:30
John Ericson 3a1c81e327 [CMake] Avoid `LLVM_BINARY_DIR` when other more specific variable are better-suited, part 2
A simple sed doing these substitutions:

- `${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}\>` -> `${LLVM_LIBRARY_DIR}`
- `${LLVM_BINARY_DIR}/bin\>` -> `${LLVM_TOOLS_BINARY_DIR}`

where `\>` means "word boundary".

The only manual modifications were reverting changes in

- `runtimes/CMakeLists.txt`

because these were "entry points" where we wanted to tread carefully not not introduce a "loop" which would end with an undefined variable being expanded to nothing.

There are some `${LLVM_BINARY_DIR}/lib` without the `${LLVM_LIBDIR_SUFFIX}`, but these refer to the lib subdirectory of the source (`llvm/lib`). That `lib` is automatically appended to make the local `CMAKE_CURRENT_BINARY_DIR` value by `add_subdirectory`; since the directory name in the source tree is fixed without any suffix, the corresponding `CMAKE_CURRENT_BINARY_DIR` will also be. We therefore do not replace it but leave it as-is.

This picks up where D133828 left off, getting the occurrences with*out* `CMAKE_CFG_INTDIR`. But this is difficult to do correctly and so not done in the (retroactively) previous diff.

This hopefully increases readability overall, and also decreases the usages of `LLVM_LIBDIR_SUFFIX`, preparing us for D130586.

Reviewed By: sebastian-ne

Differential Revision: https://reviews.llvm.org/D132316
2022-09-14 15:48:58 -04:00
Mats Petersson 9067de2a43 [flang][driver]Fix broken PowerPC tests
Tests don't work on PPC since `return` instruciton is't called `ret` (apparently)

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D133859
2022-09-14 16:33:38 +01:00
Mats Petersson b36b27b3fc [flang][driver]Fix broken flang-new mlir test
The test was added as a .mlir file, and this extension is not
in the lit.cfg.py, so it was never run. When running it, the
file would produce an error, as semicolon is not an MLIR comment.

This adds the extension and fixes the comment start by using C++
style comments.

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D133792
2022-09-14 14:16:31 +01:00
Jean Perier 9e1395ef14 [flang] Make a descriptor copy for fir.load fir.ref<fir.box>
`fir.box` and `fir.ref<fir.box>` are both lowered to LLVM as a
descriptor in memory. This is because fir.box of polymorphic and assumed
rank entities cannot be known at compile time, so fir.box cannot be
lowered to a struct value.

fir.load or fir.ref<fir.box> was previously lowered to a no-op,
propagating the operand descriptor storage as a result.
This is wrong because the operand descriptor storage may later be
modified, and these changes should not be visible in the loaded fir.box
that is an immutable SSA value.

Modify fir.load codegen for fir.box to make a copy into a new storage to
ensure the fir.box is immutable.

Differential Revision: https://reviews.llvm.org/D133779
2022-09-14 08:56:33 +02:00
Katherine Rasmussen 49f3f97674 [flang] Write semantics test for atomic_fetch_xor
Write a semantics test for the atomic intrinsic subroutine,
atomic_fetch_xor.

Reviewed By: rouson

Differential Revision: https://reviews.llvm.org/D133704
2022-09-13 16:36:11 -07:00
Jean Perier 2694234c29 [flang][runtime] ensure character compares to blank are unsigned
CompareToBlankPadding was doing signed compare on architecture where
`char` is signed. This caused `'abc'//char(128) > 'abc'` to evaluate
to false at runtime instead of true.

Differential Revision: https://reviews.llvm.org/D133693
2022-09-13 10:42:53 +02:00
Katherine Rasmussen e4ee56b4e0 [flang] Write semantics test for atomic_fetch_and
Write a semantics test for the atomic intrinsic subroutine,
atomic_fetch_and.

Reviewed By: rouson

Differential Revision: https://reviews.llvm.org/D133506
2022-09-12 08:40:15 -07:00
V Donaldson a3c05df16b [flang] Fix invalid branch optimization
Branch optimization in function rewriteIfGotos attempts to rewrite code
such as

    <<IfConstruct>>
      1 If[Then]Stmt: if(cond) goto L
      2 GotoStmt: goto L
      3 EndIfStmt
    <<End IfConstruct>>
    4 Statement: ...
    5 Statement: ...
    6 Statement: L ...

to eliminate a branch and a trivial basic block to get:

    <<IfConstruct>>
      1 If[Then]Stmt [negate]: if(cond) goto L
      4 Statement: ...
      5 Statement: ...
      3 EndIfStmt
    <<End IfConstruct>>
    6 Statement: L ...

Among other requirements, this is invalid if any statement between the
GOTO and its target is an intermediate construct statement such as a
CASE or ELSE IF statement, like the CASE DEFAULT statement in:

  select case(i)
  case (:2)
    n = i * 10
  case (5:)
    n = i * 1000
    if (i <= 6) goto 9 ! exit over 'case default'; may not be rewritten
    n = i * 10000
  case default
    n = i * 100
9 end select
2022-09-11 19:37:32 -07:00
Dylan Fleming ddc0465369 [Flang][OpenMP] Add support for logical and reduction in worksharing-loop
Adds support for .and. reductions with logical types.

Because arith.addi doesn'to work with fir.logical<4> types
logical<4> must be converted to i1 prior to the operation.

This means that the pattern matched by integer reductions
(load -> op -> store) will not match logical reductions.
Instead, the pattern being searched for here is
load -> convert(logical<4> to i1) -> op -> convert(i1 to logical<4>) -> store

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D132228
2022-09-09 17:29:49 +00:00