clang 24.0.0git
CIRMemorySlot.cpp
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
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//
9// This file implements MemorySlot-related interfaces for CIR dialect
10// operations.
11//
12//===----------------------------------------------------------------------===//
13
15
16using namespace mlir;
17
18/// Conditions the deletion of the operation to the removal of all its uses.
19static bool forwardToUsers(Operation *op,
20 SmallVectorImpl<OpOperand *> &newBlockingUses) {
21 for (Value result : op->getResults())
22 for (OpOperand &use : result.getUses())
23 newBlockingUses.push_back(&use);
24 return true;
25}
26
27//===----------------------------------------------------------------------===//
28// Interfaces for AllocaOp
29//===----------------------------------------------------------------------===//
30
31llvm::SmallVector<MemorySlot> cir::AllocaOp::getPromotableSlots() {
32 return {MemorySlot{getResult(), getAllocaType()}};
33}
34
35Value cir::AllocaOp::getDefaultValue(const MemorySlot &slot,
36 OpBuilder &builder) {
37 return cir::ConstantOp::create(builder, getLoc(),
38 cir::UndefAttr::get(slot.valueType));
39}
40
41void cir::AllocaOp::handleBlockArgument(const MemorySlot &slot,
42 BlockArgument argument,
43 OpBuilder &builder) {}
44
45std::optional<PromotableAllocationOpInterface>
46cir::AllocaOp::handlePromotionComplete(const MemorySlot &slot,
47 Value defaultValue, OpBuilder &builder) {
48 if (defaultValue && defaultValue.use_empty())
49 defaultValue.getDefiningOp()->erase();
50 this->erase();
51 return std::nullopt;
52}
53
54//===----------------------------------------------------------------------===//
55// Interfaces for LoadOp
56//===----------------------------------------------------------------------===//
57
58bool cir::LoadOp::loadsFrom(const MemorySlot &slot) {
59 return getAddr() == slot.ptr;
60}
61
62bool cir::LoadOp::storesTo(const MemorySlot &slot) { return false; }
63
64Value cir::LoadOp::getStored(const MemorySlot &slot, OpBuilder &builder,
65 Value reachingDef, const DataLayout &dataLayout) {
66 llvm_unreachable("getStored should not be called on LoadOp");
67}
68
69bool cir::LoadOp::canUsesBeRemoved(
70 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
71 SmallVectorImpl<OpOperand *> &newBlockingUses,
72 const DataLayout &dataLayout) {
73 if (blockingUses.size() != 1)
74 return false;
75
76 if (isVolatileOrAtomic())
77 return false;
78
79 Value blockingUse = (*blockingUses.begin())->get();
80 return blockingUse == slot.ptr && getAddr() == slot.ptr &&
81 getType() == slot.valueType;
82}
83
84DeletionKind cir::LoadOp::removeBlockingUses(
85 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
86 OpBuilder &builder, Value reachingDefinition,
87 const DataLayout &dataLayout) {
88 getResult().replaceAllUsesWith(reachingDefinition);
89 return DeletionKind::Delete;
90}
91
92//===----------------------------------------------------------------------===//
93// Interfaces for StoreOp
94//===----------------------------------------------------------------------===//
95
96bool cir::StoreOp::loadsFrom(const MemorySlot &slot) { return false; }
97
98bool cir::StoreOp::storesTo(const MemorySlot &slot) {
99 return getAddr() == slot.ptr;
100}
101
102Value cir::StoreOp::getStored(const MemorySlot &slot, OpBuilder &builder,
103 Value reachingDef, const DataLayout &dataLayout) {
104 return getValue();
105}
106
107bool cir::StoreOp::canUsesBeRemoved(
108 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
109 SmallVectorImpl<OpOperand *> &newBlockingUses,
110 const DataLayout &dataLayout) {
111 if (blockingUses.size() != 1)
112 return false;
113
114 if (isVolatileOrAtomic())
115 return false;
116
117 Value blockingUse = (*blockingUses.begin())->get();
118 return blockingUse == slot.ptr && getAddr() == slot.ptr &&
119 getValue() != slot.ptr && slot.valueType == getValue().getType();
120}
121
122DeletionKind cir::StoreOp::removeBlockingUses(
123 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
124 OpBuilder &builder, Value reachingDefinition,
125 const DataLayout &dataLayout) {
126 return DeletionKind::Delete;
127}
128
129//===----------------------------------------------------------------------===//
130// Interfaces for CopyOp
131//===----------------------------------------------------------------------===//
132
133bool cir::CopyOp::loadsFrom(const MemorySlot &slot) {
134 return getSrc() == slot.ptr;
135}
136
137bool cir::CopyOp::storesTo(const MemorySlot &slot) {
138 return getDst() == slot.ptr;
139}
140
141Value cir::CopyOp::getStored(const MemorySlot &slot, OpBuilder &builder,
142 Value reachingDef, const DataLayout &dataLayout) {
143 return cir::LoadOp::create(builder, getLoc(), slot.valueType, getSrc());
144}
145
146DeletionKind cir::CopyOp::removeBlockingUses(
147 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
148 OpBuilder &builder, mlir::Value reachingDefinition,
149 const DataLayout &dataLayout) {
150 if (loadsFrom(slot))
151 cir::StoreOp::create(builder, getLoc(), reachingDefinition, getDst(),
152 /*is_volatile=*/false,
153 /*is_nontemporal=*/false,
154 /*alignment=*/mlir::IntegerAttr{},
155 /*sync_scope=*/cir::SyncScopeKindAttr(),
156 /*mem-order=*/cir::MemOrderAttr());
157 return DeletionKind::Delete;
158}
159
160bool cir::CopyOp::canUsesBeRemoved(
161 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
162 SmallVectorImpl<OpOperand *> &newBlockingUses,
163 const DataLayout &dataLayout) {
164 if (getDst() == getSrc())
165 return false;
166
167 return getCopySizeInBytes(dataLayout) ==
168 dataLayout.getTypeSize(slot.valueType);
169}
170
171//===----------------------------------------------------------------------===//
172// Interfaces for MatrixColumnMajorLoadOp
173//===----------------------------------------------------------------------===//
174
175bool cir::MatrixColumnMajorLoadOp::loadsFrom(const MemorySlot &slot) {
176 return getValue() == slot.ptr;
177}
178
179bool cir::MatrixColumnMajorLoadOp::storesTo(const MemorySlot &slot) {
180 return false;
181}
182
183Value cir::MatrixColumnMajorLoadOp::getStored(const MemorySlot &slot,
184 OpBuilder &builder,
185 Value reachingDef,
186 const DataLayout &dataLayout) {
187 llvm_unreachable("getStored should not be called on MatrixColumnMajorLoadOp");
188}
189
190bool cir::MatrixColumnMajorLoadOp::canUsesBeRemoved(
191 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
192 SmallVectorImpl<OpOperand *> &newBlockingUses,
193 const DataLayout &dataLayout) {
194 if (blockingUses.size() != 1)
195 return false;
196
197 // Volatile load should not be removed.
198 if (getIsVolatile())
199 return false;
200
201 Value blockingUse = (*blockingUses.begin())->get();
202 return blockingUse == slot.ptr && getValue() == slot.ptr &&
203 getType() == slot.valueType;
204}
205
206DeletionKind cir::MatrixColumnMajorLoadOp::removeBlockingUses(
207 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
208 OpBuilder &builder, Value reachingDefinition,
209 const DataLayout &dataLayout) {
210 getResult().replaceAllUsesWith(reachingDefinition);
211 return DeletionKind::Delete;
212}
213
214//===----------------------------------------------------------------------===//
215// Interfaces for MatrixColumnMajorStoreOp
216//===----------------------------------------------------------------------===//
217
218bool cir::MatrixColumnMajorStoreOp::loadsFrom(const MemorySlot &slot) {
219 return false;
220}
221
222bool cir::MatrixColumnMajorStoreOp::storesTo(const MemorySlot &slot) {
223 return getValue() == slot.ptr;
224}
225
226Value cir::MatrixColumnMajorStoreOp::getStored(const MemorySlot &slot,
227 OpBuilder &builder,
228 Value reachingDef,
229 const DataLayout &dataLayout) {
230 return getMatrix();
231}
232
233bool cir::MatrixColumnMajorStoreOp::canUsesBeRemoved(
234 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
235 SmallVectorImpl<OpOperand *> &newBlockingUses,
236 const DataLayout &dataLayout) {
237 if (blockingUses.size() != 1)
238 return false;
239
240 // Volatile store should not be removed.
241 if (getIsVolatile())
242 return false;
243
244 Value blockingUse = (*blockingUses.begin())->get();
245 return blockingUse == slot.ptr && getValue() == slot.ptr &&
246 getValue() != slot.ptr && slot.valueType == getValue().getType();
247}
248
249DeletionKind cir::MatrixColumnMajorStoreOp::removeBlockingUses(
250 const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses,
251 OpBuilder &builder, Value reachingDefinition,
252 const DataLayout &dataLayout) {
253 return DeletionKind::Delete;
254}
255
256//===----------------------------------------------------------------------===//
257// Interfaces for CastOp
258//===----------------------------------------------------------------------===//
259
260bool cir::CastOp::canUsesBeRemoved(
261 const SmallPtrSetImpl<OpOperand *> &blockingUses,
262 SmallVectorImpl<OpOperand *> &newBlockingUses,
263 const DataLayout &dataLayout) {
264 if (getKind() == cir::CastKind::bitcast)
265 return forwardToUsers(*this, newBlockingUses);
266 return false;
267}
268
269DeletionKind cir::CastOp::removeBlockingUses(
270 const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) {
271 return DeletionKind::Delete;
272}
static bool forwardToUsers(Operation *op, SmallVectorImpl< OpOperand * > &newBlockingUses)
Conditions the deletion of the operation to the removal of all its uses.
static Decl::Kind getKind(const Decl *D)
TokenType getType() const
Returns the token's type, e.g.