MLIR  14.0.0git
Go to the documentation of this file.
1 //===- AllocLikeConversion.h - Convert allocation ops to LLVM ---*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
14 namespace mlir {
16 /// Lowering for AllocOp and AllocaOp.
22  explicit AllocLikeOpLLVMLowering(StringRef opName,
23  LLVMTypeConverter &converter)
24  : ConvertToLLVMPattern(opName, &converter.getContext(), converter) {}
26 protected:
27  // Returns 'input' aligned up to 'alignment'. Computes
28  // bumped = input + alignement - 1
29  // aligned = bumped - bumped % alignment
31  Value input, Value alignment);
33  /// Allocates the underlying buffer. Returns the allocated pointer and the
34  /// aligned pointer.
35  virtual std::tuple<Value, Value>
37  Value sizeBytes, Operation *op) const = 0;
39 private:
40  static MemRefType getMemRefResultType(Operation *op) {
41  return op->getResult(0).getType().cast<MemRefType>();
42  }
44  // An `alloc` is converted into a definition of a memref descriptor value and
45  // a call to `malloc` to allocate the underlying data buffer. The memref
46  // descriptor is of the LLVM structure type where:
47  // 1. the first element is a pointer to the allocated (typed) data buffer,
48  // 2. the second element is a pointer to the (typed) payload, aligned to the
49  // specified alignment,
50  // 3. the remaining elements serve to store all the sizes and strides of the
51  // memref using LLVM-converted `index` type.
52  //
53  // Alignment is performed by allocating `alignment` more bytes than
54  // requested and shifting the aligned pointer relative to the allocated
55  // memory. Note: `alignment - <minimum malloc alignment>` would actually be
56  // sufficient. If alignment is unspecified, the two pointers are equal.
58  // An `alloca` is converted into a definition of a memref descriptor value and
59  // an llvm.alloca to allocate the underlying data buffer.
61  matchAndRewrite(Operation *op, ArrayRef<Value> operands,
62  ConversionPatternRewriter &rewriter) const override;
63 };
65 } // namespace mlir
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
Type getIndexType() const
Gets the MLIR type wrapping the LLVM integer type whose bit width is defined by the used type convert...
Definition: Pattern.cpp:36
AllocLikeOpLLVMLowering(StringRef opName, LLVMTypeConverter &converter)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:48
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
Lowering for AllocOp and AllocaOp.
Value createIndexConstant(ConversionPatternRewriter &builder, Location loc, uint64_t value) const
Create an LLVM dialect operation defining the given index constant.
Definition: Pattern.cpp:62
OpResult getResult(unsigned idx)
Get the &#39;idx&#39;th result of this operation.
Definition: Operation.h:276
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition: Value.h:84
Type getType() const
Return the type of this value.
Definition: Value.h:117
Conversion from types in the Standard dialect to the LLVM IR dialect.
Definition: TypeConverter.h:30
This class implements a pattern rewriter for use with ConversionPatterns.
Base class for operation conversions targeting the LLVM IR dialect.
Definition: Pattern.h:34
static Value createAligned(ConversionPatternRewriter &rewriter, Location loc, Value input, Value alignment)
MLIRContext * getContext() const
Return the MLIRContext used to create this pattern.
Definition: PatternMatch.h:132
Type getVoidPtrType() const
Get the MLIR type wrapping the LLVM i8* type.
Definition: Pattern.cpp:49
U cast() const
Definition: Types.h:250