MLIR  20.0.0git
Namespaces | Macros | Functions | Variables
FuncToLLVM.cpp File Reference
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.h"
#include "mlir/Analysis/DataLayoutAnalysis.h"
#include "mlir/Conversion/ArithToLLVM/ArithToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/LLVMCommon/ConversionTarget.h"
#include "mlir/Conversion/LLVMCommon/Pattern.h"
#include "mlir/Conversion/LLVMCommon/VectorPattern.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h"
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
#include "mlir/Dialect/LLVMIR/LLVMTypes.h"
#include "mlir/Dialect/Utils/StaticValueUtils.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/IRMapping.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Transforms/DialectConversion.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Type.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include <algorithm>
#include <functional>
#include <optional>
#include "mlir/Conversion/Passes.h.inc"

Go to the source code of this file.

Namespaces

 mlir
 Include the generated interface declarations.
 

Macros

#define GEN_PASS_DEF_CONVERTFUNCTOLLVMPASS
 
#define GEN_PASS_DEF_SETLLVMMODULEDATALAYOUTPASS
 
#define PASS_NAME   "convert-func-to-llvm"
 

Functions

static bool shouldUseBarePtrCallConv (Operation *op, const LLVMTypeConverter *typeConverter)
 Return true if the op should use bare pointer calling convention. More...
 
static void filterFuncAttributes (FunctionOpInterface func, SmallVectorImpl< NamedAttribute > &result)
 Only retain those attributes that are not constructed by LLVMFuncOp::build. More...
 
static void propagateArgResAttrs (OpBuilder &builder, bool resultStructType, FunctionOpInterface funcOp, LLVM::LLVMFuncOp wrapperFuncOp)
 Propagate argument/results attributes. More...
 
static void wrapForExternalCallers (OpBuilder &rewriter, Location loc, const LLVMTypeConverter &typeConverter, FunctionOpInterface funcOp, LLVM::LLVMFuncOp newFuncOp)
 Creates an auxiliary function with pointer-to-memref-descriptor-struct arguments instead of unpacked arguments. More...
 
static void wrapExternalFunction (OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, FunctionOpInterface funcOp, LLVM::LLVMFuncOp newFuncOp)
 Creates an auxiliary function with pointer-to-memref-descriptor-struct arguments instead of unpacked arguments. More...
 
static void restoreByValRefArgumentType (ConversionPatternRewriter &rewriter, const LLVMTypeConverter &typeConverter, ArrayRef< std::optional< NamedAttribute >> byValRefNonPtrAttrs, ArrayRef< BlockArgument > oldBlockArgs, LLVM::LLVMFuncOp funcOp)
 Inserts llvm.load ops in the function body to restore the expected pointee value from llvm.byval/llvm.byref function arguments that were converted to LLVM pointer types. More...
 

Variables

static constexpr StringRef varargsAttrName = "func.varargs"
 
static constexpr StringRef linkageAttrName = "llvm.linkage"
 
static constexpr StringRef barePtrAttrName = "llvm.bareptr"
 

Macro Definition Documentation

◆ GEN_PASS_DEF_CONVERTFUNCTOLLVMPASS

#define GEN_PASS_DEF_CONVERTFUNCTOLLVMPASS

Definition at line 53 of file FuncToLLVM.cpp.

◆ GEN_PASS_DEF_SETLLVMMODULEDATALAYOUTPASS

#define GEN_PASS_DEF_SETLLVMMODULEDATALAYOUTPASS

Definition at line 54 of file FuncToLLVM.cpp.

◆ PASS_NAME

#define PASS_NAME   "convert-func-to-llvm"

Definition at line 60 of file FuncToLLVM.cpp.

Function Documentation

◆ filterFuncAttributes()

static void filterFuncAttributes ( FunctionOpInterface  func,
SmallVectorImpl< NamedAttribute > &  result 
)
static

Only retain those attributes that are not constructed by LLVMFuncOp::build.

Definition at line 75 of file FuncToLLVM.cpp.

References linkageAttrName, and varargsAttrName.

Referenced by mlir::convertFuncOpToLLVMFuncOp(), wrapExternalFunction(), and wrapForExternalCallers().

◆ propagateArgResAttrs()

static void propagateArgResAttrs ( OpBuilder builder,
bool  resultStructType,
FunctionOpInterface  funcOp,
LLVM::LLVMFuncOp  wrapperFuncOp 
)
static

Propagate argument/results attributes.

Definition at line 87 of file FuncToLLVM.cpp.

References mlir::Builder::getDictionaryAttr().

Referenced by wrapExternalFunction(), and wrapForExternalCallers().

◆ restoreByValRefArgumentType()

static void restoreByValRefArgumentType ( ConversionPatternRewriter rewriter,
const LLVMTypeConverter typeConverter,
ArrayRef< std::optional< NamedAttribute >>  byValRefNonPtrAttrs,
ArrayRef< BlockArgument oldBlockArgs,
LLVM::LLVMFuncOp  funcOp 
)
static

Inserts llvm.load ops in the function body to restore the expected pointee value from llvm.byval/llvm.byref function arguments that were converted to LLVM pointer types.

Definition at line 273 of file FuncToLLVM.cpp.

References mlir::LLVMTypeConverter::convertType(), mlir::OpBuilder::create(), mlir::ConversionPatternRewriter::replaceUsesOfBlockArgument(), and mlir::OpBuilder::setInsertionPointToStart().

Referenced by mlir::convertFuncOpToLLVMFuncOp().

◆ shouldUseBarePtrCallConv()

static bool shouldUseBarePtrCallConv ( Operation op,
const LLVMTypeConverter typeConverter 
)
static

Return true if the op should use bare pointer calling convention.

Definition at line 67 of file FuncToLLVM.cpp.

References barePtrAttrName, mlir::LLVMTypeConverter::getOptions(), mlir::Operation::hasAttr(), and mlir::LowerToLLVMOptions::useBarePtrCallConv.

Referenced by mlir::convertFuncOpToLLVMFuncOp().

◆ wrapExternalFunction()

static void wrapExternalFunction ( OpBuilder builder,
Location  loc,
const LLVMTypeConverter typeConverter,
FunctionOpInterface  funcOp,
LLVM::LLVMFuncOp  newFuncOp 
)
static

Creates an auxiliary function with pointer-to-memref-descriptor-struct arguments instead of unpacked arguments.

Creates a body for the (external) newFuncOp that allocates a memref descriptor on stack, packs the individual arguments into this descriptor and passes a pointer to it into the auxiliary function. If the result of the function cannot be directly returned, we write it to a special first argument that provides a pointer to a corresponding struct. This auxiliary external function is now compatible with functions defined in C using pointers to C structs corresponding to a memref descriptor.

Definition at line 178 of file FuncToLLVM.cpp.

References mlir::LLVMTypeConverter::convertFunctionTypeCWrapper(), mlir::LLVMTypeConverter::convertType(), mlir::OpBuilder::create(), filterFuncAttributes(), mlir::get(), mlir::Builder::getContext(), mlir::Builder::getIndexType(), mlir::Builder::getIntegerAttr(), mlir::UnrankedMemRefDescriptor::getNumUnpackedValues(), mlir::MemRefDescriptor::getNumUnpackedValues(), mlir::Value::getType(), mlir::MemRefDescriptor::pack(), mlir::UnrankedMemRefDescriptor::pack(), propagateArgResAttrs(), and mlir::OpBuilder::setInsertionPointToStart().

Referenced by mlir::convertFuncOpToLLVMFuncOp().

◆ wrapForExternalCallers()

static void wrapForExternalCallers ( OpBuilder rewriter,
Location  loc,
const LLVMTypeConverter typeConverter,
FunctionOpInterface  funcOp,
LLVM::LLVMFuncOp  newFuncOp 
)
static

Creates an auxiliary function with pointer-to-memref-descriptor-struct arguments instead of unpacked arguments.

This function can be called from C by passing a pointer to a C struct corresponding to a memref descriptor. Similarly, returned memrefs are passed via pointers to a C struct that is passed as additional argument. Internally, the auxiliary function unpacks the descriptor into individual components and forwards them to newFuncOp and forwards the results to the extra arguments.

Definition at line 118 of file FuncToLLVM.cpp.

References mlir::LLVMTypeConverter::convertFunctionTypeCWrapper(), mlir::LLVMTypeConverter::convertType(), mlir::OpBuilder::create(), mlir::detail::enumerate(), filterFuncAttributes(), propagateArgResAttrs(), mlir::OpBuilder::setInsertionPointToStart(), mlir::MemRefDescriptor::unpack(), and mlir::UnrankedMemRefDescriptor::unpack().

Referenced by mlir::convertFuncOpToLLVMFuncOp().

Variable Documentation

◆ barePtrAttrName

constexpr StringRef barePtrAttrName = "llvm.bareptr"
staticconstexpr

Definition at line 64 of file FuncToLLVM.cpp.

Referenced by shouldUseBarePtrCallConv().

◆ linkageAttrName

constexpr StringRef linkageAttrName = "llvm.linkage"
staticconstexpr

Definition at line 63 of file FuncToLLVM.cpp.

Referenced by mlir::convertFuncOpToLLVMFuncOp(), and filterFuncAttributes().

◆ varargsAttrName

constexpr StringRef varargsAttrName = "func.varargs"
staticconstexpr

Definition at line 62 of file FuncToLLVM.cpp.

Referenced by mlir::convertFuncOpToLLVMFuncOp(), and filterFuncAttributes().