Skip to content

[DenseMap] Do not align pointer sentinel values (NFC) #146595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clang/lib/AST/APValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,14 @@ APValue::LValueBase::operator bool () const {
clang::APValue::LValueBase
llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
clang::APValue::LValueBase B;
B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
B.Ptr = DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getEmptyKey();
return B;
}

clang::APValue::LValueBase
llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
clang::APValue::LValueBase B;
B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
B.Ptr = DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getTombstoneKey();
return B;
}

Expand Down
25 changes: 4 additions & 21 deletions llvm/include/llvm/ADT/DenseMapInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T*, ...> with forward
// declared key types. Assume that no pointer key type requires more than 4096
// bytes of alignment.
template<typename T>
struct DenseMapInfo<T*> {
// 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 <typename T> struct DenseMapInfo<T *> {
static inline T* getEmptyKey() {
uintptr_t Val = static_cast<uintptr_t>(-1);
Val <<= Log2MaxAlign;
return reinterpret_cast<T*>(Val);
// We assume that raw pointers do not carry alignment requirements.
return reinterpret_cast<T *>(-1);
}

static inline T* getTombstoneKey() {
uintptr_t Val = static_cast<uintptr_t>(-2);
Val <<= Log2MaxAlign;
return reinterpret_cast<T*>(Val);
}
static inline T *getTombstoneKey() { return reinterpret_cast<T *>(-2); }

static unsigned getHashValue(const T *PtrVal) {
return (unsigned((uintptr_t)PtrVal) >> 4) ^
Expand Down
14 changes: 10 additions & 4 deletions llvm/include/llvm/ADT/PointerUnion.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,19 @@ struct PointerLikeTypeTraits<PointerUnion<PTs...>> {
// Teach DenseMap how to use PointerUnions as keys.
template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> {
using Union = PointerUnion<PTs...>;
using FirstInfo =
DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>;
using FirstTypeTraits = PointerLikeTypeTraits<
typename pointer_union_detail::GetFirstType<PTs...>::type>;

static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); }
static inline Union getEmptyKey() {
uintptr_t Val = static_cast<uintptr_t>(-1);
Val <<= FirstTypeTraits::NumLowBitsAvailable;
return FirstTypeTraits::getFromVoidPointer(reinterpret_cast<void *>(Val));
}

static inline Union getTombstoneKey() {
return Union(FirstInfo::getTombstoneKey());
uintptr_t Val = static_cast<uintptr_t>(-2);
Val <<= FirstTypeTraits::NumLowBitsAvailable;
return FirstTypeTraits::getFromVoidPointer(reinterpret_cast<void *>(Val));
}

static unsigned getHashValue(const Union &UnionVal) {
Expand Down
5 changes: 3 additions & 2 deletions mlir/include/mlir/Support/TypeID.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,12 @@ namespace llvm {
template <>
struct DenseMapInfo<mlir::TypeID> {
static inline mlir::TypeID getEmptyKey() {
void *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
// Shift by 3 to satisfy the TypeID alignment requirement.
void *pointer = reinterpret_cast<void *>(uintptr_t(-1) << 3);
return mlir::TypeID::getFromOpaquePointer(pointer);
}
static inline mlir::TypeID getTombstoneKey() {
void *pointer = llvm::DenseMapInfo<void *>::getTombstoneKey();
void *pointer = reinterpret_cast<void *>(uintptr_t(-2) << 3);
return mlir::TypeID::getFromOpaquePointer(pointer);
}
static unsigned getHashValue(mlir::TypeID val) {
Expand Down
5 changes: 3 additions & 2 deletions mlir/lib/Bindings/Python/NanobindUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -408,11 +408,12 @@ namespace llvm {
template <>
struct DenseMapInfo<MlirTypeID> {
static inline MlirTypeID getEmptyKey() {
auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
// Shift by 3 to satisfy the TypeID alignment requirement.
void *pointer = reinterpret_cast<void *>(uintptr_t(-1) << 3);
return mlirTypeIDCreate(pointer);
}
static inline MlirTypeID getTombstoneKey() {
auto *pointer = llvm::DenseMapInfo<void *>::getTombstoneKey();
void *pointer = reinterpret_cast<void *>(uintptr_t(-2) << 3);
return mlirTypeIDCreate(pointer);
}
static inline unsigned getHashValue(const MlirTypeID &val) {
Expand Down
Loading