1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
//===- MemRefMem2Reg.cpp - Mem2Reg Interfaces -------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements Mem2Reg-related interfaces for MemRef dialect
// operations.
//
//===----------------------------------------------------------------------===//
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/MemRef/IR/MemRef.h"
#include "llvm/ADT/TypeSwitch.h"
using namespace mlir;
//===----------------------------------------------------------------------===//
// AllocaOp interfaces
//===----------------------------------------------------------------------===//
static bool isSupportedElementType(Type type) {
return llvm::isa<MemRefType>(type) ||
OpBuilder(type.getContext()).getZeroAttr(type);
}
SmallVector<MemorySlot> memref::AllocaOp::getPromotableSlots() {
MemRefType type = getType();
if (!isSupportedElementType(type.getElementType()))
return {};
if (!type.hasStaticShape())
return {};
// Make sure the memref contains only a single element.
if (any_of(type.getShape(), [](uint64_t dim) { return dim != 1; }))
return {};
return {MemorySlot{getResult(), type.getElementType()}};
}
Value memref::AllocaOp::getDefaultValue(const MemorySlot &slot,
OpBuilder &builder) {
assert(isSupportedElementType(slot.elemType));
// TODO: support more types.
return TypeSwitch<Type, Value>(slot.elemType)
.Case([&](MemRefType t) {
return builder.create<memref::AllocaOp>(getLoc(), t);
})
.Default([&](Type t) {
return builder.create<arith::ConstantOp>(getLoc(), t,
builder.getZeroAttr(t));
});
}
void memref::AllocaOp::handlePromotionComplete(const MemorySlot &slot,
Value defaultValue) {
if (defaultValue.use_empty())
defaultValue.getDefiningOp()->erase();
erase();
}
void memref::AllocaOp::handleBlockArgument(const MemorySlot &slot,
BlockArgument argument,
OpBuilder &builder) {}
//===----------------------------------------------------------------------===//
// LoadOp/StoreOp interfaces
//===----------------------------------------------------------------------===//
bool memref::LoadOp::loadsFrom(const MemorySlot &slot) {
return getMemRef() == slot.ptr;
}
Value memref::LoadOp::getStored(const MemorySlot &slot) { return {}; }
bool memref::LoadOp::canUsesBeRemoved(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses) {
if (blockingUses.size() != 1)
return false;
Value blockingUse = (*blockingUses.begin())->get();
return blockingUse == slot.ptr && getMemRef() == slot.ptr &&
getResult().getType() == slot.elemType;
}
DeletionKind memref::LoadOp::removeBlockingUses(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
OpBuilder &builder, Value reachingDefinition) {
// `canUsesBeRemoved` checked this blocking use must be the loaded slot
// pointer.
getResult().replaceAllUsesWith(reachingDefinition);
return DeletionKind::Delete;
}
bool memref::StoreOp::loadsFrom(const MemorySlot &slot) { return false; }
Value memref::StoreOp::getStored(const MemorySlot &slot) {
if (getMemRef() != slot.ptr)
return {};
return getValue();
}
bool memref::StoreOp::canUsesBeRemoved(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
SmallVectorImpl<OpOperand *> &newBlockingUses) {
if (blockingUses.size() != 1)
return false;
Value blockingUse = (*blockingUses.begin())->get();
return blockingUse == slot.ptr && getMemRef() == slot.ptr &&
getValue() != slot.ptr && getValue().getType() == slot.elemType;
}
DeletionKind memref::StoreOp::removeBlockingUses(
const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
OpBuilder &builder, Value reachingDefinition) {
return DeletionKind::Delete;
}
|