FLANG
Utils.h
1//===-- Optimizer/Support/Utils.h -------------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef FORTRAN_OPTIMIZER_SUPPORT_UTILS_H
14#define FORTRAN_OPTIMIZER_SUPPORT_UTILS_H
15
16#include "flang/Optimizer/Builder/FIRBuilder.h"
17#include "flang/Optimizer/Builder/Todo.h"
18#include "flang/Optimizer/Dialect/CUF/Attributes/CUFAttr.h"
19#include "flang/Optimizer/Dialect/FIROps.h"
20#include "flang/Optimizer/Dialect/FIRType.h"
21#include "flang/Optimizer/Support/FatalError.h"
22#include "flang/Support/default-kinds.h"
23#include "mlir/Dialect/Arith/IR/Arith.h"
24#include "mlir/Dialect/Func/IR/FuncOps.h"
25#include "mlir/IR/BuiltinAttributes.h"
26#include "mlir/IR/BuiltinOps.h"
27#include "mlir/Interfaces/FunctionInterfaces.h"
28#include "llvm/ADT/StringRef.h"
29#include <string>
30
31#include "flang/Optimizer/CodeGen/TypeConverter.h"
32
33namespace fir {
35inline std::int64_t toInt(mlir::arith::ConstantOp cop) {
36 return mlir::cast<mlir::IntegerAttr>(cop.getValue())
37 .getValue()
38 .getSExtValue();
39}
40
41// Translate front-end KINDs for use in the IR and code gen.
42inline std::vector<fir::KindTy>
43fromDefaultKinds(const Fortran::common::IntrinsicTypeDefaultKinds &defKinds) {
44 return {static_cast<fir::KindTy>(defKinds.GetDefaultKind(
45 Fortran::common::TypeCategory::Character)),
46 static_cast<fir::KindTy>(
47 defKinds.GetDefaultKind(Fortran::common::TypeCategory::Complex)),
48 static_cast<fir::KindTy>(defKinds.doublePrecisionKind()),
49 static_cast<fir::KindTy>(
50 defKinds.GetDefaultKind(Fortran::common::TypeCategory::Integer)),
51 static_cast<fir::KindTy>(
52 defKinds.GetDefaultKind(Fortran::common::TypeCategory::Logical)),
53 static_cast<fir::KindTy>(
54 defKinds.GetDefaultKind(Fortran::common::TypeCategory::Real))};
55}
56
57inline std::string mlirTypeToString(mlir::Type type) {
58 std::string result{};
59 llvm::raw_string_ostream sstream(result);
60 sstream << type;
61 return result;
62}
63
64inline std::optional<int> mlirFloatTypeToKind(mlir::Type type) {
65 if (type.isF16())
66 return 2;
67 else if (type.isBF16())
68 return 3;
69 else if (type.isF32())
70 return 4;
71 else if (type.isF64())
72 return 8;
73 else if (type.isF80())
74 return 10;
75 else if (type.isF128())
76 return 16;
77 return std::nullopt;
78}
79
80inline std::string mlirTypeToIntrinsicFortran(fir::FirOpBuilder &builder,
81 mlir::Type type,
82 mlir::Location loc,
83 const llvm::Twine &name) {
84 if (auto floatTy = mlir::dyn_cast<mlir::FloatType>(type)) {
85 if (std::optional<int> kind = mlirFloatTypeToKind(type))
86 return "REAL(KIND="s + std::to_string(*kind) + ")";
87 } else if (auto cplxTy = mlir::dyn_cast<mlir::ComplexType>(type)) {
88 if (std::optional<int> kind = mlirFloatTypeToKind(cplxTy.getElementType()))
89 return "COMPLEX(KIND="s + std::to_string(*kind) + ")";
90 } else if (type.isUnsignedInteger()) {
91 if (type.isInteger(8))
92 return "UNSIGNED(KIND=1)";
93 else if (type.isInteger(16))
94 return "UNSIGNED(KIND=2)";
95 else if (type.isInteger(32))
96 return "UNSIGNED(KIND=4)";
97 else if (type.isInteger(64))
98 return "UNSIGNED(KIND=8)";
99 else if (type.isInteger(128))
100 return "UNSIGNED(KIND=16)";
101 } else if (type.isInteger(8))
102 return "INTEGER(KIND=1)";
103 else if (type.isInteger(16))
104 return "INTEGER(KIND=2)";
105 else if (type.isInteger(32))
106 return "INTEGER(KIND=4)";
107 else if (type.isInteger(64))
108 return "INTEGER(KIND=8)";
109 else if (type.isInteger(128))
110 return "INTEGER(KIND=16)";
111 else if (type == fir::LogicalType::get(builder.getContext(), 1))
112 return "LOGICAL(KIND=1)";
113 else if (type == fir::LogicalType::get(builder.getContext(), 2))
114 return "LOGICAL(KIND=2)";
115 else if (type == fir::LogicalType::get(builder.getContext(), 4))
116 return "LOGICAL(KIND=4)";
117 else if (type == fir::LogicalType::get(builder.getContext(), 8))
118 return "LOGICAL(KIND=8)";
119
120 fir::emitFatalError(loc, "unsupported type in " + name + ": " +
121 fir::mlirTypeToString(type));
122}
123
124inline void intrinsicTypeTODO(fir::FirOpBuilder &builder, mlir::Type type,
125 mlir::Location loc,
126 const llvm::Twine &intrinsicName) {
127 TODO(loc,
128 "intrinsic: " +
129 fir::mlirTypeToIntrinsicFortran(builder, type, loc, intrinsicName) +
130 " in " + intrinsicName);
131}
132
133inline void intrinsicTypeTODO2(fir::FirOpBuilder &builder, mlir::Type type1,
134 mlir::Type type2, mlir::Location loc,
135 const llvm::Twine &intrinsicName) {
136 TODO(loc,
137 "intrinsic: {" +
138 fir::mlirTypeToIntrinsicFortran(builder, type2, loc, intrinsicName) +
139 ", " +
140 fir::mlirTypeToIntrinsicFortran(builder, type2, loc, intrinsicName) +
141 "} in " + intrinsicName);
142}
143
144inline std::pair<Fortran::common::TypeCategory, KindMapping::KindTy>
145mlirTypeToCategoryKind(mlir::Location loc, mlir::Type type) {
146 if (auto floatTy = mlir::dyn_cast<mlir::FloatType>(type)) {
147 if (std::optional<int> kind = mlirFloatTypeToKind(type))
148 return {Fortran::common::TypeCategory::Real, *kind};
149 } else if (auto cplxTy = mlir::dyn_cast<mlir::ComplexType>(type)) {
150 if (std::optional<int> kind = mlirFloatTypeToKind(cplxTy.getElementType()))
151 return {Fortran::common::TypeCategory::Complex, *kind};
152 } else if (type.isInteger(8))
153 return {type.isUnsignedInteger() ? Fortran::common::TypeCategory::Unsigned
154 : Fortran::common::TypeCategory::Integer,
155 1};
156 else if (type.isInteger(16))
157 return {type.isUnsignedInteger() ? Fortran::common::TypeCategory::Unsigned
158 : Fortran::common::TypeCategory::Integer,
159 2};
160 else if (type.isInteger(32))
161 return {type.isUnsignedInteger() ? Fortran::common::TypeCategory::Unsigned
162 : Fortran::common::TypeCategory::Integer,
163 4};
164 else if (type.isInteger(64))
165 return {type.isUnsignedInteger() ? Fortran::common::TypeCategory::Unsigned
166 : Fortran::common::TypeCategory::Integer,
167 8};
168 else if (type.isInteger(128))
169 return {type.isUnsignedInteger() ? Fortran::common::TypeCategory::Unsigned
170 : Fortran::common::TypeCategory::Integer,
171 16};
172 else if (auto logicalType = mlir::dyn_cast<fir::LogicalType>(type))
173 return {Fortran::common::TypeCategory::Logical, logicalType.getFKind()};
174 else if (auto charType = mlir::dyn_cast<fir::CharacterType>(type))
175 return {Fortran::common::TypeCategory::Character, charType.getFKind()};
176 else if (mlir::isa<fir::RecordType>(type))
177 return {Fortran::common::TypeCategory::Derived, 0};
178 fir::emitFatalError(loc, "unsupported type: " + fir::mlirTypeToString(type));
179}
180
185fir::TypeInfoOp
186lookupTypeInfoOp(fir::RecordType recordType, mlir::ModuleOp module,
187 const mlir::SymbolTable *symbolTable = nullptr);
188
193fir::TypeInfoOp
194lookupTypeInfoOp(llvm::StringRef name, mlir::ModuleOp module,
195 const mlir::SymbolTable *symbolTable = nullptr);
196
200std::optional<llvm::ArrayRef<int64_t>> getComponentLowerBoundsIfNonDefault(
201 fir::RecordType recordType, llvm::StringRef component,
202 mlir::ModuleOp module, const mlir::SymbolTable *symbolTable = nullptr);
203
206std::optional<bool>
207isRecordWithFinalRoutine(fir::RecordType recordType, mlir::ModuleOp module,
208 const mlir::SymbolTable *symbolTable = nullptr);
209
211mlir::LLVM::ConstantOp
212genConstantIndex(mlir::Location loc, mlir::Type ity,
213 mlir::ConversionPatternRewriter &rewriter,
214 std::int64_t offset);
215
220mlir::Value computeElementDistance(mlir::Location loc,
221 mlir::Type llvmObjectType, mlir::Type idxTy,
222 mlir::ConversionPatternRewriter &rewriter,
223 const mlir::DataLayout &dataLayout);
224
225// Compute the alloc scale size (constant factors encoded in the array type).
226// We do this for arrays without a constant interior or arrays of character with
227// dynamic length arrays, since those are the only ones that get decayed to a
228// pointer to the element type.
229mlir::Value genAllocationScaleSize(mlir::Location loc, mlir::Type dataTy,
230 mlir::Type ity,
231 mlir::ConversionPatternRewriter &rewriter);
232
237mlir::Value integerCast(const fir::LLVMTypeConverter &converter,
238 mlir::Location loc,
239 mlir::ConversionPatternRewriter &rewriter,
240 mlir::Type ty, mlir::Value val, bool fold = false);
241
247std::optional<bool> isNewAllocationResult(mlir::OpResult result);
248
251std::string getPresentableFunctionName(mlir::FunctionOpInterface func);
252} // namespace fir
253
254#endif // FORTRAN_OPTIMIZER_SUPPORT_UTILS_H
Definition default-kinds.h:26
Definition AbstractConverter.h:37
mlir::Value integerCast(const fir::LLVMTypeConverter &converter, mlir::Location loc, mlir::ConversionPatternRewriter &rewriter, mlir::Type ty, mlir::Value val, bool fold=false)
Definition Utils.cpp:110
std::string getPresentableFunctionName(mlir::FunctionOpInterface func)
Definition Utils.cpp:152
std::optional< bool > isNewAllocationResult(mlir::OpResult result)
Definition Utils.cpp:135
std::optional< bool > isRecordWithFinalRoutine(fir::RecordType recordType, mlir::ModuleOp module, const mlir::SymbolTable *symbolTable=nullptr)
Definition Utils.cpp:55
std::optional< llvm::ArrayRef< int64_t > > getComponentLowerBoundsIfNonDefault(fir::RecordType recordType, llvm::StringRef component, mlir::ModuleOp module, const mlir::SymbolTable *symbolTable=nullptr)
Definition Utils.cpp:40
std::int64_t toInt(mlir::arith::ConstantOp cop)
Return the integer value of a arith::ConstantOp.
Definition Utils.h:35
fir::TypeInfoOp lookupTypeInfoOp(fir::RecordType recordType, mlir::ModuleOp module, const mlir::SymbolTable *symbolTable=nullptr)
Definition Utils.cpp:18
mlir::LLVM::ConstantOp genConstantIndex(mlir::Location loc, mlir::Type ity, mlir::ConversionPatternRewriter &rewriter, std::int64_t offset)
Generate a LLVM constant value of type ity, using the provided offset.
Definition Utils.cpp:65
void emitFatalError(mlir::Location loc, const llvm::Twine &message, bool genCrashDiag=true)
Definition FatalError.h:25
mlir::Value computeElementDistance(mlir::Location loc, mlir::Type llvmObjectType, mlir::Type idxTy, mlir::ConversionPatternRewriter &rewriter, const mlir::DataLayout &dataLayout)
Definition Utils.cpp:73