188 Commits

Author SHA1 Message Date
Peter Klausler
b685597c03
[flang] Fix crash in reduction folding (#87201)
A reduction folding template assumed lower bounds were 1.

Fixes https://github.com/llvm/llvm-project/issues/86935.
2024-04-08 11:54:13 -07:00
Peter Klausler
606a997a3c
[flang] Fix SCALE() folding with big scale factors (#85576)
The folding of the SCALE() intrinsic function is implemented via
multiplication by a power of two; this simplifies handling of
exceptional cases. But sometimes scaling by a power of two requires an
exponent larger or smaller than a floating-point format can represent,
and two multiplications are required.
2024-03-18 14:12:09 -07:00
jeanPerier
181eab27d2
[flang] Set KIND in compiler generated COUNT for SIZE(PACK) (#79801)
Compiler was rewriting SIZE(PACK(x, MASK)) to COUNT(MASK). It was
wrapping the COUNT call without a KIND argument (leading to INTEGER(4)
result in the characteristics) in an Expr<ExtentType> (implying
INTEGER(8) result), this lead to inconsistencies that later hit verifier
errors in lowering.

Set the KIND argument to the KIND of ExtentType to ensure the built
expression is consistent.

This requires giving access to some safe place where the "kind" name can
be saved and turned into a CharBlock (count has a DIM argument that
require using the KIND keyword here). For the FoldingContext that belong
to SemanticsContext, this is the same string set as the one used by
SemanticsContext for similar purposes.
2024-01-29 18:28:56 +01:00
Peter Klausler
71e01d9fd7
[flang] Support KNINT and KIDNNT legacy intrinsic functions (#79190)
These specific intrinsic functions are legacy names that map to the
standard generic NINT(..., KIND=8).
2024-01-25 16:35:18 -08:00
Peter Klausler
d0f44ed062
[flang] Don't create impossible conversions in intrinsic extension (#79042)
We support specific intrinsic calls like `AMAX0(1.0,2)` that have
heterogeneous argument types as an optional extension in cases where the
specific intrinsic has a related generic intrinsic function capable of
handling the argument types. This feature can't be allowed to apply to
calls where the result of the related generic intrinsic function is not
convertible to the type of the specific intrinsic, as in `AMAX0('a',
'b')`.

Fixes https://github.com/llvm/llvm-project/issues/78932.
2024-01-25 16:18:20 -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
Peter Klausler
82e1e412e3
[flang][runtime] Treatment of NaN in MAXVAL/MAXLOC/MINVAL/MINLOC (#76999)
Detect NaN elements in data and handle them like gfortran does (at
runtime); namely, NaN can be returned if all the data are NaNs, but any
non-NaN value is preferable. Ensure that folding returns the same
results as runtime computation.

Fixes llvm-test-suite/Fortran/gfortran/regression/maxloc_2.f90 (and
probably others).
2024-01-15 10:29:26 -08:00
Peter Klausler
4c1f488b78
[flang] Fix folding of NEAREST(TINY(1.),-1.) (#76590)
The code to fold NEAREST would return a value that's too large when
transitioning from a normal number to a subnormal.

Fixes llvm-test-suite/Fortran/gfortran/regression/nearest_1.f90.
2024-01-02 09:38:52 -08:00
madanial0
58b514921f
[flang] Run real 10 test on x86 only (NFC) (#73911)
Remove real 10 tests for powerpc

---------

Co-authored-by: Mark Danial <mark.danial@ibm.com>
2023-12-05 10:44:21 -05:00
Peter Klausler
0fdf9123bd
[flang] Fold MATMUL() (#72176)
Implements constant folding for matrix multiplication for all four
accepted type categories.
2023-11-14 14:37:23 -08:00
Leandro Lupori
8cccda27bc [flang] Fix flang tests on MacOS (#70811)
Adjust some of the tests run by check-flang to make them pass on MacOS,
either by skipping unsupported tests or by adapting the test for correct
execution on MacOS.

For now ctofortran.f90 test is marked as unsupported, but it can be
adapted to run on MacOS once support for -isysroot flag is added to
flang.

Issues #70805 and #70807 are tracking the failing tests that remain, as
these reveal real problems with flang.
2023-11-14 10:21:08 -03:00
Peter Klausler
1c91d9bdea
[flang] Ensure that portability warnings are conditional (#71857)
Before emitting a warning message, code should check that the usage in
question should be diagnosed by calling ShouldWarn(). A fair number of
sites in the code do not, and can emit portability warnings
unconditionally, which can confuse a user that hasn't asked for them
(-pedantic) and isn't terribly concerned about portability *to* other
compilers.

Add calls to ShouldWarn() or IsEnabled() around messages that need them,
and add -pedantic to tests that now require it to test their portability
messages, and add more expected message lines to those tests when
-pedantic causes other diagnostics to fire.
2023-11-13 16:13:50 -08:00
Peter Klausler
67f15e76eb
[flang] Fold IS_IOSTAT_END() & IS_IOSTAT_EOR() (#70971)
These intrinsic functions are not particularly valuable -- one can just
compare a value to IOSTAT_END or IOSTAT_EOR directly -- but they are in
the standard and are allowed to appear in constant expressions, so
here's code to fold them.
2023-11-13 14:07:38 -08:00
jeanPerier
51a2ac645f
[flang] Fold IS_CONTIGUOUS of component refs with non-contiguous base (#69327) 2023-10-18 08:47:15 +02:00
Leandro Lupori
c38598186b
[flang] Fix constant subscript operations (#68352)
Modify ConstantBounds' methods that handle subscripts and bounds to
avoid integer overflows. This is needed to properly handle arrays
with the maximum possible upper bound (INT64_MAX).
2023-10-17 11:43:15 -03:00
Peter Klausler
b225934a4b
[flang] Avoid needless overflow when folding NORM2 (#67499)
The code that folds the relatively new NORM2 intrinsic function can
produce overflow in cases where it's not warranted. Rearrange to NORM2 =
M * SQRT((A(:)/M)**2) where M is MAXVAL(ABS(A)).
2023-10-16 15:55:33 -07:00
jeanPerier
8c1270740f
[flang] Fix issues with STORAGE_SIZE and characters (#67561)
Semantics was replacing storage_size(func()) by the length specification
expression of func result (if any), which brought meaningless symbols.
Update FunctionRef::GetType to not copy its length parameter from the
procedure designator symbol if it is not a constant expression. Note
that the deferred aspect can and must be preserved because it matters
for POINTER function results (semantics test added to ensure this).

Update lowering code to deal with characters in storage_size: simply
always call createBox to ensure the BoxEleSizeOp is legal. This will
take care of dereferencing pointers/allocatables if needed (what the
load was intended for in the previous code).
2023-09-28 08:59:44 +02: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
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
jeanPerier
8b13775d6a
[flang] Improve length information in character transformational (#65771)
Intrinsic resolution currently does not resolve constant length
information for character transformational (with "sameChar") where the
argument has constant length but is not a variable or a constant
expression.

It is not required to fold those expressions (only inquiry on constant
expression or variable with constant length is required to be a constant
expression).

But constant length information for character is valuable for lowering,
so I think this is a nice and easy to have.
2023-09-11 12:52:11 +02:00
Peter Klausler
27549ee989
[flang][test] Disable new test where not supported
Disable the new test flang/test/Evaluate/test-out_of_range.f90
on targets and systems that do not support the kinds of REAL
that it exercises.

Pushed without review to clear up broken build-bots.
2023-08-29 10:37:11 -07:00
Peter Klausler
8383d768f2
[flang] Support SELECTED_LOGICAL_KIND() up to lowering
Process and fold the new F'2023 intrinsic function SELECTED_LOGICAL_KIND.

Differential Revision: https://reviews.llvm.org/D159039
2023-08-29 10:02:00 -07:00
Peter Klausler
54784b1831
[flang] Fold OUT_OF_RANGE()
Fold the F'2018 intrinsic function OUT_OF_RANGE(), which returns .TRUE.
when a conversion of an integer or real value to an integer or real type
would yield an overflow or (for real->integer only) invalid operand
exception.  Test all type combinations, with both rounding possibilities
for the real->integer cases.

Differential Revision: https://reviews.llvm.org/D159038
2023-08-29 09:48:52 -07:00
David Truby
a519d76a03 [flang] Fix calls to interfaces with logical literal arguments
Fixes: 64698

Differential Revision: https://reviews.llvm.org/D157987
2023-08-16 10:13:46 +01:00
David Truby
e63382542f [flang] Allow logical expressions as arguments with changed logical kind
This patch enables logical expressions to be used as arguments when the
default logical kind is changed (e.g. using -fdefault-integer-8) by
converting the type of the logical expression argument to the type of
the dummy argument in the function.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D157600
2023-08-14 13:05:21 +00:00
Peter Klausler
8926f0fe62
[flang] Fold MERGE() of derived type values
Generalize FoldMerge() to accommodate derived type arguments and results,
rename it into Folder<T>::MERGE(), and remove it from the various
FoldIntrinsicFunction() routines for intrinsic types.

Fixes llvm-test-suite/Fortran/gfortran/regression/merge_init_expr_2.f90.

Differential Revision: https://reviews.llvm.org/D157345
2023-08-08 12:02:31 -07:00
Peter Klausler
351b42a2fd
[flang] Fix folding of character array kind conversion
The folding of conversions of character arrays from one kind to
another was not preserving the value of the character length expression
on the intermediate array constructor, causing spurious errors
when defining named constant arrays of one kind of character with
arrays of another kind.

Fixes llvm-test-suite/Fortran/gfortran/regression/widechar_10.f90.

Differential Revision: https://reviews.llvm.org/D157340
2023-08-08 11:26:38 -07:00
Peter Klausler
ca0525fa16
[flang] Fix crash in location reduction folding with DIM= and scalar MASK=
The shape of an expanded scalar MASK= was incorrect in the template
code that folds FINDLOC/MAXLOC/MINLOC, leading to a vector indexing
crash when a DIM= argument is also present.

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

Differential Revision: https://reviews.llvm.org/D157329
2023-08-08 10:12:54 -07:00
Slava Zakharin
449823e202 [flang] Fixed IsContiguous check for slices of parameter arrays.
A section of a parameter array may be non-contiguous,
so the current !IsVariable(expr) check is too optimistic
to claim contiguity.

This patch fixes issues with incorrect hlfir.designate op generated
during lowering: the lowering queries IsContiguous to decide whether
to use fir.box<fir.array> or plain fir.ref<fir.array> to represent
the designator result.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D156494
2023-08-02 10:45:00 -07:00
Peter Klausler
f9fdd4fdba
[flang] Don't emit false errors on LBOUND/UBOUND of assumed-rank arrays
Folding of LBOUND/UBOUND on assumed-rank arrays could lead to bogus errors
later.  This is a regression from a recent change intended to ensure that
patently bad DIM= arguments were caught even in the case of assumed-rank.
Rearrange the order of checks so that those bad DIM= argument values are
caught but otherwise defer LBOUND/UBOUND of assumed-rank arguments to
run time.  (It was tempting to always fold LBOUND(ar,DIM=n) to 1, but
that would obscure the run time error required when 'n' is out of bounds,
including the case of a scalar 'ar'.)

Differential Revision: https://reviews.llvm.org/D156755
2023-08-01 11:02:58 -07:00
Peter Klausler
4f1eec1fc6
[flang] Fix crash in folding of DPROD() with non-scalar arguments
DPROD(x,y) is defined as DBLE(x)*DBLE(y) and that's exactly how
the implementation of its rewriting and possible folding should
be implemented, instead of the current code that only works when
both arguments are scalar and crashes otherwise.

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

Differential Revision: https://reviews.llvm.org/D156754
2023-08-01 09:26:34 -07:00
Fangrui Song
4c73549ebf [test] Fix compiler_version.f90 for -DLLVM_APPEND_VC_REV=OFF builds after D152572
Checking the patch version is too strict. Just check major and minor
versions as in clang/test/Driver/immediate-options.c.
2023-07-22 16:06:29 -07:00
Peter Klausler
221ba64e05
[flang] Stricter checking of DIM= arguments to LBOUND/UBOUND/SIZE
DIM= arguments with constant values can be checked for validity
even when other arguments to an intrinsic function can't be
folded.  Handle errors with assumed-rank arguments as well.

Differential Revision: https://reviews.llvm.org/D155964
2023-07-21 12:28:19 -07:00
Peter Klausler
08bfc00288
[flang] Don't warn about a NaN argument to ISNAN()
Don't emit a warning when a compile-time constant argument to
ISNAN() or IEEE_IS_NAN() is a NaN.

Differential Revision: https://reviews.llvm.org/D154377
2023-07-03 13:20:17 -07:00
Peter Klausler
6c5ba7cddf
[flang] Check constant POS/LEN arguments IBITS even when not both constant
Apply compile-time checks to the values supplied for the POS and LEN
arguments of the IBITS intrinsic function even when only one of them
is a known constant.

Differential Revision: https://reviews.llvm.org/D154376
2023-07-03 13:04:14 -07:00
Peter Klausler
b2b43794b9
[flang] Warn about overflow after folding HYPOT
The code that folds the intrinsic function HYPOT was neglecting to
warn the programmer about overflow when it occurs.

Differential Revision: https://reviews.llvm.org/D154371
2023-07-03 11:21:32 -07:00
Peter Klausler
fa459d9882
[flang] Rewrite "1*j" to "(j)", not "j", when j is a variable
Expression folding currently unconditionally rewrites "1*j"
to "j", which is wrong when "j" is a variable, as it transforms
an expression into a variable and can lead to incorrect associations
in contexts like an actual argument or an ASSOCIATE selector.
Transform "1*j" to a parenthesized "(j)" when "j" is a variable.
Fixes LLVM bug https://github.com/llvm/llvm-project/issues/63259.

Differential Revision: https://reviews.llvm.org/D153457
2023-06-22 07:05:51 -07:00
Kelvin Li
4c7ae4ad31 [flang] Improve compiler_version test (NFC)
This patch is to allow an extra string prepended to the compiler_version.
The check is enhanced to verify that "flang version" is followed by
numbers in the form of "n.n.n".

Differential Revision: https://reviews.llvm.org/D152572
2023-06-11 21:49:03 -04:00
Peter Klausler
7db4c583db
[flang] Fix crash in shape analysis of PACK()
A CHECK() was firing when a call to the PACK intrinsic does not have a
VECTOR= argument and at least one dimension of the shape of the ARRAY=
argument could not be determined.  The CHECK was inappropriate, since
this can of course happen, such as when that argument is the result
of the SPREAD() intrinsic with non-constant DIM= or NCOPIES= arguments.
Replace with an if() statement.

Differential Revision: https://reviews.llvm.org/D152212
2023-06-05 19:32:37 -07:00
Hussain Kadhem
541f5c4a6d Flang implementation for COMPILER_VERSION and COMPILER_OPTIONS intrinsics
This revision implements the Fortran intrinsic procedures COMPILER_VERSION and COMPILER_OPTIONS from the iso_fortran_env module.
To be able to set the COMPILER_OPTIONS string according to the original compiler driver invocation, a string is passed to the frontend driver using the environment variable FLANG_COMPILER_OPTIONS_STRING, for lack of a better mechanism.

Fixes #59233

Reviewed By: awarzynski

Differential Revision: https://reviews.llvm.org/D140524
2023-06-01 12:35:16 -04:00
Peter Klausler
0e05ab6745
[flang] Fix interpretations of x87 80-bit Inf/NaN
Current implementations of x87 80-bit extended precision floating
point interpret 7FFF8000000000000000 as +Inf, not a Nan.  The explicit
MSB in the significand must be set for an infinity.

Differential Revision: https://reviews.llvm.org/D151739
2023-05-31 09:22:17 -07:00
Peter Klausler
71d5a94985
[flang] Don't fold SIZE()/SHAPE() into expression referencing optional dummy arguments
When computing the shape of an expression at compilation time as part of
folding an intrinsic function like SIZE(), don't create an expression that
increases a dependence on the presence of an optional dummy argument.

Differential Revision: https://reviews.llvm.org/D151737
2023-05-31 08:57:18 -07:00
Tobias Hieta
f98ee40f4b
[NFC][Py Reformat] Reformat python files in the rest of the dirs
This is an ongoing series of commits that are reformatting our
Python code. This catches the last of the python files to
reformat. Since they where so few I bunched them together.

Reformatting is done with `black`.

If you end up having problems merging this commit because you
have made changes to a python file, the best way to handle that
is to run git checkout --ours <yourfile> and then reformat it
with black.

If you run into any problems, post to discourse about it and
we will try to help.

RFC Thread below:

https://discourse.llvm.org/t/rfc-document-and-standardize-python-code-style

Reviewed By: jhenderson, #libc, Mordante, sivachandra

Differential Revision: https://reviews.llvm.org/D150784
2023-05-25 11:17:05 +02:00
Peter Klausler
d71297ce74
[flang] Fix SPACING() of very small values
SPACING() must return TINY() for zero arguments (which we do)
and also for subnormal values smaller than TINY() in absolute value,
which we get wrong.  Fix folding and the runtime.

Differential Revision: https://reviews.llvm.org/D151272
2023-05-24 08:04:39 -07:00
Slava Zakharin
c8b50b8600 [flang] Prevent character length setting with dangling ac-do-variable.
This fixes LEN inquiry on array constructors where the length
expression includes references to the ac-do-variable.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D150149
2023-05-08 16:57:07 -07:00
Jean Perier
6f5df4199e [flang] Fold IS_CONTIGUOUS for TYPE(*) when possible
TYPE(*) arguments fell through in IS_CONTIGUOUS folding
because they are not Expr<SomeType>. Expose entry point for
symbols in IsContiguous and use that.

The added test revealed that IS_CONTIGUOUS was folded to
false for assumed rank arguments. Fix this: the contiguity of
assumed rank without the CONTIGUOUS argument can only be
verified at runtime.

Differential Revision: https://reviews.llvm.org/D148128
2023-04-14 08:42:25 +02:00
Leandro Lupori
5e5176adb1 [flang] Don't fold operation when shapes differ
When folding a binary operation between two array constructors, it
is necessary to check if each value contained in the left operand
has the same rank and shape as the one on the right.
Otherwise, lowering would end up with an operation between values
of different ranks/shapes, which could result in a crash.

For instance, the following code was crashing the compiler:
integer :: x(4), y(2, 2), z(4)

z = (/x/) + (/y/)

Fixes #60229

Reviewed By: klausler, jeanPerier

Differential Revision: https://reviews.llvm.org/D147181
2023-03-31 09:13:14 -03:00
Peter Klausler
e3b6b9299c [flang] Emit error when a positional actual argument follows an argument with a keyword
A positional (non-keyword) actual argument or alternate return label is not
allowed to follow an actual argument with a keyword.

Differential Revision: https://reviews.llvm.org/D146575
2023-03-27 15:24:14 -07:00
Peter Klausler
e6be8da14e [flang] Fix crash in folding TRANSFER() with MOLD=substring
When a substring appears as the MOLD= argument to TRANSFER(),
it's possible for the compiler to assert if it can't figure
out a constant length for the substring.  Fix.

Differential Revision: https://reviews.llvm.org/D145740
2023-03-10 08:40:01 -08:00