|
| 1 | +//===- AttrDetail.h - Details of MLIR LLVM dialect attributes --------*- C++ |
| 2 | +//-*-===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// |
| 10 | +// This file contains implementation details, such as storage structures, of |
| 11 | +// MLIR LLVM dialect attributes. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | +#ifndef DIALECT_LLVMIR_IR_ATTRDETAIL_H |
| 15 | +#define DIALECT_LLVMIR_IR_ATTRDETAIL_H |
| 16 | + |
| 17 | +#include "mlir/Dialect/LLVMIR/LLVMAttrs.h" |
| 18 | +#include "mlir/IR/Types.h" |
| 19 | + |
| 20 | +namespace mlir { |
| 21 | +namespace LLVM { |
| 22 | +namespace detail { |
| 23 | + |
| 24 | +//===----------------------------------------------------------------------===// |
| 25 | +// DICompositeTypeAttrStorage |
| 26 | +//===----------------------------------------------------------------------===// |
| 27 | + |
| 28 | +struct DICompositeTypeAttrStorage : public ::mlir::AttributeStorage { |
| 29 | + using KeyTy = std::tuple<unsigned, StringAttr, DIFileAttr, uint32_t, |
| 30 | + DIScopeAttr, DITypeAttr, DIFlags, uint64_t, uint64_t, |
| 31 | + ArrayRef<DINodeAttr>, StringAttr>; |
| 32 | + |
| 33 | + DICompositeTypeAttrStorage(unsigned tag, StringAttr name, DIFileAttr file, |
| 34 | + uint32_t line, DIScopeAttr scope, |
| 35 | + DITypeAttr baseType, DIFlags flags, |
| 36 | + uint64_t sizeInBits, uint64_t alignInBits, |
| 37 | + ArrayRef<DINodeAttr> elements, |
| 38 | + StringAttr identifier = StringAttr()) |
| 39 | + : tag(tag), name(name), file(file), line(line), scope(scope), |
| 40 | + baseType(baseType), flags(flags), sizeInBits(sizeInBits), |
| 41 | + alignInBits(alignInBits), elements(elements), identifier(identifier) {} |
| 42 | + |
| 43 | + unsigned getTag() const { return tag; } |
| 44 | + StringAttr getName() const { return name; } |
| 45 | + DIFileAttr getFile() const { return file; } |
| 46 | + uint32_t getLine() const { return line; } |
| 47 | + DIScopeAttr getScope() const { return scope; } |
| 48 | + DITypeAttr getBaseType() const { return baseType; } |
| 49 | + DIFlags getFlags() const { return flags; } |
| 50 | + uint64_t getSizeInBits() const { return sizeInBits; } |
| 51 | + uint64_t getAlignInBits() const { return alignInBits; } |
| 52 | + ArrayRef<DINodeAttr> getElements() const { return elements; } |
| 53 | + StringAttr getIdentifier() const { return identifier; } |
| 54 | + |
| 55 | + /// Returns true if this attribute is identified. |
| 56 | + bool isIdentified() const { |
| 57 | + return !(!identifier); |
| 58 | + } |
| 59 | + |
| 60 | + /// Returns the respective key for this attribute. |
| 61 | + KeyTy getAsKey() const { |
| 62 | + if (isIdentified()) |
| 63 | + return KeyTy(tag, name, file, line, scope, baseType, flags, sizeInBits, |
| 64 | + alignInBits, elements, identifier); |
| 65 | + |
| 66 | + return KeyTy(tag, name, file, line, scope, baseType, flags, sizeInBits, |
| 67 | + alignInBits, elements, StringAttr()); |
| 68 | + } |
| 69 | + |
| 70 | + /// Compares two keys. |
| 71 | + bool operator==(const KeyTy &other) const { |
| 72 | + if (isIdentified()) |
| 73 | + // Just compare against the identifier. |
| 74 | + return identifier == std::get<10>(other); |
| 75 | + |
| 76 | + // Otherwise, compare the entire tuple. |
| 77 | + return other == getAsKey(); |
| 78 | + } |
| 79 | + |
| 80 | + /// Returns the hash value of the key. |
| 81 | + static llvm::hash_code hashKey(const KeyTy &key) { |
| 82 | + const auto &[tag, name, file, line, scope, baseType, flags, sizeInBits, |
| 83 | + alignInBits, elements, identifier] = key; |
| 84 | + |
| 85 | + if (identifier) |
| 86 | + // Only the identifier participates in the hash id. |
| 87 | + return hash_value(identifier); |
| 88 | + |
| 89 | + // Otherwise, everything else is included in the hash. |
| 90 | + return hash_combine(tag, name, file, line, scope, baseType, flags, |
| 91 | + sizeInBits, alignInBits, elements); |
| 92 | + } |
| 93 | + |
| 94 | + /// Constructs new storage for an attribute. |
| 95 | + static DICompositeTypeAttrStorage * |
| 96 | + construct(AttributeStorageAllocator &allocator, const KeyTy &key) { |
| 97 | + auto [tag, name, file, line, scope, baseType, flags, sizeInBits, |
| 98 | + alignInBits, elements, identifier] = key; |
| 99 | + elements = allocator.copyInto(elements); |
| 100 | + if (identifier) { |
| 101 | + return new (allocator.allocate<DICompositeTypeAttrStorage>()) |
| 102 | + DICompositeTypeAttrStorage(tag, name, file, line, scope, baseType, |
| 103 | + flags, sizeInBits, alignInBits, elements, |
| 104 | + identifier); |
| 105 | + } |
| 106 | + return new (allocator.allocate<DICompositeTypeAttrStorage>()) |
| 107 | + DICompositeTypeAttrStorage(tag, name, file, line, scope, baseType, |
| 108 | + flags, sizeInBits, alignInBits, elements); |
| 109 | + } |
| 110 | + |
| 111 | + LogicalResult mutate(AttributeStorageAllocator &allocator, |
| 112 | + const ArrayRef<DINodeAttr>& elements) { |
| 113 | + // Replace the elements. |
| 114 | + this->elements = allocator.copyInto(elements); |
| 115 | + return success(); |
| 116 | + } |
| 117 | + |
| 118 | +private: |
| 119 | + unsigned tag; |
| 120 | + StringAttr name; |
| 121 | + DIFileAttr file; |
| 122 | + uint32_t line; |
| 123 | + DIScopeAttr scope; |
| 124 | + DITypeAttr baseType; |
| 125 | + DIFlags flags; |
| 126 | + uint64_t sizeInBits; |
| 127 | + uint64_t alignInBits; |
| 128 | + ArrayRef<DINodeAttr> elements; |
| 129 | + StringAttr identifier; |
| 130 | +}; |
| 131 | + |
| 132 | +} // namespace detail |
| 133 | +} // namespace LLVM |
| 134 | +} // namespace mlir |
| 135 | + |
| 136 | +#endif // DIALECT_LLVMIR_IR_ATTRDETAIL_H |
0 commit comments