MLIR  16.0.0git
Namespaces | Macros | Functions
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/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/BlockAndValueMapping.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinAttributeInterfaces.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/PatternMatch.h"
#include "mlir/IR/TypeUtilities.h"
#include "mlir/Support/LogicalResult.h"
#include "mlir/Support/MathExtras.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/CommandLine.h"
#include "llvm/Support/FormatVariadic.h"
#include <algorithm>
#include <functional>
#include "mlir/Conversion/Passes.h.inc"
+ Include dependency graph for FuncToLLVM.cpp:

Go to the source code of this file.

Namespaces

 mlir
 Include the generated interface declarations.
 

Macros

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

Functions

static void filterFuncAttributes (ArrayRef< NamedAttribute > attrs, bool filterArgAndResAttrs, SmallVectorImpl< NamedAttribute > &result)
 Only retain those attributes that are not constructed by LLVMFuncOp::build. More...
 
static auto wrapAsStructAttrs (OpBuilder &b, ArrayAttr attrs)
 Helper function for wrapping all attributes into a single DictionaryAttr. More...
 
static void prependResAttrsToArgAttrs (OpBuilder &builder, SmallVectorImpl< NamedAttribute > &attributes, size_t numArguments)
 Combines all result attributes into a single DictionaryAttr and prepends to argument attrs. More...
 
static void wrapForExternalCallers (OpBuilder &rewriter, Location loc, LLVMTypeConverter &typeConverter, func::FuncOp 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, LLVMTypeConverter &typeConverter, func::FuncOp funcOp, LLVM::LLVMFuncOp newFuncOp)
 Creates an auxiliary function with pointer-to-memref-descriptor-struct arguments instead of unpacked arguments. More...
 

Macro Definition Documentation

◆ GEN_PASS_DEF_CONVERTFUNCTOLLVM

#define GEN_PASS_DEF_CONVERTFUNCTOLLVM

Definition at line 51 of file FuncToLLVM.cpp.

◆ PASS_NAME

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

Definition at line 57 of file FuncToLLVM.cpp.

Function Documentation

◆ filterFuncAttributes()

static void filterFuncAttributes ( ArrayRef< NamedAttribute attrs,
bool  filterArgAndResAttrs,
SmallVectorImpl< NamedAttribute > &  result 
)
static

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

If filterArgAttrs is set, also filter out argument attributes.

Definition at line 62 of file FuncToLLVM.cpp.

References mlir::function_interface_impl::getArgDictAttrName(), mlir::function_interface_impl::getResultDictAttrName(), mlir::SymbolTable::getSymbolAttrName(), and mlir::function_interface_impl::getTypeAttrName().

Referenced by wrapExternalFunction(), and wrapForExternalCallers().

◆ prependResAttrsToArgAttrs()

static void prependResAttrsToArgAttrs ( OpBuilder builder,
SmallVectorImpl< NamedAttribute > &  attributes,
size_t  numArguments 
)
static

Combines all result attributes into a single DictionaryAttr and prepends to argument attrs.

This is intended to be used to format the attributes for a C wrapper function when the result(s) is converted to the first function argument (in the multiple return case, all returns get wrapped into a single argument). The total number of argument attributes should be equal to (number of function arguments) + 1.

Definition at line 92 of file FuncToLLVM.cpp.

References mlir::Attribute::cast(), copy(), mlir::function_interface_impl::getArgDictAttrName(), mlir::Builder::getArrayAttr(), mlir::Builder::getContext(), mlir::Builder::getNamedAttr(), mlir::function_interface_impl::getResultDictAttrName(), mlir::NamedAttribute::getValue(), and wrapAsStructAttrs().

Referenced by wrapExternalFunction(), and wrapForExternalCallers().

◆ wrapAsStructAttrs()

static auto wrapAsStructAttrs ( OpBuilder b,
ArrayAttr  attrs 
)
static

Helper function for wrapping all attributes into a single DictionaryAttr.

Definition at line 78 of file FuncToLLVM.cpp.

References mlir::Builder::getContext(), and mlir::Builder::getNamedAttr().

Referenced by prependResAttrsToArgAttrs().

◆ wrapExternalFunction()

static void wrapExternalFunction ( OpBuilder builder,
Location  loc,
LLVMTypeConverter typeConverter,
func::FuncOp  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 193 of file FuncToLLVM.cpp.

References mlir::Type::cast(), mlir::LLVMTypeConverter::convertFunctionTypeCWrapper(), mlir::LLVMTypeConverter::convertType(), mlir::OpBuilder::create(), mlir::Value::dyn_cast(), mlir::detail::enumerate(), filterFuncAttributes(), mlir::Builder::getIndexType(), mlir::Builder::getIntegerAttr(), mlir::UnrankedMemRefDescriptor::getNumUnpackedValues(), mlir::MemRefDescriptor::getNumUnpackedValues(), mlir::Value::getType(), mlir::MemRefDescriptor::pack(), mlir::UnrankedMemRefDescriptor::pack(), prependResAttrsToArgAttrs(), and mlir::OpBuilder::setInsertionPointToStart().

◆ wrapForExternalCallers()

static void wrapForExternalCallers ( OpBuilder rewriter,
Location  loc,
LLVMTypeConverter typeConverter,
func::FuncOp  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 135 of file FuncToLLVM.cpp.

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