|
| 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 contains implementation details, such as storage structures, of |
| 10 | +// CIR dialect types. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | +#ifndef CIR_DIALECT_IR_CIRTYPESDETAILS_H |
| 14 | +#define CIR_DIALECT_IR_CIRTYPESDETAILS_H |
| 15 | + |
| 16 | +#include "mlir/IR/BuiltinAttributes.h" |
| 17 | +#include "mlir/Support/LogicalResult.h" |
| 18 | +#include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 19 | +#include "llvm/ADT/Hashing.h" |
| 20 | + |
| 21 | +namespace cir { |
| 22 | +namespace detail { |
| 23 | + |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | +// CIR RecordTypeStorage |
| 26 | +//===----------------------------------------------------------------------===// |
| 27 | + |
| 28 | +/// Type storage for CIR record types. |
| 29 | +struct RecordTypeStorage : public mlir::TypeStorage { |
| 30 | + struct KeyTy { |
| 31 | + llvm::ArrayRef<mlir::Type> members; |
| 32 | + mlir::StringAttr name; |
| 33 | + bool incomplete; |
| 34 | + bool packed; |
| 35 | + bool padded; |
| 36 | + RecordType::RecordKind kind; |
| 37 | + |
| 38 | + KeyTy(llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name, |
| 39 | + bool incomplete, bool packed, bool padded, |
| 40 | + RecordType::RecordKind kind) |
| 41 | + : members(members), name(name), incomplete(incomplete), packed(packed), |
| 42 | + padded(padded), kind(kind) {} |
| 43 | + }; |
| 44 | + |
| 45 | + llvm::ArrayRef<mlir::Type> members; |
| 46 | + mlir::StringAttr name; |
| 47 | + bool incomplete; |
| 48 | + bool packed; |
| 49 | + bool padded; |
| 50 | + RecordType::RecordKind kind; |
| 51 | + |
| 52 | + RecordTypeStorage(llvm::ArrayRef<mlir::Type> members, mlir::StringAttr name, |
| 53 | + bool incomplete, bool packed, bool padded, |
| 54 | + RecordType::RecordKind kind) |
| 55 | + : members(members), name(name), incomplete(incomplete), packed(packed), |
| 56 | + padded(padded), kind(kind) { |
| 57 | + assert(name || !incomplete && "Incomplete records must have a name"); |
| 58 | + } |
| 59 | + |
| 60 | + KeyTy getAsKey() const { |
| 61 | + return KeyTy(members, name, incomplete, packed, padded, kind); |
| 62 | + } |
| 63 | + |
| 64 | + bool operator==(const KeyTy &key) const { |
| 65 | + if (name) |
| 66 | + return (name == key.name) && (kind == key.kind); |
| 67 | + return std::tie(members, name, incomplete, packed, padded, kind) == |
| 68 | + std::tie(key.members, key.name, key.incomplete, key.packed, |
| 69 | + key.padded, key.kind); |
| 70 | + } |
| 71 | + |
| 72 | + static llvm::hash_code hashKey(const KeyTy &key) { |
| 73 | + if (key.name) |
| 74 | + return llvm::hash_combine(key.name, key.kind); |
| 75 | + return llvm::hash_combine(key.members, key.incomplete, key.packed, |
| 76 | + key.padded, key.kind); |
| 77 | + } |
| 78 | + |
| 79 | + static RecordTypeStorage *construct(mlir::TypeStorageAllocator &allocator, |
| 80 | + const KeyTy &key) { |
| 81 | + return new (allocator.allocate<RecordTypeStorage>()) |
| 82 | + RecordTypeStorage(allocator.copyInto(key.members), key.name, |
| 83 | + key.incomplete, key.packed, key.padded, key.kind); |
| 84 | + } |
| 85 | + |
| 86 | + /// Mutates the members and attributes an identified record. |
| 87 | + /// |
| 88 | + /// Once a record is mutated, it is marked as complete, preventing further |
| 89 | + /// mutations. Anonymous records are always complete and cannot be mutated. |
| 90 | + /// This method does not fail if a mutation of a complete record does not |
| 91 | + /// change the record. |
| 92 | + llvm::LogicalResult mutate(mlir::TypeStorageAllocator &allocator, |
| 93 | + llvm::ArrayRef<mlir::Type> members, bool packed, |
| 94 | + bool padded) { |
| 95 | + // Anonymous records cannot mutate. |
| 96 | + if (!name) |
| 97 | + return llvm::failure(); |
| 98 | + |
| 99 | + // Mutation of complete records are allowed if they change nothing. |
| 100 | + if (!incomplete) |
| 101 | + return mlir::success((this->members == members) && |
| 102 | + (this->packed == packed) && |
| 103 | + (this->padded == padded)); |
| 104 | + |
| 105 | + // Mutate incomplete record. |
| 106 | + this->members = allocator.copyInto(members); |
| 107 | + this->packed = packed; |
| 108 | + this->padded = padded; |
| 109 | + |
| 110 | + incomplete = false; |
| 111 | + return llvm::success(); |
| 112 | + } |
| 113 | +}; |
| 114 | + |
| 115 | +} // namespace detail |
| 116 | +} // namespace cir |
| 117 | + |
| 118 | +#endif // CIR_DIALECT_IR_CIRTYPESDETAILS_H |
0 commit comments