FLANG
FIROpPatterns.h
1//===-- FIROpPatterns.h -- FIR operation conversion patterns ----*- 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#ifndef FORTRAN_OPTIMIZER_CODEGEN_FIROPPATTERNS_H
10#define FORTRAN_OPTIMIZER_CODEGEN_FIROPPATTERNS_H
11
12#include "flang/Optimizer/CodeGen/TypeConverter.h"
13#include "mlir/Conversion/LLVMCommon/Pattern.h"
14#include "mlir/Dialect/LLVMIR/LLVMMemoryOps.h"
15#include "mlir/IR/SymbolTable.h"
16#include <type_traits>
17
18namespace mlir::LLVM {
19class ConstantOp;
20} // namespace mlir::LLVM
21
22namespace fir {
23
25
26static constexpr unsigned defaultAddressSpace = 0u;
27
69
74template <typename OpTy, typename NameT>
75OpTy lookupSymbolIn(mlir::Operation *symbolTableOp, NameT name,
76 mlir::SymbolTableCollection *symbolTables) {
77 if (!symbolTables)
78 return mlir::dyn_cast_or_null<OpTy>(
79 mlir::SymbolTable::lookupSymbolIn(symbolTableOp, name));
80 OpTy op;
81 if constexpr (std::is_same_v<NameT, mlir::SymbolRefAttr>)
82 op = symbolTables->lookupSymbolIn<OpTy>(symbolTableOp, name);
83 else
84 op = symbolTables->getSymbolTable(symbolTableOp).lookup<OpTy>(name);
85#ifdef EXPENSIVE_CHECKS
86 // A replacement is always inserted before the operation it replaces, so on
87 // a hit the linear walk must find the same operation.
88 assert((!op ||
89 op == mlir::dyn_cast_or_null<OpTy>(
90 mlir::SymbolTable::lookupSymbolIn(symbolTableOp, name))) &&
91 "symbol tables and module out of sync");
92#endif
93 return op;
94}
95
98void registerSymbol(mlir::SymbolTableCollection *symbolTables,
99 mlir::Operation *symbolTableOp, mlir::Operation *symbol);
100
104void replaceSymbol(mlir::SymbolTableCollection *symbolTables,
105 mlir::Operation *symbolTableOp, mlir::Operation *oldSymbol,
106 mlir::Operation *newSymbol);
107
109void unregisterSymbol(mlir::SymbolTableCollection *symbolTables,
110 mlir::Operation *symbolTableOp, mlir::Operation *symbol);
111
117void buildSymbolTables(mlir::SymbolTableCollection &symbolTables,
118 mlir::Operation *root);
119
120class ConvertFIRToLLVMPattern : public mlir::ConvertToLLVMPattern {
121public:
122 ConvertFIRToLLVMPattern(llvm::StringRef rootOpName,
123 mlir::MLIRContext *context,
124 const fir::LLVMTypeConverter &typeConverter,
125 const fir::FIRToLLVMPassOptions &options,
126 mlir::SymbolTableCollection *symbolTables = nullptr,
127 mlir::PatternBenefit benefit = 1);
128
129protected:
130 mlir::Type convertType(mlir::Type ty) const {
131 return lowerTy().convertType(ty);
132 }
133
134 // Convert FIR type to LLVM without turning fir.box<T> into memory
135 // reference.
136 mlir::Type convertObjectType(mlir::Type firType) const;
137
138 mlir::LLVM::ConstantOp
139 genI32Constant(mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
140 int value) const;
141
142 mlir::LLVM::ConstantOp
143 genConstantOffset(mlir::Location loc,
144 mlir::ConversionPatternRewriter &rewriter,
145 int offset) const;
146
151 mlir::Value integerCast(mlir::Location loc,
152 mlir::ConversionPatternRewriter &rewriter,
153 mlir::Type ty, mlir::Value val,
154 bool fold = false) const;
155
156 struct TypePair {
157 mlir::Type fir;
158 mlir::Type llvm;
159 };
160
161 TypePair getBoxTypePair(mlir::Type firBoxTy) const;
162
164 mlir::Value getValueFromBox(mlir::Location loc, TypePair boxTy,
165 mlir::Value box, mlir::Type resultTy,
166 mlir::ConversionPatternRewriter &rewriter,
167 int boxValue) const;
168
172 getDimsFromBox(mlir::Location loc, llvm::ArrayRef<mlir::Type> retTys,
173 TypePair boxTy, mlir::Value box, mlir::Value dim,
174 mlir::ConversionPatternRewriter &rewriter) const;
175
177 getDimsFromBox(mlir::Location loc, llvm::ArrayRef<mlir::Type> retTys,
178 TypePair boxTy, mlir::Value box, int dim,
179 mlir::ConversionPatternRewriter &rewriter) const;
180
181 mlir::Value
182 loadDimFieldFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box,
183 mlir::Value dim, int off, mlir::Type ty,
184 mlir::ConversionPatternRewriter &rewriter) const;
185
186 mlir::Value
187 getDimFieldFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box,
188 int dim, int off, mlir::Type ty,
189 mlir::ConversionPatternRewriter &rewriter) const;
190
191 mlir::Value getStrideFromBox(mlir::Location loc, TypePair boxTy,
192 mlir::Value box, unsigned dim,
193 mlir::ConversionPatternRewriter &rewriter) const;
194
196 mlir::Value
197 getBaseAddrFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box,
198 mlir::ConversionPatternRewriter &rewriter) const;
199
200 mlir::Value
201 getElementSizeFromBox(mlir::Location loc, mlir::Type resultTy, TypePair boxTy,
202 mlir::Value box,
203 mlir::ConversionPatternRewriter &rewriter) const;
204
205 mlir::Value getRankFromBox(mlir::Location loc, TypePair boxTy,
206 mlir::Value box,
207 mlir::ConversionPatternRewriter &rewriter) const;
208
209 mlir::Value getExtraFromBox(mlir::Location loc, TypePair boxTy,
210 mlir::Value box,
211 mlir::ConversionPatternRewriter &rewriter) const;
212
213 // Get the element type given an LLVM type that is of the form
214 // (array|struct|vector)+ and the provided indexes.
215 mlir::Type getBoxEleTy(mlir::Type type,
216 llvm::ArrayRef<std::int64_t> indexes) const;
217
218 // Return LLVM type of the object described by a fir.box of \p boxType.
219 mlir::Type getLlvmObjectTypeFromBoxType(mlir::Type boxType) const;
220
222 mlir::Value
223 loadTypeDescAddress(mlir::Location loc, TypePair boxTy, mlir::Value box,
224 mlir::ConversionPatternRewriter &rewriter) const;
225
226 // Load the attribute from the \p box and perform a check against \p maskValue
227 // The final comparison is implemented as `(attribute & maskValue) != 0`.
228 mlir::Value genBoxAttributeCheck(mlir::Location loc, TypePair boxTy,
229 mlir::Value box,
230 mlir::ConversionPatternRewriter &rewriter,
231 unsigned maskValue) const;
232
236 mlir::Value computeBoxSize(mlir::Location, TypePair boxTy, mlir::Value box,
237 mlir::ConversionPatternRewriter &rewriter) const;
238
239 template <typename... ARGS>
240 mlir::LLVM::GEPOp genGEP(mlir::Location loc, mlir::Type ty,
241 mlir::ConversionPatternRewriter &rewriter,
242 mlir::Value base, ARGS... args) const {
244 auto llvmPtrTy =
245 mlir::LLVM::LLVMPointerType::get(ty.getContext(), /*addressSpace=*/0);
246 return mlir::LLVM::GEPOp::create(rewriter, loc, llvmPtrTy, ty, base, cv);
247 }
248
249 // Find the Block in which the alloca should be inserted.
250 // The order to recursively find the proper block:
251 // 1. An OpenMP Op that will be outlined.
252 // 2. An OpenMP or OpenACC Op with one or more regions holding executable
253 // code.
254 // 3. A LLVMFuncOp
255 // 4. The first ancestor that is one of the above.
256 mlir::Block *getBlockForAllocaInsert(mlir::Operation *op,
257 mlir::Region *parentRegion) const;
258
259 // Generate an alloca of size 1 for an object of type \p llvmObjectTy in the
260 // allocation address space provided for the architecture in the DataLayout
261 // specification. If the address space is different from the devices
262 // program address space we perform a cast. In the case of most architectures
263 // the program and allocation address space will be the default of 0 and no
264 // cast will be emitted.
265 mlir::Value
266 genAllocaAndAddrCastWithType(mlir::Type llvmObjectTy, unsigned alignment,
267 mlir::ConversionPatternRewriter &rewriter) const;
268
269 const fir::LLVMTypeConverter &lowerTy() const {
270 return *static_cast<const fir::LLVMTypeConverter *>(
271 this->getTypeConverter());
272 }
273
274 const mlir::DataLayout &getDataLayout() const {
275 return lowerTy().getDataLayout();
276 }
277
278 void attachTBAATag(mlir::LLVM::AliasAnalysisOpInterface op,
279 mlir::Type baseFIRType, mlir::Type accessFIRType,
280 mlir::LLVM::GEPOp gep) const {
281 lowerTy().attachTBAATag(op, baseFIRType, accessFIRType, gep);
282 }
283
284 unsigned
285 getAllocaAddressSpace(mlir::ConversionPatternRewriter &rewriter) const;
286
287 unsigned
288 getProgramAddressSpace(mlir::ConversionPatternRewriter &rewriter) const;
289
290 unsigned
291 getGlobalAddressSpace(mlir::ConversionPatternRewriter &rewriter) const;
292
293 const fir::FIRToLLVMPassOptions &options;
294
298 mlir::SymbolTableCollection *symbolTables;
299
300 using ConvertToLLVMPattern::matchAndRewrite;
301};
302
303template <typename SourceOp>
304class FIROpConversion : public ConvertFIRToLLVMPattern {
305public:
306 using OpAdaptor = typename SourceOp::Adaptor;
307 using OneToNOpAdaptor = typename SourceOp::template GenericAdaptor<
308 mlir::ArrayRef<mlir::ValueRange>>;
309
310 explicit FIROpConversion(const LLVMTypeConverter &typeConverter,
311 const fir::FIRToLLVMPassOptions &options,
312 mlir::SymbolTableCollection *symbolTables = nullptr,
313 mlir::PatternBenefit benefit = 1)
314 : ConvertFIRToLLVMPattern(SourceOp::getOperationName(),
315 &typeConverter.getContext(), typeConverter,
316 options, symbolTables, benefit) {}
317
319 llvm::LogicalResult
320 matchAndRewrite(mlir::Operation *op, mlir::ArrayRef<mlir::Value> operands,
321 mlir::ConversionPatternRewriter &rewriter) const final {
322 return matchAndRewrite(mlir::cast<SourceOp>(op),
323 OpAdaptor(operands, mlir::cast<SourceOp>(op)),
324 rewriter);
325 }
326 llvm::LogicalResult
327 matchAndRewrite(mlir::Operation *op,
328 mlir::ArrayRef<mlir::ValueRange> operands,
329 mlir::ConversionPatternRewriter &rewriter) const final {
330 auto sourceOp = mlir::cast<SourceOp>(op);
331 return matchAndRewrite(sourceOp, OneToNOpAdaptor(operands, sourceOp),
332 rewriter);
333 }
336 virtual llvm::LogicalResult
337 matchAndRewrite(SourceOp op, OpAdaptor adaptor,
338 mlir::ConversionPatternRewriter &rewriter) const {
339 llvm_unreachable("matchAndRewrite is not implemented");
340 }
341 virtual llvm::LogicalResult
342 matchAndRewrite(SourceOp op, OneToNOpAdaptor adaptor,
343 mlir::ConversionPatternRewriter &rewriter) const {
344 return dispatchTo1To1(*this, op, adaptor, rewriter);
345 }
346
347private:
348 using ConvertFIRToLLVMPattern::matchAndRewrite;
349};
350
352template <typename FromOp>
353class FIROpAndTypeConversion : public FIROpConversion<FromOp> {
354public:
355 using FIROpConversion<FromOp>::FIROpConversion;
356 using OpAdaptor = typename FromOp::Adaptor;
357
358 llvm::LogicalResult
359 matchAndRewrite(FromOp op, OpAdaptor adaptor,
360 mlir::ConversionPatternRewriter &rewriter) const final {
361 mlir::Type ty = this->convertType(op.getType());
362 return doRewrite(op, ty, adaptor, rewriter);
363 }
364
365 virtual llvm::LogicalResult
366 doRewrite(FromOp addr, mlir::Type ty, OpAdaptor adaptor,
367 mlir::ConversionPatternRewriter &rewriter) const = 0;
368};
369
370} // namespace fir
371
372#endif // FORTRAN_OPTIMIZER_CODEGEN_FIROPPATTERNS_H
llvm::SmallVector< mlir::Value, 3 > getDimsFromBox(mlir::Location loc, llvm::ArrayRef< mlir::Type > retTys, TypePair boxTy, mlir::Value box, mlir::Value dim, mlir::ConversionPatternRewriter &rewriter) const
Definition FIROpPatterns.cpp:160
mlir::Value computeBoxSize(mlir::Location, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Definition FIROpPatterns.cpp:304
mlir::SymbolTableCollection * symbolTables
Definition FIROpPatterns.h:298
mlir::Value getRankFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Read base address from a fir.box. Returned address has type ty.
Definition FIROpPatterns.cpp:236
mlir::Value getBaseAddrFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Read base address from a fir.box. Returned address has type ty.
Definition FIROpPatterns.cpp:222
mlir::Value integerCast(mlir::Location loc, mlir::ConversionPatternRewriter &rewriter, mlir::Type ty, mlir::Value val, bool fold=false) const
Definition FIROpPatterns.cpp:109
mlir::Value getExtraFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Read the extra field from a fir.box.
Definition FIROpPatterns.cpp:244
mlir::Value loadTypeDescAddress(mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Read the address of the type descriptor from a box.
Definition FIROpPatterns.cpp:280
mlir::Value getValueFromBox(mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::Type resultTy, mlir::ConversionPatternRewriter &rewriter, int boxValue) const
Construct code sequence to extract the specific value from a fir.box.
Definition FIROpPatterns.cpp:141
FIR conversion pattern template.
Definition FIROpPatterns.h:353
virtual llvm::LogicalResult matchAndRewrite(SourceOp op, OpAdaptor adaptor, mlir::ConversionPatternRewriter &rewriter) const
Definition FIROpPatterns.h:337
llvm::LogicalResult matchAndRewrite(mlir::Operation *op, mlir::ArrayRef< mlir::Value > operands, mlir::ConversionPatternRewriter &rewriter) const final
Wrappers around the RewritePattern methods that pass the derived op type.
Definition FIROpPatterns.h:320
Definition TypeConverter.h:49
Definition FIRType.h:106
Definition OpenACC.h:20
Definition AbstractConverter.h:37
void buildSymbolTables(mlir::SymbolTableCollection &symbolTables, mlir::Operation *root)
Definition FIROpPatterns.cpp:72
void replaceSymbol(mlir::SymbolTableCollection *symbolTables, mlir::Operation *symbolTableOp, mlir::Operation *oldSymbol, mlir::Operation *newSymbol)
Definition FIROpPatterns.cpp:57
OpTy lookupSymbolIn(mlir::Operation *symbolTableOp, NameT name, mlir::SymbolTableCollection *symbolTables)
Definition FIROpPatterns.h:75
void registerSymbol(mlir::SymbolTableCollection *symbolTables, mlir::Operation *symbolTableOp, mlir::Operation *symbol)
Definition FIROpPatterns.cpp:42
void unregisterSymbol(mlir::SymbolTableCollection *symbolTables, mlir::Operation *symbolTableOp, mlir::Operation *symbol)
Forget symbol, which is about to be erased. No-op without a collection.
Definition FIROpPatterns.cpp:66
Definition FIROpPatterns.h:156
FIR to LLVM translation pass options.
Definition CodeGen.h:40