7669 Commits

Author SHA1 Message Date
Yi Wu
a2d4a4c0b2 Apply kind code check on exitstat and cmdstat (#78286)
When testing on gcc, both exitstat and cmdstat must be a kind=4 integer,
e.g. DefaultInt. This patch changes the input arg requirement from
`AnyInt` to `TypePattern{IntType, KindCode::greaterOrEqualToKind, n}`.

The standard stated in 16.9.73
- EXITSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least nine.
- CMDSTAT (optional) shall be a scalar of type integer with a decimal
exponent range of at least four.

```fortran
program bug
  implicit none
  integer(kind = 2) :: exitstatvar
  integer(kind = 4) :: cmdstatvar
  character(len=256) :: msg
  character(len=:), allocatable :: command
  command='echo hello'
  call execute_command_line(command, exitstat=exitstatvar, cmdstat=cmdstatvar)
end program
```
When testing the above program with exitstatvar kind<4, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'exitstat=' has bad type or kind 'INTEGER(2)'
    call execute_command_line(command, exitstat=exitstatvar)
```

When testing the above program with exitstatvar kind<2, an error would
occur:
```
$ ../build-release/bin/flang-new test.f90
error: Semantic errors in test.f90
./test.f90:8:47: error: Actual argument for 'cmdstat=' has bad type or kind 'INTEGER(1)'
    call execute_command_line(command, cmdstat=cmdstatvar)
```

Test file for this semantics has been added to `flang/test/Semantic`
Fixes: https://github.com/llvm/llvm-project/issues/77990

(cherry picked from commit 14a15103cc9dbdb3e95c04627e0b96b5e3aa4944)
2024-01-30 16:45:35 +00:00
Saiyedul Islam
082f87c9d4
[AMDGPU] Change default AMDHSA Code Object version to 5 (#79038)
Also update LIT tests and docs.
For more details, see
https://llvm.org/docs/AMDGPUUsage.html#code-object-v5-metadata

Corresponding llvm-objdump AMDGPU lit tests are updated
in a follow-up PR.
2024-01-23 17:08:18 +05:30
Yi Wu
1715866a94
fix optional wait wrongly treated as false (#78149) 2024-01-23 10:05:42 +00:00
harishch4
47bcc91e06
[Flang][OpenMP] Fix to variables not inheriting data sharing attributes (#79017)
When a default(none) clause exists and a threadprivate variable is used
inside the construct, the variable does not inherit threadprivate
behavior and throws the below error.

> error: The DEFAULT(NONE) clause requires that 'a' must be listed in a
data-sharing attribute clause

Added a condition to skip the error if it is a threadprivate variable.

Fixes: https://github.com/llvm/llvm-project/issues/49545
2024-01-23 15:16:49 +05:30
Michael Klemm
ab32a3c166
[flang] Do not leak intrinsics used by ISO_C_BINDING and ISO_FORTRAN_ENV (#79006)
This resolves bug #78953. Intrinsics used by the MODULE definition are
being declared PRIVATE, so that they do not leak into the namespace of
the code that USEs the modules.
2024-01-23 08:43:29 +01:00
Tom Eccles
50e2581a19
[flang] Allow assumed-shape element pass to dummy arg with ignore_tkr (#78196)
This is allowed by gfortran and ifort with `![GCC|DEC]$ ATTRIBUTES
NO_ARG_CHECK`
2024-01-22 23:16:22 +00:00
Tom Eccles
d37d1c8dea
[flang][driver] deprecate manual usage of -lFortran_main (#79016)
Intended to warn users of the 18.x release not to do this.

A better solution should be found for the 19.x release. See discussion
in https://github.com/llvm/llvm-project/pull/78152.

Unfortunately there is no warning on Windows currently. I am rushing to
get this landed before 18.x branches.
2024-01-22 23:14:09 +00:00
Kazu Hirata
bcfdab8705 [flang] Fix a warning
This patch fixes:

  flang/lib/Lower/OpenACC.cpp:1964:15: error: unused variable
  'loopDirective' [-Werror,-Wunused-variable]
2024-01-22 11:24:55 -08:00
Valentin Clement
16d2583254
[flang][openacc] Fix test with new loop design 2024-01-22 10:48:17 -08:00
Valentin Clement (バレンタイン クレメン)
5062a178bf
[flang][openacc] Lower loop directive to the new acc.loop op design (#65417)
acc.loop was redesigned in https://reviews.llvm.org/D159229. This patch
updates the lowering to match the new op.

DO CONCURRENT construct will be added in a follow up patch.

Note that the pre-commit ci will fail until D159229 is merged. 

Depends on #67355
2024-01-22 10:31:37 -08:00
David Spickett
e302950023
[flang] Handle -S assemble only flag in flang-to-external-fc (#78979)
Flang was recently updated on Compiler Explorer and by default it's in
assemble only mode, you have to enable linking and executing.

This means that the default output for flang-to-external-fc is nothing,
as it doesn't know what `-S` means. You'd have to know to enable the
link to binary option to see any output.

Handle `-S` so that users of Compiler Explorer don't have to wonder why
the "compiler" is broken.
2024-01-22 15:55:05 +00:00
Krzysztof Parzyszek
af1741ed40 [Flang][OpenMP] Reword comment for clarification, NFC 2024-01-22 09:05:44 -06:00
Krzysztof Parzyszek
55cb52bbc5
[Flang][OpenMP] Restructure recursive lowering in createBodyOfOp (#77761)
This brings `createBodyOfOp` to its final intended form. First, input
privatization is performed, then the recursive lowering takes place, and
finally the output privatization (lastprivate) is done.

This enables fixing a known issue with infinite loops inside of an
OpenMP region, and the fix is included in this patch.

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

Recursive lowering [5/5]

---------

Co-authored-by: Kiran Chandramohan <kiran.chandramohan@arm.com>
2024-01-22 08:41:07 -06:00
David Green
49212d1601
[Flang] Fix for replacing loop uses in LoopVersioning pass (#77899)
The added test case has a loop that is versioned, which has a use of the
loop in an if block after the loop. The current code replaces all uses
of the loop with the new version If, but only if the parent blocks
match. As far as I can see it should be safe to replace all the uses,
then construct the result for the If with op.op.
2024-01-20 22:16:05 +00:00
Kiran Chandramohan
aac1d9710b
[Flang][OpenMP] Consider renames when processing reduction intrinsics (#70822)
Fixes #68654

Depends on https://github.com/llvm/llvm-project/pull/70790
2024-01-19 15:17:21 +00:00
Yi Wu
5a7f9a5a9c
[flang] use setsid to assign the child to prevent zombie as it will be clean up by init process (#77944)
When using `setsid()` in a child process created by `fork()`, a new
session is created, and the child becomes a session leader. If the
parent process terminates before the child, the child becomes an orphan
and is adopted by the `init` process. The `init` process will eventually
clean up the child process once it exits.

However, killing the parent does not automatically kill the child; the
child will continue running until it exits.
Proper cleanup involves waiting for the child process to exit using
`wait()` or `waitpid()` in the parent process to avoid zombie processes,
but this approach is not valid for `EXECUTE_COMMAND_LINE` with async
mode.
Fix: https://github.com/llvm/llvm-project/issues/77803
2024-01-19 14:18:57 +00:00
jeanPerier
eaa8def929
[flang] Expand parent component in procedure pointer component ref (#78593)
For simplicity, lowering relies on semantics expansion of parent
components in designators.

This was not done in `call x%p()` where `p` is a procedure component
pointer of a parent component of `x`.

Do it and turn lowering TODO into a new lowering TODO for `call bar(x%type_bound_procedure)` (passing a tybe bound procedure is allowed as an extension, but lowering does not handle this extension yet. This is a lowering issue, will do in different patch).
2024-01-19 15:09:25 +01:00
Kiran Chandramohan
a90347ecc7 Revert "Revert "[Flang][OpenMP] NFC: Minor refactoring of Reduction lowering code" (#73139)"
This reverts commit c2b3f16fb595fa88bfd21b455785c59ac6a21ed4.
2024-01-19 10:43:53 +00:00
Kareem Ergawy
10317da203
[flang][re-apply] Fix seg fault CodeGenAction::executeAction() (#78672)
If `generateLLVMIR()` fails, we still continue using the module we
failed to generate which causes a seg fault if LLVM code-gen failed for
some reason or another. This commit fixes this issue.

Re-applies PR #78269 and adds LLVM and MLIR dependencies that were
missed in the PR. The missing libs were: `LLVMCore` & `MLIRIR`.

This reverts commit 4fc75062745eb5232ea60c37b9ffe61177efa12a.
2024-01-19 10:40:25 +01:00
Valentin Clement (バレンタイン クレメン)
b8967e003e
[flang][openacc] Support multiple device_type when lowering (#78634)
routine, data, parallel, serial, kernels and loop construct all support
the device_type clause. This clause takes a list of device_type.
Previously the lowering code was assuming that the list s a single item.
This PR updates the lowering to handle any number of device_types.
2024-01-18 21:20:28 -08:00
Kareem Ergawy
4fc7506274
Revert "[flang] Fix seg fault CodeGenAction::executeAction() (#78269)" (#78667)
This reverts commit 99cae9a44fca4cfbd6ee82f196051cbdf6571fa1.

Temporarily until I reproduce and fix a linker issue:
```
FAILED: tools/flang/unittests/Frontend/FlangFrontendTests
...
/usr/bin/ld: tools/flang/unittests/Frontend/CMakeFiles/FlangFrontendTests.dir/CodeGenActionTest.cpp.o: undefined reference to symbol '_ZN4llvm11LLVMContextC1Ev'
/usr/bin/ld: /work1/omp-nightly/build/git/trunk18.0/build/llvm-project/lib/libLLVMCore.so.18git: error adding symbols: DSO missing from command line
```
2024-01-19 05:30:29 +01:00
Kareem Ergawy
99cae9a44f
[flang] Fix seg fault CodeGenAction::executeAction() (#78269) 2024-01-18 20:36:27 +01:00
Peter Klausler
fa6c3df6ad
[flang] Don't use \uXXXX encodings unless \-escapes are enabled (#78326)
Don't put \uXXXX escapes into the cooked character stream while
prescanning; it should always be UTF-8.
2024-01-18 11:05:04 -08:00
Valentin Clement (バレンタイン クレメン)
b06bc7c6a0
[mlir][flang][openacc] Device type support on acc routine op (#78375)
This patch add support for device_type on the acc.routine operation.
device_type can be specified on seq, worker, vector, gang and bind
information.

The support is following the same design than the one for compute
operations, data operation and the loop operation.
2024-01-18 09:04:11 -08:00
Sergio Afonso
2747193058
[Flang][MLIR][OpenMP] Remove the early outlining interface (#78450)
After the removal of the OpenMP early outlining MLIR pass in #67319, the
`EarlyOutliningInterface` stopped doing any useful work. It used to be
necessary to tie the name of the function from which a target region was
outlined to that new function, so it would be used when translating to
LLVM IR in place of the outlined function's name.

This is not necessary anymore, so this patch removes all references to
this interface and uses of the `omp.outline_parent_name` discardable
attribute in tests.
2024-01-18 15:33:43 +00:00
madanial0
87ac65a994
[flang] Match the length size in comparison (NFC) (#78302)
The template function call CheckDescriptorEqInt((exitStat.get(), 127) is
deduced to have INT_T equal to std::int32_t instead of std::int64_t, but
the length descriptor points to a 64-byte storage. The comparison does
not work in a big endian.

Co-authored-by: Mark Danial <mark.danial@ibm.com>
2024-01-18 10:18:21 -05:00
madanial0
fe4d502524
[flang] fix unsafe memory access using mlir::ValueRange (#78435)
When running the `flang/test/HLFIR/simplify-hlfir-intrinsics.fir` test
case on AIX we encounter issues building op as they are not found in the
mlir context:
```
LLVM ERROR: Building op `arith.subi` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management
LLVM ERROR: Building op `hlfir.yield_element` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management
LLVM ERROR: Building op `hlfir.yield_element` but it isn't known in this MLIRContext: the dialect may not be loaded or this operation hasn't been added by the dialect. See also https://mlir.llvm.org/getting_started/Faq/#registered-loaded-dependent-whats-up-with-dialects-management
```
The issue is caused by the "Merge disjoint stack slots" pass and the
error is not present if the source is built with `-mllvm
--no-stack-coloring`

Thanks to investigation by @stefanp-ibm we found that "the
initializer_list {inputIndices[1], inputIndices[0]} has a lifetime that
only exists for the range of the constructor for ValueRange. Once we get
to stack coloring we merge the stack slot for that element with another
stack slot and then it gets overwritten which corrupts
transposedIndices"

The changes below prevents the corruption of transposedIndices and
passes the test case.

Co-authored-by: Mark Danial <mark.danial@ibm.com>
2024-01-18 10:17:53 -05:00
Leandro Lupori
07abde2717
[flang][driver] Fix Driver/isysroot.f90 test (#78478)
Check for DEFAULT_SYSROOT, because when it is set -isysroot has no
effect.
2024-01-18 11:21:57 -03:00
Sergio Afonso
0c76865da9
[Flang][OpenMP][Lower] NFC: Combine two calls to ClauseProcessor::processTODO (#78451)
Just a minimal readability improvement that we overlooked during
refactoring.
2024-01-18 14:01:08 +00:00
Krzysztof Parzyszek
e5a34f9226
[Flang][OpenMP] Push genEval closer to leaf lowering functions (#77760)
This moves the lowering of the nested evaluations all the way to the
bottom of the call stack. This PR does not attempt to change the leaf
lowering functions beyond placing the call to `genEval` in there.
Whether the nested evaluations should be lowered for any given op
depends on the context in which that op is created, hence a `genNested`
parameter was added.

Contexts in which nested evaluations should not be lowered are during
lowering of composite constructs, such as PARALLEL SECTIONS. This
particular case is considered a block construct tied to the SECTIONS
directive, and the lowering code will first create an empty parallel op,
and then recursively lower the SECTIONS code. Similar situations occur
when lowering most (if not all) compound/composite constructs.

Recursive lowering [4/5]
2024-01-18 07:47:35 -06:00
jeanPerier
4f62a183d9
[flang] Allow user to define free via BIND(C) (#78428)
A user defining and using free/malloc via BIND(C) would previously cause
flang to crash when generating LLVM IR with error "redefinition of
symbol named 'free'". This was caused by flang codegen not expecting to
find a mlir::func::FuncOp definition of these function and emitting a
new mlir::LLVM::FuncOp that later conflicted when translating the
mlir::func::FuncOp.
2024-01-18 09:37:44 +01:00
Peter Klausler
345c1ea881
[flang] Avoid new spurious error under -fopenacc (#78504)
Don't create HostAssocDetails symbols for subprograms in OpenACC
regions; it can cause warnings to became errors later in compilation
when calls do not appear to be to external procedures with implicit
interfaces.
2024-01-17 14:03:47 -08:00
jeanPerier
a96b4671b9
[flang] Lower BIND(C) module variables (#78279)
Lower initialized BIND(C) module variable as regular module variable,
except that the fir.global symbol name is the binding label.

For uninitialized variables, add the common linkage so that C code may
define the variables. The standard does not provide a way to indicate
that a variable is defined in C, but there are use cases.

Beware that if the module file compiled object is added to a shared
library, the variable will become a regular global definition and may
override the C variable depending on the linking order.
2024-01-17 17:44:39 +01:00
agozillon
abeb6c9f58
[Flang][MLIR] Add basic initial support for alloca and program address space handling in FIR->LLVMIR codegen (#77518)
This is a slightly more slimmed down and up-to-date version of the older
PR from here: https://reviews.llvm.org/D144203, written by @jsjodin,
which has already under gone some review.

This PR places allocas in the alloca address space specified by the
provided data layout (default is 0 for all address spaces, unless
explicitly specified by the layout), and then will cast these alloca's
to the program address space if this address space is different from the
allocation address space. For most architectures data layouts, this will
be a no-op, as they have a flat address space. But in the case of AMDGPU
it will result in allocas being placed in the correct address space (5,
private), and then casted into the correct program address space (0,
generic). This results in correct (partially, a follow up PR will be
forthcoming soon) generation of allocations inside of device code.

This PR is in addition to the work by @skatrak in this PR:
https://github.com/llvm/llvm-project/pull/69599 and adds seperate and
neccesary functionality of casting alloca's from their address space to
the program address space, both are independent PRs, although there is
some minor overlap e.g. this PR incorporates some of the useful helper
functions from 69599, so whichever lands first will need a minor rebase.

Co-author: jsjodin
2024-01-17 17:37:16 +01:00
Kelvin Li
3d3c63da6b
[flang] Add structure constructor with allocatable component (#77845)
Enable the structure constructor with allocatable component support. 
Handling of `null()` for the allocatable component is added.
2024-01-17 11:24:05 -05:00
ostannard
13e977d173
[AArch64][Driver] Better handling of target feature dependencies (#78270)
Currently there are several bits of code in the AArch64 driver which
attempt to enforce dependencies between optional features in the -march=
and -mcpu= options. However, these are based on the list of feature
names being enabled/disabled, so they have a lot of logic to consider
the order in which features were turned on and off, which doesn't scale
well as dependency chains get longer.

This patch moves the code handling these dependencies to TargetParser,
and changes them to use a Bitset of enabled features. This makes it easy
to check which features are enabled, and is converted back to a list of
LLVM feature names once all of the command-line options are parsed.

The motivating example for this was the -mcpu=cortex-r82+nofp option.
Previously, the code handling the dependency between the fp16 and
fp16fml extensions did not consider the nofp modifier, so it added
+fullfp16 to the feature list. This should have been disabled by the
+nofp modifier, and also the backend did follow the dependency between
fullfp16 and fp, resulting in fp being turned back on in the backend.

Most of the dependencies added to AArch64TargetParser.h weren't known
about by clang before, I built that list by checking what the backend
thinks the dependencies between SubtargetFeatures are.
2024-01-17 16:20:34 +00:00
Matthias Springer
5fcf907b34
[mlir][IR] Rename "update root" to "modify op" in rewriter API (#78260)
This commit renames 4 pattern rewriter API functions:
* `updateRootInPlace` -> `modifyOpInPlace`
* `startRootUpdate` -> `startOpModification`
* `finalizeRootUpdate` -> `finalizeOpModification`
* `cancelRootUpdate` -> `cancelOpModification`

The term "root" is a misnomer. The root is the op that a rewrite pattern
matches against
(https://mlir.llvm.org/docs/PatternRewriter/#root-operation-name-optional).
A rewriter must be notified of all in-place op modifications, not just
in-place modifications of the root
(https://mlir.llvm.org/docs/PatternRewriter/#pattern-rewriter). The old
function names were confusing and have contributed to various broken
rewrite patterns.

Note: The new function names use the term "modify" instead of "update"
for consistency with the `RewriterBase::Listener` terminology
(`notifyOperationModified`).
2024-01-17 11:08:59 +01:00
Tom Eccles
03f260fd83
[flang] Add install target to install flang headers (#78151)
This makes it more convenient to install flang without using the general
`ninja install` to install everything.

To install flang-new and associated things one needs `ninja
install-flang-new install-flang-libraries install-flang-headers`.
2024-01-16 17:04:52 +00:00
Tarun Prabhu
aa440aae36
[flang][Driver] Support -pthread in the frontend (#77360)
The -pthread option seems to be extensively tested for various platforms by
clang. Since flang uses those parts of the clang driver, the only test added
is to ensure that the option is recognized by the frontend.
2024-01-16 08:25:36 -07:00
Krzysztof Parzyszek
12c7d4c4b4
[Flang][OpenMP] Remove space before :: in member function definition,… (#78205)
… NFC

The names were
```
bool DataSharingProcessor ::needBarrier()
void DataSharingProcessor ::insertBarrier()
```

It seems like clang-format treats those as references to the global
namespace instead of separators in a qualified name.
2024-01-16 07:09:06 -06:00
Kelvin Li
8b6b882f27
[flang] allow _POSIX_SOURCE to be defined without a value (#78179)
The `_POSIX_SOURCE` macro is defined without a value on AIX. Change the check to `defined(_POSIX_SOURCE)`.
2024-01-15 16:54:51 -05:00
Peter Klausler
8f3bdea9b4
[flang][NFC] Restore documentation (#78211)
Add a paragraph to Extensions.md that fell out of a recent patch.
2024-01-15 13:06:47 -08:00
Peter Klausler
c66645da55
[flang] Catch more initialization errors (#77850)
[flang] Catch more initialization errors
    
    Diagnose some error cases related to initialization that are
slipping past semantic checking: don't allow multiple initializations
    of the same symbol, and don't allow an object that was initialized
    as a scalar to become an array afterward.
    
Fixes llvm-test-suite/Fortran/gfortran/regression/initialization_17.f90.
2024-01-15 13:02:04 -08:00
Peter Klausler
4fcefbd3a4
[flang][runtime] Emit leading space before zero-length list-directed … (#77835)
…character

Don't omit the leading space in list-directed output before a line
comprising only a zero-length character value.

Fixes https://github.com/llvm/llvm-project/issues/77736.
2024-01-15 12:52:18 -08:00
Peter Klausler
6e0a2031f0
[flang] Catch name resolution error due to global scoping (#77683)
In
    CALL FOO
    PRINT *, ABS(FOO)
we currently resolve the first FOO to a global external subprogram, but
then the second FOO is treated as an implicitly typed local variable.
This happens because the name FOO is not present in the local scope.

Fix by adding FOO to the local scope using a place-holding
HostAssocDetails symbol whose existence prevents the creation of another
FOO in the local scope. The symbol stored in the parser::Name parse tree
nodes or used in typed expressions will all continue to point to the
global external subprogram.

Resolves llvm-test-suite/Fortran/gfortran/regression/pr71859.f90.
2024-01-15 12:40:46 -08:00
Peter Klausler
691770ca67
[flang] Add portability warning for F'2008 feature (#77526)
Using the VALUE attribute for assumed-length CHARACTER dummy arguments
became standard in F'2008 but still lacks widespread implementation;
emit a portability warning when they are enabled.

Resolves llvm-test-suite/Fortran/gfortran/regression/value_5.f90.
2024-01-15 12:31:52 -08:00
Peter Klausler
9fdd25e18c
[flang] Don't change size of allocatable in error situation (#77386)
When an already-allocated allocatable array is about to fail
reallocation, don't allow its size or other characteristics to be
changed.

Fixes
llvm-test-suite/Fortran/gfortran/regression/multiple_allocation_1.f90
and .../multiple_allocation_3.f90.
2024-01-15 12:18:34 -08:00
Peter Klausler
7b80123387
[flang] More support for assumed-size Cray pointees (#77381)
Recognize Cray pointees as such when they are declared as assumed size
arrays, and don't emit a bogus error message about implied shape arrays.

Fixes https://github.com/llvm/llvm-project/issues/77330.
2024-01-15 12:08:00 -08:00
Peter Klausler
f9b089a7c0
[flang] Fix semantic checks for MOVE_ALLOC (#77362)
The checking of calls to the intrinsic subroutine MOVE_ALLOC is not
insisting that its first two arguments be whole allocatable variables or
components. Fix, move the code into check-calls.cpp (a better home for
such things), and clean up the tests.

Fixes https://github.com/llvm/llvm-project/issues/77230.
2024-01-15 11:57:37 -08:00
Peter Klausler
f089691157
[flang][runtime] Better real MOD/MODULO results (#77167)
The Fortran standard defines real MOD and MODULO with expressions like
MOD(a,p) = a - AINT(a/p)*p. Unfortunately, these definitions have poor
accuracy when a is much larger in magnitude than p, and every Fortran
compiler uses better algorithms instead.

Fixes llvm-test-suite/Fortran/gfortran/regression/mod_large_1.f90.
2024-01-15 11:48:36 -08:00