1 //===- FuncToLLVM.cpp - Func to LLVM dialect conversion -------------------===//
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 // This file implements a pass to convert MLIR Func and builtin dialects
10 // into the LLVM IR dialect.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "../PassDetail.h"
15 #include "mlir/Analysis/DataLayoutAnalysis.h"
16 #include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
17 #include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
18 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
19 #include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
20 #include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
21 #include "mlir/Conversion/LLVMCommon/Pattern.h"
22 #include "mlir/Conversion/LLVMCommon/VectorPattern.h"
23 #include "mlir/Dialect/Func/IR/FuncOps.h"
24 #include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
25 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
26 #include "mlir/Dialect/Utils/StaticValueUtils.h"
27 #include "mlir/IR/Attributes.h"
28 #include "mlir/IR/BlockAndValueMapping.h"
29 #include "mlir/IR/Builders.h"
30 #include "mlir/IR/BuiltinOps.h"
31 #include "mlir/IR/PatternMatch.h"
32 #include "mlir/IR/TypeUtilities.h"
33 #include "mlir/Support/LogicalResult.h"
34 #include "mlir/Support/MathExtras.h"
35 #include "mlir/Transforms/DialectConversion.h"
36 #include "mlir/Transforms/Passes.h"
37 #include "llvm/ADT/TypeSwitch.h"
38 #include "llvm/IR/DerivedTypes.h"
39 #include "llvm/IR/IRBuilder.h"
40 #include "llvm/IR/Type.h"
41 #include "llvm/Support/CommandLine.h"
42 #include "llvm/Support/FormatVariadic.h"
43 #include <algorithm>
44 #include <functional>
45 
46 using namespace mlir;
47 
48 #define PASS_NAME "convert-func-to-llvm"
49 
50 /// Only retain those attributes that are not constructed by
51 /// `LLVMFuncOp::build`. If `filterArgAttrs` is set, also filter out argument
52 /// attributes.
53 static void filterFuncAttributes(ArrayRef<NamedAttribute> attrs,
54                                  bool filterArgAndResAttrs,
55                                  SmallVectorImpl<NamedAttribute> &result) {
56   for (const auto &attr : attrs) {
57     if (attr.getName() == SymbolTable::getSymbolAttrName() ||
58         attr.getName() == FunctionOpInterface::getTypeAttrName() ||
59         attr.getName() == "func.varargs" ||
60         (filterArgAndResAttrs &&
61          (attr.getName() == FunctionOpInterface::getArgDictAttrName() ||
62           attr.getName() == FunctionOpInterface::getResultDictAttrName())))
63       continue;
64     result.push_back(attr);
65   }
66 }
67 
68 /// Helper function for wrapping all attributes into a single DictionaryAttr
69 static auto wrapAsStructAttrs(OpBuilder &b, ArrayAttr attrs) {
70   return DictionaryAttr::get(
71       b.getContext(),
72       b.getNamedAttr(LLVM::LLVMDialect::getStructAttrsAttrName(), attrs));
73 }
74 
75 /// Combines all result attributes into a single DictionaryAttr
76 /// and prepends to argument attrs.
77 /// This is intended to be used to format the attributes for a C wrapper
78 /// function when the result(s) is converted to the first function argument
79 /// (in the multiple return case, all returns get wrapped into a single
80 /// argument). The total number of argument attributes should be equal to
81 /// (number of function arguments) + 1.
82 static void
83 prependResAttrsToArgAttrs(OpBuilder &builder,
84                           SmallVectorImpl<NamedAttribute> &attributes,
85                           size_t numArguments) {
86   auto allAttrs = SmallVector<Attribute>(
87       numArguments + 1, DictionaryAttr::get(builder.getContext()));
88   NamedAttribute *argAttrs = nullptr;
89   for (auto *it = attributes.begin(); it != attributes.end();) {
90     if (it->getName() == FunctionOpInterface::getArgDictAttrName()) {
91       auto arrayAttrs = it->getValue().cast<ArrayAttr>();
92       assert(arrayAttrs.size() == numArguments &&
93              "Number of arg attrs and args should match");
94       std::copy(arrayAttrs.begin(), arrayAttrs.end(), allAttrs.begin() + 1);
95       argAttrs = it;
96     } else if (it->getName() == FunctionOpInterface::getResultDictAttrName()) {
97       auto arrayAttrs = it->getValue().cast<ArrayAttr>();
98       assert(!arrayAttrs.empty() && "expected array to be non-empty");
99       allAttrs[0] = (arrayAttrs.size() == 1)
100                         ? arrayAttrs[0]
101                         : wrapAsStructAttrs(builder, arrayAttrs);
102       it = attributes.erase(it);
103       continue;
104     }
105     it++;
106   }
107 
108   auto newArgAttrs =
109       builder.getNamedAttr(FunctionOpInterface::getArgDictAttrName(),
110                            builder.getArrayAttr(allAttrs));
111   if (!argAttrs) {
112     attributes.emplace_back(newArgAttrs);
113     return;
114   }
115   *argAttrs = newArgAttrs;
116 }
117 
118 /// Creates an auxiliary function with pointer-to-memref-descriptor-struct
119 /// arguments instead of unpacked arguments. This function can be called from C
120 /// by passing a pointer to a C struct corresponding to a memref descriptor.
121 /// Similarly, returned memrefs are passed via pointers to a C struct that is
122 /// passed as additional argument.
123 /// Internally, the auxiliary function unpacks the descriptor into individual
124 /// components and forwards them to `newFuncOp` and forwards the results to
125 /// the extra arguments.
126 static void wrapForExternalCallers(OpBuilder &rewriter, Location loc,
127                                    LLVMTypeConverter &typeConverter,
128                                    func::FuncOp funcOp,
129                                    LLVM::LLVMFuncOp newFuncOp) {
130   auto type = funcOp.getFunctionType();
131   SmallVector<NamedAttribute, 4> attributes;
132   filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/false,
133                        attributes);
134   Type wrapperFuncType;
135   bool resultIsNowArg;
136   std::tie(wrapperFuncType, resultIsNowArg) =
137       typeConverter.convertFunctionTypeCWrapper(type);
138   if (resultIsNowArg)
139     prependResAttrsToArgAttrs(rewriter, attributes, funcOp.getNumArguments());
140   auto wrapperFuncOp = rewriter.create<LLVM::LLVMFuncOp>(
141       loc, llvm::formatv("_mlir_ciface_{0}", funcOp.getName()).str(),
142       wrapperFuncType, LLVM::Linkage::External, /*dsoLocal*/ false, attributes);
143 
144   OpBuilder::InsertionGuard guard(rewriter);
145   rewriter.setInsertionPointToStart(wrapperFuncOp.addEntryBlock());
146 
147   SmallVector<Value, 8> args;
148   size_t argOffset = resultIsNowArg ? 1 : 0;
149   for (auto &en : llvm::enumerate(type.getInputs())) {
150     Value arg = wrapperFuncOp.getArgument(en.index() + argOffset);
151     if (auto memrefType = en.value().dyn_cast<MemRefType>()) {
152       Value loaded = rewriter.create<LLVM::LoadOp>(loc, arg);
153       MemRefDescriptor::unpack(rewriter, loc, loaded, memrefType, args);
154       continue;
155     }
156     if (en.value().isa<UnrankedMemRefType>()) {
157       Value loaded = rewriter.create<LLVM::LoadOp>(loc, arg);
158       UnrankedMemRefDescriptor::unpack(rewriter, loc, loaded, args);
159       continue;
160     }
161 
162     args.push_back(arg);
163   }
164 
165   auto call = rewriter.create<LLVM::CallOp>(loc, newFuncOp, args);
166 
167   if (resultIsNowArg) {
168     rewriter.create<LLVM::StoreOp>(loc, call.getResult(0),
169                                    wrapperFuncOp.getArgument(0));
170     rewriter.create<LLVM::ReturnOp>(loc, ValueRange{});
171   } else {
172     rewriter.create<LLVM::ReturnOp>(loc, call.getResults());
173   }
174 }
175 
176 /// Creates an auxiliary function with pointer-to-memref-descriptor-struct
177 /// arguments instead of unpacked arguments. Creates a body for the (external)
178 /// `newFuncOp` that allocates a memref descriptor on stack, packs the
179 /// individual arguments into this descriptor and passes a pointer to it into
180 /// the auxiliary function. If the result of the function cannot be directly
181 /// returned, we write it to a special first argument that provides a pointer
182 /// to a corresponding struct. This auxiliary external function is now
183 /// compatible with functions defined in C using pointers to C structs
184 /// corresponding to a memref descriptor.
185 static void wrapExternalFunction(OpBuilder &builder, Location loc,
186                                  LLVMTypeConverter &typeConverter,
187                                  func::FuncOp funcOp,
188                                  LLVM::LLVMFuncOp newFuncOp) {
189   OpBuilder::InsertionGuard guard(builder);
190 
191   Type wrapperType;
192   bool resultIsNowArg;
193   std::tie(wrapperType, resultIsNowArg) =
194       typeConverter.convertFunctionTypeCWrapper(funcOp.getFunctionType());
195   // This conversion can only fail if it could not convert one of the argument
196   // types. But since it has been applied to a non-wrapper function before, it
197   // should have failed earlier and not reach this point at all.
198   assert(wrapperType && "unexpected type conversion failure");
199 
200   SmallVector<NamedAttribute, 4> attributes;
201   filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/false,
202                        attributes);
203 
204   if (resultIsNowArg)
205     prependResAttrsToArgAttrs(builder, attributes, funcOp.getNumArguments());
206   // Create the auxiliary function.
207   auto wrapperFunc = builder.create<LLVM::LLVMFuncOp>(
208       loc, llvm::formatv("_mlir_ciface_{0}", funcOp.getName()).str(),
209       wrapperType, LLVM::Linkage::External, /*dsoLocal*/ false, attributes);
210 
211   builder.setInsertionPointToStart(newFuncOp.addEntryBlock());
212 
213   // Get a ValueRange containing arguments.
214   FunctionType type = funcOp.getFunctionType();
215   SmallVector<Value, 8> args;
216   args.reserve(type.getNumInputs());
217   ValueRange wrapperArgsRange(newFuncOp.getArguments());
218 
219   if (resultIsNowArg) {
220     // Allocate the struct on the stack and pass the pointer.
221     Type resultType =
222         wrapperType.cast<LLVM::LLVMFunctionType>().getParamType(0);
223     Value one = builder.create<LLVM::ConstantOp>(
224         loc, typeConverter.convertType(builder.getIndexType()),
225         builder.getIntegerAttr(builder.getIndexType(), 1));
226     Value result = builder.create<LLVM::AllocaOp>(loc, resultType, one);
227     args.push_back(result);
228   }
229 
230   // Iterate over the inputs of the original function and pack values into
231   // memref descriptors if the original type is a memref.
232   for (auto &en : llvm::enumerate(type.getInputs())) {
233     Value arg;
234     int numToDrop = 1;
235     auto memRefType = en.value().dyn_cast<MemRefType>();
236     auto unrankedMemRefType = en.value().dyn_cast<UnrankedMemRefType>();
237     if (memRefType || unrankedMemRefType) {
238       numToDrop = memRefType
239                       ? MemRefDescriptor::getNumUnpackedValues(memRefType)
240                       : UnrankedMemRefDescriptor::getNumUnpackedValues();
241       Value packed =
242           memRefType
243               ? MemRefDescriptor::pack(builder, loc, typeConverter, memRefType,
244                                        wrapperArgsRange.take_front(numToDrop))
245               : UnrankedMemRefDescriptor::pack(
246                     builder, loc, typeConverter, unrankedMemRefType,
247                     wrapperArgsRange.take_front(numToDrop));
248 
249       auto ptrTy = LLVM::LLVMPointerType::get(packed.getType());
250       Value one = builder.create<LLVM::ConstantOp>(
251           loc, typeConverter.convertType(builder.getIndexType()),
252           builder.getIntegerAttr(builder.getIndexType(), 1));
253       Value allocated =
254           builder.create<LLVM::AllocaOp>(loc, ptrTy, one, /*alignment=*/0);
255       builder.create<LLVM::StoreOp>(loc, packed, allocated);
256       arg = allocated;
257     } else {
258       arg = wrapperArgsRange[0];
259     }
260 
261     args.push_back(arg);
262     wrapperArgsRange = wrapperArgsRange.drop_front(numToDrop);
263   }
264   assert(wrapperArgsRange.empty() && "did not map some of the arguments");
265 
266   auto call = builder.create<LLVM::CallOp>(loc, wrapperFunc, args);
267 
268   if (resultIsNowArg) {
269     Value result = builder.create<LLVM::LoadOp>(loc, args.front());
270     builder.create<LLVM::ReturnOp>(loc, ValueRange{result});
271   } else {
272     builder.create<LLVM::ReturnOp>(loc, call.getResults());
273   }
274 }
275 
276 namespace {
277 
278 struct FuncOpConversionBase : public ConvertOpToLLVMPattern<func::FuncOp> {
279 protected:
280   using ConvertOpToLLVMPattern<func::FuncOp>::ConvertOpToLLVMPattern;
281 
282   // Convert input FuncOp to LLVMFuncOp by using the LLVMTypeConverter provided
283   // to this legalization pattern.
284   LLVM::LLVMFuncOp
285   convertFuncOpToLLVMFuncOp(func::FuncOp funcOp,
286                             ConversionPatternRewriter &rewriter) const {
287     // Convert the original function arguments. They are converted using the
288     // LLVMTypeConverter provided to this legalization pattern.
289     auto varargsAttr = funcOp->getAttrOfType<BoolAttr>("func.varargs");
290     TypeConverter::SignatureConversion result(funcOp.getNumArguments());
291     auto llvmType = getTypeConverter()->convertFunctionSignature(
292         funcOp.getFunctionType(), varargsAttr && varargsAttr.getValue(),
293         result);
294     if (!llvmType)
295       return nullptr;
296 
297     // Propagate argument/result attributes to all converted arguments/result
298     // obtained after converting a given original argument/result.
299     SmallVector<NamedAttribute, 4> attributes;
300     filterFuncAttributes(funcOp->getAttrs(), /*filterArgAndResAttrs=*/true,
301                          attributes);
302     if (ArrayAttr resAttrDicts = funcOp.getAllResultAttrs()) {
303       assert(!resAttrDicts.empty() && "expected array to be non-empty");
304       auto newResAttrDicts =
305           (funcOp.getNumResults() == 1)
306               ? resAttrDicts
307               : rewriter.getArrayAttr(
308                     {wrapAsStructAttrs(rewriter, resAttrDicts)});
309       attributes.push_back(rewriter.getNamedAttr(
310           FunctionOpInterface::getResultDictAttrName(), newResAttrDicts));
311     }
312     if (ArrayAttr argAttrDicts = funcOp.getAllArgAttrs()) {
313       SmallVector<Attribute, 4> newArgAttrs(
314           llvmType.cast<LLVM::LLVMFunctionType>().getNumParams());
315       for (unsigned i = 0, e = funcOp.getNumArguments(); i < e; ++i) {
316         auto mapping = result.getInputMapping(i);
317         assert(mapping.hasValue() &&
318                "unexpected deletion of function argument");
319         for (size_t j = 0; j < mapping->size; ++j)
320           newArgAttrs[mapping->inputNo + j] = argAttrDicts[i];
321       }
322       attributes.push_back(
323           rewriter.getNamedAttr(FunctionOpInterface::getArgDictAttrName(),
324                                 rewriter.getArrayAttr(newArgAttrs)));
325     }
326     for (const auto &pair : llvm::enumerate(attributes)) {
327       if (pair.value().getName() == "llvm.linkage") {
328         attributes.erase(attributes.begin() + pair.index());
329         break;
330       }
331     }
332 
333     // Create an LLVM function, use external linkage by default until MLIR
334     // functions have linkage.
335     LLVM::Linkage linkage = LLVM::Linkage::External;
336     if (funcOp->hasAttr("llvm.linkage")) {
337       auto attr =
338           funcOp->getAttr("llvm.linkage").dyn_cast<mlir::LLVM::LinkageAttr>();
339       if (!attr) {
340         funcOp->emitError()
341             << "Contains llvm.linkage attribute not of type LLVM::LinkageAttr";
342         return nullptr;
343       }
344       linkage = attr.getLinkage();
345     }
346     auto newFuncOp = rewriter.create<LLVM::LLVMFuncOp>(
347         funcOp.getLoc(), funcOp.getName(), llvmType, linkage,
348         /*dsoLocal*/ false, attributes);
349     rewriter.inlineRegionBefore(funcOp.getBody(), newFuncOp.getBody(),
350                                 newFuncOp.end());
351     if (failed(rewriter.convertRegionTypes(&newFuncOp.getBody(), *typeConverter,
352                                            &result)))
353       return nullptr;
354 
355     return newFuncOp;
356   }
357 };
358 
359 /// FuncOp legalization pattern that converts MemRef arguments to pointers to
360 /// MemRef descriptors (LLVM struct data types) containing all the MemRef type
361 /// information.
362 static constexpr StringRef kEmitIfaceAttrName = "llvm.emit_c_interface";
363 struct FuncOpConversion : public FuncOpConversionBase {
364   FuncOpConversion(LLVMTypeConverter &converter)
365       : FuncOpConversionBase(converter) {}
366 
367   LogicalResult
368   matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
369                   ConversionPatternRewriter &rewriter) const override {
370     auto newFuncOp = convertFuncOpToLLVMFuncOp(funcOp, rewriter);
371     if (!newFuncOp)
372       return failure();
373 
374     if (getTypeConverter()->getOptions().emitCWrappers ||
375         funcOp->getAttrOfType<UnitAttr>(kEmitIfaceAttrName)) {
376       if (newFuncOp.isExternal())
377         wrapExternalFunction(rewriter, funcOp.getLoc(), *getTypeConverter(),
378                              funcOp, newFuncOp);
379       else
380         wrapForExternalCallers(rewriter, funcOp.getLoc(), *getTypeConverter(),
381                                funcOp, newFuncOp);
382     }
383 
384     rewriter.eraseOp(funcOp);
385     return success();
386   }
387 };
388 
389 /// FuncOp legalization pattern that converts MemRef arguments to bare pointers
390 /// to the MemRef element type. This will impact the calling convention and ABI.
391 struct BarePtrFuncOpConversion : public FuncOpConversionBase {
392   using FuncOpConversionBase::FuncOpConversionBase;
393 
394   LogicalResult
395   matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor,
396                   ConversionPatternRewriter &rewriter) const override {
397 
398     // TODO: bare ptr conversion could be handled by argument materialization
399     // and most of the code below would go away. But to do this, we would need a
400     // way to distinguish between FuncOp and other regions in the
401     // addArgumentMaterialization hook.
402 
403     // Store the type of memref-typed arguments before the conversion so that we
404     // can promote them to MemRef descriptor at the beginning of the function.
405     SmallVector<Type, 8> oldArgTypes =
406         llvm::to_vector<8>(funcOp.getFunctionType().getInputs());
407 
408     auto newFuncOp = convertFuncOpToLLVMFuncOp(funcOp, rewriter);
409     if (!newFuncOp)
410       return failure();
411     if (newFuncOp.getBody().empty()) {
412       rewriter.eraseOp(funcOp);
413       return success();
414     }
415 
416     // Promote bare pointers from memref arguments to memref descriptors at the
417     // beginning of the function so that all the memrefs in the function have a
418     // uniform representation.
419     Block *entryBlock = &newFuncOp.getBody().front();
420     auto blockArgs = entryBlock->getArguments();
421     assert(blockArgs.size() == oldArgTypes.size() &&
422            "The number of arguments and types doesn't match");
423 
424     OpBuilder::InsertionGuard guard(rewriter);
425     rewriter.setInsertionPointToStart(entryBlock);
426     for (auto it : llvm::zip(blockArgs, oldArgTypes)) {
427       BlockArgument arg = std::get<0>(it);
428       Type argTy = std::get<1>(it);
429 
430       // Unranked memrefs are not supported in the bare pointer calling
431       // convention. We should have bailed out before in the presence of
432       // unranked memrefs.
433       assert(!argTy.isa<UnrankedMemRefType>() &&
434              "Unranked memref is not supported");
435       auto memrefTy = argTy.dyn_cast<MemRefType>();
436       if (!memrefTy)
437         continue;
438 
439       // Replace barePtr with a placeholder (undef), promote barePtr to a ranked
440       // or unranked memref descriptor and replace placeholder with the last
441       // instruction of the memref descriptor.
442       // TODO: The placeholder is needed to avoid replacing barePtr uses in the
443       // MemRef descriptor instructions. We may want to have a utility in the
444       // rewriter to properly handle this use case.
445       Location loc = funcOp.getLoc();
446       auto placeholder = rewriter.create<LLVM::UndefOp>(
447           loc, getTypeConverter()->convertType(memrefTy));
448       rewriter.replaceUsesOfBlockArgument(arg, placeholder);
449 
450       Value desc = MemRefDescriptor::fromStaticShape(
451           rewriter, loc, *getTypeConverter(), memrefTy, arg);
452       rewriter.replaceOp(placeholder, {desc});
453     }
454 
455     rewriter.eraseOp(funcOp);
456     return success();
457   }
458 };
459 
460 struct ConstantOpLowering : public ConvertOpToLLVMPattern<func::ConstantOp> {
461   using ConvertOpToLLVMPattern<func::ConstantOp>::ConvertOpToLLVMPattern;
462 
463   LogicalResult
464   matchAndRewrite(func::ConstantOp op, OpAdaptor adaptor,
465                   ConversionPatternRewriter &rewriter) const override {
466     auto type = typeConverter->convertType(op.getResult().getType());
467     if (!type || !LLVM::isCompatibleType(type))
468       return rewriter.notifyMatchFailure(op, "failed to convert result type");
469 
470     auto newOp =
471         rewriter.create<LLVM::AddressOfOp>(op.getLoc(), type, op.getValue());
472     for (const NamedAttribute &attr : op->getAttrs()) {
473       if (attr.getName().strref() == "value")
474         continue;
475       newOp->setAttr(attr.getName(), attr.getValue());
476     }
477     rewriter.replaceOp(op, newOp->getResults());
478     return success();
479   }
480 };
481 
482 // A CallOp automatically promotes MemRefType to a sequence of alloca/store and
483 // passes the pointer to the MemRef across function boundaries.
484 template <typename CallOpType>
485 struct CallOpInterfaceLowering : public ConvertOpToLLVMPattern<CallOpType> {
486   using ConvertOpToLLVMPattern<CallOpType>::ConvertOpToLLVMPattern;
487   using Super = CallOpInterfaceLowering<CallOpType>;
488   using Base = ConvertOpToLLVMPattern<CallOpType>;
489 
490   LogicalResult
491   matchAndRewrite(CallOpType callOp, typename CallOpType::Adaptor adaptor,
492                   ConversionPatternRewriter &rewriter) const override {
493     // Pack the result types into a struct.
494     Type packedResult = nullptr;
495     unsigned numResults = callOp.getNumResults();
496     auto resultTypes = llvm::to_vector<4>(callOp.getResultTypes());
497 
498     if (numResults != 0) {
499       if (!(packedResult =
500                 this->getTypeConverter()->packFunctionResults(resultTypes)))
501         return failure();
502     }
503 
504     auto promoted = this->getTypeConverter()->promoteOperands(
505         callOp.getLoc(), /*opOperands=*/callOp->getOperands(),
506         adaptor.getOperands(), rewriter);
507     auto newOp = rewriter.create<LLVM::CallOp>(
508         callOp.getLoc(), packedResult ? TypeRange(packedResult) : TypeRange(),
509         promoted, callOp->getAttrs());
510 
511     SmallVector<Value, 4> results;
512     if (numResults < 2) {
513       // If < 2 results, packing did not do anything and we can just return.
514       results.append(newOp.result_begin(), newOp.result_end());
515     } else {
516       // Otherwise, it had been converted to an operation producing a structure.
517       // Extract individual results from the structure and return them as list.
518       results.reserve(numResults);
519       for (unsigned i = 0; i < numResults; ++i) {
520         auto type =
521             this->typeConverter->convertType(callOp.getResult(i).getType());
522         results.push_back(rewriter.create<LLVM::ExtractValueOp>(
523             callOp.getLoc(), type, newOp->getResult(0),
524             rewriter.getI64ArrayAttr(i)));
525       }
526     }
527 
528     if (this->getTypeConverter()->getOptions().useBarePtrCallConv) {
529       // For the bare-ptr calling convention, promote memref results to
530       // descriptors.
531       assert(results.size() == resultTypes.size() &&
532              "The number of arguments and types doesn't match");
533       this->getTypeConverter()->promoteBarePtrsToDescriptors(
534           rewriter, callOp.getLoc(), resultTypes, results);
535     } else if (failed(this->copyUnrankedDescriptors(rewriter, callOp.getLoc(),
536                                                     resultTypes, results,
537                                                     /*toDynamic=*/false))) {
538       return failure();
539     }
540 
541     rewriter.replaceOp(callOp, results);
542     return success();
543   }
544 };
545 
546 struct CallOpLowering : public CallOpInterfaceLowering<func::CallOp> {
547   using Super::Super;
548 };
549 
550 struct CallIndirectOpLowering
551     : public CallOpInterfaceLowering<func::CallIndirectOp> {
552   using Super::Super;
553 };
554 
555 struct UnrealizedConversionCastOpLowering
556     : public ConvertOpToLLVMPattern<UnrealizedConversionCastOp> {
557   using ConvertOpToLLVMPattern<
558       UnrealizedConversionCastOp>::ConvertOpToLLVMPattern;
559 
560   LogicalResult
561   matchAndRewrite(UnrealizedConversionCastOp op, OpAdaptor adaptor,
562                   ConversionPatternRewriter &rewriter) const override {
563     SmallVector<Type> convertedTypes;
564     if (succeeded(typeConverter->convertTypes(op.getOutputs().getTypes(),
565                                               convertedTypes)) &&
566         convertedTypes == adaptor.getInputs().getTypes()) {
567       rewriter.replaceOp(op, adaptor.getInputs());
568       return success();
569     }
570 
571     convertedTypes.clear();
572     if (succeeded(typeConverter->convertTypes(adaptor.getInputs().getTypes(),
573                                               convertedTypes)) &&
574         convertedTypes == op.getOutputs().getType()) {
575       rewriter.replaceOp(op, adaptor.getInputs());
576       return success();
577     }
578     return failure();
579   }
580 };
581 
582 // Special lowering pattern for `ReturnOps`.  Unlike all other operations,
583 // `ReturnOp` interacts with the function signature and must have as many
584 // operands as the function has return values.  Because in LLVM IR, functions
585 // can only return 0 or 1 value, we pack multiple values into a structure type.
586 // Emit `UndefOp` followed by `InsertValueOp`s to create such structure if
587 // necessary before returning it
588 struct ReturnOpLowering : public ConvertOpToLLVMPattern<func::ReturnOp> {
589   using ConvertOpToLLVMPattern<func::ReturnOp>::ConvertOpToLLVMPattern;
590 
591   LogicalResult
592   matchAndRewrite(func::ReturnOp op, OpAdaptor adaptor,
593                   ConversionPatternRewriter &rewriter) const override {
594     Location loc = op.getLoc();
595     unsigned numArguments = op.getNumOperands();
596     SmallVector<Value, 4> updatedOperands;
597 
598     if (getTypeConverter()->getOptions().useBarePtrCallConv) {
599       // For the bare-ptr calling convention, extract the aligned pointer to
600       // be returned from the memref descriptor.
601       for (auto it : llvm::zip(op->getOperands(), adaptor.getOperands())) {
602         Type oldTy = std::get<0>(it).getType();
603         Value newOperand = std::get<1>(it);
604         if (oldTy.isa<MemRefType>() && getTypeConverter()->canConvertToBarePtr(
605                                            oldTy.cast<BaseMemRefType>())) {
606           MemRefDescriptor memrefDesc(newOperand);
607           newOperand = memrefDesc.alignedPtr(rewriter, loc);
608         } else if (oldTy.isa<UnrankedMemRefType>()) {
609           // Unranked memref is not supported in the bare pointer calling
610           // convention.
611           return failure();
612         }
613         updatedOperands.push_back(newOperand);
614       }
615     } else {
616       updatedOperands = llvm::to_vector<4>(adaptor.getOperands());
617       (void)copyUnrankedDescriptors(rewriter, loc, op.getOperands().getTypes(),
618                                     updatedOperands,
619                                     /*toDynamic=*/true);
620     }
621 
622     // If ReturnOp has 0 or 1 operand, create it and return immediately.
623     if (numArguments == 0) {
624       rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, TypeRange(), ValueRange(),
625                                                   op->getAttrs());
626       return success();
627     }
628     if (numArguments == 1) {
629       rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(
630           op, TypeRange(), updatedOperands, op->getAttrs());
631       return success();
632     }
633 
634     // Otherwise, we need to pack the arguments into an LLVM struct type before
635     // returning.
636     auto packedType = getTypeConverter()->packFunctionResults(
637         llvm::to_vector<4>(op.getOperandTypes()));
638 
639     Value packed = rewriter.create<LLVM::UndefOp>(loc, packedType);
640     for (unsigned i = 0; i < numArguments; ++i) {
641       packed = rewriter.create<LLVM::InsertValueOp>(
642           loc, packedType, packed, updatedOperands[i],
643           rewriter.getI64ArrayAttr(i));
644     }
645     rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, TypeRange(), packed,
646                                                 op->getAttrs());
647     return success();
648   }
649 };
650 } // namespace
651 
652 void mlir::populateFuncToLLVMFuncOpConversionPattern(
653     LLVMTypeConverter &converter, RewritePatternSet &patterns) {
654   if (converter.getOptions().useBarePtrCallConv)
655     patterns.add<BarePtrFuncOpConversion>(converter);
656   else
657     patterns.add<FuncOpConversion>(converter);
658 }
659 
660 void mlir::populateFuncToLLVMConversionPatterns(LLVMTypeConverter &converter,
661                                                 RewritePatternSet &patterns) {
662   populateFuncToLLVMFuncOpConversionPattern(converter, patterns);
663   // clang-format off
664   patterns.add<
665       CallIndirectOpLowering,
666       CallOpLowering,
667       ConstantOpLowering,
668       ReturnOpLowering>(converter);
669   // clang-format on
670 }
671 
672 namespace {
673 /// A pass converting Func operations into the LLVM IR dialect.
674 struct ConvertFuncToLLVMPass
675     : public ConvertFuncToLLVMBase<ConvertFuncToLLVMPass> {
676   ConvertFuncToLLVMPass() = default;
677   ConvertFuncToLLVMPass(bool useBarePtrCallConv, bool emitCWrappers,
678                         unsigned indexBitwidth, bool useAlignedAlloc,
679                         const llvm::DataLayout &dataLayout) {
680     this->useBarePtrCallConv = useBarePtrCallConv;
681     this->emitCWrappers = emitCWrappers;
682     this->indexBitwidth = indexBitwidth;
683     this->dataLayout = dataLayout.getStringRepresentation();
684   }
685 
686   /// Run the dialect converter on the module.
687   void runOnOperation() override {
688     if (useBarePtrCallConv && emitCWrappers) {
689       getOperation().emitError()
690           << "incompatible conversion options: bare-pointer calling convention "
691              "and C wrapper emission";
692       signalPassFailure();
693       return;
694     }
695     if (failed(LLVM::LLVMDialect::verifyDataLayoutString(
696             this->dataLayout, [this](const Twine &message) {
697               getOperation().emitError() << message.str();
698             }))) {
699       signalPassFailure();
700       return;
701     }
702 
703     ModuleOp m = getOperation();
704     const auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>();
705 
706     LowerToLLVMOptions options(&getContext(),
707                                dataLayoutAnalysis.getAtOrAbove(m));
708     options.useBarePtrCallConv = useBarePtrCallConv;
709     options.emitCWrappers = emitCWrappers;
710     if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout)
711       options.overrideIndexBitwidth(indexBitwidth);
712     options.dataLayout = llvm::DataLayout(this->dataLayout);
713 
714     LLVMTypeConverter typeConverter(&getContext(), options,
715                                     &dataLayoutAnalysis);
716 
717     RewritePatternSet patterns(&getContext());
718     populateFuncToLLVMConversionPatterns(typeConverter, patterns);
719 
720     // TODO: Remove these in favor of their dedicated conversion passes.
721     arith::populateArithmeticToLLVMConversionPatterns(typeConverter, patterns);
722     cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns);
723 
724     LLVMConversionTarget target(getContext());
725     if (failed(applyPartialConversion(m, target, std::move(patterns))))
726       signalPassFailure();
727 
728     m->setAttr(LLVM::LLVMDialect::getDataLayoutAttrName(),
729                StringAttr::get(m.getContext(), this->dataLayout));
730   }
731 };
732 } // namespace
733 
734 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertFuncToLLVMPass() {
735   return std::make_unique<ConvertFuncToLLVMPass>();
736 }
737 
738 std::unique_ptr<OperationPass<ModuleOp>>
739 mlir::createConvertFuncToLLVMPass(const LowerToLLVMOptions &options) {
740   auto allocLowering = options.allocLowering;
741   // There is no way to provide additional patterns for pass, so
742   // AllocLowering::None will always fail.
743   assert(allocLowering != LowerToLLVMOptions::AllocLowering::None &&
744          "ConvertFuncToLLVMPass doesn't support AllocLowering::None");
745   bool useAlignedAlloc =
746       (allocLowering == LowerToLLVMOptions::AllocLowering::AlignedAlloc);
747   return std::make_unique<ConvertFuncToLLVMPass>(
748       options.useBarePtrCallConv, options.emitCWrappers,
749       options.getIndexBitwidth(), useAlignedAlloc, options.dataLayout);
750 }
751