diff --git a/clang/lib/AST/APValue.cpp b/clang/lib/AST/APValue.cpp index c641ff6b99bab..acba55742a967 100644 --- a/clang/lib/AST/APValue.cpp +++ b/clang/lib/AST/APValue.cpp @@ -187,14 +187,14 @@ APValue::LValueBase::operator bool () const { clang::APValue::LValueBase llvm::DenseMapInfo::getEmptyKey() { clang::APValue::LValueBase B; - B.Ptr = DenseMapInfo::getEmptyKey(); + B.Ptr = DenseMapInfo::getEmptyKey(); return B; } clang::APValue::LValueBase llvm::DenseMapInfo::getTombstoneKey() { clang::APValue::LValueBase B; - B.Ptr = DenseMapInfo::getTombstoneKey(); + B.Ptr = DenseMapInfo::getTombstoneKey(); return B; } diff --git a/llvm/include/llvm/ADT/DenseMapInfo.h b/llvm/include/llvm/ADT/DenseMapInfo.h index 07c37e353a40b..b371611e7d948 100644 --- a/llvm/include/llvm/ADT/DenseMapInfo.h +++ b/llvm/include/llvm/ADT/DenseMapInfo.h @@ -56,30 +56,13 @@ struct DenseMapInfo { //static bool isEqual(const T &LHS, const T &RHS); }; -// Provide DenseMapInfo for all pointers. Come up with sentinel pointer values -// that are aligned to alignof(T) bytes, but try to avoid requiring T to be -// complete. This allows clients to instantiate DenseMap with forward -// declared key types. Assume that no pointer key type requires more than 4096 -// bytes of alignment. -template -struct DenseMapInfo { - // The following should hold, but it would require T to be complete: - // static_assert(alignof(T) <= (1 << Log2MaxAlign), - // "DenseMap does not support pointer keys requiring more than " - // "Log2MaxAlign bits of alignment"); - static constexpr uintptr_t Log2MaxAlign = 12; - +template struct DenseMapInfo { static inline T* getEmptyKey() { - uintptr_t Val = static_cast(-1); - Val <<= Log2MaxAlign; - return reinterpret_cast(Val); + // We assume that raw pointers do not carry alignment requirements. + return reinterpret_cast(-1); } - static inline T* getTombstoneKey() { - uintptr_t Val = static_cast(-2); - Val <<= Log2MaxAlign; - return reinterpret_cast(Val); - } + static inline T *getTombstoneKey() { return reinterpret_cast(-2); } static unsigned getHashValue(const T *PtrVal) { return (unsigned((uintptr_t)PtrVal) >> 4) ^ diff --git a/llvm/include/llvm/ADT/PointerUnion.h b/llvm/include/llvm/ADT/PointerUnion.h index cdbd76d7f505b..86248a2cf836f 100644 --- a/llvm/include/llvm/ADT/PointerUnion.h +++ b/llvm/include/llvm/ADT/PointerUnion.h @@ -286,13 +286,19 @@ struct PointerLikeTypeTraits> { // Teach DenseMap how to use PointerUnions as keys. template struct DenseMapInfo> { using Union = PointerUnion; - using FirstInfo = - DenseMapInfo::type>; + using FirstTypeTraits = PointerLikeTypeTraits< + typename pointer_union_detail::GetFirstType::type>; - static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); } + static inline Union getEmptyKey() { + uintptr_t Val = static_cast(-1); + Val <<= FirstTypeTraits::NumLowBitsAvailable; + return FirstTypeTraits::getFromVoidPointer(reinterpret_cast(Val)); + } static inline Union getTombstoneKey() { - return Union(FirstInfo::getTombstoneKey()); + uintptr_t Val = static_cast(-2); + Val <<= FirstTypeTraits::NumLowBitsAvailable; + return FirstTypeTraits::getFromVoidPointer(reinterpret_cast(Val)); } static unsigned getHashValue(const Union &UnionVal) { diff --git a/mlir/include/mlir/Support/TypeID.h b/mlir/include/mlir/Support/TypeID.h index 459e9dae12a9f..e2c25474087ae 100644 --- a/mlir/include/mlir/Support/TypeID.h +++ b/mlir/include/mlir/Support/TypeID.h @@ -395,11 +395,12 @@ namespace llvm { template <> struct DenseMapInfo { static inline mlir::TypeID getEmptyKey() { - void *pointer = llvm::DenseMapInfo::getEmptyKey(); + // Shift by 3 to satisfy the TypeID alignment requirement. + void *pointer = reinterpret_cast(uintptr_t(-1) << 3); return mlir::TypeID::getFromOpaquePointer(pointer); } static inline mlir::TypeID getTombstoneKey() { - void *pointer = llvm::DenseMapInfo::getTombstoneKey(); + void *pointer = reinterpret_cast(uintptr_t(-2) << 3); return mlir::TypeID::getFromOpaquePointer(pointer); } static unsigned getHashValue(mlir::TypeID val) { diff --git a/mlir/lib/Bindings/Python/NanobindUtils.h b/mlir/lib/Bindings/Python/NanobindUtils.h index 64ea4329f65f1..535fc2328c482 100644 --- a/mlir/lib/Bindings/Python/NanobindUtils.h +++ b/mlir/lib/Bindings/Python/NanobindUtils.h @@ -408,11 +408,12 @@ namespace llvm { template <> struct DenseMapInfo { static inline MlirTypeID getEmptyKey() { - auto *pointer = llvm::DenseMapInfo::getEmptyKey(); + // Shift by 3 to satisfy the TypeID alignment requirement. + void *pointer = reinterpret_cast(uintptr_t(-1) << 3); return mlirTypeIDCreate(pointer); } static inline MlirTypeID getTombstoneKey() { - auto *pointer = llvm::DenseMapInfo::getTombstoneKey(); + void *pointer = reinterpret_cast(uintptr_t(-2) << 3); return mlirTypeIDCreate(pointer); } static inline unsigned getHashValue(const MlirTypeID &val) {