| 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 | |
| 14 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 15 | |
| 16 | using namespace mlir; |
| 17 | |
| 18 | /// Conditions the deletion of the operation to the removal of all its uses. |
| 19 | static bool forwardToUsers(Operation *op, |
| 20 | SmallVectorImpl<OpOperand *> &newBlockingUses) { |
| 21 | for (Value result : op->getResults()) |
| 22 | for (OpOperand &use : result.getUses()) |
| 23 | newBlockingUses.push_back(Elt: &use); |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // Interfaces for AllocaOp |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | llvm::SmallVector<MemorySlot> cir::AllocaOp::getPromotableSlots() { |
| 32 | return {MemorySlot{.ptr: getResult(), .valueType: getAllocaType()}}; |
| 33 | } |
| 34 | |
| 35 | Value cir::AllocaOp::getDefaultValue(const MemorySlot &slot, |
| 36 | OpBuilder &builder) { |
| 37 | return cir::ConstantOp::create(builder, location: getLoc(), |
| 38 | value: cir::UndefAttr::get(type: slot.valueType)); |
| 39 | } |
| 40 | |
| 41 | void cir::AllocaOp::handleBlockArgument(const MemorySlot &slot, |
| 42 | BlockArgument argument, |
| 43 | OpBuilder &builder) {} |
| 44 | |
| 45 | std::optional<PromotableAllocationOpInterface> |
| 46 | cir::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 | |
| 58 | bool cir::LoadOp::loadsFrom(const MemorySlot &slot) { |
| 59 | return getAddr() == slot.ptr; |
| 60 | } |
| 61 | |
| 62 | bool cir::LoadOp::storesTo(const MemorySlot &slot) { return false; } |
| 63 | |
| 64 | Value 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 | |
| 69 | bool 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 | |
| 84 | DeletionKind cir::LoadOp::removeBlockingUses( |
| 85 | const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses, |
| 86 | OpBuilder &builder, Value reachingDefinition, |
| 87 | const DataLayout &dataLayout) { |
| 88 | getResult().replaceAllUsesWith(newValue: reachingDefinition); |
| 89 | return DeletionKind::Delete; |
| 90 | } |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | // Interfaces for StoreOp |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | |
| 96 | bool cir::StoreOp::loadsFrom(const MemorySlot &slot) { return false; } |
| 97 | |
| 98 | bool cir::StoreOp::storesTo(const MemorySlot &slot) { |
| 99 | return getAddr() == slot.ptr; |
| 100 | } |
| 101 | |
| 102 | Value cir::StoreOp::getStored(const MemorySlot &slot, OpBuilder &builder, |
| 103 | Value reachingDef, const DataLayout &dataLayout) { |
| 104 | return getValue(); |
| 105 | } |
| 106 | |
| 107 | bool 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 | |
| 122 | DeletionKind 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 | |
| 133 | bool cir::CopyOp::loadsFrom(const MemorySlot &slot) { |
| 134 | return getSrc() == slot.ptr; |
| 135 | } |
| 136 | |
| 137 | bool cir::CopyOp::storesTo(const MemorySlot &slot) { |
| 138 | return getDst() == slot.ptr; |
| 139 | } |
| 140 | |
| 141 | Value cir::CopyOp::getStored(const MemorySlot &slot, OpBuilder &builder, |
| 142 | Value reachingDef, const DataLayout &dataLayout) { |
| 143 | return cir::LoadOp::create(builder, location: getLoc(), result: slot.valueType, addr: getSrc()); |
| 144 | } |
| 145 | |
| 146 | DeletionKind 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, location: getLoc(), value: reachingDefinition, addr: getDst(), |
| 152 | /*is_volatile=*/false, |
| 153 | /*is_nontemporal=*/false, |
| 154 | /*alignment=*/mlir::IntegerAttr{}, |
| 155 | /*sync_scope=*/cir::SyncScopeKindAttr(), |
| 156 | /*mem-order=*/mem_order: cir::MemOrderAttr()); |
| 157 | return DeletionKind::Delete; |
| 158 | } |
| 159 | |
| 160 | bool 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(dl: dataLayout) == |
| 168 | dataLayout.getTypeSize(t: slot.valueType); |
| 169 | } |
| 170 | |
| 171 | //===----------------------------------------------------------------------===// |
| 172 | // Interfaces for MatrixColumnMajorLoadOp |
| 173 | //===----------------------------------------------------------------------===// |
| 174 | |
| 175 | bool cir::MatrixColumnMajorLoadOp::loadsFrom(const MemorySlot &slot) { |
| 176 | return getValue() == slot.ptr; |
| 177 | } |
| 178 | |
| 179 | bool cir::MatrixColumnMajorLoadOp::storesTo(const MemorySlot &slot) { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | Value 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 | |
| 190 | bool 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 | |
| 206 | DeletionKind cir::MatrixColumnMajorLoadOp::removeBlockingUses( |
| 207 | const MemorySlot &slot, const SmallPtrSetImpl<OpOperand *> &blockingUses, |
| 208 | OpBuilder &builder, Value reachingDefinition, |
| 209 | const DataLayout &dataLayout) { |
| 210 | getResult().replaceAllUsesWith(newValue: reachingDefinition); |
| 211 | return DeletionKind::Delete; |
| 212 | } |
| 213 | |
| 214 | //===----------------------------------------------------------------------===// |
| 215 | // Interfaces for MatrixColumnMajorStoreOp |
| 216 | //===----------------------------------------------------------------------===// |
| 217 | |
| 218 | bool cir::MatrixColumnMajorStoreOp::loadsFrom(const MemorySlot &slot) { |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | bool cir::MatrixColumnMajorStoreOp::storesTo(const MemorySlot &slot) { |
| 223 | return getValue() == slot.ptr; |
| 224 | } |
| 225 | |
| 226 | Value cir::MatrixColumnMajorStoreOp::getStored(const MemorySlot &slot, |
| 227 | OpBuilder &builder, |
| 228 | Value reachingDef, |
| 229 | const DataLayout &dataLayout) { |
| 230 | return getMatrix(); |
| 231 | } |
| 232 | |
| 233 | bool 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 | |
| 249 | DeletionKind 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 | |
| 260 | bool 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(op: *this, newBlockingUses); |
| 266 | return false; |
| 267 | } |
| 268 | |
| 269 | DeletionKind cir::CastOp::removeBlockingUses( |
| 270 | const SmallPtrSetImpl<OpOperand *> &blockingUses, OpBuilder &builder) { |
| 271 | return DeletionKind::Delete; |
| 272 | } |
| 273 | |