aboutsummaryrefslogtreecommitdiff
path: root/flang/lib/Optimizer/CodeGen/TargetRewrite.cpp
AgeCommit message (Collapse)AuthorFilesLines
2025-04-23[flang][cuda] Carry over the CUDA attribute in target rewrite (#136811)Valentin Clement (バレンタイン クレメン)1-0/+5
2025-04-15[flang] Propagate fast-math flags in target-rewrite (#135723)Asher Mancinelli1-0/+1
TargetRewritePass was dropping fast-math flags. Add the flags to the new call ops and update tests.
2025-03-12[flang] also set llvm ABI argument attributes on direct calls (#130736)jeanPerier1-29/+29
So far, flang was not setting argument attributes on direct calls assuming that putting them on the function operation was enough. It was clarified in https://github.com/llvm/llvm-project/commit/38565da5259729898c2a552d54b72f3314241734 that they must be set on both call and functions, even for direct calls. Crashes have been observed because of the lack of the attribute when compiling `abs(x)` at `O2` and above on X86-64 for complex(16).
2025-02-12[flang] add ABI argument attributes in indirect calls (#126896)jeanPerier1-9/+34
Last piece that implements the TODO for sret and byval setting on indirect calls. This includes a fix to the codegen last patch. I thought types in in type attributes were automatically converted in dialect conversion passes, but that is not the case. The sret and byval type needs to be converted to llvm types in codegen (mlir FuncOp conversion is doing a similar conversion).
2025-02-03[mlir] share argument attributes interface between calls and callables (#123176)jeanPerier1-0/+2
This patch shares core interface methods dealing with argument and result attributes from CallableOpInterface with the CallOpInterface and makes them mandatory to gives more consistent guarantees about concrete operations using these interfaces. This allows adding argument attributes on call like operations, which is sometimes required to get proper ABI, like with llvm.call (and llvm.invoke). The patch adds optional `arg_attrs` and `res_attrs` attributes to operations using these interfaces that did not have that already. They can then re-use the common "rich function signature" printing/parsing helpers if they want (for the LLVM dialect, this is done in the next patch). Part of RFC: https://discourse.llvm.org/t/mlir-rfc-adding-argument-and-result-attributes-to-llvm-call/84107
2025-01-30[Flang][MLIR] Extend DataLayout utilities to have basic GPU Module support ↵agozillon1-1/+1
(#123149) As there is now certain areas where we now have the possibility of having either a ModuleOp or GPUModuleOp and both of these modules can have DataLayout's and we may require utilising the DataLayout utilities in these areas I've taken the liberty of trying to extend them for use with both. Those with more knowledge of how they wish the GPUModuleOp's to interact with their parent ModuleOp's DataLayout may have further alterations they wish to make in the future, but for the moment, it'll simply utilise the basic data layout construction which I believe combines parent and child datalayouts from the ModuleOp and GPUModuleOp. If there is no GPUModuleOp DataLayout it should default to the parent ModuleOp. It's worth noting there is some weirdness if you have two module operations defining builtin dialect DataLayout Entries, it appears the combinatorial functionality for DataLayouts doesn't support the merging of these. This behaviour is useful for areas like: https://github.com/llvm/llvm-project/pull/119585/files#diff-19fc4bcb38829d085e25d601d344bbd85bf7ef749ca359e348f4a7c750eae89dR1412 where we have a crossroads between the two different module operations.
2024-12-16[flang][cuda] Adapt TargetRewrite to support gpu.launch_func (#119933)Valentin Clement (バレンタイン クレメン)1-8/+31
2024-12-10[flang][cuda] Update target rewrite to work on gpu.func (#119283)Valentin Clement (バレンタイン クレメン)1-49/+61
Update the pass so it can perform the signature rewrite on gpu.func.
2024-12-04Reland "[flang][cuda] Run target rewrite in gpu.module" (#118682)Valentin Clement (バレンタイン クレメン)1-0/+6
#118679
2024-12-04Revert "[flang][cuda] Run target rewrite in gpu.module" (#118679)Valentin Clement (バレンタイン クレメン)1-6/+0
Reverts llvm/llvm-project#118592
2024-12-04[flang][cuda] Run target rewrite in gpu.module (#118592)Valentin Clement (バレンタイン クレメン)1-0/+6
Apply signature conversion for `func.func` in the gpu.module. More work will need to be done for gpu.func op and implement the NVVM ABI for conversion in the gpu module.
2024-12-02[flang] remove unused var after #118121 (#118295)jeanPerier1-4/+2
Fix https://lab.llvm.org/buildbot/#/builders/89/builds/11704
2024-12-02[flang][fir] fix ABI bug 116844 (#118121)jeanPerier1-27/+11
Fix issue #116844. The issue came from a look-up on the func.func for the sret attribute when lowering fir.call with character arguments. This was broken because the func.func may or may not have been rewritten when dealing with the fir.call, but the lookup assumed it had not been rewritten yet. If the func.func was rewritten and the result moved to a sret argument, the call was lowered as if the character was meant to be the result, leading to bad call code and an assert. It turns out that the whole logic is actually useless since fir.boxchar are never lowered as sret arguments, instead, lowering directly breaks the character result into the first two `fir.ref<>, i64` arguments. So, the sret case was actually never used, except in this bug. Hence, instead of fixing the logic (probably by looking for argument attributes on the call itself), just remove this logic that brings unnecessary complexity.
2024-10-14[flang] correctly deal with bind(c) derived type result ABI (#111969)jeanPerier1-30/+107
Derived type results of BIND(C) function should be returned according the the C ABI for returning the related C struct type. This currently did not happen since the abstract-result pass was forcing the Fortran ABI for all derived type results. use the bind_c attribute that was added on call/func/dispatch in FIR to prevent such rewrite in the abstract result pass, and update the target-rewrite pass to deal with the struct return ABI. So far, the target specific part of the target-rewrite is only implemented for X86-64 according to the "System V Application Binary Interface AMD64 v1", the other targets will hit a TODO, just like for BIND(C), VALUE derived type arguments. This intends to deal with #102113. This is a re-land of #111678 with an extra commit to keep rewriting `type(c_ptr)` results to `!fir.ref<none>` in the abstract result pass regardless of the ABIs.
2024-10-10Revert "[flang] correctly deal with bind(c) derived type result ABI" (#111858)jeanPerier1-107/+30
Reverts llvm/llvm-project#111678 Causes ARM failure in test suite. TYPE(C_PTR) result should not regress even if struct ABI no implemented for the target. https://lab.llvm.org/buildbot/#/builders/143/builds/2731 I need to revisit this.
2024-10-10[flang] correctly deal with bind(c) derived type result ABI (#111678)jeanPerier1-30/+107
Derived type results of BIND(C) function should be returned according the the C ABI for returning the related C struct type. This currently did not happen since the abstract-result pass was forcing the Fortran ABI for all derived type results. use the bind_c attribute that was added on call/func/dispatch in FIR to prevent such rewrite in the abstract result pass, and update the target-rewrite pass to deal with the struct return ABI. So far, the target specific part of the target-rewrite is only implemented for X86-64 according to the "System V Application Binary Interface AMD64 v1", the other targets will hit a TODO, just like for BIND(C), VALUE derived type arguments. This intends to deal with https://github.com/llvm/llvm-project/issues/102113.
2024-10-04[flang][FIR] remove fir.complex type and its fir.real element type (#111025)jeanPerier1-36/+12
Final patch of https://discourse.llvm.org/t/rfc-flang-replace-usages-of-fir-complex-by-mlir-complex-type/82292 Since fir.real was only still used as fir.complex element type, this patch removes it at the same time.
2024-10-03[flang] add procedure flags to fir.dispatch (#110970)jeanPerier1-1/+2
Currently, it is not possible to distinguish between BIND(C) from non-BIND(C) type bound procedure call at the FIR level. This will be a problem when dealing with derived type BIND(C) function where the ABI differ between BIND(C)/non-BIND(C) but the FIR signature looks like the same at the FIR level. Fix this by adding the Fortran procedure attributes to fir.distpatch, and propagating it until the related fir.call is generated in fir.dispatch codegen.
2024-09-16[flang][NFC] use llvm.intr.stacksave/restore instead of opaque calls (#108562)Tom Eccles1-11/+4
The new LLVM stack save/restore intrinsic operations are more convenient than function calls because they do not add function declarations to the module and therefore do not block the parallelisation of passes. Furthermore they could be much more easily marked with memory effects than function calls if that ever proved useful. This builds on top of #107879. Resolves #108016
2024-09-10[flang] Use LLVM dialect ops for stack save/restore in target-rewrite (#107879)jeanPerier1-11/+12
Mostly NFC, I was bothered by the declaration that were always made even if unsued, and I think using LLVM Ops is nicer anyway with regards to side effects here. ``` func.func private @llvm.stacksave.p0() -> !fir.ref<i8> func.func private @llvm.stackrestore.p0(!fir.ref<i8>) ``` There are other places in lowering that are using the calls instead of the LLVM intrinsics, but I will deal with them another time (the issue there is mostly to get the proper address space for the llvm.ptr type).
2024-07-16Add basic -mtune support (#98517)Alexis Perry-Holby1-1/+11
Initial implementation for the -mtune flag in Flang. This PR is a clean version of PR #96688, which is a re-land of PR #95043
2024-07-02mlir/LogicalResult: move into llvm (#97309)Ramkumar Ramachandra1-1/+1
This patch is part of a project to move the Presburger library into LLVM.
2024-06-25Revert "[flang] Add basic -mtune support" (#96678)Tarun Prabhu1-11/+1
Reverts llvm/llvm-project#95043
2024-06-25[flang] Add basic -mtune support (#95043)Alexis Perry-Holby1-1/+11
This PR adds -mtune as a valid flang flag and passes the information through to LLVM IR as an attribute on all functions. No specific architecture optimizations are added at this time.
2024-06-05[flang][CodeGen][NFC] Reduce TargetRewrite pass boilerplate (#94450)Tom Eccles1-10/+1
Tablegen can automatically generate the pass constructor. Tablegen will create a constructor for all of the pass options (not only the subset in the old constructor), but the pass options seem unused anyway. This pass does not require any modification to support alternative top-level ops. It walks all operations in the module. Functions have special handling (adding attributes, converting signatures) but this wouldn't make sense for top level operations in general.
2024-04-28Reapply "[mlir] Mark `isa/dyn_cast/cast/...` member functions depreca… ↵Christian Sigg1-10/+11
(#90406) …ted. (#89998)" (#90250) This partially reverts commit 7aedd7dc754c74a49fe84ed2640e269c25414087. This change removes calls to the deprecated member functions. It does not mark the functions deprecated yet and does not disable the deprecation warning in TypeSwitch. This seems to cause problems with MSVC.
2024-04-26Revert "[mlir] Mark `isa/dyn_cast/cast/...` member functions deprecated. ↵dyung1-11/+10
(#89998)" (#90250) This reverts commit 950b7ce0b88318f9099e9a7c9817d224ebdc6337. This change is causing build failures on a bot https://lab.llvm.org/buildbot/#/builders/216/builds/38157
2024-04-26[mlir] Mark `isa/dyn_cast/cast/...` member functions deprecated. (#89998)Christian Sigg1-10/+11
See https://mlir.llvm.org/deprecation and https://discourse.llvm.org/t/preferred-casting-style-going-forward.
2024-01-30[Flang][Lower] Attach target_cpu and target_features attributes to MLIR ↵Sergio Afonso1-4/+23
functions (#78289) This patch forwards the target CPU and features information from the Flang frontend to MLIR func.func operation attributes, which are later used to populate the target_cpu and target_features llvm.func attributes. This is achieved in two stages: 1. Introduce the `fir.target_cpu` and `fir.target_features` module attributes with information from the target machine immediately after the initial creation of the MLIR module in the lowering bridge. 2. Update the target rewrite flang pass to get this information from the module and pass it along to all func.func MLIR operations, respectively as attributes named `target_cpu` and `target_features`. These attributes will be automatically picked up during Func to LLVM dialect lowering and used to initialize the corresponding llvm.func named attributes. The target rewrite and FIR to LLVM lowering passes are updated with the ability to override these module attributes, and the `CodeGenSpecifics` optimizer class is augmented to make this information available to target-specific MLIR transformations. This completes a full flow by which target CPU and features make it all the way from compiler options to LLVM IR function attributes.
2024-01-12[flang] finish BIND(C) VALUE derived type passing ABI on X86-64 (#77742)jeanPerier1-60/+112
Derived type passed with VALUE in BIND(C) context must be passed like C struct and LLVM is not implementing the ABI for this (it is up to the frontends like clang). Previous patch #75802 implemented the simple cases where the derived type have one field, this patch implements the general case. Note that the generated LLVM IR is compliant from a X86-64 C ABI point of view and compatible with clang generated assembly, but that it is not guaranteed to match the LLVM IR signatures generated by clang for the C equivalent functions because several LLVM IR signatures may lead to the same X86-64 signature.
2023-12-20[flang] Add option to skip struct argument rewrite in target-rewrite (#75939)jeanPerier1-1/+15
Be consistent with complex and character rewrite so that the pass can be run selectively.
2023-12-12[flang] Add struct passing target rewrite hooks and partial X86-64 impl (#74829)jeanPerier1-163/+296
In the context of C/Fortran interoperability (BIND(C)), it is possible to give the VALUE attribute to a BIND(C) derived type dummy, which according to Fortran 2018 18.3.6 - 2. (4) implies that it must be passed like the equivalent C structure value. The way C structure value are passed is ABI dependent. LLVM does not implement the C struct ABI passing for LLVM aggregate type arguments. It is up to the front-end, like clang is doing, to split the struct into registers or pass the struct on the stack (llvm "byval") as required by the target ABI. So the logic for C struct passing sits in clang. Using it from flang requires setting up a lot of clang context and to bridge FIR/MLIR representation to clang AST representation for function signatures (in both directions). It is a non trivial task. See https://stackoverflow.com/questions/39438033/passing-structs-by-value-in-llvm-ir/75002581#75002581. Since BIND(C) struct are rather limited as opposed to generic C struct (e.g. no bit fields). It is easier to provide a limited implementation of it for the case that matter to Fortran. This patch: - Updates the generic target rewrite pass to keep track of both the new argument type and attributes. The motivation for this is to be able to tell if a previously marshalled argument is passed in memory (it is a C pointer), or if it is being passed on the stack (has the byval llvm attributes). - Adds an entry point in the target specific codegen to marshal struct arguments, and use it in the generic target rewrite pass. - Implements limited support for the X86-64 case. So far, the support allows telling if a struct must be passed in register or on the stack, and to deal with the stack case. The register case is left TODO in this patch. The X86-64 ABI implemented is the System V ABI for AMD64 version 1.0
2023-06-05[flang] Store KindMapping by value in FirOpBuilderTom Eccles1-5/+3
Previously only a constant reference was stored in the FirOpBuilder. However, a lot of code was merged using FirOpBuilder builder{rewriter, getKindMapping(mod)}; This is incorrect because the KindMapping returned will go out of scope as soon as FirOpBuilder's constructor had run. This led to an infinite loop running some tests using HLFIR (because the stack space containing the kind mapping was re-used and corrupted). One solution would have just been to fix the incorrect call sites, however, as a large number of these had already made it past review, I decided to instead change FirOpBuilder to store its own copy of the KindMapping. This is not costly because nearly every time we construct a KindMapping is exclusively to construct a FirOpBuilder. To make this common pattern simpler, I added a new constructor to FirOpBuilder which calls getKindMapping(). Differential Revision: https://reviews.llvm.org/D151881
2023-04-26[flang] Restore stack after allocas created by TargetRewrite.Slava Zakharin1-11/+71
This resolves issues with running out of stack on examples like https://fortran-lang.discourse.group/t/modern-fortran-sample-code/2019/18 reported by @clementval. When target rewrite creates alloca(s) around a call, we need to insert stacksave/stackrestore to free the allocated stack. Better performant code may be achieved by placing the alloca(s) outside of loops, but the placement has to behave correctly with regards to OpenMP/OpenACC/etc. dialect operations that have special representation for "private" objects. This is a concervative fix for correctness issue. Differential Revision: https://reviews.llvm.org/D149222
2023-04-10[flang][NFC] Move TypeConverter.h header file to include dirValentin Clement1-1/+1
After the extraction of the TypeConverter, move the header files to the include dir so the shared library build is fine. Reviewed By: PeteSteinfeld Differential Revision: https://reviews.llvm.org/D147979
2023-03-09Break circular dependency between FIR dialect and utilitiesRenaud-K1-1/+1
2023-03-07[flang] Enable target rewrite for int args/rets of bind(c) functions.Slava Zakharin1-11/+16
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-02-10[flang] Fixed selective TargetRewrite.Slava Zakharin1-1/+16
Some conversions were still happening under no-complex/character-conversion options. This change fixes that and adds a LIT test. Differential Revision: https://reviews.llvm.org/D143685
2023-01-30[flang] Use proper attributes for runtime calls with 'i1' arguments/returns.Slava Zakharin1-9/+58
Clang uses signext/zeroext attributes for integer arguments shorter than the default 'int' type on a target. So Flang has to match this for functions from Fortran runtime and also for BIND(C) routines. This patch implements ABI adjustments only for Fortran runtime calls. BIND(C) part will be done separately. This resolves https://github.com/llvm/llvm-project/issues/58579 Differential Revision: https://reviews.llvm.org/D142677
2023-01-07[flang] Use std::optional instead of llvm::Optional (NFC)Kazu Hirata1-2/+2
This patch replaces (llvm::|)Optional< with std::optional<. I'll post a separate patch to remove #include "llvm/ADT/Optional.h". This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2023-01-07[flang] Add #include <optional> (NFC)Kazu Hirata1-0/+1
This patch adds #include <optional> to those files containing llvm::Optional<...> or Optional<...>. I'll post a separate patch to actually replace llvm::Optional with std::optional. This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
2022-12-10[mlir] FunctionOpInterface: turn required attributes into interface methods ↵Jeff Niu1-1/+2
(Reland) Reland D139447, D139471 With flang actually working - FunctionOpInterface: make get/setFunctionType interface methods This patch removes the concept of a `function_type`-named type attribute as a requirement for implementors of FunctionOpInterface. Instead, this type should be provided through two interface methods, `getFunctionType` and `setFunctionTypeAttr` (*Attr because functions may use different concrete function types), which should be automatically implemented by ODS for ops that define a `$function_type` attribute. This also allows FunctionOpInterface to materialize function types if they don't carry them in an attribute, for example. Importantly, all the function "helper" still accept an attribute name to use in parsing and printing functions, for example. - FunctionOpInterface: arg and result attrs dispatch to interface This patch removes the `arg_attrs` and `res_attrs` named attributes as a requirement for FunctionOpInterface and replaces them with interface methods for the getters, setters, and removers of the relevent attributes. This allows operations to use their own storage for the argument and result attributes. Reviewed By: jpienaar Differential Revision: https://reviews.llvm.org/D139736
2022-12-09Revert "[mlir] FunctionOpInterface: arg and result attrs dispatch to interface"David Spickett1-2/+1
and "[flang] Fix flang after MLIR update" This reverts commit dd74e6b6f4fb7a4685086a4895c1934e043f875b and 1897b67ae86470ad54f6baea6f220933d8053b5b due to ongoing test failures on flang bots e.g. https://lab.llvm.org/buildbot/#/builders/179/builds/5050
2022-12-08[flang] Fix flang after MLIR updateJeff Niu1-1/+2
2022-12-01Revert "[flang] Use proper attributes for runtime calls with 'i1' ↵Slava Zakharin1-58/+9
arguments/returns." This reverts commit d5b0de35bdd9a3f4d4a093e7938b06add34678eb.
2022-11-30[flang] Use proper attributes for runtime calls with 'i1' arguments/returns.Slava Zakharin1-9/+58
Clang uses signext/zeroext attributes for integer arguments shorter than the default 'int' type on a target. So Flang has to match this for functions from Fortran runtime and also for BIND(C) routines. This patch implements ABI adjustments only for Fortran runtime calls. BIND(C) part will be done separately. This resolves https://github.com/llvm/llvm-project/issues/58579 Differential Revision: https://reviews.llvm.org/D137050
2022-11-28[flang] Adapt target rewrite for fir.dispatch operationValentin Clement1-2/+15
Handle rewriting dispatch operation with complex arguments or return. sret will be done in a separate patch. Reviewed By: jeanPerier, PeteSteinfeld Differential Revision: https://reviews.llvm.org/D138820
2022-11-18Revert "[flang] Use proper attributes for runtime calls with 'i1' ↵Slava Zakharin1-58/+9
arguments/returns." Buildbot might be failing because of this: https://lab.llvm.org/buildbot/#/builders/65/builds/7283 This reverts commit 396ead93e3cead59727947afdea1fc2b49f0fc34.
2022-11-18[flang] Use proper attributes for runtime calls with 'i1' arguments/returns.Slava Zakharin1-9/+58
Clang uses signext/zeroext attributes for integer arguments shorter than the default 'int' type on a target. So Flang has to match this for functions from Fortran runtime and also for BIND(C) routines. This patch implements ABI adjustments only for Fortran runtime calls. BIND(C) part will be done separately. This resolves https://github.com/llvm/llvm-project/issues/58579 Differential Revision: https://reviews.llvm.org/D137050
2022-11-03[Flang] Run clang-format on all flang filesPeter Steinfeld1-11/+11
This will make it easier for me to do reviews. Differential Revision: https://reviews.llvm.org/D137291