Commit Graph

54 Commits

Author SHA1 Message Date
jeanPerier
0ac1dfa311 [flang] lower c_f_procpointer (#76071)
This is equivalent to a procedure pointer assignment, except that the
target is a C_FUNPTR.
2023-12-22 11:01:03 +01:00
jeanPerier
f3fa603d74 [flang] lower ASSOCIATED for procedure pointers (#76067)
This is a lot less complex than the data case where the shape has to be
accounted for, so the implementation is done inline.

One corner case will not be supported correctly for now: the case where
POINTER and TARGET points to the same internal procedure may return
false because lowering is creating fir.embox_proc each time the address
of an internal procedure is taken, so different thunk for the same
internal procedure/host link may be created and compare to false. This
will be fixed in a later patch that moves creating of internal procedure
fir.embox_proc in the host so that the addresses are the same when the
host link is the same. This change is required to properly support the
required lifetime of internal procedure addresses anyway (should be the
always be the lifetime of the host, even when the address is taken in an
internal procedure).
2023-12-22 10:59:01 +01:00
Kazu Hirata
11efccea8f [flang] Use StringRef::{starts,ends}_with (NFC)
This patch replaces uses of StringRef::{starts,ends}with with
StringRef::{starts,ends}_with for consistency with
std::{string,string_view}::{starts,ends}_with in C++20.

I'm planning to deprecate and eventually remove
StringRef::{starts,ends}with.
2023-12-13 23:48:53 -08:00
vdonaldson
3aba9264b3 [flang] IEEE_ARITHMETIC and IEEE_EXCEPTIONS intrinsic module procedures (#74138)
Implement a selection of intrinsic module procedures that involve
exceptions.

 - IEEE_GET_FLAG
 - IEEE_GET_HALTING_MODE
 - IEEE_GET_MODES
 - IEEE_GET_STATUS
 - IEEE_LOGB
 - [f23] IEEE_MAX, IEEE_MAX_MAG, IEEE_MAX_NUM, IEEE_MAX_NUM_MAG
 - [f23] IEEE_MIN, IEEE_MIN_MAG, IEEE_MIN_NUM, IEEE_MIN_NUM_MAG
 - IEEE_QUIET_EQ, IEEE_QUIET_GE, IEEE_QUIET_GT,
 - IEEE_QUIET_LE, IEEE_QUIET_LT, IEEE_QUIET_NE
 - IEEE_SET_FLAG
 - IEEE_SET_HALTING_MODE
 - IEEE_SET_MODES
 - IEEE_SET_STATUS
 - IEEE_SIGNALING_EQ, IEEE_SIGNALING_GE, IEEE_SIGNALING_GT,
 - IEEE_SIGNALING_LE, IEEE_SIGNALING_LT, IEEE_SIGNALING_NE
 - IEEE_SUPPORT_FLAG
 - IEEE_SUPPORT_HALTING
2023-12-04 09:55:54 -08:00
Pete Steinfeld
04b185302b [flang] Cleanup of NYI messages (#73740)
This update makes the user visible messages relating to features that
are not yet implemented be more consistent. I also cleaned up some of
the code.

For NYI messages that refer to intrinsics, I made sure the the message
begins with "not yet implemented: intrinsic:" to make them easier to
recognize.

I created some utility functions for NYI reporting that I put into
.../include/Optimizer/Support/Utils.h. These mainly convert MLIR types
to their Fortran equivalents.

I converted the NYI code to use the newly created utility functions.
2023-11-29 09:20:46 -08:00
jeanPerier
437a48b2d9 [flang] Handle absent optional in LOC (#73530)
LOC is a GNU extension, so gfortran is the reference for it, and it
accepts absent OPTIONAL and returns zero. Support this use case in flang
too.

Update the LOC test to use HLFIR while touching it.

Fixes https://github.com/llvm/llvm-project/issues/72823.
2023-11-29 13:21:41 +01:00
Yi Wu
de58aa8372 [flang] GETPID runtime and lower intrinsic implementation (#70442)
Runtime function GetPID calls the function getpid
from unistd.h or process.h base on the OS.
2023-11-13 10:31:36 +00:00
Pete Steinfeld
38c9fab8f3 [flang] Regularize TODO messages for coarray intrinsics (#70281)
Apply a tag to coarray intrinsics to make them easier to recognize along
with other coarray constructs.

See pull request #69227 for a similar change.
2023-10-26 05:50:38 -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
Kelvin Li
00769d69fb [flang] Add -fppc-native-vector-element-order option to control the element order in PowerPC vector types
This patch also adds a LIT test for the vec_cvf intrinsic that
can be affected by the option.

Co-authored-by: Mark Danial <Mark.Danial@ibm.com>
Co-authored-by: Daniel Chen <cdchen@ca.ibm.com>

Differential Revision: https://reviews.llvm.org/D155852
2023-08-04 17:11:30 -04:00
Kelvin Li
2c2d427ca3 [flang] Add a subset of PowerPC MMA (Matrix Multiply Accelerate) intrinsics
This patch includes the a subset of MMA intrinsics that are included in
the mma intrinsic module:

mma_assemble_acc
mma_assemble_pair
mma_build_acc
mma_disassemble_acc
mma_disassemble_pair

Submit on behalf of Daniel Chen <cdchen@ca.ibm.com>

Differential Revision: https://reviews.llvm.org/D155725
2023-07-27 14:30:06 -04:00
V Donaldson
ba531a55c6 [flang] Implement procedure IEEE_COPY_SIGN with mlir::math::CopySignOp 2023-07-25 10:52:48 -07:00
Razvan Lupusoru
1e27425ada [flang] Fix complex libm use logic problems
Fix the various complex libm selection logic issues from D155310:
- disableMlirComplex is set to false. This means that using mlir complex
is enabled. Yet, the current code still selects libm.
- If we enable mlir complex, we should not check if use approx is
enabled. Namely, we should use mlir complex either if we enable mlir
complex OR use approx is enabled.

To fix the issues, we flip the logic of `disableMlirComplex` to enable
instead. We set it to false by default since the intention from D155310
is to use libm by default. Then we use a logical `&&` with use approx
so that we select libm when BOTH mlir complex and use approx are
disabled.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D155737
2023-07-19 13:30:40 -07:00
David Truby
1ae04d1c89 [flang] Implement tand intrinsic
This implements the tand intrinsic by performing a multiplication
by pi/180 to the argument before calling tan inline.

This is a commonly provided extension that is used by OpenRadioss

Differential Revision: https://reviews.llvm.org/D154614
2023-07-19 14:42:04 +01:00
David Truby
3681a7dd78 [flang] Use libm functions for complex operations by default
This patch changes the default lowering for complex operations to use
the more accurate libm operations as opposed to the mlir complex
operations. This is necessary due to precision issues in the mlir
complex dialect that cause failures in e.g. the LAPACK tests.

The mlir complex dialect lowering will still be used when
`-fapprox-func` is set (and by extension `-ffast-math` and `-Ofast`)

Differential Revision: https://reviews.llvm.org/D155310
2023-07-19 14:34:38 +01:00
David Truby
f52c64b115 [flang] Add fastmath flags to localBuilder in IntrinsicCall
Currently the local builder used in IntrinsicCall doesn't have the
fastmath flags passed to it. This results in the fastmath attribute
not being added to certain runtime calls. This patch simply forwards
the fastmath flags from the parent builder.

Differential Revision: https://reviews.llvm.org/D154611
2023-07-11 18:53:31 +01:00
V Donaldson
09ea692d16 [flang] IEEE_ARITHMETIC intrinsic module procedures
Implement

 - IEEE_CLASS
 - IEEE_COPY_SIGN
 - IEEE_GET_ROUNDING_MODE
 - IEEE_IS_FINITE
 - IEEE_IS_NAN
 - IEEE_IS_NEGATIVE
 - IEEE_IS_NORMAL
 - IEEE_SET_ROUNDING_MODE
 - IEEE_SIGNBIT
 - IEEE_SUPPORT_ROUNDING
 - IEEE_UNORDERED
 - IEEE_VALUE

for all REAL kinds (2, 3, 4, 8, 10, 16) where applicable.
2023-06-29 16:46:22 -07:00
Jean Perier
1fb0707da6 [flang][hlfir] Simplify hlfir::convertToValue
Use hlfir::loadTrivialScalars to dereference pointer, allocatables, and
load numerical and logical scalars.

This has a small fallout on tests:

- load is done on the HLFIR entity (#0 of hlfir.declare) and not the FIR one (#1). This makes no difference at the FIR level (#1 and #0 only differs to account for assumed and explicit shape lower bounds).

- loadTrivialScalars get rids of allocatable fir.box for monomoprhic scalars
  (it is not needed). This exposed a bug in lowering of MERGE with
  a polymorphic and a monomorphic argument: when the monomorphic is not
  a fir.box, the polymorphic fir.class should not be reboxed but its
  address should be read.

Reviewed By: tblah

Differential Revision: https://reviews.llvm.org/D153252
2023-06-23 09:04:25 +02:00
Paul Scoropan
ce782ebdc7 [Flang] Split PowerPC-specific code out of IntrinsicCall into PPCIntrinsicCall
This patch moves PPC intrinsic generator code to PPCIntrinsicCall.cpp. In order to move PowerPC intrinsic code out of IntrinsicCall.cpp, we need to also move some declarations to IntrinsicCall.h. handlers[] and mathOperations[] were also chosen to be moved to the IntrinsicCall header. Similarly, ppcHandlers[] and ppcMathOperations[] were moved to the PPCIntrinsicCall header. There are future patches coming up that will introduce many new PPC intrinsics, these will now be defined in PPCIntrinsicCall.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D152460
2023-06-16 17:26:41 +00:00
Kelvin Li
99794e2239 [flang] Fix flang-aarch64-latest-gcc build failure
The failure is due to mismatch of the SmallVector parameter and the
return when built by gcc.
2023-06-13 23:17:59 -04:00
Kelvin Li
a9e1d2e760 [flang] Add PowerPC vec_add, vec_and, vec_mul, vec_sub and vec_xor intrinsics
Differential Revision: https://reviews.llvm.org/D151857
2023-06-13 16:05:21 -04:00
Christian Ulmann
48b126e30b [mlir][llvm] Ensure immediate usage in intrinsics
This commit changes intrinsics that have immarg parameter attributes to
model these parameters as attributes, instead of operands. Using
operands only works if the operation is an `llvm.mlir.constant`,
otherwise the exported LLVMIR is invalid.

Reviewed By: gysit

Differential Revision: https://reviews.llvm.org/D151692
2023-06-12 06:57:42 +00:00
Paul Scoropan
e96648a9bd [Flang] Replace intrinsic function type generators with single generic function type generator
In a future patch we plan on introducing a large set of Power-PC specific intrinsics. During our prototyping we found that the number of function type generators we were defining, plus those already defined, were reaching an unreasonable number. This patch introduces a generic function type generator function that can be used for almost all cases. The generator supports creating function types with up to 4 arguments and with arguments/return type of types: void, integer, real, and comlex. The intention is for a future patch, which introduces a set of PowerPC-specific vector intrinsics, to also introduce support in the generator for: integer vector, unsigned vector, and real vector types.

Reviewed By: luporl

Differential Revision: https://reviews.llvm.org/D151812
2023-06-08 15:17:22 +00:00
Shao-Ce SUN
68f55d78e3 [Flang] Map ieee_fma intrinsic to llvm.fma
Map `ieee_fma` intrinsic to LLVM IR as `llvm.fma`.

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D151872
2023-06-06 02:01:36 +08:00
Paul Scoropan
e0df106818 [Flang] Move several definitions to IntrinsicCall header for code cleanliness and reusability
In the future we intend to add support for many PowerPC-specific intrinsics that ideally will exist in a separate new PPCIntrinsicCall file. But first we need to move definitions to the IntrinsicCall header file to increase code cleanliness and readability and to make code reusable for when we add PPCIntrinsicCall.

Reviewed By: vzakhari

Differential Revision: https://reviews.llvm.org/D151715
2023-05-31 15:03:53 +00:00
Razvan Lupusoru
036549fc6c [flang] Inline array size call when dim is compile time constant
Instead of calling _FortranASizeDim, we can instead load extent
directly from descriptor. Add this support for cases where dim
is a known constant at compile time.

Reviewed By: clementval

Differential Revision: https://reviews.llvm.org/D150385
2023-05-11 14:50:43 -07:00
Kiran Chandramohan
c3a0df1903 [Flang] Change complex divide lowering
Currently complex division is lowered to a fir.divc operation and the
fir.divc is later converted to a sequence of llvm operations to perform
complex division, however this causes issues for extreme values when
the calculations overflow.

This patch changes the lowering of complex division to use the Intrinsic
Call functionality to lower into library calls (for single, double,
extended and quad precisions) or an MLIR complex dialect division operation
(for half and bfloat precisions).

 A new wrapper function `genLibSplitComplexArgsCall` is written to handle
 the case of the arguments of the Complex Library calls being split to
its real and imaginary real components.

Note 1: If the Complex To Standard conversion of division operation
matures then we can use it for all precisions. Currently it has the
same issues as the conversion of fir.divc.
Note 2: A previous patch (D145808) did the same but during conversion of
the fir.divc operation. But using function calls at that stage leads to
ABI issues since the conversion to LLVM is not aware of the complex target
rewrite.
Note 3: If the patch is accepted, fir.divc can be removed from FIR. We
can use the complex.div operation where any transformation is required.

Reviewed By: vzakhari, PeteSteinfeld, DavidTruby, jeanPerier

Differential Revision: https://reviews.llvm.org/D149546
2023-05-11 11:52:35 +00:00
Kiran Chandramohan
28e99cccf1 Revert "[Flang] Change complex divide lowering"
This reverts commit 1b9d0deb6d.

Reverting since the signature of the called division functions is not
correct.
2023-05-05 17:43:08 +00:00
Kiran Chandramohan
1b9d0deb6d [Flang] Change complex divide lowering
Currently complex division is lowered to a fir.divc operation and the
fir.divc is later converted to a sequence of llvm operations to perform
complex division, however this causes issues for extreme values when
the calculations overflow.

This patch changes the lowering of complex division to use the Intrinsic
Call functionality to lower into library calls (for single, double,
extended and quad precisions) or an MLIR complex dialect division operation
(for half and bfloat precisions).

Note 1: If the Complex To Standard conversion of division operation
matures then we can use it for all precisions. Currently it has the
same issues as the conversion of fir.divc.
Note 2: A previous patch (D145808) did the same but during conversion of
the fir.divc operation. But using function calls at that stage leads to
ABI issues since the conversion to LLVM is not aware of the complex target
rewrite.
Note 3: If the patch is accepted, fir.divc can be removed from FIR.

Reviewed By: vzakhari, PeteSteinfeld, DavidTruby

Differential Revision: https://reviews.llvm.org/D149546
2023-05-05 13:04:21 +00:00
Paul Scoropan
adc56f37cd [Flang][PowerPC] Implement PPC mtfsf/mtfsfi intrinsics
Implements the PowerPC mtfsf and mtfsfi intrinsics as well as introduces semantic error checking code for PowerPC intrinsics

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D144876
2023-04-04 13:42:00 +00:00
Jean Perier
99d9a700d3 [flang] move ASSOCIATED intrinsic optional TARGET handling
ASSOCIATED intrinsic TARGET handling is weird for OPTIONAL, because as
opposed to other intrinsic arguments, OPTIONAL allocatable and pointers
may be absent when passed to it, and a diassociated pointer TARGET is not
the same as when TARGET is not provided. Hence, it needs custom
handling in lowering.

The handling was done late (in genIntrinsicCall, without the semantic
context), and assumed it would be possible to retrieve the optionality
aspects, but this is brittle, and hard to share with HLFIR.
Move it in CustomIntrinsicCall that is intended to deal with these
corner case.

Also avoid using fir.box<None> as the related fir.if result, and used
the correct fir.box/fir.class type for the target: using a fir.box<None>
here is risky since fir.box<None> are now meant for scalar TYPE(*), and
the TARGET may be ranked.

Move the introduction of the fir.box<None> around the runtime (when
assumed rank are supported, these will become !fir.box<!fir.array<..xNone>>).

Differential Revision: https://reviews.llvm.org/D147224
2023-03-31 09:10:32 +02:00
Paul Scoropan
5a222c0b82 [Flang] [PowerPC] Implement remaining PPC math operation intrinsics that do not require semantic error checks
This review implements the following PowerPC math operations that we care about:
- fnabs
- fre
- fres
- frsqrte
- frsqrtes

None of these intrinsics require additional error checks in semantics. The interfaces handle checking types and kinds

Reviewed By: kkwli0

Differential Revision: https://reviews.llvm.org/D146139
2023-03-20 15:04:16 +00:00
Tom Eccles
43eb96cab8 [flang][hlfir] lower hlfir.matmul_transpose to runtime call
Depends on D145960

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D145961
2023-03-17 09:30:04 +00:00
Valentin Clement
942bc18d20 [flang] Rebox the polymorphic argument in merge intrinsic
When fsource or tsource is not polymorphic, the result is not
polymorphic. Rebox the polymoprhic arguement so the dynamic
type of the result is correct.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D146133
2023-03-15 16:32:29 +01:00
Slava Zakharin
c536367893 [flang] Avoid ICE in case of subprogram name clash with runtime namespace.
This is related to llvm-project#61074.
In general, it is undefined behavior if user subprogram is declared
with a name that matches a name of function from any runtime library
that Flang is using (e.g. FortranRuntime, libm, etc.). With this change-set
we avoid ICE for invalid calls generated during math lowering by
type casing the function before the call. This happens when a user function
call is lowered before the math function call with the same name.
To detect the name clash in cases when the math function call is lowered
before the user function call we set fir.runtime attribute for the math
functions and check it when we lower the user function call.

The warnings are currently emitted only in debug compiler and
under llvm debug options. I think they should be reported
in the same way as regular Flang warnings.

Note that this change-set does not resolve issues with the conversion
passes that might introduce libm calls after the lowering.

Differential Revision: https://reviews.llvm.org/D145653
2023-03-13 10:16:33 -07:00
Renaud-K
b07ef9e7cd Break circular dependency between FIR dialect and utilities 2023-03-09 15:24:51 -08:00
V Donaldson
18c6b057a7 [flang] is_iostat_end and is_iostat_eor intrinsics 2023-03-08 13:27:16 -08:00
David Truby
00671c0fa7 [flang] Implement atand intrinsic
This implements the atand intrinsic by performing a multiplication
by 180/pi to the result of a call to atan inline.

Differential Revision: https://reviews.llvm.org/D144885
2023-03-08 13:56:21 +00:00
Slava Zakharin
2c8d33897d [flang] Enable target rewrite for int args/rets of bind(c) functions.
BIND(C) subprograms must use the same target ABI as the C processor,
so 1/2-byte int args/rets must be rewritten to use signext attribute.
This change-set also sets fir.bindc_name for the math functions used
during lowering so that they can be fixed up as well (though, currently
none of them needs to be affected).

Differential Revision: https://reviews.llvm.org/D145537
2023-03-07 19:08:02 -08:00
Paul Scoropan
070dd18e31 [Flang] [PowerPC} Implement the floating-point conversion PowerPC intrinsics
All the fc* floating point conversion PowerPC intrinsics are simply lowered to their LLVM IR intrinsic counterparts and do not require any additional error checking.

Reviewed By: klausler, jeanPerier

Differential Revision: https://reviews.llvm.org/D145080
2023-03-06 09:50:48 -05:00
David Truby
75ddabd520 [flang] Implement ieee_is_normal
This patch adds an implementation of ieee_is_normal using a call
to llvm.is.fpclass.

Depends on D144649

Differential Revision: https://reviews.llvm.org/D144966
2023-03-06 14:46:48 +00:00
Valentin Clement
ed05dcc57b [flang] MERGE result is polymorphic only if TSOURCE and FSOURCE are polymorphic
16.9.129 point 4: the result is polymorphic if and only if both TSOURCE and
FSOURCE are polymorphic.

If neither TSOURCE and FSOURCE are polymorphic then the current behavior is
preserved.

Depends on D145058

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D145060
2023-03-01 15:46:14 +01:00
Valentin Clement
457c4fe3d9 [flang] Allow scalar boxed record type in intrinsic elemental lowering
Relax a bit the condition added in D144417 and allow scalar polymorphic entities
and boxed scalar record type.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D145058
2023-03-01 15:42:25 +01:00
David Truby
4ee8aef4de [flang] Implement isnan and ieee_is_nan intrinsics
To implement these we call the LLVM intrinsic is.fpclass indicating that
we are checking for either a quiet or signalling NaN.

Differential Revision: https://reviews.llvm.org/D144649
2023-03-01 13:28:28 +00:00
Valentin Clement
e77fc0dd76 [flang] Relax the check for polymorphic pointer in storage_size intrinsic
The runtime check was a too strong as the standard says.
> If it is polymorphic it shall not be an undefined pointer.
The check was checking if the pointer was associated.
Remove the check as other compilers do.

Depends on D144643

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D144644
2023-02-23 17:30:21 +01:00
Valentin Clement
b7691d7be0 [flang] Accept polymorphic component element in storage_size
When an element is extracted from a polymorphic array, it is
represented as a PolymorphicValue. The PolymorphicValue is
not a boxed value but holds the original polyrmophic array
and the element itself. This was raising an error in storage_size
lowering since we expect a boxed value to take advantage of
fir.box_elesize.
This patch handles PolymorphicValue correctly.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D144643
2023-02-23 17:28:25 +01:00
Valentin Clement
0c444ff514 [flang] Accept polymorphic scalar in elemental intrinsic lowering
When lowering an elemental intrinsic like MERGE, a scalar
polymorphic entity was not recognized as a scalar. Update the check
so polyrmorphic entity can be used.

Reviewed By: PeteSteinfeld

Differential Revision: https://reviews.llvm.org/D144417
2023-02-21 10:13:31 +01:00
Kelvin Li
838a4d348d [Flang] Add PowerPC intrinsics
This patch adds a subset of PowerPC intrinsics - fmadd,
fmsub, fnmadd and fnmsub.

Differential Revision: https://reviews.llvm.org/D143951
2023-02-18 11:56:03 -05:00
Valentin Clement
fe655717bf [flang] Support polymorphic inputs for the TRANSPOSE intrinsic
Force TRANSPOSE with polymorphic inputs through the runtime call
and carry the polymorphic type information from the matrix to
the result.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D143709
2023-02-12 16:58:50 +01:00
Valentin Clement
623c896aaf [flang] Support polymorphic inputs for TRANSFER intrinsic
Result must carry the polymorphic type information from the mold.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D143651
2023-02-09 20:17:31 +01:00