9#ifndef FORTRAN_OPTIMIZER_CODEGEN_FIROPPATTERNS_H
10#define FORTRAN_OPTIMIZER_CODEGEN_FIROPPATTERNS_H
12#include "flang/Optimizer/CodeGen/TypeConverter.h"
13#include "mlir/Conversion/LLVMCommon/Pattern.h"
14#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18struct FIRToLLVMPassOptions;
20static constexpr unsigned defaultAddressSpace = 0u;
25 mlir::MLIRContext *context,
28 mlir::PatternBenefit benefit = 1);
31 mlir::Type convertType(mlir::Type ty)
const {
32 return lowerTy().convertType(ty);
37 mlir::Type convertObjectType(mlir::Type firType)
const;
39 mlir::LLVM::ConstantOp
40 genI32Constant(mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
43 mlir::LLVM::ConstantOp
44 genConstantOffset(mlir::Location loc,
45 mlir::ConversionPatternRewriter &rewriter,
53 mlir::ConversionPatternRewriter &rewriter,
54 mlir::Type ty, mlir::Value val,
55 bool fold =
false)
const;
62 TypePair getBoxTypePair(mlir::Type firBoxTy)
const;
66 mlir::Value box, mlir::Type resultTy,
67 mlir::ConversionPatternRewriter &rewriter,
74 TypePair boxTy, mlir::Value box, mlir::Value dim,
75 mlir::ConversionPatternRewriter &rewriter)
const;
79 TypePair boxTy, mlir::Value box,
int dim,
80 mlir::ConversionPatternRewriter &rewriter)
const;
83 loadDimFieldFromBox(mlir::Location loc,
TypePair boxTy, mlir::Value box,
84 mlir::Value dim,
int off, mlir::Type ty,
85 mlir::ConversionPatternRewriter &rewriter)
const;
88 getDimFieldFromBox(mlir::Location loc,
TypePair boxTy, mlir::Value box,
89 int dim,
int off, mlir::Type ty,
90 mlir::ConversionPatternRewriter &rewriter)
const;
92 mlir::Value getStrideFromBox(mlir::Location loc,
TypePair boxTy,
93 mlir::Value box,
unsigned dim,
94 mlir::ConversionPatternRewriter &rewriter)
const;
99 mlir::ConversionPatternRewriter &rewriter)
const;
102 getElementSizeFromBox(mlir::Location loc, mlir::Type resultTy,
TypePair boxTy,
104 mlir::ConversionPatternRewriter &rewriter)
const;
108 mlir::ConversionPatternRewriter &rewriter)
const;
112 mlir::ConversionPatternRewriter &rewriter)
const;
116 mlir::Type getBoxEleTy(mlir::Type type,
120 mlir::Type getLlvmObjectTypeFromBoxType(mlir::Type boxType)
const;
125 mlir::ConversionPatternRewriter &rewriter)
const;
129 mlir::Value genBoxAttributeCheck(mlir::Location loc,
TypePair boxTy,
131 mlir::ConversionPatternRewriter &rewriter,
132 unsigned maskValue)
const;
138 mlir::ConversionPatternRewriter &rewriter)
const;
140 template <
typename... ARGS>
141 mlir::LLVM::GEPOp genGEP(mlir::Location loc, mlir::Type ty,
142 mlir::ConversionPatternRewriter &rewriter,
143 mlir::Value base, ARGS... args)
const {
146 mlir::LLVM::LLVMPointerType::get(ty.getContext(), 0);
147 return rewriter.create<mlir::LLVM::GEPOp>(loc, llvmPtrTy, ty, base, cv);
157 mlir::Block *getBlockForAllocaInsert(mlir::Operation *op,
158 mlir::Region *parentRegion)
const;
167 genAllocaAndAddrCastWithType(mlir::Location loc, mlir::Type llvmObjectTy,
169 mlir::ConversionPatternRewriter &rewriter)
const;
173 this->getTypeConverter());
176 void attachTBAATag(mlir::LLVM::AliasAnalysisOpInterface op,
177 mlir::Type baseFIRType, mlir::Type accessFIRType,
178 mlir::LLVM::GEPOp gep)
const {
179 lowerTy().attachTBAATag(op, baseFIRType, accessFIRType, gep);
183 getAllocaAddressSpace(mlir::ConversionPatternRewriter &rewriter)
const;
186 getProgramAddressSpace(mlir::ConversionPatternRewriter &rewriter)
const;
190 using ConvertToLLVMPattern::match;
191 using ConvertToLLVMPattern::matchAndRewrite;
194template <
typename SourceOp>
197 using OpAdaptor =
typename SourceOp::Adaptor;
198 using OneToNOpAdaptor =
typename SourceOp::template GenericAdaptor<
199 mlir::ArrayRef<mlir::ValueRange>>;
203 mlir::PatternBenefit benefit = 1)
205 &typeConverter.getContext(), typeConverter,
209 void rewrite(mlir::Operation *op, mlir::ArrayRef<mlir::Value> operands,
210 mlir::ConversionPatternRewriter &rewriter)
const final {
211 rewrite(mlir::cast<SourceOp>(op),
212 OpAdaptor(operands, mlir::cast<SourceOp>(op)), rewriter);
214 void rewrite(mlir::Operation *op, mlir::ArrayRef<mlir::ValueRange> operands,
215 mlir::ConversionPatternRewriter &rewriter)
const final {
216 auto sourceOp = llvm::cast<SourceOp>(op);
217 rewrite(llvm::cast<SourceOp>(op), OneToNOpAdaptor(operands, sourceOp),
220 llvm::LogicalResult match(mlir::Operation *op)
const final {
221 return match(mlir::cast<SourceOp>(op));
224 matchAndRewrite(mlir::Operation *op, mlir::ArrayRef<mlir::Value> operands,
225 mlir::ConversionPatternRewriter &rewriter)
const final {
226 return matchAndRewrite(mlir::cast<SourceOp>(op),
227 OpAdaptor(operands, mlir::cast<SourceOp>(op)),
231 matchAndRewrite(mlir::Operation *op,
232 mlir::ArrayRef<mlir::ValueRange> operands,
233 mlir::ConversionPatternRewriter &rewriter)
const final {
234 auto sourceOp = mlir::cast<SourceOp>(op);
235 return matchAndRewrite(sourceOp, OneToNOpAdaptor(operands, sourceOp),
240 virtual llvm::LogicalResult
match(SourceOp op)
const {
241 llvm_unreachable(
"must override match or matchAndRewrite");
243 virtual void rewrite(SourceOp op, OpAdaptor adaptor,
244 mlir::ConversionPatternRewriter &rewriter)
const {
245 llvm_unreachable(
"must override rewrite or matchAndRewrite");
247 virtual void rewrite(SourceOp op, OneToNOpAdaptor adaptor,
248 mlir::ConversionPatternRewriter &rewriter)
const {
250 getOneToOneAdaptorOperands(adaptor.getOperands());
251 rewrite(op, OpAdaptor(oneToOneOperands, adaptor), rewriter);
253 virtual llvm::LogicalResult
254 matchAndRewrite(SourceOp op, OpAdaptor adaptor,
255 mlir::ConversionPatternRewriter &rewriter)
const {
256 if (mlir::failed(match(op)))
257 return mlir::failure();
258 rewrite(op, adaptor, rewriter);
259 return mlir::success();
261 virtual llvm::LogicalResult
262 matchAndRewrite(SourceOp op, OneToNOpAdaptor adaptor,
263 mlir::ConversionPatternRewriter &rewriter)
const {
265 getOneToOneAdaptorOperands(adaptor.getOperands());
266 return matchAndRewrite(op, OpAdaptor(oneToOneOperands, adaptor), rewriter);
270 using ConvertFIRToLLVMPattern::matchAndRewrite;
271 using ConvertToLLVMPattern::match;
275template <
typename FromOp>
279 using OpAdaptor =
typename FromOp::Adaptor;
282 matchAndRewrite(FromOp op, OpAdaptor adaptor,
283 mlir::ConversionPatternRewriter &rewriter)
const final {
284 mlir::Type ty = this->convertType(op.getType());
285 return doRewrite(op, ty, adaptor, rewriter);
288 virtual llvm::LogicalResult
289 doRewrite(FromOp addr, mlir::Type ty, OpAdaptor adaptor,
290 mlir::ConversionPatternRewriter &rewriter)
const = 0;
Definition: FIROpPatterns.h:22
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:116
mlir::Value computeBoxSize(mlir::Location, TypePair boxTy, mlir::Value box, mlir::ConversionPatternRewriter &rewriter) const
Definition: FIROpPatterns.cpp:259
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:191
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:177
mlir::Value integerCast(mlir::Location loc, mlir::ConversionPatternRewriter &rewriter, mlir::Type ty, mlir::Value val, bool fold=false) const
Definition: FIROpPatterns.cpp:65
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:199
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:235
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:97
FIR conversion pattern template.
Definition: FIROpPatterns.h:276
Definition: FIROpPatterns.h:195
void rewrite(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:209
virtual llvm::LogicalResult match(SourceOp op) const
Definition: FIROpPatterns.h:240
Definition: TypeConverter.h:50
Definition: AbstractConverter.h:31
Definition: FIROpPatterns.h:57
FIR to LLVM translation pass options.
Definition: CodeGen.h:32