Skip to content

Commit c3c3919

Browse files
committed
Revert "[DenseMap] Do not align pointer sentinel values (NFC) (#146595)"
This reverts commit 7a6435b. This causes ubsan failures when the sentinel pointers are upcast using static_cast<>, which checks alignment.
1 parent 6063031 commit c3c3919

File tree

5 files changed

+31
-22
lines changed

5 files changed

+31
-22
lines changed

clang/lib/AST/APValue.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,14 @@ APValue::LValueBase::operator bool () const {
187187
clang::APValue::LValueBase
188188
llvm::DenseMapInfo<clang::APValue::LValueBase>::getEmptyKey() {
189189
clang::APValue::LValueBase B;
190-
B.Ptr = DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getEmptyKey();
190+
B.Ptr = DenseMapInfo<const ValueDecl*>::getEmptyKey();
191191
return B;
192192
}
193193

194194
clang::APValue::LValueBase
195195
llvm::DenseMapInfo<clang::APValue::LValueBase>::getTombstoneKey() {
196196
clang::APValue::LValueBase B;
197-
B.Ptr = DenseMapInfo<clang::APValue::LValueBase::PtrTy>::getTombstoneKey();
197+
B.Ptr = DenseMapInfo<const ValueDecl*>::getTombstoneKey();
198198
return B;
199199
}
200200

llvm/include/llvm/ADT/DenseMapInfo.h

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,30 @@ struct DenseMapInfo {
5656
//static bool isEqual(const T &LHS, const T &RHS);
5757
};
5858

59-
template <typename T> struct DenseMapInfo<T *> {
59+
// Provide DenseMapInfo for all pointers. Come up with sentinel pointer values
60+
// that are aligned to alignof(T) bytes, but try to avoid requiring T to be
61+
// complete. This allows clients to instantiate DenseMap<T*, ...> with forward
62+
// declared key types. Assume that no pointer key type requires more than 4096
63+
// bytes of alignment.
64+
template<typename T>
65+
struct DenseMapInfo<T*> {
66+
// The following should hold, but it would require T to be complete:
67+
// static_assert(alignof(T) <= (1 << Log2MaxAlign),
68+
// "DenseMap does not support pointer keys requiring more than "
69+
// "Log2MaxAlign bits of alignment");
70+
static constexpr uintptr_t Log2MaxAlign = 12;
71+
6072
static inline T* getEmptyKey() {
61-
// We assume that raw pointers do not carry alignment requirements.
62-
return reinterpret_cast<T *>(-1);
73+
uintptr_t Val = static_cast<uintptr_t>(-1);
74+
Val <<= Log2MaxAlign;
75+
return reinterpret_cast<T*>(Val);
6376
}
6477

65-
static inline T *getTombstoneKey() { return reinterpret_cast<T *>(-2); }
78+
static inline T* getTombstoneKey() {
79+
uintptr_t Val = static_cast<uintptr_t>(-2);
80+
Val <<= Log2MaxAlign;
81+
return reinterpret_cast<T*>(Val);
82+
}
6683

6784
static unsigned getHashValue(const T *PtrVal) {
6885
return (unsigned((uintptr_t)PtrVal) >> 4) ^

llvm/include/llvm/ADT/PointerUnion.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,19 +286,13 @@ struct PointerLikeTypeTraits<PointerUnion<PTs...>> {
286286
// Teach DenseMap how to use PointerUnions as keys.
287287
template <typename ...PTs> struct DenseMapInfo<PointerUnion<PTs...>> {
288288
using Union = PointerUnion<PTs...>;
289-
using FirstTypeTraits = PointerLikeTypeTraits<
290-
typename pointer_union_detail::GetFirstType<PTs...>::type>;
289+
using FirstInfo =
290+
DenseMapInfo<typename pointer_union_detail::GetFirstType<PTs...>::type>;
291291

292-
static inline Union getEmptyKey() {
293-
uintptr_t Val = static_cast<uintptr_t>(-1);
294-
Val <<= FirstTypeTraits::NumLowBitsAvailable;
295-
return FirstTypeTraits::getFromVoidPointer(reinterpret_cast<void *>(Val));
296-
}
292+
static inline Union getEmptyKey() { return Union(FirstInfo::getEmptyKey()); }
297293

298294
static inline Union getTombstoneKey() {
299-
uintptr_t Val = static_cast<uintptr_t>(-2);
300-
Val <<= FirstTypeTraits::NumLowBitsAvailable;
301-
return FirstTypeTraits::getFromVoidPointer(reinterpret_cast<void *>(Val));
295+
return Union(FirstInfo::getTombstoneKey());
302296
}
303297

304298
static unsigned getHashValue(const Union &UnionVal) {

mlir/include/mlir/Support/TypeID.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,11 @@ namespace llvm {
395395
template <>
396396
struct DenseMapInfo<mlir::TypeID> {
397397
static inline mlir::TypeID getEmptyKey() {
398-
// Shift by 3 to satisfy the TypeID alignment requirement.
399-
void *pointer = reinterpret_cast<void *>(uintptr_t(-1) << 3);
398+
void *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
400399
return mlir::TypeID::getFromOpaquePointer(pointer);
401400
}
402401
static inline mlir::TypeID getTombstoneKey() {
403-
void *pointer = reinterpret_cast<void *>(uintptr_t(-2) << 3);
402+
void *pointer = llvm::DenseMapInfo<void *>::getTombstoneKey();
404403
return mlir::TypeID::getFromOpaquePointer(pointer);
405404
}
406405
static unsigned getHashValue(mlir::TypeID val) {

mlir/lib/Bindings/Python/NanobindUtils.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,12 +408,11 @@ namespace llvm {
408408
template <>
409409
struct DenseMapInfo<MlirTypeID> {
410410
static inline MlirTypeID getEmptyKey() {
411-
// Shift by 3 to satisfy the TypeID alignment requirement.
412-
void *pointer = reinterpret_cast<void *>(uintptr_t(-1) << 3);
411+
auto *pointer = llvm::DenseMapInfo<void *>::getEmptyKey();
413412
return mlirTypeIDCreate(pointer);
414413
}
415414
static inline MlirTypeID getTombstoneKey() {
416-
void *pointer = reinterpret_cast<void *>(uintptr_t(-2) << 3);
415+
auto *pointer = llvm::DenseMapInfo<void *>::getTombstoneKey();
417416
return mlirTypeIDCreate(pointer);
418417
}
419418
static inline unsigned getHashValue(const MlirTypeID &val) {

0 commit comments

Comments
 (0)