17 static FailureOr<LLVM::LLVMFuncOp>
27 static FailureOr<LLVM::LLVMFuncOp>
42 Value bump = rewriter.
create<LLVM::SubOp>(loc, alignment, one);
43 Value bumped = rewriter.
create<LLVM::AddOp>(loc, input, bump);
44 Value mod = rewriter.
create<LLVM::URemOp>(loc, bumped, alignment);
45 return rewriter.
create<LLVM::SubOp>(loc, bumped, mod);
50 MemRefType memRefType,
Type elementPtrType,
52 auto allocatedPtrTy = cast<LLVM::LLVMPointerType>(allocatedPtr.
getType());
53 FailureOr<unsigned> maybeMemrefAddrSpace =
55 if (failed(maybeMemrefAddrSpace))
57 unsigned memrefAddrSpace = *maybeMemrefAddrSpace;
58 if (allocatedPtrTy.getAddressSpace() != memrefAddrSpace)
59 allocatedPtr = rewriter.
create<LLVM::AddrSpaceCastOp>(
70 sizeBytes = rewriter.
create<LLVM::AddOp>(loc, sizeBytes, alignment);
76 if (!elementPtrType) {
77 emitError(loc,
"conversion of memref memory space ")
78 << memRefType.getMemorySpace()
79 <<
" to integer address space "
80 "failed. Consider adding memory space conversions.";
85 if (failed(allocFuncOp))
88 rewriter.
create<LLVM::CallOp>(loc, allocFuncOp.value(), sizeBytes);
95 Value alignedPtr = allocatedPtr;
102 rewriter.
create<LLVM::IntToPtrOp>(loc, elementPtrType, alignmentInt);
105 return std::make_tuple(allocatedPtr, alignedPtr);
108 unsigned AllocationOpLLVMLowering::getMemRefEltSizeInBytes(
114 layout = &analysis->getAbove(op);
116 Type elementType = memRefType.getElementType();
117 if (
auto memRefElementType = dyn_cast<MemRefType>(elementType))
120 if (
auto memRefElementType = dyn_cast<UnrankedMemRefType>(elementType))
122 memRefElementType, *layout);
126 bool AllocationOpLLVMLowering::isMemRefSizeMultipleOf(
127 MemRefType type, uint64_t factor,
Operation *op,
129 uint64_t sizeDivisor = getMemRefEltSizeInBytes(type, op, defaultLayout);
130 for (
unsigned i = 0, e = type.getRank(); i < e; i++) {
131 if (type.isDynamicDim(i))
133 sizeDivisor = sizeDivisor * type.getDimSize(i);
135 return sizeDivisor % factor == 0;
141 Value allocAlignment =
147 if (!isMemRefSizeMultipleOf(memRefType, alignment, op, defaultLayout))
148 sizeBytes =
createAligned(rewriter, loc, sizeBytes, allocAlignment);
154 if (failed(allocFuncOp))
156 auto results = rewriter.
create<LLVM::CallOp>(
157 loc, allocFuncOp.value(),
ValueRange({allocAlignment, sizeBytes}));
164 requiresNumElements =
true;
167 LogicalResult AllocLikeOpLLVMLowering::matchAndRewrite(
183 strides, size, !requiresNumElements);
186 auto [allocatedPtr, alignedPtr] =
189 if (!allocatedPtr || !alignedPtr)
191 "underlying buffer allocation failed");
195 loc, memRefType, allocatedPtr, alignedPtr, sizes, strides, rewriter);
198 rewriter.
replaceOp(op, {memRefDescriptor});
static FailureOr< LLVM::LLVMFuncOp > getAlignedAllocFn(const LLVMTypeConverter *typeConverter, Operation *module, Type indexType)
static Value castAllocFuncResult(ConversionPatternRewriter &rewriter, Location loc, Value allocatedPtr, MemRefType memRefType, Type elementPtrType, const LLVMTypeConverter &typeConverter)
static FailureOr< LLVM::LLVMFuncOp > getNotalignedAllocFn(const LLVMTypeConverter *typeConverter, Operation *module, Type indexType)
MLIRContext * getContext() const
This class implements a pattern rewriter for use with ConversionPatterns.
void replaceOp(Operation *op, ValueRange newValues) override
Replace the given operation with the new values.
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.
void getMemRefDescriptorSizes(Location loc, MemRefType memRefType, ValueRange dynamicSizes, ConversionPatternRewriter &rewriter, SmallVectorImpl< Value > &sizes, SmallVectorImpl< Value > &strides, Value &size, bool sizeInBytes=true) const
Computes sizes, strides and buffer size of memRefType with identity layout.
const LLVMTypeConverter * getTypeConverter() const
Type getElementPtrType(MemRefType type) const
Returns the type of a pointer to an element of the memref.
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.
llvm::TypeSize 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 getUnrankedMemRefDescriptorSize(UnrankedMemRefType type, const DataLayout &layout) const
Returns the size of the unranked memref descriptor object in bytes.
FailureOr< unsigned > getMemRefAddressSpace(BaseMemRefType type) const
Return the LLVM address space corresponding to the memory space of the memref type type or failure if...
unsigned getMemRefDescriptorSize(MemRefType type, const DataLayout &layout) const
Returns the size of the 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.
A trait used to provide symbol table functionalities to a region operation.
Operation is the basic unit of execution within MLIR.
Location getLoc()
The source location the operation was defined or derived from.
Operation * getParentWithTrait()
Returns the closest surrounding parent operation with trait Trait.
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
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.
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateAlignedAllocFn(Operation *moduleOp, Type indexType)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateMallocFn(Operation *moduleOp, Type indexType)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateGenericAlignedAllocFn(Operation *moduleOp, Type indexType)
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateGenericAllocFn(Operation *moduleOp, Type indexType)
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
void setRequiresNumElements()
Sets the flag 'requiresNumElements', specifying the Op requires the number of elements instead of the...
virtual std::tuple< Value, Value > allocateBuffer(ConversionPatternRewriter &rewriter, Location loc, Value size, 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.
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.