MLIR  14.0.0git
AllocLikeConversion.cpp
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 https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 
12 using namespace mlir;
13 
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 }
23 
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();
31 
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);
40 
41  // Allocate the underlying buffer.
42  Value allocatedPtr;
43  Value alignedPtr;
44  std::tie(allocatedPtr, alignedPtr) =
45  this->allocateBuffer(rewriter, loc, sizeBytes, op);
46 
47  // Create the MemRef descriptor.
48  auto memRefDescriptor = this->createMemRefDescriptor(
49  loc, memRefType, allocatedPtr, alignedPtr, sizes, strides, rewriter);
50 
51  // Return the final value of the descriptor.
52  rewriter.replaceOp(op, {memRefDescriptor});
53  return success();
54 }
Include the generated interface declarations.
OpTy create(Location location, Args &&...args)
Create an operation of specific op type at the current insertion point.
Definition: Builders.h:430
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
LogicalResult notifyMatchFailure(Operation *op, function_ref< void(Diagnostic &)> reasonCallback) override
PatternRewriter hook for notifying match failure reasons.
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
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:106
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:84
bool isConvertibleAndHasIdentityMaps(MemRefType type) const
Returns if the given memref has identity maps and the element type is convertible to LLVM...
Definition: Pattern.cpp:105
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:54
Type getType() const
Return the type of this value.
Definition: Value.h:117
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:119
static Value createAligned(ConversionPatternRewriter &rewriter, Location loc, Value input, Value alignment)