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 struct FuncOpConversion : public FuncOpConversionBase { 365 FuncOpConversion(LLVMTypeConverter &converter) 366 : FuncOpConversionBase(converter) {} 367 368 LogicalResult 369 matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor, 370 ConversionPatternRewriter &rewriter) const override { 371 auto newFuncOp = convertFuncOpToLLVMFuncOp(funcOp, rewriter); 372 if (!newFuncOp) 373 return failure(); 374 375 if (funcOp->getAttrOfType<UnitAttr>( 376 LLVM::LLVMDialect::getEmitCWrapperAttrName())) { 377 if (newFuncOp.isExternal()) 378 wrapExternalFunction(rewriter, funcOp.getLoc(), *getTypeConverter(), 379 funcOp, newFuncOp); 380 else 381 wrapForExternalCallers(rewriter, funcOp.getLoc(), *getTypeConverter(), 382 funcOp, newFuncOp); 383 } 384 385 rewriter.eraseOp(funcOp); 386 return success(); 387 } 388 }; 389 390 /// FuncOp legalization pattern that converts MemRef arguments to bare pointers 391 /// to the MemRef element type. This will impact the calling convention and ABI. 392 struct BarePtrFuncOpConversion : public FuncOpConversionBase { 393 using FuncOpConversionBase::FuncOpConversionBase; 394 395 LogicalResult 396 matchAndRewrite(func::FuncOp funcOp, OpAdaptor adaptor, 397 ConversionPatternRewriter &rewriter) const override { 398 399 // TODO: bare ptr conversion could be handled by argument materialization 400 // and most of the code below would go away. But to do this, we would need a 401 // way to distinguish between FuncOp and other regions in the 402 // addArgumentMaterialization hook. 403 404 // Store the type of memref-typed arguments before the conversion so that we 405 // can promote them to MemRef descriptor at the beginning of the function. 406 SmallVector<Type, 8> oldArgTypes = 407 llvm::to_vector<8>(funcOp.getFunctionType().getInputs()); 408 409 auto newFuncOp = convertFuncOpToLLVMFuncOp(funcOp, rewriter); 410 if (!newFuncOp) 411 return failure(); 412 if (newFuncOp.getBody().empty()) { 413 rewriter.eraseOp(funcOp); 414 return success(); 415 } 416 417 // Promote bare pointers from memref arguments to memref descriptors at the 418 // beginning of the function so that all the memrefs in the function have a 419 // uniform representation. 420 Block *entryBlock = &newFuncOp.getBody().front(); 421 auto blockArgs = entryBlock->getArguments(); 422 assert(blockArgs.size() == oldArgTypes.size() && 423 "The number of arguments and types doesn't match"); 424 425 OpBuilder::InsertionGuard guard(rewriter); 426 rewriter.setInsertionPointToStart(entryBlock); 427 for (auto it : llvm::zip(blockArgs, oldArgTypes)) { 428 BlockArgument arg = std::get<0>(it); 429 Type argTy = std::get<1>(it); 430 431 // Unranked memrefs are not supported in the bare pointer calling 432 // convention. We should have bailed out before in the presence of 433 // unranked memrefs. 434 assert(!argTy.isa<UnrankedMemRefType>() && 435 "Unranked memref is not supported"); 436 auto memrefTy = argTy.dyn_cast<MemRefType>(); 437 if (!memrefTy) 438 continue; 439 440 // Replace barePtr with a placeholder (undef), promote barePtr to a ranked 441 // or unranked memref descriptor and replace placeholder with the last 442 // instruction of the memref descriptor. 443 // TODO: The placeholder is needed to avoid replacing barePtr uses in the 444 // MemRef descriptor instructions. We may want to have a utility in the 445 // rewriter to properly handle this use case. 446 Location loc = funcOp.getLoc(); 447 auto placeholder = rewriter.create<LLVM::UndefOp>( 448 loc, getTypeConverter()->convertType(memrefTy)); 449 rewriter.replaceUsesOfBlockArgument(arg, placeholder); 450 451 Value desc = MemRefDescriptor::fromStaticShape( 452 rewriter, loc, *getTypeConverter(), memrefTy, arg); 453 rewriter.replaceOp(placeholder, {desc}); 454 } 455 456 rewriter.eraseOp(funcOp); 457 return success(); 458 } 459 }; 460 461 struct ConstantOpLowering : public ConvertOpToLLVMPattern<func::ConstantOp> { 462 using ConvertOpToLLVMPattern<func::ConstantOp>::ConvertOpToLLVMPattern; 463 464 LogicalResult 465 matchAndRewrite(func::ConstantOp op, OpAdaptor adaptor, 466 ConversionPatternRewriter &rewriter) const override { 467 auto type = typeConverter->convertType(op.getResult().getType()); 468 if (!type || !LLVM::isCompatibleType(type)) 469 return rewriter.notifyMatchFailure(op, "failed to convert result type"); 470 471 auto newOp = 472 rewriter.create<LLVM::AddressOfOp>(op.getLoc(), type, op.getValue()); 473 for (const NamedAttribute &attr : op->getAttrs()) { 474 if (attr.getName().strref() == "value") 475 continue; 476 newOp->setAttr(attr.getName(), attr.getValue()); 477 } 478 rewriter.replaceOp(op, newOp->getResults()); 479 return success(); 480 } 481 }; 482 483 // A CallOp automatically promotes MemRefType to a sequence of alloca/store and 484 // passes the pointer to the MemRef across function boundaries. 485 template <typename CallOpType> 486 struct CallOpInterfaceLowering : public ConvertOpToLLVMPattern<CallOpType> { 487 using ConvertOpToLLVMPattern<CallOpType>::ConvertOpToLLVMPattern; 488 using Super = CallOpInterfaceLowering<CallOpType>; 489 using Base = ConvertOpToLLVMPattern<CallOpType>; 490 491 LogicalResult 492 matchAndRewrite(CallOpType callOp, typename CallOpType::Adaptor adaptor, 493 ConversionPatternRewriter &rewriter) const override { 494 // Pack the result types into a struct. 495 Type packedResult = nullptr; 496 unsigned numResults = callOp.getNumResults(); 497 auto resultTypes = llvm::to_vector<4>(callOp.getResultTypes()); 498 499 if (numResults != 0) { 500 if (!(packedResult = 501 this->getTypeConverter()->packFunctionResults(resultTypes))) 502 return failure(); 503 } 504 505 auto promoted = this->getTypeConverter()->promoteOperands( 506 callOp.getLoc(), /*opOperands=*/callOp->getOperands(), 507 adaptor.getOperands(), rewriter); 508 auto newOp = rewriter.create<LLVM::CallOp>( 509 callOp.getLoc(), packedResult ? TypeRange(packedResult) : TypeRange(), 510 promoted, callOp->getAttrs()); 511 512 SmallVector<Value, 4> results; 513 if (numResults < 2) { 514 // If < 2 results, packing did not do anything and we can just return. 515 results.append(newOp.result_begin(), newOp.result_end()); 516 } else { 517 // Otherwise, it had been converted to an operation producing a structure. 518 // Extract individual results from the structure and return them as list. 519 results.reserve(numResults); 520 for (unsigned i = 0; i < numResults; ++i) { 521 auto type = 522 this->typeConverter->convertType(callOp.getResult(i).getType()); 523 results.push_back(rewriter.create<LLVM::ExtractValueOp>( 524 callOp.getLoc(), type, newOp->getResult(0), 525 rewriter.getI64ArrayAttr(i))); 526 } 527 } 528 529 if (this->getTypeConverter()->getOptions().useBarePtrCallConv) { 530 // For the bare-ptr calling convention, promote memref results to 531 // descriptors. 532 assert(results.size() == resultTypes.size() && 533 "The number of arguments and types doesn't match"); 534 this->getTypeConverter()->promoteBarePtrsToDescriptors( 535 rewriter, callOp.getLoc(), resultTypes, results); 536 } else if (failed(this->copyUnrankedDescriptors(rewriter, callOp.getLoc(), 537 resultTypes, results, 538 /*toDynamic=*/false))) { 539 return failure(); 540 } 541 542 rewriter.replaceOp(callOp, results); 543 return success(); 544 } 545 }; 546 547 struct CallOpLowering : public CallOpInterfaceLowering<func::CallOp> { 548 using Super::Super; 549 }; 550 551 struct CallIndirectOpLowering 552 : public CallOpInterfaceLowering<func::CallIndirectOp> { 553 using Super::Super; 554 }; 555 556 struct UnrealizedConversionCastOpLowering 557 : public ConvertOpToLLVMPattern<UnrealizedConversionCastOp> { 558 using ConvertOpToLLVMPattern< 559 UnrealizedConversionCastOp>::ConvertOpToLLVMPattern; 560 561 LogicalResult 562 matchAndRewrite(UnrealizedConversionCastOp op, OpAdaptor adaptor, 563 ConversionPatternRewriter &rewriter) const override { 564 SmallVector<Type> convertedTypes; 565 if (succeeded(typeConverter->convertTypes(op.getOutputs().getTypes(), 566 convertedTypes)) && 567 convertedTypes == adaptor.getInputs().getTypes()) { 568 rewriter.replaceOp(op, adaptor.getInputs()); 569 return success(); 570 } 571 572 convertedTypes.clear(); 573 if (succeeded(typeConverter->convertTypes(adaptor.getInputs().getTypes(), 574 convertedTypes)) && 575 convertedTypes == op.getOutputs().getType()) { 576 rewriter.replaceOp(op, adaptor.getInputs()); 577 return success(); 578 } 579 return failure(); 580 } 581 }; 582 583 // Special lowering pattern for `ReturnOps`. Unlike all other operations, 584 // `ReturnOp` interacts with the function signature and must have as many 585 // operands as the function has return values. Because in LLVM IR, functions 586 // can only return 0 or 1 value, we pack multiple values into a structure type. 587 // Emit `UndefOp` followed by `InsertValueOp`s to create such structure if 588 // necessary before returning it 589 struct ReturnOpLowering : public ConvertOpToLLVMPattern<func::ReturnOp> { 590 using ConvertOpToLLVMPattern<func::ReturnOp>::ConvertOpToLLVMPattern; 591 592 LogicalResult 593 matchAndRewrite(func::ReturnOp op, OpAdaptor adaptor, 594 ConversionPatternRewriter &rewriter) const override { 595 Location loc = op.getLoc(); 596 unsigned numArguments = op.getNumOperands(); 597 SmallVector<Value, 4> updatedOperands; 598 599 if (getTypeConverter()->getOptions().useBarePtrCallConv) { 600 // For the bare-ptr calling convention, extract the aligned pointer to 601 // be returned from the memref descriptor. 602 for (auto it : llvm::zip(op->getOperands(), adaptor.getOperands())) { 603 Type oldTy = std::get<0>(it).getType(); 604 Value newOperand = std::get<1>(it); 605 if (oldTy.isa<MemRefType>() && getTypeConverter()->canConvertToBarePtr( 606 oldTy.cast<BaseMemRefType>())) { 607 MemRefDescriptor memrefDesc(newOperand); 608 newOperand = memrefDesc.alignedPtr(rewriter, loc); 609 } else if (oldTy.isa<UnrankedMemRefType>()) { 610 // Unranked memref is not supported in the bare pointer calling 611 // convention. 612 return failure(); 613 } 614 updatedOperands.push_back(newOperand); 615 } 616 } else { 617 updatedOperands = llvm::to_vector<4>(adaptor.getOperands()); 618 (void)copyUnrankedDescriptors(rewriter, loc, op.getOperands().getTypes(), 619 updatedOperands, 620 /*toDynamic=*/true); 621 } 622 623 // If ReturnOp has 0 or 1 operand, create it and return immediately. 624 if (numArguments == 0) { 625 rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, TypeRange(), ValueRange(), 626 op->getAttrs()); 627 return success(); 628 } 629 if (numArguments == 1) { 630 rewriter.replaceOpWithNewOp<LLVM::ReturnOp>( 631 op, TypeRange(), updatedOperands, op->getAttrs()); 632 return success(); 633 } 634 635 // Otherwise, we need to pack the arguments into an LLVM struct type before 636 // returning. 637 auto packedType = getTypeConverter()->packFunctionResults( 638 llvm::to_vector<4>(op.getOperandTypes())); 639 640 Value packed = rewriter.create<LLVM::UndefOp>(loc, packedType); 641 for (unsigned i = 0; i < numArguments; ++i) { 642 packed = rewriter.create<LLVM::InsertValueOp>( 643 loc, packedType, packed, updatedOperands[i], 644 rewriter.getI64ArrayAttr(i)); 645 } 646 rewriter.replaceOpWithNewOp<LLVM::ReturnOp>(op, TypeRange(), packed, 647 op->getAttrs()); 648 return success(); 649 } 650 }; 651 } // namespace 652 653 void mlir::populateFuncToLLVMFuncOpConversionPattern( 654 LLVMTypeConverter &converter, RewritePatternSet &patterns) { 655 if (converter.getOptions().useBarePtrCallConv) 656 patterns.add<BarePtrFuncOpConversion>(converter); 657 else 658 patterns.add<FuncOpConversion>(converter); 659 } 660 661 void mlir::populateFuncToLLVMConversionPatterns(LLVMTypeConverter &converter, 662 RewritePatternSet &patterns) { 663 populateFuncToLLVMFuncOpConversionPattern(converter, patterns); 664 // clang-format off 665 patterns.add< 666 CallIndirectOpLowering, 667 CallOpLowering, 668 ConstantOpLowering, 669 ReturnOpLowering>(converter); 670 // clang-format on 671 } 672 673 namespace { 674 /// A pass converting Func operations into the LLVM IR dialect. 675 struct ConvertFuncToLLVMPass 676 : public ConvertFuncToLLVMBase<ConvertFuncToLLVMPass> { 677 ConvertFuncToLLVMPass() = default; 678 ConvertFuncToLLVMPass(bool useBarePtrCallConv, unsigned indexBitwidth, 679 bool useAlignedAlloc, 680 const llvm::DataLayout &dataLayout) { 681 this->useBarePtrCallConv = useBarePtrCallConv; 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 (failed(LLVM::LLVMDialect::verifyDataLayoutString( 689 this->dataLayout, [this](const Twine &message) { 690 getOperation().emitError() << message.str(); 691 }))) { 692 signalPassFailure(); 693 return; 694 } 695 696 ModuleOp m = getOperation(); 697 const auto &dataLayoutAnalysis = getAnalysis<DataLayoutAnalysis>(); 698 699 LowerToLLVMOptions options(&getContext(), 700 dataLayoutAnalysis.getAtOrAbove(m)); 701 options.useBarePtrCallConv = useBarePtrCallConv; 702 if (indexBitwidth != kDeriveIndexBitwidthFromDataLayout) 703 options.overrideIndexBitwidth(indexBitwidth); 704 options.dataLayout = llvm::DataLayout(this->dataLayout); 705 706 LLVMTypeConverter typeConverter(&getContext(), options, 707 &dataLayoutAnalysis); 708 709 RewritePatternSet patterns(&getContext()); 710 populateFuncToLLVMConversionPatterns(typeConverter, patterns); 711 712 // TODO: Remove these in favor of their dedicated conversion passes. 713 arith::populateArithmeticToLLVMConversionPatterns(typeConverter, patterns); 714 cf::populateControlFlowToLLVMConversionPatterns(typeConverter, patterns); 715 716 LLVMConversionTarget target(getContext()); 717 if (failed(applyPartialConversion(m, target, std::move(patterns)))) 718 signalPassFailure(); 719 720 m->setAttr(LLVM::LLVMDialect::getDataLayoutAttrName(), 721 StringAttr::get(m.getContext(), this->dataLayout)); 722 } 723 }; 724 } // namespace 725 726 std::unique_ptr<OperationPass<ModuleOp>> mlir::createConvertFuncToLLVMPass() { 727 return std::make_unique<ConvertFuncToLLVMPass>(); 728 } 729 730 std::unique_ptr<OperationPass<ModuleOp>> 731 mlir::createConvertFuncToLLVMPass(const LowerToLLVMOptions &options) { 732 auto allocLowering = options.allocLowering; 733 // There is no way to provide additional patterns for pass, so 734 // AllocLowering::None will always fail. 735 assert(allocLowering != LowerToLLVMOptions::AllocLowering::None && 736 "ConvertFuncToLLVMPass doesn't support AllocLowering::None"); 737 bool useAlignedAlloc = 738 (allocLowering == LowerToLLVMOptions::AllocLowering::AlignedAlloc); 739 return std::make_unique<ConvertFuncToLLVMPass>( 740 options.useBarePtrCallConv, options.getIndexBitwidth(), useAlignedAlloc, 741 options.dataLayout); 742 } 743