Lines Matching full:mlir

9 // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
14 #include "mlir/Dialect/OpenMP/OpenMPDialect.h"
17 static inline mlir::Type getLlvmPtrType(mlir::MLIRContext *context,
19 return mlir::LLVM::LLVMPointerType::get(context, addressSpace);
22 static unsigned getTypeDescFieldId(mlir::Type ty) {
23 auto isArray = mlir::isa<fir::SequenceType>(fir::dyn_cast_ptrOrBoxEleTy(ty));
30 llvm::StringRef rootOpName, mlir::MLIRContext *context,
32 const fir::FIRToLLVMPassOptions &options, mlir::PatternBenefit benefit)
38 mlir::Type
39 ConvertFIRToLLVMPattern::convertObjectType(mlir::Type firType) const {
40 if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(firType))
45 mlir::LLVM::ConstantOp ConvertFIRToLLVMPattern::genI32Constant(
46 mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
48 mlir::Type i32Ty = rewriter.getI32Type();
49 mlir::IntegerAttr attr = rewriter.getI32IntegerAttr(value);
50 return rewriter.create<mlir::LLVM::ConstantOp>(loc, i32Ty, attr);
53 mlir::LLVM::ConstantOp ConvertFIRToLLVMPattern::genConstantOffset(
54 mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
56 mlir::Type ity = lowerTy().offsetType();
57 mlir::IntegerAttr cattr = rewriter.getI32IntegerAttr(offset);
58 return rewriter.create<mlir::LLVM::ConstantOp>(loc, ity, cattr);
65 mlir::Value ConvertFIRToLLVMPattern::integerCast(
66 mlir::Location loc, mlir::ConversionPatternRewriter &rewriter,
67 mlir::Type ty, mlir::Value val, bool fold) const {
71 if (!mlir::isa<mlir::IntegerType>(valTy))
73 auto toSize = mlir::LLVM::getPrimitiveTypeSizeInBits(ty);
74 auto fromSize = mlir::LLVM::getPrimitiveTypeSizeInBits(valTy);
77 return rewriter.createOrFold<mlir::LLVM::TruncOp>(loc, ty, val);
79 return rewriter.createOrFold<mlir::LLVM::SExtOp>(loc, ty, val);
82 return rewriter.create<mlir::LLVM::TruncOp>(loc, ty, val);
84 return rewriter.create<mlir::LLVM::SExtOp>(loc, ty, val);
90 ConvertFIRToLLVMPattern::getBoxTypePair(mlir::Type firBoxTy) const {
91 mlir::Type llvmBoxTy =
92 lowerTy().convertBoxTypeAsStruct(mlir::cast<fir::BaseBoxType>(firBoxTy));
97 mlir::Value ConvertFIRToLLVMPattern::getValueFromBox(
98 mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::Type resultTy,
99 mlir::ConversionPatternRewriter &rewriter, int boxValue) const {
100 if (mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType())) {
102 auto p = rewriter.create<mlir::LLVM::GEPOp>(
104 llvm::ArrayRef<mlir::LLVM::GEPArg>{0, boxValue});
106 auto loadOp = rewriter.create<mlir::LLVM::LoadOp>(loc, fldTy, p);
111 return rewriter.create<mlir::LLVM::ExtractValueOp>(loc, box, boxValue);
116 llvm::SmallVector<mlir::Value, 3> ConvertFIRToLLVMPattern::getDimsFromBox(
117 mlir::Location loc, llvm::ArrayRef<mlir::Type> retTys, TypePair boxTy,
118 mlir::Value box, mlir::Value dim,
119 mlir::ConversionPatternRewriter &rewriter) const {
120 mlir::Value l0 =
122 mlir::Value l1 =
124 mlir::Value l2 =
129 llvm::SmallVector<mlir::Value, 3> ConvertFIRToLLVMPattern::getDimsFromBox(
130 mlir::Location loc, llvm::ArrayRef<mlir::Type> retTys, TypePair boxTy,
131 mlir::Value box, int dim, mlir::ConversionPatternRewriter &rewriter) const {
132 mlir::Value l0 =
134 mlir::Value l1 =
136 mlir::Value l2 =
141 mlir::Value ConvertFIRToLLVMPattern::loadDimFieldFromBox(
142 mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::Value dim,
143 int off, mlir::Type ty, mlir::ConversionPatternRewriter &rewriter) const {
144 assert(mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType()) &&
147 mlir::LLVM::GEPOp p = genGEP(loc, boxTy.llvm, rewriter, box, 0,
149 auto loadOp = rewriter.create<mlir::LLVM::LoadOp>(loc, ty, p);
154 mlir::Value ConvertFIRToLLVMPattern::getDimFieldFromBox(
155 mlir::Location loc, TypePair boxTy, mlir::Value box, int dim, int off,
156 mlir::Type ty, mlir::ConversionPatternRewriter &rewriter) const {
157 if (mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType())) {
158 mlir::LLVM::GEPOp p = genGEP(loc, boxTy.llvm, rewriter, box, 0,
160 auto loadOp = rewriter.create<mlir::LLVM::LoadOp>(loc, ty, p);
164 return rewriter.create<mlir::LLVM::ExtractValueOp>(
168 mlir::Value ConvertFIRToLLVMPattern::getStrideFromBox(
169 mlir::Location loc, TypePair boxTy, mlir::Value box, unsigned dim,
170 mlir::ConversionPatternRewriter &rewriter) const {
177 mlir::Value ConvertFIRToLLVMPattern::getBaseAddrFromBox(
178 mlir::Location loc, TypePair boxTy, mlir::Value box,
179 mlir::ConversionPatternRewriter &rewriter) const {
180 mlir::Type resultTy = ::getLlvmPtrType(boxTy.llvm.getContext());
184 mlir::Value ConvertFIRToLLVMPattern::getElementSizeFromBox(
185 mlir::Location loc, mlir::Type resultTy, TypePair boxTy, mlir::Value box,
186 mlir::ConversionPatternRewriter &rewriter) const {
191 mlir::Value ConvertFIRToLLVMPattern::getRankFromBox(
192 mlir::Location loc, TypePair boxTy, mlir::Value box,
193 mlir::ConversionPatternRewriter &rewriter) const {
194 mlir::Type resultTy = getBoxEleTy(boxTy.llvm, {kRankPosInBox});
199 mlir::Value ConvertFIRToLLVMPattern::getExtraFromBox(
200 mlir::Location loc, TypePair boxTy, mlir::Value box,
201 mlir::ConversionPatternRewriter &rewriter) const {
202 mlir::Type resultTy = getBoxEleTy(boxTy.llvm, {kExtraPosInBox});
208 mlir::Type ConvertFIRToLLVMPattern::getBoxEleTy(
209 mlir::Type type, llvm::ArrayRef<std::int64_t> indexes) const {
211 if (auto t = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(type)) {
214 } else if (auto t = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(type)) {
216 } else if (auto t = mlir::dyn_cast<mlir::VectorType>(type)) {
219 fir::emitFatalError(mlir::UnknownLoc::get(type.getContext()),
227 mlir::Type ConvertFIRToLLVMPattern::getLlvmObjectTypeFromBoxType(
228 mlir::Type boxType) const {
229 mlir::Type objectType = fir::dyn_cast_ptrOrBoxEleTy(boxType);
235 mlir::Value ConvertFIRToLLVMPattern::loadTypeDescAddress(
236 mlir::Location loc, TypePair boxTy, mlir::Value box,
237 mlir::ConversionPatternRewriter &rewriter) const {
239 mlir::Type tdescType = lowerTy().convertTypeDescType(rewriter.getContext());
245 mlir::Value ConvertFIRToLLVMPattern::genBoxAttributeCheck(
246 mlir::Location loc, TypePair boxTy, mlir::Value box,
247 mlir::ConversionPatternRewriter &rewriter, unsigned maskValue) const {
248 mlir::Type attrTy = rewriter.getI32Type();
249 mlir::Value attribute =
251 mlir::LLVM::ConstantOp attrMask = genConstantOffset(loc, rewriter, maskValue);
253 rewriter.create<mlir::LLVM::AndOp>(loc, attrTy, attribute, attrMask);
254 mlir::LLVM::ConstantOp c0 = genConstantOffset(loc, rewriter, 0);
255 return rewriter.create<mlir::LLVM::ICmpOp>(loc, mlir::LLVM::ICmpPredicate::ne,
259 mlir::Value ConvertFIRToLLVMPattern::computeBoxSize(
260 mlir::Location loc, TypePair boxTy, mlir::Value box,
261 mlir::ConversionPatternRewriter &rewriter) const {
262 auto firBoxType = mlir::dyn_cast<fir::BaseBoxType>(boxTy.fir);
264 const mlir::DataLayout &dl = lowerTy().getDataLayout();
268 mlir::Type llvmScalarBoxType =
271 mlir::Value scalarBoxSize =
273 mlir::Value rawRank = getRankFromBox(loc, boxTy, box, rewriter);
274 mlir::Value rank =
276 mlir::Type llvmDimsType = getBoxEleTy(boxTy.llvm, {kDimsPosInBox, 1});
282 mlir::Value sizePerDim = genConstantOffset(loc, rewriter, sizePerDimCst);
283 mlir::Value dimsSize = rewriter.create<mlir::LLVM::MulOp>(
285 mlir::Value size = rewriter.create<mlir::LLVM::AddOp>(
296 mlir::Block *ConvertFIRToLLVMPattern::getBlockForAllocaInsert(
297 mlir::Operation *op, mlir::Region *parentRegion) const {
298 if (auto iface = mlir::dyn_cast<mlir::omp::OutlineableOpenMPOpInterface>(op))
300 if (auto recipeIface = mlir::dyn_cast<mlir::accomp::RecipeInterface>(op))
302 if (auto llvmFuncOp = mlir::dyn_cast<mlir::LLVM::LLVMFuncOp>(op))
314 mlir::Value ConvertFIRToLLVMPattern::genAllocaAndAddrCastWithType(
315 mlir::Location loc, mlir::Type llvmObjectTy, unsigned alignment,
316 mlir::ConversionPatternRewriter &rewriter) const {
318 mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp();
319 mlir::Region *parentRegion = rewriter.getInsertionBlock()->getParent();
320 mlir::Block *insertBlock = getBlockForAllocaInsert(parentOp, parentRegion);
326 mlir::Value al = rewriter.create<mlir::LLVM::AllocaOp>(
336 al = rewriter.create<mlir::LLVM::AddrSpaceCastOp>(
345 mlir::ConversionPatternRewriter &rewriter) const {
346 mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp();
349 if (auto module = parentOp->getParentOfType<mlir::ModuleOp>())
350 if (mlir::Attribute addrSpace =
351 mlir::DataLayout(module).getAllocaMemorySpace())
352 return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt();
357 mlir::ConversionPatternRewriter &rewriter) const {
358 mlir::Operation *parentOp = rewriter.getInsertionBlock()->getParentOp();
361 if (auto module = parentOp->getParentOfType<mlir::ModuleOp>())
362 if (mlir::Attribute addrSpace =
363 mlir::DataLayout(module).getProgramMemorySpace())
364 return llvm::cast<mlir::IntegerAttr>(addrSpace).getUInt();