MLIR  16.0.0git
Go to the documentation of this file.
1 //===- AllocLikeConversion.cpp - LLVM conversion for alloc operations -----===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
12 using namespace mlir;
15  ConversionPatternRewriter &rewriter, Location loc, Value input,
16  Value alignment) {
17  Value one = createIndexAttrConstant(rewriter, loc, alignment.getType(), 1);
18  Value bump = rewriter.create<LLVM::SubOp>(loc, alignment, one);
19  Value bumped = rewriter.create<LLVM::AddOp>(loc, input, bump);
20  Value mod = rewriter.create<LLVM::URemOp>(loc, bumped, alignment);
21  return rewriter.create<LLVM::SubOp>(loc, bumped, mod);
22 }
24 LogicalResult AllocLikeOpLLVMLowering::matchAndRewrite(
25  Operation *op, ArrayRef<Value> operands,
26  ConversionPatternRewriter &rewriter) const {
27  MemRefType memRefType = getMemRefResultType(op);
28  if (!isConvertibleAndHasIdentityMaps(memRefType))
29  return rewriter.notifyMatchFailure(op, "incompatible memref type");
30  auto loc = op->getLoc();
32  // Get actual sizes of the memref as values: static sizes are constant
33  // values and dynamic sizes are passed to 'alloc' as operands. In case of
34  // zero-dimensional memref, assume a scalar (size 1).
36  SmallVector<Value, 4> strides;
37  Value sizeBytes;
38  this->getMemRefDescriptorSizes(loc, memRefType, operands, rewriter, sizes,
39  strides, sizeBytes);
41  // Allocate the underlying buffer.
42  auto [allocatedPtr, alignedPtr] =
43  this->allocateBuffer(rewriter, loc, sizeBytes, op);
45  // Create the MemRef descriptor.
46  auto memRefDescriptor = this->createMemRefDescriptor(
47  loc, memRefType, allocatedPtr, alignedPtr, sizes, strides, rewriter);
49  // Return the final value of the descriptor.
50  rewriter.replaceOp(op, {memRefDescriptor});
51  return success();
52 }
Include the generated interface declarations.
virtual std::tuple< Value, Value > allocateBuffer(ConversionPatternRewriter &rewriter, Location loc, Value sizeBytes, Operation *op) const =0
Allocates the underlying buffer.
Operation is a basic unit of execution within MLIR.
Definition: Operation.h:28
void replaceOp(Operation *op, ValueRange newValues) override
PatternRewriter hook for replacing the results of an operation.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:404
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
int64_t mod(int64_t lhs, int64_t rhs)
Returns MLIR&#39;s mod operation on constants.
Definition: MathExtras.h:45
Location getLoc()
The source location the operation was defined or derived from.
Definition: Operation.h:154
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.
Definition: Pattern.cpp:198
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:85
bool isConvertibleAndHasIdentityMaps(MemRefType type) const
Returns if the given memref has identity maps and the element type is convertible to LLVM...
Definition: Pattern.cpp:106
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.
Definition: Pattern.cpp:55
Type getType() const
Return the type of this value.
Definition: Value.h:118
This class implements a pattern rewriter for use with ConversionPatterns.
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.
Definition: Pattern.cpp:120
static Value createAligned(ConversionPatternRewriter &rewriter, Location loc, Value input, Value alignment)