MLIR  15.0.0git
Macros | Functions
FuncToLLVM.cpp File Reference
#include "../PassDetail.h"
#include "mlir/Analysis/DataLayoutAnalysis.h"
#include "mlir/Conversion/ArithmeticToLLVM/ArithmeticToLLVM.h"
#include "mlir/Conversion/ControlFlowToLLVM/ControlFlowToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVM.h"
#include "mlir/Conversion/FuncToLLVM/ConvertFuncToLLVMPass.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/Utils/StaticValueUtils.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BlockAndValueMapping.h"
#include "mlir/IR/Builders.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/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 dependency graph for FuncToLLVM.cpp:

Go to the source code of this file.

Macros

#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

◆ PASS_NAME

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

Definition at line 48 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 53 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 83 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 69 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 186 of file FuncToLLVM.cpp.

References mlir::MemRefDescriptor::alignedPtr(), mlir::Type::cast(), mlir::LLVMTypeConverter::convertFunctionTypeCWrapper(), mlir::ConversionPatternRewriter::convertRegionTypes(), mlir::TypeConverter::convertType(), mlir::OpBuilder::create(), mlir::Value::dyn_cast(), mlir::detail::enumerate(), mlir::ConversionPatternRewriter::eraseOp(), mlir::failed(), mlir::failure(), filterFuncAttributes(), mlir::MemRefDescriptor::fromStaticShape(), mlir::Block::front(), mlir::LLVM::LLVMPointerType::get(), mlir::function_interface_impl::getArgDictAttrName(), mlir::Block::getArguments(), mlir::Builder::getArrayAttr(), mlir::Builder::getI64ArrayAttr(), mlir::Builder::getIndexType(), mlir::Builder::getIntegerAttr(), mlir::Builder::getNamedAttr(), mlir::LLVM::LLVMFunctionType::getNumParams(), mlir::MemRefDescriptor::getNumUnpackedValues(), mlir::UnrankedMemRefDescriptor::getNumUnpackedValues(), mlir::function_interface_impl::getResultDictAttrName(), mlir::Value::getType(), mlir::ConversionPatternRewriter::inlineRegionBefore(), mlir::Type::isa(), mlir::LLVM::isCompatibleType(), mlir::ConversionPatternRewriter::notifyMatchFailure(), mlir::MemRefDescriptor::pack(), mlir::UnrankedMemRefDescriptor::pack(), prependResAttrsToArgAttrs(), mlir::ConversionPatternRewriter::replaceOp(), mlir::RewriterBase::replaceOpWithNewOp(), mlir::ConversionPatternRewriter::replaceUsesOfBlockArgument(), mlir::Operation::setAttr(), mlir::OpBuilder::setInsertionPointToStart(), mlir::succeeded(), mlir::success(), and wrapForExternalCallers().

◆ 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 126 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().

Referenced by wrapExternalFunction().