MLIR 22.0.0git
Pattern.cpp
Go to the documentation of this file.
1//===- Pattern.cpp - Conversion pattern to the LLVM dialect ---------------===//
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
13#include "mlir/IR/AffineMap.h"
15
16using namespace mlir;
17
18//===----------------------------------------------------------------------===//
19// ConvertToLLVMPattern
20//===----------------------------------------------------------------------===//
21
23 StringRef rootOpName, MLIRContext *context,
24 const LLVMTypeConverter &typeConverter, PatternBenefit benefit)
25 : ConversionPattern(typeConverter, rootOpName, benefit, context) {}
26
28 return static_cast<const LLVMTypeConverter *>(
29 ConversionPattern::getTypeConverter());
30}
31
32LLVM::LLVMDialect &ConvertToLLVMPattern::getDialect() const {
33 return *getTypeConverter()->getDialect();
34}
35
39
40Type ConvertToLLVMPattern::getIntPtrType(unsigned addressSpace) const {
41 return IntegerType::get(&getTypeConverter()->getContext(),
42 getTypeConverter()->getPointerBitwidth(addressSpace));
43}
44
46 return LLVM::LLVMVoidType::get(&getTypeConverter()->getContext());
47}
48
49Type ConvertToLLVMPattern::getPtrType(unsigned addressSpace) const {
50 return LLVM::LLVMPointerType::get(&getTypeConverter()->getContext(),
51 addressSpace);
52}
53
55
57 Location loc,
58 Type resultType,
59 int64_t value) {
60 return LLVM::ConstantOp::create(builder, loc, resultType,
61 builder.getIndexAttr(value));
62}
63
65 ConversionPatternRewriter &rewriter, Location loc, MemRefType type,
66 Value memRefDesc, ValueRange indices,
67 LLVM::GEPNoWrapFlags noWrapFlags) const {
68 return LLVM::getStridedElementPtr(rewriter, loc, *getTypeConverter(), type,
69 memRefDesc, indices, noWrapFlags);
70}
71
72// Check if the MemRefType `type` is supported by the lowering. We currently
73// only support memrefs with identity maps.
75 MemRefType type) const {
76 if (!type.getLayout().isIdentity())
77 return false;
78 return static_cast<bool>(typeConverter->convertType(type));
79}
80
82 auto addressSpace = getTypeConverter()->getMemRefAddressSpace(type);
83 if (failed(addressSpace))
84 return {};
85 return LLVM::LLVMPointerType::get(type.getContext(), *addressSpace);
86}
87
89 Location loc, MemRefType memRefType, ValueRange dynamicSizes,
90 ConversionPatternRewriter &rewriter, SmallVectorImpl<Value> &sizes,
91 SmallVectorImpl<Value> &strides, Value &size, bool sizeInBytes) const {
92 assert(isConvertibleAndHasIdentityMaps(memRefType) &&
93 "layout maps must have been normalized away");
94 assert(count(memRefType.getShape(), ShapedType::kDynamic) ==
95 static_cast<ssize_t>(dynamicSizes.size()) &&
96 "dynamicSizes size doesn't match dynamic sizes count in memref shape");
97
98 sizes.reserve(memRefType.getRank());
99 unsigned dynamicIndex = 0;
100 Type indexType = getIndexType();
101 for (int64_t size : memRefType.getShape()) {
102 sizes.push_back(
103 size == ShapedType::kDynamic
104 ? dynamicSizes[dynamicIndex++]
105 : createIndexAttrConstant(rewriter, loc, indexType, size));
106 }
107
108 // Strides: iterate sizes in reverse order and multiply.
109 int64_t stride = 1;
110 Value runningStride = createIndexAttrConstant(rewriter, loc, indexType, 1);
111 strides.resize(memRefType.getRank());
112 for (auto i = memRefType.getRank(); i-- > 0;) {
113 strides[i] = runningStride;
114
115 int64_t staticSize = memRefType.getShape()[i];
116 bool useSizeAsStride = stride == 1;
117 if (staticSize == ShapedType::kDynamic)
118 stride = ShapedType::kDynamic;
119 if (stride != ShapedType::kDynamic)
120 stride *= staticSize;
121
122 if (useSizeAsStride)
123 runningStride = sizes[i];
124 else if (stride == ShapedType::kDynamic)
125 runningStride =
126 LLVM::MulOp::create(rewriter, loc, runningStride, sizes[i]);
127 else
128 runningStride = createIndexAttrConstant(rewriter, loc, indexType, stride);
129 }
130 if (sizeInBytes) {
131 // Buffer size in bytes.
132 Type elementType = typeConverter->convertType(memRefType.getElementType());
133 auto elementPtrType = LLVM::LLVMPointerType::get(rewriter.getContext());
134 Value nullPtr = LLVM::ZeroOp::create(rewriter, loc, elementPtrType);
135 Value gepPtr = LLVM::GEPOp::create(rewriter, loc, elementPtrType,
136 elementType, nullPtr, runningStride);
137 size = LLVM::PtrToIntOp::create(rewriter, loc, getIndexType(), gepPtr);
138 } else {
139 size = runningStride;
140 }
141}
142
144 Location loc, Type type, ConversionPatternRewriter &rewriter) const {
145 // Compute the size of an individual element. This emits the MLIR equivalent
146 // of the following sizeof(...) implementation in LLVM IR:
147 // %0 = getelementptr %elementType* null, %indexType 1
148 // %1 = ptrtoint %elementType* %0 to %indexType
149 // which is a common pattern of getting the size of a type in bytes.
150 Type llvmType = typeConverter->convertType(type);
151 auto convertedPtrType = LLVM::LLVMPointerType::get(rewriter.getContext());
152 auto nullPtr = LLVM::ZeroOp::create(rewriter, loc, convertedPtrType);
153 auto gep = LLVM::GEPOp::create(rewriter, loc, convertedPtrType, llvmType,
154 nullPtr, ArrayRef<LLVM::GEPArg>{1});
155 return LLVM::PtrToIntOp::create(rewriter, loc, getIndexType(), gep);
156}
157
159 Location loc, MemRefType memRefType, ValueRange dynamicSizes,
160 ConversionPatternRewriter &rewriter) const {
161 assert(count(memRefType.getShape(), ShapedType::kDynamic) ==
162 static_cast<ssize_t>(dynamicSizes.size()) &&
163 "dynamicSizes size doesn't match dynamic sizes count in memref shape");
164
165 Type indexType = getIndexType();
166 Value numElements = memRefType.getRank() == 0
167 ? createIndexAttrConstant(rewriter, loc, indexType, 1)
168 : nullptr;
169 unsigned dynamicIndex = 0;
170
171 // Compute the total number of memref elements.
172 for (int64_t staticSize : memRefType.getShape()) {
173 if (numElements) {
174 Value size =
175 staticSize == ShapedType::kDynamic
176 ? dynamicSizes[dynamicIndex++]
177 : createIndexAttrConstant(rewriter, loc, indexType, staticSize);
178 numElements = LLVM::MulOp::create(rewriter, loc, numElements, size);
179 } else {
180 numElements =
181 staticSize == ShapedType::kDynamic
182 ? dynamicSizes[dynamicIndex++]
183 : createIndexAttrConstant(rewriter, loc, indexType, staticSize);
184 }
185 }
186 return numElements;
187}
188
189/// Creates and populates the memref descriptor struct given all its fields.
191 Location loc, MemRefType memRefType, Value allocatedPtr, Value alignedPtr,
192 ArrayRef<Value> sizes, ArrayRef<Value> strides,
193 ConversionPatternRewriter &rewriter) const {
194 auto structType = typeConverter->convertType(memRefType);
195 auto memRefDescriptor = MemRefDescriptor::poison(rewriter, loc, structType);
196
197 // Field 1: Allocated pointer, used for malloc/free.
198 memRefDescriptor.setAllocatedPtr(rewriter, loc, allocatedPtr);
199
200 // Field 2: Actual aligned pointer to payload.
201 memRefDescriptor.setAlignedPtr(rewriter, loc, alignedPtr);
202
203 // Field 3: Offset in aligned pointer.
204 Type indexType = getIndexType();
205 memRefDescriptor.setOffset(
206 rewriter, loc, createIndexAttrConstant(rewriter, loc, indexType, 0));
207
208 // Fields 4: Sizes.
209 for (const auto &en : llvm::enumerate(sizes))
210 memRefDescriptor.setSize(rewriter, loc, en.index(), en.value());
211
212 // Field 5: Strides.
213 for (const auto &en : llvm::enumerate(strides))
214 memRefDescriptor.setStride(rewriter, loc, en.index(), en.value());
215
216 return memRefDescriptor;
217}
218
220 OpBuilder &builder, Location loc, UnrankedMemRefType memRefType,
221 Value operand, bool toDynamic) const {
222 // Convert memory space.
223 FailureOr<unsigned> addressSpace =
225 if (failed(addressSpace))
226 return {};
227
228 // Get frequently used types.
229 Type indexType = getTypeConverter()->getIndexType();
230
231 // Find the malloc and free, or declare them if necessary.
232 auto module = builder.getInsertionPoint()->getParentOfType<ModuleOp>();
233 FailureOr<LLVM::LLVMFuncOp> freeFunc, mallocFunc;
234 if (toDynamic) {
235 mallocFunc = LLVM::lookupOrCreateMallocFn(builder, module, indexType);
236 if (failed(mallocFunc))
237 return {};
238 }
239 if (!toDynamic) {
240 freeFunc = LLVM::lookupOrCreateFreeFn(builder, module);
241 if (failed(freeFunc))
242 return {};
243 }
244
245 UnrankedMemRefDescriptor desc(operand);
247 builder, loc, *getTypeConverter(), desc, *addressSpace);
248
249 // Allocate memory, copy, and free the source if necessary.
250 Value memory = toDynamic
251 ? LLVM::CallOp::create(builder, loc, mallocFunc.value(),
252 allocationSize)
253 .getResult()
254 : LLVM::AllocaOp::create(builder, loc, getPtrType(),
255 IntegerType::get(getContext(), 8),
256 allocationSize,
257 /*alignment=*/0);
258 Value source = desc.memRefDescPtr(builder, loc);
259 LLVM::MemcpyOp::create(builder, loc, memory, source, allocationSize, false);
260 if (!toDynamic)
261 LLVM::CallOp::create(builder, loc, freeFunc.value(), source);
262
263 // Create a new descriptor. The same descriptor can be returned multiple
264 // times, attempting to modify its pointer can lead to memory leaks
265 // (allocated twice and overwritten) or double frees (the caller does not
266 // know if the descriptor points to the same memory).
267 Type descriptorType = getTypeConverter()->convertType(memRefType);
268 if (!descriptorType)
269 return {};
270 auto updatedDesc =
271 UnrankedMemRefDescriptor::poison(builder, loc, descriptorType);
272 Value rank = desc.rank(builder, loc);
273 updatedDesc.setRank(builder, loc, rank);
274 updatedDesc.setMemRefDescPtr(builder, loc, memory);
275 return updatedDesc;
276}
277
279 OpBuilder &builder, Location loc, TypeRange origTypes,
280 SmallVectorImpl<Value> &operands, bool toDynamic) const {
281 assert(origTypes.size() == operands.size() &&
282 "expected as may original types as operands");
283 for (unsigned i = 0, e = operands.size(); i < e; ++i) {
284 if (auto memRefType = dyn_cast<UnrankedMemRefType>(origTypes[i])) {
285 Value updatedDesc = copyUnrankedDescriptor(builder, loc, memRefType,
286 operands[i], toDynamic);
287 if (!updatedDesc)
288 return failure();
289 operands[i] = updatedDesc;
290 }
291 }
292 return success();
293}
294
295//===----------------------------------------------------------------------===//
296// Detail methods
297//===----------------------------------------------------------------------===//
298
299/// Replaces the given operation "op" with a new operation of type "targetOp"
300/// and given operands.
302 Operation *op, StringRef targetOp, ValueRange operands,
303 ArrayRef<NamedAttribute> targetAttrs, Attribute propertiesAttr,
304 const LLVMTypeConverter &typeConverter,
305 ConversionPatternRewriter &rewriter) {
306 unsigned numResults = op->getNumResults();
307
308 SmallVector<Type> resultTypes;
309 if (numResults != 0) {
310 resultTypes.push_back(
311 typeConverter.packOperationResults(op->getResultTypes()));
312 if (!resultTypes.back())
313 return failure();
314 }
315
316 // Create the operation through state since we don't know its C++ type.
317 OperationState state(op->getLoc(), rewriter.getStringAttr(targetOp), operands,
318 resultTypes, targetAttrs);
319 state.propertiesAttr = propertiesAttr;
320 Operation *newOp = rewriter.create(state);
321
322 // If the operation produced 0 or 1 result, return them immediately.
323 if (numResults == 0)
324 return rewriter.eraseOp(op), success();
325 if (numResults == 1)
326 return rewriter.replaceOp(op, newOp->getResult(0)), success();
327
328 // Otherwise, it had been converted to an operation producing a structure.
329 // Extract individual results from the structure and return them as list.
330 SmallVector<Value, 4> results;
331 results.reserve(numResults);
332 for (unsigned i = 0; i < numResults; ++i) {
333 results.push_back(LLVM::ExtractValueOp::create(rewriter, op->getLoc(),
334 newOp->getResult(0), i));
335 }
336 rewriter.replaceOp(op, results);
337 return success();
338}
339
341 Operation *op, StringRef intrinsic, ValueRange operands,
342 const LLVMTypeConverter &typeConverter, RewriterBase &rewriter) {
343 auto loc = op->getLoc();
344
345 if (!llvm::all_of(operands, [](Value value) {
346 return LLVM::isCompatibleType(value.getType());
347 }))
348 return failure();
349
350 unsigned numResults = op->getNumResults();
351 Type resType;
352 if (numResults != 0)
353 resType = typeConverter.packOperationResults(op->getResultTypes());
354
355 auto callIntrOp = LLVM::CallIntrinsicOp::create(
356 rewriter, loc, resType, rewriter.getStringAttr(intrinsic), operands);
357 // Propagate attributes.
358 callIntrOp->setAttrs(op->getAttrDictionary());
359
360 if (numResults <= 1) {
361 // Directly replace the original op.
362 rewriter.replaceOp(op, callIntrOp);
363 return success();
364 }
365
366 // Extract individual results from packed structure and use them as
367 // replacements.
368 SmallVector<Value, 4> results;
369 results.reserve(numResults);
370 Value intrRes = callIntrOp.getResults();
371 for (unsigned i = 0; i < numResults; ++i)
372 results.push_back(LLVM::ExtractValueOp::create(rewriter, loc, intrRes, i));
373 rewriter.replaceOp(op, results);
374
375 return success();
376}
377
378static unsigned getBitWidth(Type type) {
379 if (type.isIntOrFloat())
380 return type.getIntOrFloatBitWidth();
381
382 auto vec = cast<VectorType>(type);
383 assert(!vec.isScalable() && "scalable vectors are not supported");
384 return vec.getNumElements() * getBitWidth(vec.getElementType());
385}
386
388 int32_t value) {
389 Type i32 = builder.getI32Type();
390 return LLVM::ConstantOp::create(builder, loc, i32, value);
391}
392
394 Value src, Type dstType) {
395 Type srcType = src.getType();
396 if (srcType == dstType)
397 return {src};
398
399 unsigned srcBitWidth = getBitWidth(srcType);
400 unsigned dstBitWidth = getBitWidth(dstType);
401 if (srcBitWidth == dstBitWidth) {
402 Value cast = LLVM::BitcastOp::create(builder, loc, dstType, src);
403 return {cast};
404 }
405
406 if (dstBitWidth > srcBitWidth) {
407 auto smallerInt = builder.getIntegerType(srcBitWidth);
408 if (srcType != smallerInt)
409 src = LLVM::BitcastOp::create(builder, loc, smallerInt, src);
410
411 auto largerInt = builder.getIntegerType(dstBitWidth);
412 Value res = LLVM::ZExtOp::create(builder, loc, largerInt, src);
413 return {res};
414 }
415 assert(srcBitWidth % dstBitWidth == 0 &&
416 "src bit width must be a multiple of dst bit width");
417 int64_t numElements = srcBitWidth / dstBitWidth;
418 auto vecType = VectorType::get(numElements, dstType);
419
420 src = LLVM::BitcastOp::create(builder, loc, vecType, src);
421
423 for (auto i : llvm::seq(numElements)) {
424 Value idx = createI32Constant(builder, loc, i);
425 Value elem = LLVM::ExtractElementOp::create(builder, loc, src, idx);
426 res.emplace_back(elem);
427 }
428
429 return res;
430}
431
433 Type dstType) {
434 assert(!src.empty() && "src range must not be empty");
435 if (src.size() == 1) {
436 Value res = src.front();
437 if (res.getType() == dstType)
438 return res;
439
440 unsigned srcBitWidth = getBitWidth(res.getType());
441 unsigned dstBitWidth = getBitWidth(dstType);
442 if (dstBitWidth < srcBitWidth) {
443 auto largerInt = builder.getIntegerType(srcBitWidth);
444 if (res.getType() != largerInt)
445 res = LLVM::BitcastOp::create(builder, loc, largerInt, res);
446
447 auto smallerInt = builder.getIntegerType(dstBitWidth);
448 res = LLVM::TruncOp::create(builder, loc, smallerInt, res);
449 }
450
451 if (res.getType() != dstType)
452 res = LLVM::BitcastOp::create(builder, loc, dstType, res);
453
454 return res;
455 }
456
457 int64_t numElements = src.size();
458 auto srcType = VectorType::get(numElements, src.front().getType());
459 Value res = LLVM::PoisonOp::create(builder, loc, srcType);
460 for (auto &&[i, elem] : llvm::enumerate(src)) {
461 Value idx = createI32Constant(builder, loc, i);
462 res = LLVM::InsertElementOp::create(builder, loc, srcType, res, elem, idx);
463 }
464
465 if (res.getType() != dstType)
466 res = LLVM::BitcastOp::create(builder, loc, dstType, res);
467
468 return res;
469}
470
472 const LLVMTypeConverter &converter,
473 MemRefType type, Value memRefDesc,
475 LLVM::GEPNoWrapFlags noWrapFlags) {
476 auto [strides, offset] = type.getStridesAndOffset();
477
478 MemRefDescriptor memRefDescriptor(memRefDesc);
479 // Use a canonical representation of the start address so that later
480 // optimizations have a longer sequence of instructions to CSE.
481 // If we don't do that we would sprinkle the memref.offset in various
482 // position of the different address computations.
483 Value base = memRefDescriptor.bufferPtr(builder, loc, converter, type);
484
485 LLVM::IntegerOverflowFlags intOverflowFlags =
486 LLVM::IntegerOverflowFlags::none;
487 if (LLVM::bitEnumContainsAny(noWrapFlags, LLVM::GEPNoWrapFlags::nusw)) {
488 intOverflowFlags = intOverflowFlags | LLVM::IntegerOverflowFlags::nsw;
489 }
490 if (LLVM::bitEnumContainsAny(noWrapFlags, LLVM::GEPNoWrapFlags::nuw)) {
491 intOverflowFlags = intOverflowFlags | LLVM::IntegerOverflowFlags::nuw;
492 }
493
494 Type indexType = converter.getIndexType();
495 Value index;
496 for (int i = 0, e = indices.size(); i < e; ++i) {
497 Value increment = indices[i];
498 if (strides[i] != 1) { // Skip if stride is 1.
499 Value stride =
500 ShapedType::isDynamic(strides[i])
501 ? memRefDescriptor.stride(builder, loc, i)
502 : LLVM::ConstantOp::create(builder, loc, indexType,
503 builder.getIndexAttr(strides[i]));
504 increment = LLVM::MulOp::create(builder, loc, increment, stride,
505 intOverflowFlags);
506 }
507 index = index ? LLVM::AddOp::create(builder, loc, index, increment,
508 intOverflowFlags)
509 : increment;
510 }
511
512 Type elementPtrType = memRefDescriptor.getElementPtrType();
513 return index
514 ? LLVM::GEPOp::create(builder, loc, elementPtrType,
515 converter.convertType(type.getElementType()),
516 base, index, noWrapFlags)
517 : base;
518}
static Value createI32Constant(ConversionPatternRewriter &rewriter, Location loc, int32_t value)
return success()
static unsigned getBitWidth(Type type)
Definition Pattern.cpp:378
b getContext())
Attributes are known-constant values of operations.
Definition Attributes.h:25
IntegerAttr getIndexAttr(int64_t value)
Definition Builders.cpp:108
IntegerType getI32Type()
Definition Builders.cpp:63
IntegerType getIntegerType(unsigned width)
Definition Builders.cpp:67
StringAttr getStringAttr(const Twine &bytes)
Definition Builders.cpp:262
Type getVoidType() const
Gets the MLIR type wrapping the LLVM void type.
Definition Pattern.cpp:45
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:190
ConvertToLLVMPattern(StringRef rootOpName, MLIRContext *context, const LLVMTypeConverter &typeConverter, PatternBenefit benefit=1)
Definition Pattern.cpp:22
Value getStridedElementPtr(ConversionPatternRewriter &rewriter, Location loc, MemRefType type, Value memRefDesc, ValueRange indices, LLVM::GEPNoWrapFlags noWrapFlags=LLVM::GEPNoWrapFlags::none) const
Convenience wrapper for the corresponding helper utility.
Definition Pattern.cpp:64
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.
Definition Pattern.cpp:88
Type getPtrType(unsigned addressSpace=0) const
Get the MLIR type wrapping the LLVM ptr type.
Definition Pattern.cpp:49
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
const LLVMTypeConverter * getTypeConverter() const
Definition Pattern.cpp:27
Value getNumElements(Location loc, MemRefType memRefType, ValueRange dynamicSizes, ConversionPatternRewriter &rewriter) const
Computes total number of elements for the given MemRef and dynamicSizes.
Definition Pattern.cpp:158
LLVM::LLVMDialect & getDialect() const
Returns the LLVM dialect.
Definition Pattern.cpp:32
Value getSizeInBytes(Location loc, Type type, ConversionPatternRewriter &rewriter) const
Computes the size of type in bytes.
Definition Pattern.cpp:143
Type getIntPtrType(unsigned addressSpace=0) const
Gets the MLIR type wrapping the LLVM integer type whose bit width corresponds to that of a LLVM point...
Definition Pattern.cpp:40
Value copyUnrankedDescriptor(OpBuilder &builder, Location loc, UnrankedMemRefType memRefType, Value operand, bool toDynamic) const
Copies the given unranked memory descriptor to heap-allocated memory (if toDynamic is true) or to sta...
Definition Pattern.cpp:219
LogicalResult copyUnrankedDescriptors(OpBuilder &builder, Location loc, TypeRange origTypes, SmallVectorImpl< Value > &operands, bool toDynamic) const
Copies the memory descriptor for any operands that were unranked descriptors originally to heap-alloc...
Definition Pattern.cpp:278
Type getElementPtrType(MemRefType type) const
Returns the type of a pointer to an element of the memref.
Definition Pattern.cpp:81
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:56
bool isConvertibleAndHasIdentityMaps(MemRefType type) const
Returns if the given memref type is convertible to LLVM and has an identity layout map.
Definition Pattern.cpp:74
Type getVoidPtrType() const
Get the MLIR type wrapping the LLVM i8* type.
Definition Pattern.cpp:54
Conversion from types to the LLVM IR dialect.
Type packOperationResults(TypeRange types) const
Convert a non-empty list of types of values produced by an operation into an LLVM-compatible type.
FailureOr< unsigned > getMemRefAddressSpace(BaseMemRefType type) const
Return the LLVM address space corresponding to the memory space of the memref type type or failure if...
LLVM::LLVMDialect * getDialect() const
Returns the LLVM dialect.
Type getIndexType() const
Gets the LLVM representation of the index type.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
Helper class to produce LLVM dialect operations extracting or inserting elements of a MemRef descript...
Value bufferPtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &converter, MemRefType type)
Builds IR for getting the start address of the buffer represented by this memref: memref....
LLVM::LLVMPointerType getElementPtrType()
Returns the (LLVM) pointer type this descriptor contains.
Value stride(OpBuilder &builder, Location loc, unsigned pos)
Builds IR extracting the pos-th size from the descriptor.
static MemRefDescriptor poison(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating a poison value of the descriptor type.
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
DictionaryAttr getAttrDictionary()
Return all of the attributes on this operation as a DictionaryAttr.
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:407
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
result_type_range getResultTypes()
Definition Operation.h:428
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:404
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void replaceOp(Operation *op, ValueRange newValues)
Replace the results of the given (original) operation with the specified list of values (replacements...
This class provides an abstraction over the various different ranges of value types.
Definition TypeRange.h:37
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
bool isIntOrFloat() const
Return true if this is an integer (of any signedness) or a float type.
Definition Types.cpp:116
unsigned getIntOrFloatBitWidth() const
Return the bit width of an integer or a float type, assert failure on other types.
Definition Types.cpp:122
static Value computeSize(OpBuilder &builder, Location loc, const LLVMTypeConverter &typeConverter, UnrankedMemRefDescriptor desc, unsigned addressSpace)
Builds and returns IR computing the size in bytes (suitable for opaque allocation).
Value memRefDescPtr(OpBuilder &builder, Location loc) const
Builds IR extracting ranked memref descriptor ptr.
static UnrankedMemRefDescriptor poison(OpBuilder &builder, Location loc, Type descriptorType)
Builds IR creating an undef value of the descriptor type.
Value rank(OpBuilder &builder, Location loc) const
Builds IR extracting the rank from the descriptor.
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
type_range getType() const
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
Type getType() const
Return the type of this value.
Definition Value.h:105
LogicalResult oneToOneRewrite(Operation *op, StringRef targetOp, ValueRange operands, ArrayRef< NamedAttribute > targetAttrs, Attribute propertiesAttr, const LLVMTypeConverter &typeConverter, ConversionPatternRewriter &rewriter)
Replaces the given operation "op" with a new operation of type "targetOp" and given operands.
Definition Pattern.cpp:301
LogicalResult intrinsicRewrite(Operation *op, StringRef intrinsic, ValueRange operands, const LLVMTypeConverter &typeConverter, RewriterBase &rewriter)
Replaces the given operation "op" with a call to an LLVM intrinsic with the specified name "intrinsic...
Definition Pattern.cpp:340
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateFreeFn(OpBuilder &b, Operation *moduleOp, SymbolTableCollection *symbolTables=nullptr)
Value getStridedElementPtr(OpBuilder &builder, Location loc, const LLVMTypeConverter &converter, MemRefType type, Value memRefDesc, ValueRange indices, LLVM::GEPNoWrapFlags noWrapFlags=LLVM::GEPNoWrapFlags::none)
Performs the index computation to get to the element at indices of the memory pointed to by memRefDes...
Definition Pattern.cpp:471
Value composeValue(OpBuilder &builder, Location loc, ValueRange src, Type dstType)
Composes a set of src values into a single value of type dstType through series of bitcasts and vecto...
Definition Pattern.cpp:432
SmallVector< Value > decomposeValue(OpBuilder &builder, Location loc, Value src, Type dstType)
Decomposes a src value into a set of values of type dstType through series of bitcasts and vector ops...
Definition Pattern.cpp:393
FailureOr< LLVM::LLVMFuncOp > lookupOrCreateMallocFn(OpBuilder &b, Operation *moduleOp, Type indexType, SymbolTableCollection *symbolTables=nullptr)
bool isCompatibleType(Type type)
Returns true if the given type is compatible with the LLVM dialect.
Include the generated interface declarations.
This represents an operation in an abstracted form, suitable for use with the builder APIs.
Attribute propertiesAttr
This Attribute is used to opaquely construct the properties of the operation.