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