22 ModuleOp module,
Type indexType) {
32 ModuleOp module,
Type indexType) {
47 Value bump = rewriter.
create<LLVM::SubOp>(loc, alignment, one);
48 Value bumped = rewriter.
create<LLVM::AddOp>(loc, input, bump);
50 return rewriter.
create<LLVM::SubOp>(loc, bumped,
mod);
58 sizeBytes = rewriter.
create<LLVM::AddOp>(loc, sizeBytes, alignment);
64 LLVM::LLVMFuncOp allocFuncOp = getNotalignedAllocFn(
66 auto results = rewriter.
create<LLVM::CallOp>(loc, allocFuncOp, sizeBytes);
67 Value allocatedPtr = rewriter.
create<LLVM::BitcastOp>(loc, elementPtrType,
70 Value alignedPtr = allocatedPtr;
77 rewriter.
create<LLVM::IntToPtrOp>(loc, elementPtrType, alignmentInt);
80 return std::make_tuple(allocatedPtr, alignedPtr);
83 unsigned AllocationOpLLVMLowering::getMemRefEltSizeInBytes(
89 layout = &analysis->getAbove(op);
91 Type elementType = memRefType.getElementType();
92 if (
auto memRefElementType = elementType.
dyn_cast<MemRefType>())
97 memRefElementType, *layout);
101 bool AllocationOpLLVMLowering::isMemRefSizeMultipleOf(
102 MemRefType type, uint64_t factor,
Operation *op,
104 uint64_t sizeDivisor = getMemRefEltSizeInBytes(type, op, defaultLayout);
105 for (
unsigned i = 0, e = type.getRank(); i < e; i++) {
106 if (ShapedType::isDynamic(type.getDimSize(i)))
108 sizeDivisor = sizeDivisor * type.getDimSize(i);
110 return sizeDivisor % factor == 0;
121 if (!isMemRefSizeMultipleOf(memRefType, alignment, op, defaultLayout))
122 sizeBytes =
createAligned(rewriter, loc, sizeBytes, allocAlignment);
125 LLVM::LLVMFuncOp allocFuncOp = getAlignedAllocFn(
127 auto results = rewriter.
create<LLVM::CallOp>(
128 loc, allocFuncOp,
ValueRange({allocAlignment, sizeBytes}));
129 Value allocatedPtr = rewriter.
create<LLVM::BitcastOp>(loc, elementPtrType,
130 results.getResult());
153 auto [allocatedPtr, alignedPtr] =
158 loc, memRefType, allocatedPtr, alignedPtr, sizes, strides, rewriter);
161 rewriter.
replaceOp(op, {memRefDescriptor});
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
PatternRewriter hook for replacing the results of an operation.
LogicalResult notifyMatchFailure(Location loc, function_ref< void(Diagnostic &)> reasonCallback) override
PatternRewriter hook for notifying match failure reasons.
MemRefDescriptor createMemRefDescriptor(Location loc, MemRefType memRefType, Value allocatedPtr, Value alignedPtr, ArrayRef< Value > sizes, ArrayRef< Value > strides, ConversionPatternRewriter &rewriter) const
Creates and populates a canonical memref descriptor struct.
LLVMTypeConverter * getTypeConverter() const
void getMemRefDescriptorSizes(Location loc, MemRefType memRefType, ValueRange dynamicSizes, ConversionPatternRewriter &rewriter, SmallVectorImpl< Value > &sizes, SmallVectorImpl< Value > &strides, Value &sizeBytes) const
Computes sizes, strides and buffer size in bytes of memRefType with identity layout.
Type getElementPtrType(MemRefType type) const
Returns the type of a pointer to an element of the memref.
static Value createIndexAttrConstant(OpBuilder &builder, Location loc, Type resultType, int64_t value)
Create a constant Op producing a value of resultType from an index-typed integer attribute.
bool isConvertibleAndHasIdentityMaps(MemRefType type) const
Returns if the given memref has identity maps and the element type is convertible to LLVM.
Stores data layout objects for each operation that specifies the data layout above and below the give...
The main mechanism for performing data layout queries.
unsigned getTypeSize(Type t) const
Returns the size of the given type in the current scope.
Conversion from types to the LLVM IR dialect.
const LowerToLLVMOptions & getOptions() const
unsigned getMemRefDescriptorSize(MemRefType type, const DataLayout &layout)
Returns the size of the memref descriptor object in bytes.
unsigned getUnrankedMemRefDescriptorSize(UnrankedMemRefType type, const DataLayout &layout)
Returns the size of the unranked memref descriptor object in bytes.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Operation is the basic unit of execution within MLIR.
Location getLoc()
The source location the operation was defined or derived from.
OpTy getParentOfType()
Return the closest surrounding parent operation that is of type 'OpTy'.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
This class provides an abstraction over the different types of ranges over Values.
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Type getType() const
Return the type of this value.
LLVM::LLVMFuncOp lookupOrCreateAlignedAllocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateGenericAlignedAllocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateMallocFn(ModuleOp moduleOp, Type indexType)
LLVM::LLVMFuncOp lookupOrCreateGenericAllocFn(ModuleOp moduleOp, Type indexType)
Include the generated interface declarations.
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
int64_t mod(int64_t lhs, int64_t rhs)
Returns MLIR's mod operation on constants.
virtual std::tuple< Value, Value > allocateBuffer(ConversionPatternRewriter &rewriter, Location loc, Value sizeBytes, Operation *op) const =0
Allocates the underlying buffer.
Type getIndexType() const
Gets the MLIR type wrapping the LLVM integer type whose bit width is defined by the used type convert...
static Value createAligned(ConversionPatternRewriter &rewriter, Location loc, Value input, Value alignment)
Computes the aligned value for 'input' as follows: bumped = input + alignement - 1 aligned = bumped -...
Value allocateBufferAutoAlign(ConversionPatternRewriter &rewriter, Location loc, Value sizeBytes, Operation *op, const DataLayout *defaultLayout, int64_t alignment) const
Allocates a memory buffer using an aligned allocation method.
static MemRefType getMemRefResultType(Operation *op)
std::tuple< Value, Value > allocateBufferManuallyAlign(ConversionPatternRewriter &rewriter, Location loc, Value sizeBytes, Operation *op, Value alignment) const
Allocates a memory buffer using an allocation method that doesn't guarantee alignment.
Value createIndexConstant(ConversionPatternRewriter &builder, Location loc, uint64_t value) const
Create an LLVM dialect operation defining the given index constant.
This class represents an efficient way to signal success or failure.