| 1 | //===- PtrDialect.cpp - Pointer dialect ---------------------*- C++ -*-===// |
| 2 | // |
| 3 | // This file is licensed 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 the Pointer dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Dialect/Ptr/IR/PtrOps.h" |
| 14 | #include "mlir/IR/Matchers.h" |
| 15 | #include "mlir/Interfaces/DataLayoutInterfaces.h" |
| 16 | #include "mlir/Transforms/InliningUtils.h" |
| 17 | |
| 18 | using namespace mlir; |
| 19 | using namespace mlir::ptr; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // Pointer dialect |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | |
| 25 | void PtrDialect::initialize() { |
| 26 | addOperations< |
| 27 | #define GET_OP_LIST |
| 28 | #include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc" |
| 29 | >(); |
| 30 | addAttributes< |
| 31 | #define GET_ATTRDEF_LIST |
| 32 | #include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.cpp.inc" |
| 33 | >(); |
| 34 | addTypes< |
| 35 | #define GET_TYPEDEF_LIST |
| 36 | #include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc" |
| 37 | >(); |
| 38 | } |
| 39 | |
| 40 | //===----------------------------------------------------------------------===// |
| 41 | // Common helper functions. |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
| 44 | /// Verifies that the alignment attribute is a power of 2 if present. |
| 45 | static LogicalResult |
| 46 | verifyAlignment(std::optional<int64_t> alignment, |
| 47 | function_ref<InFlightDiagnostic()> emitError) { |
| 48 | if (!alignment) |
| 49 | return success(); |
| 50 | if (alignment.value() <= 0) |
| 51 | return emitError() << "alignment must be positive" ; |
| 52 | if (!llvm::isPowerOf2_64(Value: alignment.value())) |
| 53 | return emitError() << "alignment must be a power of 2" ; |
| 54 | return success(); |
| 55 | } |
| 56 | |
| 57 | //===----------------------------------------------------------------------===// |
| 58 | // ConstantOp |
| 59 | //===----------------------------------------------------------------------===// |
| 60 | |
| 61 | OpFoldResult ConstantOp::fold(FoldAdaptor adaptor) { return getValue(); } |
| 62 | |
| 63 | //===----------------------------------------------------------------------===// |
| 64 | // FromPtrOp |
| 65 | //===----------------------------------------------------------------------===// |
| 66 | |
| 67 | OpFoldResult FromPtrOp::fold(FoldAdaptor adaptor) { |
| 68 | // Fold the pattern: |
| 69 | // %ptr = ptr.to_ptr %v : type -> ptr |
| 70 | // (%mda = ptr.get_metadata %v : type)? |
| 71 | // %val = ptr.from_ptr %ptr (metadata %mda)? : ptr -> type |
| 72 | // To: |
| 73 | // %val -> %v |
| 74 | Value ptrLike; |
| 75 | FromPtrOp fromPtr = *this; |
| 76 | while (fromPtr != nullptr) { |
| 77 | auto toPtr = fromPtr.getPtr().getDefiningOp<ToPtrOp>(); |
| 78 | // Cannot fold if it's not a `to_ptr` op or the initial and final types are |
| 79 | // different. |
| 80 | if (!toPtr || toPtr.getPtr().getType() != fromPtr.getType()) |
| 81 | return ptrLike; |
| 82 | Value md = fromPtr.getMetadata(); |
| 83 | // If the type has trivial metadata fold. |
| 84 | if (!fromPtr.getType().hasPtrMetadata()) { |
| 85 | ptrLike = toPtr.getPtr(); |
| 86 | } else if (md) { |
| 87 | // Fold if the metadata can be verified to be equal. |
| 88 | if (auto mdOp = md.getDefiningOp<GetMetadataOp>(); |
| 89 | mdOp && mdOp.getPtr() == toPtr.getPtr()) |
| 90 | ptrLike = toPtr.getPtr(); |
| 91 | } |
| 92 | // Check for a sequence of casts. |
| 93 | fromPtr = ptrLike ? ptrLike.getDefiningOp<FromPtrOp>() : nullptr; |
| 94 | } |
| 95 | return ptrLike; |
| 96 | } |
| 97 | |
| 98 | LogicalResult FromPtrOp::verify() { |
| 99 | if (isa<PtrType>(Val: getType())) |
| 100 | return emitError() << "the result type cannot be `!ptr.ptr`" ; |
| 101 | if (getType().getMemorySpace() != getPtr().getType().getMemorySpace()) { |
| 102 | return emitError() |
| 103 | << "expected the input and output to have the same memory space" ; |
| 104 | } |
| 105 | return success(); |
| 106 | } |
| 107 | |
| 108 | //===----------------------------------------------------------------------===// |
| 109 | // GatherOp |
| 110 | //===----------------------------------------------------------------------===// |
| 111 | |
| 112 | void GatherOp::getEffects( |
| 113 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 114 | &effects) { |
| 115 | // Gather performs reads from multiple memory locations specified by ptrs |
| 116 | effects.emplace_back(Args: MemoryEffects::Read::get(), Args: &getPtrsMutable()); |
| 117 | } |
| 118 | |
| 119 | LogicalResult GatherOp::verify() { |
| 120 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 121 | |
| 122 | // Verify that the pointer type's memory space allows loads. |
| 123 | MemorySpaceAttrInterface ms = |
| 124 | cast<PtrType>(Val: getPtrs().getType().getElementType()).getMemorySpace(); |
| 125 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 126 | if (!ms.isValidLoad(type: getResult().getType(), ordering: AtomicOrdering::not_atomic, |
| 127 | alignment: getAlignment(), dataLayout: &dataLayout, emitError: emitDiag)) |
| 128 | return failure(); |
| 129 | |
| 130 | // Verify the alignment. |
| 131 | return verifyAlignment(alignment: getAlignment(), emitError: emitDiag); |
| 132 | } |
| 133 | |
| 134 | void GatherOp::build(OpBuilder &builder, OperationState &state, Type resultType, |
| 135 | Value ptrs, Value mask, Value passthrough, |
| 136 | unsigned alignment) { |
| 137 | build(odsBuilder&: builder, odsState&: state, result: resultType, ptrs, mask, passthrough, |
| 138 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt); |
| 139 | } |
| 140 | |
| 141 | //===----------------------------------------------------------------------===// |
| 142 | // LoadOp |
| 143 | //===----------------------------------------------------------------------===// |
| 144 | |
| 145 | /// Verifies the attributes and the type of atomic memory access operations. |
| 146 | template <typename OpTy> |
| 147 | static LogicalResult |
| 148 | verifyAtomicMemOp(OpTy memOp, ArrayRef<AtomicOrdering> unsupportedOrderings) { |
| 149 | if (memOp.getOrdering() != AtomicOrdering::not_atomic) { |
| 150 | if (llvm::is_contained(unsupportedOrderings, memOp.getOrdering())) |
| 151 | return memOp.emitOpError("unsupported ordering '" ) |
| 152 | << stringifyAtomicOrdering(memOp.getOrdering()) << "'" ; |
| 153 | if (!memOp.getAlignment()) |
| 154 | return memOp.emitOpError("expected alignment for atomic access" ); |
| 155 | return success(); |
| 156 | } |
| 157 | if (memOp.getSyncscope()) { |
| 158 | return memOp.emitOpError( |
| 159 | "expected syncscope to be null for non-atomic access" ); |
| 160 | } |
| 161 | return success(); |
| 162 | } |
| 163 | |
| 164 | void LoadOp::getEffects( |
| 165 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 166 | &effects) { |
| 167 | effects.emplace_back(Args: MemoryEffects::Read::get(), Args: &getPtrMutable()); |
| 168 | // Volatile operations can have target-specific read-write effects on |
| 169 | // memory besides the one referred to by the pointer operand. |
| 170 | // Similarly, atomic operations that are monotonic or stricter cause |
| 171 | // synchronization that from a language point-of-view, are arbitrary |
| 172 | // read-writes into memory. |
| 173 | if (getVolatile_() || (getOrdering() != AtomicOrdering::not_atomic && |
| 174 | getOrdering() != AtomicOrdering::unordered)) { |
| 175 | effects.emplace_back(Args: MemoryEffects::Write::get()); |
| 176 | effects.emplace_back(Args: MemoryEffects::Read::get()); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | LogicalResult LoadOp::verify() { |
| 181 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 182 | MemorySpaceAttrInterface ms = getPtr().getType().getMemorySpace(); |
| 183 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 184 | if (!ms.isValidLoad(type: getResult().getType(), ordering: getOrdering(), alignment: getAlignment(), |
| 185 | dataLayout: &dataLayout, emitError: emitDiag)) |
| 186 | return failure(); |
| 187 | if (failed(Result: verifyAlignment(alignment: getAlignment(), emitError: emitDiag))) |
| 188 | return failure(); |
| 189 | return verifyAtomicMemOp(memOp: *this, |
| 190 | unsupportedOrderings: {AtomicOrdering::release, AtomicOrdering::acq_rel}); |
| 191 | } |
| 192 | |
| 193 | void LoadOp::build(OpBuilder &builder, OperationState &state, Type type, |
| 194 | Value addr, unsigned alignment, bool isVolatile, |
| 195 | bool isNonTemporal, bool isInvariant, bool isInvariantGroup, |
| 196 | AtomicOrdering ordering, StringRef syncscope) { |
| 197 | build(odsBuilder&: builder, odsState&: state, value: type, ptr: addr, |
| 198 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt, |
| 199 | volatile_: isVolatile, nontemporal: isNonTemporal, invariant: isInvariant, invariantGroup: isInvariantGroup, ordering, |
| 200 | syncscope: syncscope.empty() ? nullptr : builder.getStringAttr(bytes: syncscope)); |
| 201 | } |
| 202 | //===----------------------------------------------------------------------===// |
| 203 | // MaskedLoadOp |
| 204 | //===----------------------------------------------------------------------===// |
| 205 | |
| 206 | void MaskedLoadOp::getEffects( |
| 207 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 208 | &effects) { |
| 209 | // MaskedLoad performs reads from the memory location specified by ptr. |
| 210 | effects.emplace_back(Args: MemoryEffects::Read::get(), Args: &getPtrMutable()); |
| 211 | } |
| 212 | |
| 213 | LogicalResult MaskedLoadOp::verify() { |
| 214 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 215 | // Verify that the pointer type's memory space allows loads. |
| 216 | MemorySpaceAttrInterface ms = getPtr().getType().getMemorySpace(); |
| 217 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 218 | if (!ms.isValidLoad(type: getResult().getType(), ordering: AtomicOrdering::not_atomic, |
| 219 | alignment: getAlignment(), dataLayout: &dataLayout, emitError: emitDiag)) |
| 220 | return failure(); |
| 221 | |
| 222 | // Verify the alignment. |
| 223 | return verifyAlignment(alignment: getAlignment(), emitError: emitDiag); |
| 224 | } |
| 225 | |
| 226 | void MaskedLoadOp::build(OpBuilder &builder, OperationState &state, |
| 227 | Type resultType, Value ptr, Value mask, |
| 228 | Value passthrough, unsigned alignment) { |
| 229 | build(odsBuilder&: builder, odsState&: state, result: resultType, ptr, mask, passthrough, |
| 230 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt); |
| 231 | } |
| 232 | |
| 233 | //===----------------------------------------------------------------------===// |
| 234 | // MaskedStoreOp |
| 235 | //===----------------------------------------------------------------------===// |
| 236 | |
| 237 | void MaskedStoreOp::getEffects( |
| 238 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 239 | &effects) { |
| 240 | // MaskedStore performs writes to the memory location specified by ptr |
| 241 | effects.emplace_back(Args: MemoryEffects::Write::get(), Args: &getPtrMutable()); |
| 242 | } |
| 243 | |
| 244 | LogicalResult MaskedStoreOp::verify() { |
| 245 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 246 | // Verify that the pointer type's memory space allows stores. |
| 247 | MemorySpaceAttrInterface ms = getPtr().getType().getMemorySpace(); |
| 248 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 249 | if (!ms.isValidStore(type: getValue().getType(), ordering: AtomicOrdering::not_atomic, |
| 250 | alignment: getAlignment(), dataLayout: &dataLayout, emitError: emitDiag)) |
| 251 | return failure(); |
| 252 | |
| 253 | // Verify the alignment. |
| 254 | return verifyAlignment(alignment: getAlignment(), emitError: emitDiag); |
| 255 | } |
| 256 | |
| 257 | void MaskedStoreOp::build(OpBuilder &builder, OperationState &state, |
| 258 | Value value, Value ptr, Value mask, |
| 259 | unsigned alignment) { |
| 260 | build(odsBuilder&: builder, odsState&: state, value, ptr, mask, |
| 261 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt); |
| 262 | } |
| 263 | |
| 264 | //===----------------------------------------------------------------------===// |
| 265 | // ScatterOp |
| 266 | //===----------------------------------------------------------------------===// |
| 267 | |
| 268 | void ScatterOp::getEffects( |
| 269 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 270 | &effects) { |
| 271 | // Scatter performs writes to multiple memory locations specified by ptrs |
| 272 | effects.emplace_back(Args: MemoryEffects::Write::get(), Args: &getPtrsMutable()); |
| 273 | } |
| 274 | |
| 275 | LogicalResult ScatterOp::verify() { |
| 276 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 277 | |
| 278 | // Verify that the pointer type's memory space allows stores. |
| 279 | MemorySpaceAttrInterface ms = |
| 280 | cast<PtrType>(Val: getPtrs().getType().getElementType()).getMemorySpace(); |
| 281 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 282 | if (!ms.isValidStore(type: getValue().getType(), ordering: AtomicOrdering::not_atomic, |
| 283 | alignment: getAlignment(), dataLayout: &dataLayout, emitError: emitDiag)) |
| 284 | return failure(); |
| 285 | |
| 286 | // Verify the alignment. |
| 287 | return verifyAlignment(alignment: getAlignment(), emitError: emitDiag); |
| 288 | } |
| 289 | |
| 290 | void ScatterOp::build(OpBuilder &builder, OperationState &state, Value value, |
| 291 | Value ptrs, Value mask, unsigned alignment) { |
| 292 | build(odsBuilder&: builder, odsState&: state, value, ptrs, mask, |
| 293 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt); |
| 294 | } |
| 295 | |
| 296 | //===----------------------------------------------------------------------===// |
| 297 | // StoreOp |
| 298 | //===----------------------------------------------------------------------===// |
| 299 | |
| 300 | void StoreOp::getEffects( |
| 301 | SmallVectorImpl<SideEffects::EffectInstance<MemoryEffects::Effect>> |
| 302 | &effects) { |
| 303 | effects.emplace_back(Args: MemoryEffects::Write::get(), Args: &getPtrMutable()); |
| 304 | // Volatile operations can have target-specific read-write effects on |
| 305 | // memory besides the one referred to by the pointer operand. |
| 306 | // Similarly, atomic operations that are monotonic or stricter cause |
| 307 | // synchronization that from a language point-of-view, are arbitrary |
| 308 | // read-writes into memory. |
| 309 | if (getVolatile_() || (getOrdering() != AtomicOrdering::not_atomic && |
| 310 | getOrdering() != AtomicOrdering::unordered)) { |
| 311 | effects.emplace_back(Args: MemoryEffects::Write::get()); |
| 312 | effects.emplace_back(Args: MemoryEffects::Read::get()); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | LogicalResult StoreOp::verify() { |
| 317 | auto emitDiag = [&]() -> InFlightDiagnostic { return emitError(); }; |
| 318 | MemorySpaceAttrInterface ms = getPtr().getType().getMemorySpace(); |
| 319 | DataLayout dataLayout = DataLayout::closest(op: *this); |
| 320 | if (!ms.isValidStore(type: getValue().getType(), ordering: getOrdering(), alignment: getAlignment(), |
| 321 | dataLayout: &dataLayout, emitError: emitDiag)) |
| 322 | return failure(); |
| 323 | if (failed(Result: verifyAlignment(alignment: getAlignment(), emitError: emitDiag))) |
| 324 | return failure(); |
| 325 | return verifyAtomicMemOp(memOp: *this, |
| 326 | unsupportedOrderings: {AtomicOrdering::acquire, AtomicOrdering::acq_rel}); |
| 327 | } |
| 328 | |
| 329 | void StoreOp::build(OpBuilder &builder, OperationState &state, Value value, |
| 330 | Value addr, unsigned alignment, bool isVolatile, |
| 331 | bool isNonTemporal, bool isInvariantGroup, |
| 332 | AtomicOrdering ordering, StringRef syncscope) { |
| 333 | build(odsBuilder&: builder, odsState&: state, value, ptr: addr, |
| 334 | alignment: alignment ? std::optional<int64_t>(alignment) : std::nullopt, |
| 335 | volatile_: isVolatile, nontemporal: isNonTemporal, invariantGroup: isInvariantGroup, ordering, |
| 336 | syncscope: syncscope.empty() ? nullptr : builder.getStringAttr(bytes: syncscope)); |
| 337 | } |
| 338 | |
| 339 | //===----------------------------------------------------------------------===// |
| 340 | // PtrAddOp |
| 341 | //===----------------------------------------------------------------------===// |
| 342 | |
| 343 | /// Fold: ptradd ptr + 0 -> ptr |
| 344 | OpFoldResult PtrAddOp::fold(FoldAdaptor adaptor) { |
| 345 | Attribute attr = adaptor.getOffset(); |
| 346 | // A shaped offset can broadcast a scalar base to a shaped result. |
| 347 | if (!attr || getBase().getType() != getType()) |
| 348 | return nullptr; |
| 349 | if (llvm::APInt value; m_ConstantInt(bind_value: &value).match(attr) && value.isZero()) |
| 350 | return getBase(); |
| 351 | return nullptr; |
| 352 | } |
| 353 | |
| 354 | LogicalResult PtrAddOp::inferReturnTypes( |
| 355 | MLIRContext *context, std::optional<Location> location, ValueRange operands, |
| 356 | DictionaryAttr attributes, PropertyRef properties, RegionRange regions, |
| 357 | SmallVectorImpl<Type> &inferredReturnTypes) { |
| 358 | // Get the base pointer and offset types. |
| 359 | Type baseType = operands[0].getType(); |
| 360 | Type offsetType = operands[1].getType(); |
| 361 | |
| 362 | auto offTy = dyn_cast<ShapedType>(Val&: offsetType); |
| 363 | if (!offTy) { |
| 364 | // If the offset isn't shaped, the result is always the base type. |
| 365 | inferredReturnTypes.push_back(Elt: baseType); |
| 366 | return success(); |
| 367 | } |
| 368 | auto baseTy = dyn_cast<ShapedType>(Val&: baseType); |
| 369 | if (!baseTy) { |
| 370 | // Base isn't shaped, but offset is, use the ShapedType from offset with the |
| 371 | // base pointer as element type. |
| 372 | inferredReturnTypes.push_back(Elt: offTy.clone(elementType: baseType)); |
| 373 | return success(); |
| 374 | } |
| 375 | |
| 376 | // Both are shaped, their shape must match. |
| 377 | if (offTy.getShape() != baseTy.getShape()) { |
| 378 | if (location) |
| 379 | mlir::emitError(loc: *location) << "shapes of base and offset must match" ; |
| 380 | return failure(); |
| 381 | } |
| 382 | |
| 383 | // Make sure they are the same kind of shaped type. |
| 384 | if (baseType.getTypeID() != offsetType.getTypeID()) { |
| 385 | if (location) |
| 386 | mlir::emitError(loc: *location) << "the shaped containers type must match" ; |
| 387 | return failure(); |
| 388 | } |
| 389 | inferredReturnTypes.push_back(Elt: baseType); |
| 390 | return success(); |
| 391 | } |
| 392 | |
| 393 | //===----------------------------------------------------------------------===// |
| 394 | // PtrDiffOp |
| 395 | //===----------------------------------------------------------------------===// |
| 396 | |
| 397 | LogicalResult PtrDiffOp::verify() { |
| 398 | // If the operands are not shaped early exit. |
| 399 | if (!isa<ShapedType>(Val: getLhs().getType())) |
| 400 | return success(); |
| 401 | |
| 402 | // Just check the container type matches, `SameOperandsAndResultShape` handles |
| 403 | // the actual shape. |
| 404 | if (getResult().getType().getTypeID() != getLhs().getType().getTypeID()) { |
| 405 | return emitError() << "expected the result to have the same container " |
| 406 | "type as the operands when operands are shaped" ; |
| 407 | } |
| 408 | |
| 409 | return success(); |
| 410 | } |
| 411 | |
| 412 | ptr::PtrType PtrDiffOp::getPtrType() { |
| 413 | Type lhsType = getLhs().getType(); |
| 414 | if (auto shapedType = dyn_cast<ShapedType>(Val&: lhsType)) |
| 415 | return cast<ptr::PtrType>(Val: shapedType.getElementType()); |
| 416 | return cast<ptr::PtrType>(Val&: lhsType); |
| 417 | } |
| 418 | |
| 419 | Type PtrDiffOp::getIntType() { |
| 420 | Type resultType = getResult().getType(); |
| 421 | if (auto shapedType = dyn_cast<ShapedType>(Val&: resultType)) |
| 422 | return shapedType.getElementType(); |
| 423 | return resultType; |
| 424 | } |
| 425 | |
| 426 | //===----------------------------------------------------------------------===// |
| 427 | // ToPtrOp |
| 428 | //===----------------------------------------------------------------------===// |
| 429 | |
| 430 | OpFoldResult ToPtrOp::fold(FoldAdaptor adaptor) { |
| 431 | // Fold the pattern: |
| 432 | // %val = ptr.from_ptr %p (metadata ...)? : ptr -> type |
| 433 | // %ptr = ptr.to_ptr %val : type -> ptr |
| 434 | // To: |
| 435 | // %ptr -> %p |
| 436 | Value ptr; |
| 437 | ToPtrOp toPtr = *this; |
| 438 | while (toPtr != nullptr) { |
| 439 | auto fromPtr = toPtr.getPtr().getDefiningOp<FromPtrOp>(); |
| 440 | // Cannot fold if it's not a `from_ptr` op. |
| 441 | if (!fromPtr) |
| 442 | return ptr; |
| 443 | ptr = fromPtr.getPtr(); |
| 444 | // Check for chains of casts. |
| 445 | toPtr = ptr.getDefiningOp<ToPtrOp>(); |
| 446 | } |
| 447 | return ptr; |
| 448 | } |
| 449 | |
| 450 | LogicalResult ToPtrOp::verify() { |
| 451 | if (isa<PtrType>(Val: getPtr().getType())) |
| 452 | return emitError() << "the input value cannot be of type `!ptr.ptr`" ; |
| 453 | if (getType().getMemorySpace() != getPtr().getType().getMemorySpace()) { |
| 454 | return emitError() |
| 455 | << "expected the input and output to have the same memory space" ; |
| 456 | } |
| 457 | return success(); |
| 458 | } |
| 459 | |
| 460 | //===----------------------------------------------------------------------===// |
| 461 | // TypeOffsetOp |
| 462 | //===----------------------------------------------------------------------===// |
| 463 | |
| 464 | llvm::TypeSize TypeOffsetOp::getTypeSize(std::optional<DataLayout> layout) { |
| 465 | if (!layout) |
| 466 | layout.emplace(args: DataLayout::closest(op: *this)); |
| 467 | return layout->getTypeAllocSize(t: getElementType()); |
| 468 | } |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | // Pointer API. |
| 472 | //===----------------------------------------------------------------------===// |
| 473 | |
| 474 | #include "mlir/Dialect/Ptr/IR/PtrOpsDialect.cpp.inc" |
| 475 | |
| 476 | #define GET_ATTRDEF_CLASSES |
| 477 | #include "mlir/Dialect/Ptr/IR/PtrOpsAttrs.cpp.inc" |
| 478 | |
| 479 | #include "mlir/Dialect/Ptr/IR/PtrOpsEnums.cpp.inc" |
| 480 | |
| 481 | #define GET_TYPEDEF_CLASSES |
| 482 | #include "mlir/Dialect/Ptr/IR/PtrOpsTypes.cpp.inc" |
| 483 | |
| 484 | #define GET_OP_CLASSES |
| 485 | #include "mlir/Dialect/Ptr/IR/PtrOps.cpp.inc" |
| 486 | |