1044d5b5dSValentin Clement //===-- CodeGen.cpp -- bridge to lower to LLVM ----------------------------===//
2044d5b5dSValentin Clement //
3044d5b5dSValentin Clement // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4044d5b5dSValentin Clement // See https://llvm.org/LICENSE.txt for license information.
5044d5b5dSValentin Clement // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6044d5b5dSValentin Clement //
7044d5b5dSValentin Clement //===----------------------------------------------------------------------===//
8044d5b5dSValentin Clement //
9044d5b5dSValentin Clement // Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
10044d5b5dSValentin Clement //
11044d5b5dSValentin Clement //===----------------------------------------------------------------------===//
12044d5b5dSValentin Clement 
13044d5b5dSValentin Clement #include "flang/Optimizer/CodeGen/CodeGen.h"
14044d5b5dSValentin Clement #include "PassDetail.h"
15044d5b5dSValentin Clement #include "flang/Optimizer/Dialect/FIROps.h"
16044d5b5dSValentin Clement #include "flang/Optimizer/Dialect/FIRType.h"
177b5132daSValentin Clement #include "flang/Optimizer/Support/FIRContext.h"
18044d5b5dSValentin Clement #include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
19044d5b5dSValentin Clement #include "mlir/Conversion/LLVMCommon/Pattern.h"
20044d5b5dSValentin Clement #include "mlir/Conversion/LLVMCommon/TypeConverter.h"
21044d5b5dSValentin Clement #include "mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h"
22044d5b5dSValentin Clement #include "mlir/IR/BuiltinTypes.h"
233ae8e442SValentin Clement #include "mlir/IR/Matchers.h"
24044d5b5dSValentin Clement #include "mlir/Pass/Pass.h"
25044d5b5dSValentin Clement #include "llvm/ADT/ArrayRef.h"
26044d5b5dSValentin Clement 
27044d5b5dSValentin Clement #define DEBUG_TYPE "flang-codegen"
28044d5b5dSValentin Clement 
29044d5b5dSValentin Clement // fir::LLVMTypeConverter for converting to LLVM IR dialect types.
30044d5b5dSValentin Clement #include "TypeConverter.h"
31044d5b5dSValentin Clement 
32044d5b5dSValentin Clement namespace {
33044d5b5dSValentin Clement /// FIR conversion pattern template
34044d5b5dSValentin Clement template <typename FromOp>
35044d5b5dSValentin Clement class FIROpConversion : public mlir::ConvertOpToLLVMPattern<FromOp> {
36044d5b5dSValentin Clement public:
37044d5b5dSValentin Clement   explicit FIROpConversion(fir::LLVMTypeConverter &lowering)
38044d5b5dSValentin Clement       : mlir::ConvertOpToLLVMPattern<FromOp>(lowering) {}
39044d5b5dSValentin Clement 
40044d5b5dSValentin Clement protected:
41044d5b5dSValentin Clement   mlir::Type convertType(mlir::Type ty) const {
42044d5b5dSValentin Clement     return lowerTy().convertType(ty);
43044d5b5dSValentin Clement   }
44044d5b5dSValentin Clement 
45044d5b5dSValentin Clement   fir::LLVMTypeConverter &lowerTy() const {
46044d5b5dSValentin Clement     return *static_cast<fir::LLVMTypeConverter *>(this->getTypeConverter());
47044d5b5dSValentin Clement   }
48044d5b5dSValentin Clement };
49044d5b5dSValentin Clement 
503ae8e442SValentin Clement /// FIR conversion pattern template
513ae8e442SValentin Clement template <typename FromOp>
523ae8e442SValentin Clement class FIROpAndTypeConversion : public FIROpConversion<FromOp> {
533ae8e442SValentin Clement public:
543ae8e442SValentin Clement   using FIROpConversion<FromOp>::FIROpConversion;
553ae8e442SValentin Clement   using OpAdaptor = typename FromOp::Adaptor;
563ae8e442SValentin Clement 
573ae8e442SValentin Clement   mlir::LogicalResult
583ae8e442SValentin Clement   matchAndRewrite(FromOp op, OpAdaptor adaptor,
593ae8e442SValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const final {
603ae8e442SValentin Clement     mlir::Type ty = this->convertType(op.getType());
613ae8e442SValentin Clement     return doRewrite(op, ty, adaptor, rewriter);
623ae8e442SValentin Clement   }
633ae8e442SValentin Clement 
643ae8e442SValentin Clement   virtual mlir::LogicalResult
653ae8e442SValentin Clement   doRewrite(FromOp addr, mlir::Type ty, OpAdaptor adaptor,
663ae8e442SValentin Clement             mlir::ConversionPatternRewriter &rewriter) const = 0;
673ae8e442SValentin Clement };
683ae8e442SValentin Clement 
690c4a7a52SValentin Clement // Lower `fir.address_of` operation to `llvm.address_of` operation.
70044d5b5dSValentin Clement struct AddrOfOpConversion : public FIROpConversion<fir::AddrOfOp> {
71044d5b5dSValentin Clement   using FIROpConversion::FIROpConversion;
72044d5b5dSValentin Clement 
73044d5b5dSValentin Clement   mlir::LogicalResult
74044d5b5dSValentin Clement   matchAndRewrite(fir::AddrOfOp addr, OpAdaptor adaptor,
75044d5b5dSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
76044d5b5dSValentin Clement     auto ty = convertType(addr.getType());
77044d5b5dSValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::AddressOfOp>(
78044d5b5dSValentin Clement         addr, ty, addr.symbol().getRootReference().getValue());
79044d5b5dSValentin Clement     return success();
80044d5b5dSValentin Clement   }
81044d5b5dSValentin Clement };
82044d5b5dSValentin Clement 
83ddd11b9aSAndrzej Warzynski // `fir.call` -> `llvm.call`
84ddd11b9aSAndrzej Warzynski struct CallOpConversion : public FIROpConversion<fir::CallOp> {
85ddd11b9aSAndrzej Warzynski   using FIROpConversion::FIROpConversion;
86ddd11b9aSAndrzej Warzynski 
87ddd11b9aSAndrzej Warzynski   mlir::LogicalResult
88ddd11b9aSAndrzej Warzynski   matchAndRewrite(fir::CallOp call, OpAdaptor adaptor,
89ddd11b9aSAndrzej Warzynski                   mlir::ConversionPatternRewriter &rewriter) const override {
90ddd11b9aSAndrzej Warzynski     SmallVector<mlir::Type> resultTys;
91ddd11b9aSAndrzej Warzynski     for (auto r : call.getResults())
92ddd11b9aSAndrzej Warzynski       resultTys.push_back(convertType(r.getType()));
93ddd11b9aSAndrzej Warzynski     rewriter.replaceOpWithNewOp<mlir::LLVM::CallOp>(
94ddd11b9aSAndrzej Warzynski         call, resultTys, adaptor.getOperands(), call->getAttrs());
95ddd11b9aSAndrzej Warzynski     return success();
96ddd11b9aSAndrzej Warzynski   }
97ddd11b9aSAndrzej Warzynski };
98ddd11b9aSAndrzej Warzynski 
99092cee5fSValentin Clement static mlir::Type getComplexEleTy(mlir::Type complex) {
100092cee5fSValentin Clement   if (auto cc = complex.dyn_cast<mlir::ComplexType>())
101092cee5fSValentin Clement     return cc.getElementType();
102092cee5fSValentin Clement   return complex.cast<fir::ComplexType>().getElementType();
103092cee5fSValentin Clement }
104092cee5fSValentin Clement 
105092cee5fSValentin Clement /// convert value of from-type to value of to-type
106092cee5fSValentin Clement struct ConvertOpConversion : public FIROpConversion<fir::ConvertOp> {
107092cee5fSValentin Clement   using FIROpConversion::FIROpConversion;
108092cee5fSValentin Clement 
109092cee5fSValentin Clement   static bool isFloatingPointTy(mlir::Type ty) {
110092cee5fSValentin Clement     return ty.isa<mlir::FloatType>();
111092cee5fSValentin Clement   }
112092cee5fSValentin Clement 
113092cee5fSValentin Clement   mlir::LogicalResult
114092cee5fSValentin Clement   matchAndRewrite(fir::ConvertOp convert, OpAdaptor adaptor,
115092cee5fSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
116092cee5fSValentin Clement     auto fromTy = convertType(convert.value().getType());
117092cee5fSValentin Clement     auto toTy = convertType(convert.res().getType());
118092cee5fSValentin Clement     mlir::Value op0 = adaptor.getOperands()[0];
119092cee5fSValentin Clement     if (fromTy == toTy) {
120092cee5fSValentin Clement       rewriter.replaceOp(convert, op0);
121092cee5fSValentin Clement       return success();
122092cee5fSValentin Clement     }
123092cee5fSValentin Clement     auto loc = convert.getLoc();
124092cee5fSValentin Clement     auto convertFpToFp = [&](mlir::Value val, unsigned fromBits,
125092cee5fSValentin Clement                              unsigned toBits, mlir::Type toTy) -> mlir::Value {
126092cee5fSValentin Clement       if (fromBits == toBits) {
127092cee5fSValentin Clement         // TODO: Converting between two floating-point representations with the
128092cee5fSValentin Clement         // same bitwidth is not allowed for now.
129092cee5fSValentin Clement         mlir::emitError(loc,
130092cee5fSValentin Clement                         "cannot implicitly convert between two floating-point "
131092cee5fSValentin Clement                         "representations of the same bitwidth");
132092cee5fSValentin Clement         return {};
133092cee5fSValentin Clement       }
134092cee5fSValentin Clement       if (fromBits > toBits)
135092cee5fSValentin Clement         return rewriter.create<mlir::LLVM::FPTruncOp>(loc, toTy, val);
136092cee5fSValentin Clement       return rewriter.create<mlir::LLVM::FPExtOp>(loc, toTy, val);
137092cee5fSValentin Clement     };
138092cee5fSValentin Clement     // Complex to complex conversion.
139092cee5fSValentin Clement     if (fir::isa_complex(convert.value().getType()) &&
140092cee5fSValentin Clement         fir::isa_complex(convert.res().getType())) {
141092cee5fSValentin Clement       // Special case: handle the conversion of a complex such that both the
142092cee5fSValentin Clement       // real and imaginary parts are converted together.
143092cee5fSValentin Clement       auto zero = mlir::ArrayAttr::get(convert.getContext(),
144092cee5fSValentin Clement                                        rewriter.getI32IntegerAttr(0));
145092cee5fSValentin Clement       auto one = mlir::ArrayAttr::get(convert.getContext(),
146092cee5fSValentin Clement                                       rewriter.getI32IntegerAttr(1));
147092cee5fSValentin Clement       auto ty = convertType(getComplexEleTy(convert.value().getType()));
148092cee5fSValentin Clement       auto rp = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, ty, op0, zero);
149092cee5fSValentin Clement       auto ip = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, ty, op0, one);
150092cee5fSValentin Clement       auto nt = convertType(getComplexEleTy(convert.res().getType()));
151092cee5fSValentin Clement       auto fromBits = mlir::LLVM::getPrimitiveTypeSizeInBits(ty);
152092cee5fSValentin Clement       auto toBits = mlir::LLVM::getPrimitiveTypeSizeInBits(nt);
153092cee5fSValentin Clement       auto rc = convertFpToFp(rp, fromBits, toBits, nt);
154092cee5fSValentin Clement       auto ic = convertFpToFp(ip, fromBits, toBits, nt);
155092cee5fSValentin Clement       auto un = rewriter.create<mlir::LLVM::UndefOp>(loc, toTy);
156092cee5fSValentin Clement       auto i1 =
157092cee5fSValentin Clement           rewriter.create<mlir::LLVM::InsertValueOp>(loc, toTy, un, rc, zero);
158092cee5fSValentin Clement       rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(convert, toTy, i1,
159092cee5fSValentin Clement                                                              ic, one);
160092cee5fSValentin Clement       return mlir::success();
161092cee5fSValentin Clement     }
162092cee5fSValentin Clement     // Floating point to floating point conversion.
163092cee5fSValentin Clement     if (isFloatingPointTy(fromTy)) {
164092cee5fSValentin Clement       if (isFloatingPointTy(toTy)) {
165092cee5fSValentin Clement         auto fromBits = mlir::LLVM::getPrimitiveTypeSizeInBits(fromTy);
166092cee5fSValentin Clement         auto toBits = mlir::LLVM::getPrimitiveTypeSizeInBits(toTy);
167092cee5fSValentin Clement         auto v = convertFpToFp(op0, fromBits, toBits, toTy);
168092cee5fSValentin Clement         rewriter.replaceOp(convert, v);
169092cee5fSValentin Clement         return mlir::success();
170092cee5fSValentin Clement       }
171092cee5fSValentin Clement       if (toTy.isa<mlir::IntegerType>()) {
172092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::FPToSIOp>(convert, toTy, op0);
173092cee5fSValentin Clement         return mlir::success();
174092cee5fSValentin Clement       }
175092cee5fSValentin Clement     } else if (fromTy.isa<mlir::IntegerType>()) {
176092cee5fSValentin Clement       // Integer to integer conversion.
177092cee5fSValentin Clement       if (toTy.isa<mlir::IntegerType>()) {
178092cee5fSValentin Clement         auto fromBits = mlir::LLVM::getPrimitiveTypeSizeInBits(fromTy);
179092cee5fSValentin Clement         auto toBits = mlir::LLVM::getPrimitiveTypeSizeInBits(toTy);
180092cee5fSValentin Clement         assert(fromBits != toBits);
181092cee5fSValentin Clement         if (fromBits > toBits) {
182092cee5fSValentin Clement           rewriter.replaceOpWithNewOp<mlir::LLVM::TruncOp>(convert, toTy, op0);
183092cee5fSValentin Clement           return mlir::success();
184092cee5fSValentin Clement         }
185092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::SExtOp>(convert, toTy, op0);
186092cee5fSValentin Clement         return mlir::success();
187092cee5fSValentin Clement       }
188092cee5fSValentin Clement       // Integer to floating point conversion.
189092cee5fSValentin Clement       if (isFloatingPointTy(toTy)) {
190092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::SIToFPOp>(convert, toTy, op0);
191092cee5fSValentin Clement         return mlir::success();
192092cee5fSValentin Clement       }
193092cee5fSValentin Clement       // Integer to pointer conversion.
194092cee5fSValentin Clement       if (toTy.isa<mlir::LLVM::LLVMPointerType>()) {
195092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::IntToPtrOp>(convert, toTy, op0);
196092cee5fSValentin Clement         return mlir::success();
197092cee5fSValentin Clement       }
198092cee5fSValentin Clement     } else if (fromTy.isa<mlir::LLVM::LLVMPointerType>()) {
199092cee5fSValentin Clement       // Pointer to integer conversion.
200092cee5fSValentin Clement       if (toTy.isa<mlir::IntegerType>()) {
201092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::PtrToIntOp>(convert, toTy, op0);
202092cee5fSValentin Clement         return mlir::success();
203092cee5fSValentin Clement       }
204092cee5fSValentin Clement       // Pointer to pointer conversion.
205092cee5fSValentin Clement       if (toTy.isa<mlir::LLVM::LLVMPointerType>()) {
206092cee5fSValentin Clement         rewriter.replaceOpWithNewOp<mlir::LLVM::BitcastOp>(convert, toTy, op0);
207092cee5fSValentin Clement         return mlir::success();
208092cee5fSValentin Clement       }
209092cee5fSValentin Clement     }
210092cee5fSValentin Clement     return emitError(loc) << "cannot convert " << fromTy << " to " << toTy;
211092cee5fSValentin Clement   }
212092cee5fSValentin Clement };
213092cee5fSValentin Clement 
2140c4a7a52SValentin Clement /// Lower `fir.has_value` operation to `llvm.return` operation.
215044d5b5dSValentin Clement struct HasValueOpConversion : public FIROpConversion<fir::HasValueOp> {
216044d5b5dSValentin Clement   using FIROpConversion::FIROpConversion;
217044d5b5dSValentin Clement 
218044d5b5dSValentin Clement   mlir::LogicalResult
219044d5b5dSValentin Clement   matchAndRewrite(fir::HasValueOp op, OpAdaptor adaptor,
220044d5b5dSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
221044d5b5dSValentin Clement     rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, adaptor.getOperands());
222044d5b5dSValentin Clement     return success();
223044d5b5dSValentin Clement   }
224044d5b5dSValentin Clement };
225044d5b5dSValentin Clement 
2260c4a7a52SValentin Clement /// Lower `fir.global` operation to `llvm.global` operation.
2270c4a7a52SValentin Clement /// `fir.insert_on_range` operations are replaced with constant dense attribute
2280c4a7a52SValentin Clement /// if they are applied on the full range.
229044d5b5dSValentin Clement struct GlobalOpConversion : public FIROpConversion<fir::GlobalOp> {
230044d5b5dSValentin Clement   using FIROpConversion::FIROpConversion;
231044d5b5dSValentin Clement 
232044d5b5dSValentin Clement   mlir::LogicalResult
233044d5b5dSValentin Clement   matchAndRewrite(fir::GlobalOp global, OpAdaptor adaptor,
234044d5b5dSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
235044d5b5dSValentin Clement     auto tyAttr = convertType(global.getType());
236044d5b5dSValentin Clement     if (global.getType().isa<fir::BoxType>())
237044d5b5dSValentin Clement       tyAttr = tyAttr.cast<mlir::LLVM::LLVMPointerType>().getElementType();
238044d5b5dSValentin Clement     auto loc = global.getLoc();
239044d5b5dSValentin Clement     mlir::Attribute initAttr{};
240044d5b5dSValentin Clement     if (global.initVal())
241044d5b5dSValentin Clement       initAttr = global.initVal().getValue();
242044d5b5dSValentin Clement     auto linkage = convertLinkage(global.linkName());
243044d5b5dSValentin Clement     auto isConst = global.constant().hasValue();
244044d5b5dSValentin Clement     auto g = rewriter.create<mlir::LLVM::GlobalOp>(
245044d5b5dSValentin Clement         loc, tyAttr, isConst, linkage, global.sym_name(), initAttr);
246044d5b5dSValentin Clement     auto &gr = g.getInitializerRegion();
247044d5b5dSValentin Clement     rewriter.inlineRegionBefore(global.region(), gr, gr.end());
248044d5b5dSValentin Clement     if (!gr.empty()) {
249044d5b5dSValentin Clement       // Replace insert_on_range with a constant dense attribute if the
250044d5b5dSValentin Clement       // initialization is on the full range.
251044d5b5dSValentin Clement       auto insertOnRangeOps = gr.front().getOps<fir::InsertOnRangeOp>();
252044d5b5dSValentin Clement       for (auto insertOp : insertOnRangeOps) {
253044d5b5dSValentin Clement         if (isFullRange(insertOp.coor(), insertOp.getType())) {
254044d5b5dSValentin Clement           auto seqTyAttr = convertType(insertOp.getType());
255044d5b5dSValentin Clement           auto *op = insertOp.val().getDefiningOp();
256044d5b5dSValentin Clement           auto constant = mlir::dyn_cast<mlir::arith::ConstantOp>(op);
257044d5b5dSValentin Clement           if (!constant) {
258044d5b5dSValentin Clement             auto convertOp = mlir::dyn_cast<fir::ConvertOp>(op);
259044d5b5dSValentin Clement             if (!convertOp)
260044d5b5dSValentin Clement               continue;
261044d5b5dSValentin Clement             constant = cast<mlir::arith::ConstantOp>(
262044d5b5dSValentin Clement                 convertOp.value().getDefiningOp());
263044d5b5dSValentin Clement           }
264044d5b5dSValentin Clement           mlir::Type vecType = mlir::VectorType::get(
265044d5b5dSValentin Clement               insertOp.getType().getShape(), constant.getType());
266044d5b5dSValentin Clement           auto denseAttr = mlir::DenseElementsAttr::get(
267044d5b5dSValentin Clement               vecType.cast<ShapedType>(), constant.value());
268044d5b5dSValentin Clement           rewriter.setInsertionPointAfter(insertOp);
269044d5b5dSValentin Clement           rewriter.replaceOpWithNewOp<mlir::arith::ConstantOp>(
270044d5b5dSValentin Clement               insertOp, seqTyAttr, denseAttr);
271044d5b5dSValentin Clement         }
272044d5b5dSValentin Clement       }
273044d5b5dSValentin Clement     }
274044d5b5dSValentin Clement     rewriter.eraseOp(global);
275044d5b5dSValentin Clement     return success();
276044d5b5dSValentin Clement   }
277044d5b5dSValentin Clement 
278044d5b5dSValentin Clement   bool isFullRange(mlir::ArrayAttr indexes, fir::SequenceType seqTy) const {
279044d5b5dSValentin Clement     auto extents = seqTy.getShape();
280044d5b5dSValentin Clement     if (indexes.size() / 2 != extents.size())
281044d5b5dSValentin Clement       return false;
282044d5b5dSValentin Clement     for (unsigned i = 0; i < indexes.size(); i += 2) {
283044d5b5dSValentin Clement       if (indexes[i].cast<IntegerAttr>().getInt() != 0)
284044d5b5dSValentin Clement         return false;
285044d5b5dSValentin Clement       if (indexes[i + 1].cast<IntegerAttr>().getInt() != extents[i / 2] - 1)
286044d5b5dSValentin Clement         return false;
287044d5b5dSValentin Clement     }
288044d5b5dSValentin Clement     return true;
289044d5b5dSValentin Clement   }
290044d5b5dSValentin Clement 
2910c4a7a52SValentin Clement   // TODO: String comparaison should be avoided. Replace linkName with an
2920c4a7a52SValentin Clement   // enumeration.
293044d5b5dSValentin Clement   mlir::LLVM::Linkage convertLinkage(Optional<StringRef> optLinkage) const {
294044d5b5dSValentin Clement     if (optLinkage.hasValue()) {
295044d5b5dSValentin Clement       auto name = optLinkage.getValue();
296044d5b5dSValentin Clement       if (name == "internal")
297044d5b5dSValentin Clement         return mlir::LLVM::Linkage::Internal;
298044d5b5dSValentin Clement       if (name == "linkonce")
299044d5b5dSValentin Clement         return mlir::LLVM::Linkage::Linkonce;
300044d5b5dSValentin Clement       if (name == "common")
301044d5b5dSValentin Clement         return mlir::LLVM::Linkage::Common;
302044d5b5dSValentin Clement       if (name == "weak")
303044d5b5dSValentin Clement         return mlir::LLVM::Linkage::Weak;
304044d5b5dSValentin Clement     }
305044d5b5dSValentin Clement     return mlir::LLVM::Linkage::External;
306044d5b5dSValentin Clement   }
307044d5b5dSValentin Clement };
308044d5b5dSValentin Clement 
3098c239909SValentin Clement template <typename OP>
3108c239909SValentin Clement void selectMatchAndRewrite(fir::LLVMTypeConverter &lowering, OP select,
3118c239909SValentin Clement                            typename OP::Adaptor adaptor,
3128c239909SValentin Clement                            mlir::ConversionPatternRewriter &rewriter) {
3138c239909SValentin Clement   unsigned conds = select.getNumConditions();
3148c239909SValentin Clement   auto cases = select.getCases().getValue();
3158c239909SValentin Clement   mlir::Value selector = adaptor.selector();
3168c239909SValentin Clement   auto loc = select.getLoc();
3178c239909SValentin Clement   assert(conds > 0 && "select must have cases");
3188c239909SValentin Clement 
3198c239909SValentin Clement   llvm::SmallVector<mlir::Block *> destinations;
3208c239909SValentin Clement   llvm::SmallVector<mlir::ValueRange> destinationsOperands;
3218c239909SValentin Clement   mlir::Block *defaultDestination;
3228c239909SValentin Clement   mlir::ValueRange defaultOperands;
3238c239909SValentin Clement   llvm::SmallVector<int32_t> caseValues;
3248c239909SValentin Clement 
3258c239909SValentin Clement   for (unsigned t = 0; t != conds; ++t) {
3268c239909SValentin Clement     mlir::Block *dest = select.getSuccessor(t);
3278c239909SValentin Clement     auto destOps = select.getSuccessorOperands(adaptor.getOperands(), t);
3288c239909SValentin Clement     const mlir::Attribute &attr = cases[t];
3298c239909SValentin Clement     if (auto intAttr = attr.template dyn_cast<mlir::IntegerAttr>()) {
3308c239909SValentin Clement       destinations.push_back(dest);
3318c239909SValentin Clement       destinationsOperands.push_back(destOps.hasValue() ? *destOps
3328c239909SValentin Clement                                                         : ValueRange());
3338c239909SValentin Clement       caseValues.push_back(intAttr.getInt());
3348c239909SValentin Clement       continue;
3358c239909SValentin Clement     }
3368c239909SValentin Clement     assert(attr.template dyn_cast_or_null<mlir::UnitAttr>());
3378c239909SValentin Clement     assert((t + 1 == conds) && "unit must be last");
3388c239909SValentin Clement     defaultDestination = dest;
3398c239909SValentin Clement     defaultOperands = destOps.hasValue() ? *destOps : ValueRange();
3408c239909SValentin Clement   }
3418c239909SValentin Clement 
3428c239909SValentin Clement   // LLVM::SwitchOp takes a i32 type for the selector.
3438c239909SValentin Clement   if (select.getSelector().getType() != rewriter.getI32Type())
3448c239909SValentin Clement     selector =
3458c239909SValentin Clement         rewriter.create<LLVM::TruncOp>(loc, rewriter.getI32Type(), selector);
3468c239909SValentin Clement 
3478c239909SValentin Clement   rewriter.replaceOpWithNewOp<mlir::LLVM::SwitchOp>(
3488c239909SValentin Clement       select, selector,
3498c239909SValentin Clement       /*defaultDestination=*/defaultDestination,
3508c239909SValentin Clement       /*defaultOperands=*/defaultOperands,
3518c239909SValentin Clement       /*caseValues=*/caseValues,
3528c239909SValentin Clement       /*caseDestinations=*/destinations,
3538c239909SValentin Clement       /*caseOperands=*/destinationsOperands,
3548c239909SValentin Clement       /*branchWeights=*/ArrayRef<int32_t>());
3558c239909SValentin Clement }
3568c239909SValentin Clement 
3578c239909SValentin Clement /// conversion of fir::SelectOp to an if-then-else ladder
3588c239909SValentin Clement struct SelectOpConversion : public FIROpConversion<fir::SelectOp> {
3598c239909SValentin Clement   using FIROpConversion::FIROpConversion;
3608c239909SValentin Clement 
3618c239909SValentin Clement   mlir::LogicalResult
3628c239909SValentin Clement   matchAndRewrite(fir::SelectOp op, OpAdaptor adaptor,
3638c239909SValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
3648c239909SValentin Clement     selectMatchAndRewrite<fir::SelectOp>(lowerTy(), op, adaptor, rewriter);
3658c239909SValentin Clement     return success();
3668c239909SValentin Clement   }
3678c239909SValentin Clement };
3688c239909SValentin Clement 
369*e3349fa1SAndrzej Warzynski /// `fir.load` --> `llvm.load`
370*e3349fa1SAndrzej Warzynski struct LoadOpConversion : public FIROpConversion<fir::LoadOp> {
371*e3349fa1SAndrzej Warzynski   using FIROpConversion::FIROpConversion;
372*e3349fa1SAndrzej Warzynski 
373*e3349fa1SAndrzej Warzynski   mlir::LogicalResult
374*e3349fa1SAndrzej Warzynski   matchAndRewrite(fir::LoadOp load, OpAdaptor adaptor,
375*e3349fa1SAndrzej Warzynski                   mlir::ConversionPatternRewriter &rewriter) const override {
376*e3349fa1SAndrzej Warzynski     // fir.box is a special case because it is considered as an ssa values in
377*e3349fa1SAndrzej Warzynski     // fir, but it is lowered as a pointer to a descriptor. So fir.ref<fir.box>
378*e3349fa1SAndrzej Warzynski     // and fir.box end up being the same llvm types and loading a
379*e3349fa1SAndrzej Warzynski     // fir.ref<fir.box> is actually a no op in LLVM.
380*e3349fa1SAndrzej Warzynski     if (load.getType().isa<fir::BoxType>()) {
381*e3349fa1SAndrzej Warzynski       rewriter.replaceOp(load, adaptor.getOperands()[0]);
382*e3349fa1SAndrzej Warzynski     } else {
383*e3349fa1SAndrzej Warzynski       mlir::Type ty = convertType(load.getType());
384*e3349fa1SAndrzej Warzynski       ArrayRef<NamedAttribute> at = load->getAttrs();
385*e3349fa1SAndrzej Warzynski       rewriter.replaceOpWithNewOp<mlir::LLVM::LoadOp>(
386*e3349fa1SAndrzej Warzynski           load, ty, adaptor.getOperands(), at);
387*e3349fa1SAndrzej Warzynski     }
388*e3349fa1SAndrzej Warzynski     return success();
389*e3349fa1SAndrzej Warzynski   }
390*e3349fa1SAndrzej Warzynski };
391*e3349fa1SAndrzej Warzynski 
3928c239909SValentin Clement /// conversion of fir::SelectRankOp to an if-then-else ladder
3938c239909SValentin Clement struct SelectRankOpConversion : public FIROpConversion<fir::SelectRankOp> {
3948c239909SValentin Clement   using FIROpConversion::FIROpConversion;
3958c239909SValentin Clement 
3968c239909SValentin Clement   mlir::LogicalResult
3978c239909SValentin Clement   matchAndRewrite(fir::SelectRankOp op, OpAdaptor adaptor,
3988c239909SValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
3998c239909SValentin Clement     selectMatchAndRewrite<fir::SelectRankOp>(lowerTy(), op, adaptor, rewriter);
4008c239909SValentin Clement     return success();
4018c239909SValentin Clement   }
4028c239909SValentin Clement };
4038c239909SValentin Clement 
404*e3349fa1SAndrzej Warzynski /// `fir.store` --> `llvm.store`
405*e3349fa1SAndrzej Warzynski struct StoreOpConversion : public FIROpConversion<fir::StoreOp> {
406*e3349fa1SAndrzej Warzynski   using FIROpConversion::FIROpConversion;
407*e3349fa1SAndrzej Warzynski 
408*e3349fa1SAndrzej Warzynski   mlir::LogicalResult
409*e3349fa1SAndrzej Warzynski   matchAndRewrite(fir::StoreOp store, OpAdaptor adaptor,
410*e3349fa1SAndrzej Warzynski                   mlir::ConversionPatternRewriter &rewriter) const override {
411*e3349fa1SAndrzej Warzynski     if (store.value().getType().isa<fir::BoxType>()) {
412*e3349fa1SAndrzej Warzynski       // fir.box value is actually in memory, load it first before storing it.
413*e3349fa1SAndrzej Warzynski       mlir::Location loc = store.getLoc();
414*e3349fa1SAndrzej Warzynski       mlir::Type boxPtrTy = adaptor.getOperands()[0].getType();
415*e3349fa1SAndrzej Warzynski       auto val = rewriter.create<mlir::LLVM::LoadOp>(
416*e3349fa1SAndrzej Warzynski           loc, boxPtrTy.cast<mlir::LLVM::LLVMPointerType>().getElementType(),
417*e3349fa1SAndrzej Warzynski           adaptor.getOperands()[0]);
418*e3349fa1SAndrzej Warzynski       rewriter.replaceOpWithNewOp<mlir::LLVM::StoreOp>(
419*e3349fa1SAndrzej Warzynski           store, val, adaptor.getOperands()[1]);
420*e3349fa1SAndrzej Warzynski     } else {
421*e3349fa1SAndrzej Warzynski       rewriter.replaceOpWithNewOp<mlir::LLVM::StoreOp>(
422*e3349fa1SAndrzej Warzynski           store, adaptor.getOperands()[0], adaptor.getOperands()[1]);
423*e3349fa1SAndrzej Warzynski     }
424*e3349fa1SAndrzej Warzynski     return success();
425*e3349fa1SAndrzej Warzynski   }
426*e3349fa1SAndrzej Warzynski };
427*e3349fa1SAndrzej Warzynski 
428*e3349fa1SAndrzej Warzynski /// convert to LLVM IR dialect `undef`
429044d5b5dSValentin Clement struct UndefOpConversion : public FIROpConversion<fir::UndefOp> {
430044d5b5dSValentin Clement   using FIROpConversion::FIROpConversion;
431044d5b5dSValentin Clement 
432044d5b5dSValentin Clement   mlir::LogicalResult
433044d5b5dSValentin Clement   matchAndRewrite(fir::UndefOp undef, OpAdaptor,
434044d5b5dSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
435044d5b5dSValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::UndefOp>(
436044d5b5dSValentin Clement         undef, convertType(undef.getType()));
437044d5b5dSValentin Clement     return success();
438044d5b5dSValentin Clement   }
439044d5b5dSValentin Clement };
440a7a61359SValentin Clement 
441*e3349fa1SAndrzej Warzynski /// `fir.unreachable` --> `llvm.unreachable`
44232e08248SAndrzej Warzynski struct UnreachableOpConversion : public FIROpConversion<fir::UnreachableOp> {
44332e08248SAndrzej Warzynski   using FIROpConversion::FIROpConversion;
44432e08248SAndrzej Warzynski 
44532e08248SAndrzej Warzynski   mlir::LogicalResult
44632e08248SAndrzej Warzynski   matchAndRewrite(fir::UnreachableOp unreach, OpAdaptor adaptor,
44732e08248SAndrzej Warzynski                   mlir::ConversionPatternRewriter &rewriter) const override {
44832e08248SAndrzej Warzynski     rewriter.replaceOpWithNewOp<mlir::LLVM::UnreachableOp>(unreach);
44932e08248SAndrzej Warzynski     return success();
45032e08248SAndrzej Warzynski   }
45132e08248SAndrzej Warzynski };
45232e08248SAndrzej Warzynski 
453a7a61359SValentin Clement struct ZeroOpConversion : public FIROpConversion<fir::ZeroOp> {
454a7a61359SValentin Clement   using FIROpConversion::FIROpConversion;
455a7a61359SValentin Clement 
456a7a61359SValentin Clement   mlir::LogicalResult
457a7a61359SValentin Clement   matchAndRewrite(fir::ZeroOp zero, OpAdaptor,
458a7a61359SValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
459a7a61359SValentin Clement     auto ty = convertType(zero.getType());
460a7a61359SValentin Clement     if (ty.isa<mlir::LLVM::LLVMPointerType>()) {
461a7a61359SValentin Clement       rewriter.replaceOpWithNewOp<mlir::LLVM::NullOp>(zero, ty);
462a7a61359SValentin Clement     } else if (ty.isa<mlir::IntegerType>()) {
463a7a61359SValentin Clement       rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
464a7a61359SValentin Clement           zero, ty, mlir::IntegerAttr::get(zero.getType(), 0));
465a7a61359SValentin Clement     } else if (mlir::LLVM::isCompatibleFloatingPointType(ty)) {
466a7a61359SValentin Clement       rewriter.replaceOpWithNewOp<mlir::LLVM::ConstantOp>(
467a7a61359SValentin Clement           zero, ty, mlir::FloatAttr::get(zero.getType(), 0.0));
468a7a61359SValentin Clement     } else {
469a7a61359SValentin Clement       // TODO: create ConstantAggregateZero for FIR aggregate/array types.
47052d813edSValentin Clement       return rewriter.notifyMatchFailure(
47152d813edSValentin Clement           zero,
472a7a61359SValentin Clement           "conversion of fir.zero with aggregate type not implemented yet");
473a7a61359SValentin Clement     }
474a7a61359SValentin Clement     return success();
475a7a61359SValentin Clement   }
476a7a61359SValentin Clement };
47732e08248SAndrzej Warzynski 
47854c56347SValentin Clement // Code shared between insert_value and extract_value Ops.
47954c56347SValentin Clement struct ValueOpCommon {
48054c56347SValentin Clement   // Translate the arguments pertaining to any multidimensional array to
48154c56347SValentin Clement   // row-major order for LLVM-IR.
48254c56347SValentin Clement   static void toRowMajor(SmallVectorImpl<mlir::Attribute> &attrs,
48354c56347SValentin Clement                          mlir::Type ty) {
48454c56347SValentin Clement     assert(ty && "type is null");
48554c56347SValentin Clement     const auto end = attrs.size();
48654c56347SValentin Clement     for (std::remove_const_t<decltype(end)> i = 0; i < end; ++i) {
48754c56347SValentin Clement       if (auto seq = ty.dyn_cast<mlir::LLVM::LLVMArrayType>()) {
48854c56347SValentin Clement         const auto dim = getDimension(seq);
48954c56347SValentin Clement         if (dim > 1) {
49054c56347SValentin Clement           auto ub = std::min(i + dim, end);
49154c56347SValentin Clement           std::reverse(attrs.begin() + i, attrs.begin() + ub);
49254c56347SValentin Clement           i += dim - 1;
49354c56347SValentin Clement         }
49454c56347SValentin Clement         ty = getArrayElementType(seq);
49554c56347SValentin Clement       } else if (auto st = ty.dyn_cast<mlir::LLVM::LLVMStructType>()) {
49654c56347SValentin Clement         ty = st.getBody()[attrs[i].cast<mlir::IntegerAttr>().getInt()];
49754c56347SValentin Clement       } else {
49854c56347SValentin Clement         llvm_unreachable("index into invalid type");
49954c56347SValentin Clement       }
50054c56347SValentin Clement     }
50154c56347SValentin Clement   }
50254c56347SValentin Clement 
50354c56347SValentin Clement   static llvm::SmallVector<mlir::Attribute>
50454c56347SValentin Clement   collectIndices(mlir::ConversionPatternRewriter &rewriter,
50554c56347SValentin Clement                  mlir::ArrayAttr arrAttr) {
50654c56347SValentin Clement     llvm::SmallVector<mlir::Attribute> attrs;
50754c56347SValentin Clement     for (auto i = arrAttr.begin(), e = arrAttr.end(); i != e; ++i) {
50854c56347SValentin Clement       if (i->isa<mlir::IntegerAttr>()) {
50954c56347SValentin Clement         attrs.push_back(*i);
51054c56347SValentin Clement       } else {
51154c56347SValentin Clement         auto fieldName = i->cast<mlir::StringAttr>().getValue();
51254c56347SValentin Clement         ++i;
51354c56347SValentin Clement         auto ty = i->cast<mlir::TypeAttr>().getValue();
51454c56347SValentin Clement         auto index = ty.cast<fir::RecordType>().getFieldIndex(fieldName);
51554c56347SValentin Clement         attrs.push_back(mlir::IntegerAttr::get(rewriter.getI32Type(), index));
51654c56347SValentin Clement       }
51754c56347SValentin Clement     }
51854c56347SValentin Clement     return attrs;
51954c56347SValentin Clement   }
52054c56347SValentin Clement 
52154c56347SValentin Clement private:
52254c56347SValentin Clement   static unsigned getDimension(mlir::LLVM::LLVMArrayType ty) {
52354c56347SValentin Clement     unsigned result = 1;
52454c56347SValentin Clement     for (auto eleTy = ty.getElementType().dyn_cast<mlir::LLVM::LLVMArrayType>();
52554c56347SValentin Clement          eleTy;
52654c56347SValentin Clement          eleTy = eleTy.getElementType().dyn_cast<mlir::LLVM::LLVMArrayType>())
52754c56347SValentin Clement       ++result;
52854c56347SValentin Clement     return result;
52954c56347SValentin Clement   }
53054c56347SValentin Clement 
53154c56347SValentin Clement   static mlir::Type getArrayElementType(mlir::LLVM::LLVMArrayType ty) {
53254c56347SValentin Clement     auto eleTy = ty.getElementType();
53354c56347SValentin Clement     while (auto arrTy = eleTy.dyn_cast<mlir::LLVM::LLVMArrayType>())
53454c56347SValentin Clement       eleTy = arrTy.getElementType();
53554c56347SValentin Clement     return eleTy;
53654c56347SValentin Clement   }
53754c56347SValentin Clement };
53854c56347SValentin Clement 
53954c56347SValentin Clement /// Extract a subobject value from an ssa-value of aggregate type
54054c56347SValentin Clement struct ExtractValueOpConversion
54154c56347SValentin Clement     : public FIROpAndTypeConversion<fir::ExtractValueOp>,
54254c56347SValentin Clement       public ValueOpCommon {
54354c56347SValentin Clement   using FIROpAndTypeConversion::FIROpAndTypeConversion;
54454c56347SValentin Clement 
54554c56347SValentin Clement   mlir::LogicalResult
54654c56347SValentin Clement   doRewrite(fir::ExtractValueOp extractVal, mlir::Type ty, OpAdaptor adaptor,
54754c56347SValentin Clement             mlir::ConversionPatternRewriter &rewriter) const override {
54854c56347SValentin Clement     auto attrs = collectIndices(rewriter, extractVal.coor());
54954c56347SValentin Clement     toRowMajor(attrs, adaptor.getOperands()[0].getType());
55054c56347SValentin Clement     auto position = mlir::ArrayAttr::get(extractVal.getContext(), attrs);
55154c56347SValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::ExtractValueOp>(
55254c56347SValentin Clement         extractVal, ty, adaptor.getOperands()[0], position);
55354c56347SValentin Clement     return success();
55454c56347SValentin Clement   }
55554c56347SValentin Clement };
55654c56347SValentin Clement 
55754c56347SValentin Clement /// InsertValue is the generalized instruction for the composition of new
55854c56347SValentin Clement /// aggregate type values.
55954c56347SValentin Clement struct InsertValueOpConversion
56054c56347SValentin Clement     : public FIROpAndTypeConversion<fir::InsertValueOp>,
56154c56347SValentin Clement       public ValueOpCommon {
56254c56347SValentin Clement   using FIROpAndTypeConversion::FIROpAndTypeConversion;
56354c56347SValentin Clement 
56454c56347SValentin Clement   mlir::LogicalResult
56554c56347SValentin Clement   doRewrite(fir::InsertValueOp insertVal, mlir::Type ty, OpAdaptor adaptor,
56654c56347SValentin Clement             mlir::ConversionPatternRewriter &rewriter) const override {
56754c56347SValentin Clement     auto attrs = collectIndices(rewriter, insertVal.coor());
56854c56347SValentin Clement     toRowMajor(attrs, adaptor.getOperands()[0].getType());
56954c56347SValentin Clement     auto position = mlir::ArrayAttr::get(insertVal.getContext(), attrs);
57054c56347SValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(
57154c56347SValentin Clement         insertVal, ty, adaptor.getOperands()[0], adaptor.getOperands()[1],
57254c56347SValentin Clement         position);
57354c56347SValentin Clement     return success();
57454c56347SValentin Clement   }
57554c56347SValentin Clement };
57654c56347SValentin Clement 
5773ae8e442SValentin Clement /// InsertOnRange inserts a value into a sequence over a range of offsets.
5783ae8e442SValentin Clement struct InsertOnRangeOpConversion
5793ae8e442SValentin Clement     : public FIROpAndTypeConversion<fir::InsertOnRangeOp> {
5803ae8e442SValentin Clement   using FIROpAndTypeConversion::FIROpAndTypeConversion;
5813ae8e442SValentin Clement 
5823ae8e442SValentin Clement   // Increments an array of subscripts in a row major fasion.
5833ae8e442SValentin Clement   void incrementSubscripts(const SmallVector<uint64_t> &dims,
5843ae8e442SValentin Clement                            SmallVector<uint64_t> &subscripts) const {
5853ae8e442SValentin Clement     for (size_t i = dims.size(); i > 0; --i) {
5863ae8e442SValentin Clement       if (++subscripts[i - 1] < dims[i - 1]) {
5873ae8e442SValentin Clement         return;
5883ae8e442SValentin Clement       }
5893ae8e442SValentin Clement       subscripts[i - 1] = 0;
5903ae8e442SValentin Clement     }
5913ae8e442SValentin Clement   }
5923ae8e442SValentin Clement 
5933ae8e442SValentin Clement   mlir::LogicalResult
5943ae8e442SValentin Clement   doRewrite(fir::InsertOnRangeOp range, mlir::Type ty, OpAdaptor adaptor,
5953ae8e442SValentin Clement             mlir::ConversionPatternRewriter &rewriter) const override {
5963ae8e442SValentin Clement 
5973ae8e442SValentin Clement     llvm::SmallVector<uint64_t> dims;
5983ae8e442SValentin Clement     auto type = adaptor.getOperands()[0].getType();
5993ae8e442SValentin Clement 
6003ae8e442SValentin Clement     // Iteratively extract the array dimensions from the type.
6013ae8e442SValentin Clement     while (auto t = type.dyn_cast<mlir::LLVM::LLVMArrayType>()) {
6023ae8e442SValentin Clement       dims.push_back(t.getNumElements());
6033ae8e442SValentin Clement       type = t.getElementType();
6043ae8e442SValentin Clement     }
6053ae8e442SValentin Clement 
6063ae8e442SValentin Clement     SmallVector<uint64_t> lBounds;
6073ae8e442SValentin Clement     SmallVector<uint64_t> uBounds;
6083ae8e442SValentin Clement 
6093ae8e442SValentin Clement     // Extract integer value from the attribute
6103ae8e442SValentin Clement     SmallVector<int64_t> coordinates = llvm::to_vector<4>(
6113ae8e442SValentin Clement         llvm::map_range(range.coor(), [](Attribute a) -> int64_t {
6123ae8e442SValentin Clement           return a.cast<IntegerAttr>().getInt();
6133ae8e442SValentin Clement         }));
6143ae8e442SValentin Clement 
6153ae8e442SValentin Clement     // Unzip the upper and lower bound and convert to a row major format.
6163ae8e442SValentin Clement     for (auto i = coordinates.rbegin(), e = coordinates.rend(); i != e; ++i) {
6173ae8e442SValentin Clement       uBounds.push_back(*i++);
6183ae8e442SValentin Clement       lBounds.push_back(*i);
6193ae8e442SValentin Clement     }
6203ae8e442SValentin Clement 
6213ae8e442SValentin Clement     auto &subscripts = lBounds;
6223ae8e442SValentin Clement     auto loc = range.getLoc();
6233ae8e442SValentin Clement     mlir::Value lastOp = adaptor.getOperands()[0];
6243ae8e442SValentin Clement     mlir::Value insertVal = adaptor.getOperands()[1];
6253ae8e442SValentin Clement 
6263ae8e442SValentin Clement     auto i64Ty = rewriter.getI64Type();
6273ae8e442SValentin Clement     while (subscripts != uBounds) {
6283ae8e442SValentin Clement       // Convert uint64_t's to Attribute's.
6293ae8e442SValentin Clement       SmallVector<mlir::Attribute> subscriptAttrs;
6303ae8e442SValentin Clement       for (const auto &subscript : subscripts)
6313ae8e442SValentin Clement         subscriptAttrs.push_back(IntegerAttr::get(i64Ty, subscript));
6323ae8e442SValentin Clement       lastOp = rewriter.create<mlir::LLVM::InsertValueOp>(
6333ae8e442SValentin Clement           loc, ty, lastOp, insertVal,
6343ae8e442SValentin Clement           ArrayAttr::get(range.getContext(), subscriptAttrs));
6353ae8e442SValentin Clement 
6363ae8e442SValentin Clement       incrementSubscripts(dims, subscripts);
6373ae8e442SValentin Clement     }
6383ae8e442SValentin Clement 
6393ae8e442SValentin Clement     // Convert uint64_t's to Attribute's.
6403ae8e442SValentin Clement     SmallVector<mlir::Attribute> subscriptAttrs;
6413ae8e442SValentin Clement     for (const auto &subscript : subscripts)
6423ae8e442SValentin Clement       subscriptAttrs.push_back(
6433ae8e442SValentin Clement           IntegerAttr::get(rewriter.getI64Type(), subscript));
6443ae8e442SValentin Clement     mlir::ArrayRef<mlir::Attribute> arrayRef(subscriptAttrs);
6453ae8e442SValentin Clement 
6463ae8e442SValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(
6473ae8e442SValentin Clement         range, ty, lastOp, insertVal,
6483ae8e442SValentin Clement         ArrayAttr::get(range.getContext(), arrayRef));
6493ae8e442SValentin Clement 
6503ae8e442SValentin Clement     return success();
6513ae8e442SValentin Clement   }
6523ae8e442SValentin Clement };
6537b5132daSValentin Clement 
6547b5132daSValentin Clement //
6557b5132daSValentin Clement // Primitive operations on Complex types
6567b5132daSValentin Clement //
6577b5132daSValentin Clement 
6587b5132daSValentin Clement /// Generate inline code for complex addition/subtraction
6597b5132daSValentin Clement template <typename LLVMOP, typename OPTY>
6607b5132daSValentin Clement mlir::LLVM::InsertValueOp complexSum(OPTY sumop, mlir::ValueRange opnds,
6617b5132daSValentin Clement                                      mlir::ConversionPatternRewriter &rewriter,
6627b5132daSValentin Clement                                      fir::LLVMTypeConverter &lowering) {
6637b5132daSValentin Clement   mlir::Value a = opnds[0];
6647b5132daSValentin Clement   mlir::Value b = opnds[1];
6657b5132daSValentin Clement   auto loc = sumop.getLoc();
6667b5132daSValentin Clement   auto ctx = sumop.getContext();
6677b5132daSValentin Clement   auto c0 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(0));
6687b5132daSValentin Clement   auto c1 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(1));
6697b5132daSValentin Clement   mlir::Type eleTy = lowering.convertType(getComplexEleTy(sumop.getType()));
6707b5132daSValentin Clement   mlir::Type ty = lowering.convertType(sumop.getType());
6717b5132daSValentin Clement   auto x0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c0);
6727b5132daSValentin Clement   auto y0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c1);
6737b5132daSValentin Clement   auto x1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c0);
6747b5132daSValentin Clement   auto y1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c1);
6757b5132daSValentin Clement   auto rx = rewriter.create<LLVMOP>(loc, eleTy, x0, x1);
6767b5132daSValentin Clement   auto ry = rewriter.create<LLVMOP>(loc, eleTy, y0, y1);
6777b5132daSValentin Clement   auto r0 = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
6787b5132daSValentin Clement   auto r1 = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, r0, rx, c0);
6797b5132daSValentin Clement   return rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, r1, ry, c1);
6807b5132daSValentin Clement }
6817b5132daSValentin Clement 
6827b5132daSValentin Clement struct AddcOpConversion : public FIROpConversion<fir::AddcOp> {
6837b5132daSValentin Clement   using FIROpConversion::FIROpConversion;
6847b5132daSValentin Clement 
6857b5132daSValentin Clement   mlir::LogicalResult
6867b5132daSValentin Clement   matchAndRewrite(fir::AddcOp addc, OpAdaptor adaptor,
6877b5132daSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
6887b5132daSValentin Clement     // given: (x + iy) + (x' + iy')
6897b5132daSValentin Clement     // result: (x + x') + i(y + y')
6907b5132daSValentin Clement     auto r = complexSum<mlir::LLVM::FAddOp>(addc, adaptor.getOperands(),
6917b5132daSValentin Clement                                             rewriter, lowerTy());
6927b5132daSValentin Clement     rewriter.replaceOp(addc, r.getResult());
6937b5132daSValentin Clement     return success();
6947b5132daSValentin Clement   }
6957b5132daSValentin Clement };
6967b5132daSValentin Clement 
6977b5132daSValentin Clement struct SubcOpConversion : public FIROpConversion<fir::SubcOp> {
6987b5132daSValentin Clement   using FIROpConversion::FIROpConversion;
6997b5132daSValentin Clement 
7007b5132daSValentin Clement   mlir::LogicalResult
7017b5132daSValentin Clement   matchAndRewrite(fir::SubcOp subc, OpAdaptor adaptor,
7027b5132daSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
7037b5132daSValentin Clement     // given: (x + iy) - (x' + iy')
7047b5132daSValentin Clement     // result: (x - x') + i(y - y')
7057b5132daSValentin Clement     auto r = complexSum<mlir::LLVM::FSubOp>(subc, adaptor.getOperands(),
7067b5132daSValentin Clement                                             rewriter, lowerTy());
7077b5132daSValentin Clement     rewriter.replaceOp(subc, r.getResult());
7087b5132daSValentin Clement     return success();
7097b5132daSValentin Clement   }
7107b5132daSValentin Clement };
7117b5132daSValentin Clement 
7127b5132daSValentin Clement /// Inlined complex multiply
7137b5132daSValentin Clement struct MulcOpConversion : public FIROpConversion<fir::MulcOp> {
7147b5132daSValentin Clement   using FIROpConversion::FIROpConversion;
7157b5132daSValentin Clement 
7167b5132daSValentin Clement   mlir::LogicalResult
7177b5132daSValentin Clement   matchAndRewrite(fir::MulcOp mulc, OpAdaptor adaptor,
7187b5132daSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
7197b5132daSValentin Clement     // TODO: Can we use a call to __muldc3 ?
7207b5132daSValentin Clement     // given: (x + iy) * (x' + iy')
7217b5132daSValentin Clement     // result: (xx'-yy')+i(xy'+yx')
7227b5132daSValentin Clement     mlir::Value a = adaptor.getOperands()[0];
7237b5132daSValentin Clement     mlir::Value b = adaptor.getOperands()[1];
7247b5132daSValentin Clement     auto loc = mulc.getLoc();
7257b5132daSValentin Clement     auto *ctx = mulc.getContext();
7267b5132daSValentin Clement     auto c0 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(0));
7277b5132daSValentin Clement     auto c1 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(1));
7287b5132daSValentin Clement     mlir::Type eleTy = convertType(getComplexEleTy(mulc.getType()));
7297b5132daSValentin Clement     mlir::Type ty = convertType(mulc.getType());
7307b5132daSValentin Clement     auto x0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c0);
7317b5132daSValentin Clement     auto y0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c1);
7327b5132daSValentin Clement     auto x1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c0);
7337b5132daSValentin Clement     auto y1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c1);
7347b5132daSValentin Clement     auto xx = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, x0, x1);
7357b5132daSValentin Clement     auto yx = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, y0, x1);
7367b5132daSValentin Clement     auto xy = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, x0, y1);
7377b5132daSValentin Clement     auto ri = rewriter.create<mlir::LLVM::FAddOp>(loc, eleTy, xy, yx);
7387b5132daSValentin Clement     auto yy = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, y0, y1);
7397b5132daSValentin Clement     auto rr = rewriter.create<mlir::LLVM::FSubOp>(loc, eleTy, xx, yy);
7407b5132daSValentin Clement     auto ra = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
7417b5132daSValentin Clement     auto r1 = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, ra, rr, c0);
7427b5132daSValentin Clement     auto r0 = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, r1, ri, c1);
7437b5132daSValentin Clement     rewriter.replaceOp(mulc, r0.getResult());
7447b5132daSValentin Clement     return success();
7457b5132daSValentin Clement   }
7467b5132daSValentin Clement };
7477b5132daSValentin Clement 
7487b5132daSValentin Clement /// Inlined complex division
7497b5132daSValentin Clement struct DivcOpConversion : public FIROpConversion<fir::DivcOp> {
7507b5132daSValentin Clement   using FIROpConversion::FIROpConversion;
7517b5132daSValentin Clement 
7527b5132daSValentin Clement   mlir::LogicalResult
7537b5132daSValentin Clement   matchAndRewrite(fir::DivcOp divc, OpAdaptor adaptor,
7547b5132daSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
7557b5132daSValentin Clement     // TODO: Can we use a call to __divdc3 instead?
7567b5132daSValentin Clement     // Just generate inline code for now.
7577b5132daSValentin Clement     // given: (x + iy) / (x' + iy')
7587b5132daSValentin Clement     // result: ((xx'+yy')/d) + i((yx'-xy')/d) where d = x'x' + y'y'
7597b5132daSValentin Clement     mlir::Value a = adaptor.getOperands()[0];
7607b5132daSValentin Clement     mlir::Value b = adaptor.getOperands()[1];
7617b5132daSValentin Clement     auto loc = divc.getLoc();
7627b5132daSValentin Clement     auto *ctx = divc.getContext();
7637b5132daSValentin Clement     auto c0 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(0));
7647b5132daSValentin Clement     auto c1 = mlir::ArrayAttr::get(ctx, rewriter.getI32IntegerAttr(1));
7657b5132daSValentin Clement     mlir::Type eleTy = convertType(getComplexEleTy(divc.getType()));
7667b5132daSValentin Clement     mlir::Type ty = convertType(divc.getType());
7677b5132daSValentin Clement     auto x0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c0);
7687b5132daSValentin Clement     auto y0 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, a, c1);
7697b5132daSValentin Clement     auto x1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c0);
7707b5132daSValentin Clement     auto y1 = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, b, c1);
7717b5132daSValentin Clement     auto xx = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, x0, x1);
7727b5132daSValentin Clement     auto x1x1 = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, x1, x1);
7737b5132daSValentin Clement     auto yx = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, y0, x1);
7747b5132daSValentin Clement     auto xy = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, x0, y1);
7757b5132daSValentin Clement     auto yy = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, y0, y1);
7767b5132daSValentin Clement     auto y1y1 = rewriter.create<mlir::LLVM::FMulOp>(loc, eleTy, y1, y1);
7777b5132daSValentin Clement     auto d = rewriter.create<mlir::LLVM::FAddOp>(loc, eleTy, x1x1, y1y1);
7787b5132daSValentin Clement     auto rrn = rewriter.create<mlir::LLVM::FAddOp>(loc, eleTy, xx, yy);
7797b5132daSValentin Clement     auto rin = rewriter.create<mlir::LLVM::FSubOp>(loc, eleTy, yx, xy);
7807b5132daSValentin Clement     auto rr = rewriter.create<mlir::LLVM::FDivOp>(loc, eleTy, rrn, d);
7817b5132daSValentin Clement     auto ri = rewriter.create<mlir::LLVM::FDivOp>(loc, eleTy, rin, d);
7827b5132daSValentin Clement     auto ra = rewriter.create<mlir::LLVM::UndefOp>(loc, ty);
7837b5132daSValentin Clement     auto r1 = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, ra, rr, c0);
7847b5132daSValentin Clement     auto r0 = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, r1, ri, c1);
7857b5132daSValentin Clement     rewriter.replaceOp(divc, r0.getResult());
7867b5132daSValentin Clement     return success();
7877b5132daSValentin Clement   }
7887b5132daSValentin Clement };
7897b5132daSValentin Clement 
7907b5132daSValentin Clement /// Inlined complex negation
7917b5132daSValentin Clement struct NegcOpConversion : public FIROpConversion<fir::NegcOp> {
7927b5132daSValentin Clement   using FIROpConversion::FIROpConversion;
7937b5132daSValentin Clement 
7947b5132daSValentin Clement   mlir::LogicalResult
7957b5132daSValentin Clement   matchAndRewrite(fir::NegcOp neg, OpAdaptor adaptor,
7967b5132daSValentin Clement                   mlir::ConversionPatternRewriter &rewriter) const override {
7977b5132daSValentin Clement     // given: -(x + iy)
7987b5132daSValentin Clement     // result: -x - iy
7997b5132daSValentin Clement     auto *ctxt = neg.getContext();
8007b5132daSValentin Clement     auto eleTy = convertType(getComplexEleTy(neg.getType()));
8017b5132daSValentin Clement     auto ty = convertType(neg.getType());
8027b5132daSValentin Clement     auto loc = neg.getLoc();
8037b5132daSValentin Clement     mlir::Value o0 = adaptor.getOperands()[0];
8047b5132daSValentin Clement     auto c0 = mlir::ArrayAttr::get(ctxt, rewriter.getI32IntegerAttr(0));
8057b5132daSValentin Clement     auto c1 = mlir::ArrayAttr::get(ctxt, rewriter.getI32IntegerAttr(1));
8067b5132daSValentin Clement     auto rp = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, o0, c0);
8077b5132daSValentin Clement     auto ip = rewriter.create<mlir::LLVM::ExtractValueOp>(loc, eleTy, o0, c1);
8087b5132daSValentin Clement     auto nrp = rewriter.create<mlir::LLVM::FNegOp>(loc, eleTy, rp);
8097b5132daSValentin Clement     auto nip = rewriter.create<mlir::LLVM::FNegOp>(loc, eleTy, ip);
8107b5132daSValentin Clement     auto r = rewriter.create<mlir::LLVM::InsertValueOp>(loc, ty, o0, nrp, c0);
8117b5132daSValentin Clement     rewriter.replaceOpWithNewOp<mlir::LLVM::InsertValueOp>(neg, ty, r, nip, c1);
8127b5132daSValentin Clement     return success();
8137b5132daSValentin Clement   }
8147b5132daSValentin Clement };
8157b5132daSValentin Clement 
816044d5b5dSValentin Clement } // namespace
817044d5b5dSValentin Clement 
818044d5b5dSValentin Clement namespace {
819044d5b5dSValentin Clement /// Convert FIR dialect to LLVM dialect
820044d5b5dSValentin Clement ///
821044d5b5dSValentin Clement /// This pass lowers all FIR dialect operations to LLVM IR dialect. An
822044d5b5dSValentin Clement /// MLIR pass is used to lower residual Std dialect to LLVM IR dialect.
823044d5b5dSValentin Clement ///
824044d5b5dSValentin Clement /// This pass is not complete yet. We are upstreaming it in small patches.
825044d5b5dSValentin Clement class FIRToLLVMLowering : public fir::FIRToLLVMLoweringBase<FIRToLLVMLowering> {
826044d5b5dSValentin Clement public:
827044d5b5dSValentin Clement   mlir::ModuleOp getModule() { return getOperation(); }
828044d5b5dSValentin Clement 
829044d5b5dSValentin Clement   void runOnOperation() override final {
8307b5132daSValentin Clement     auto mod = getModule();
8317b5132daSValentin Clement     if (!forcedTargetTriple.empty()) {
8327b5132daSValentin Clement       fir::setTargetTriple(mod, forcedTargetTriple);
8337b5132daSValentin Clement     }
8347b5132daSValentin Clement 
835044d5b5dSValentin Clement     auto *context = getModule().getContext();
836044d5b5dSValentin Clement     fir::LLVMTypeConverter typeConverter{getModule()};
837044d5b5dSValentin Clement     mlir::OwningRewritePatternList pattern(context);
838*e3349fa1SAndrzej Warzynski     pattern.insert<AddcOpConversion, AddrOfOpConversion, CallOpConversion,
839*e3349fa1SAndrzej Warzynski                    ConvertOpConversion, DivcOpConversion,
840*e3349fa1SAndrzej Warzynski                    ExtractValueOpConversion, HasValueOpConversion,
841*e3349fa1SAndrzej Warzynski                    GlobalOpConversion, InsertOnRangeOpConversion,
842*e3349fa1SAndrzej Warzynski                    InsertValueOpConversion, LoadOpConversion, NegcOpConversion,
843*e3349fa1SAndrzej Warzynski                    MulcOpConversion, SelectOpConversion, SelectRankOpConversion,
844*e3349fa1SAndrzej Warzynski                    StoreOpConversion, SubcOpConversion, UndefOpConversion,
8457b5132daSValentin Clement                    UnreachableOpConversion, ZeroOpConversion>(typeConverter);
846044d5b5dSValentin Clement     mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
847044d5b5dSValentin Clement     mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
848044d5b5dSValentin Clement                                                             pattern);
849044d5b5dSValentin Clement     mlir::ConversionTarget target{*context};
850044d5b5dSValentin Clement     target.addLegalDialect<mlir::LLVM::LLVMDialect>();
851044d5b5dSValentin Clement 
852044d5b5dSValentin Clement     // required NOPs for applying a full conversion
853044d5b5dSValentin Clement     target.addLegalOp<mlir::ModuleOp>();
854044d5b5dSValentin Clement 
855044d5b5dSValentin Clement     // apply the patterns
856044d5b5dSValentin Clement     if (mlir::failed(mlir::applyFullConversion(getModule(), target,
857044d5b5dSValentin Clement                                                std::move(pattern)))) {
858044d5b5dSValentin Clement       signalPassFailure();
859044d5b5dSValentin Clement     }
860044d5b5dSValentin Clement   }
861044d5b5dSValentin Clement };
862044d5b5dSValentin Clement } // namespace
863044d5b5dSValentin Clement 
864044d5b5dSValentin Clement std::unique_ptr<mlir::Pass> fir::createFIRToLLVMPass() {
865044d5b5dSValentin Clement   return std::make_unique<FIRToLLVMLowering>();
866044d5b5dSValentin Clement }
867