Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit c5c615c

Browse files
authored
[DenseMap] Do not align pointer sentinel values (NFC) (#146595)
DenseMapInfo for pointers currently uses empty/tombstone values that are aligned (by assuming a very conservative alignment). However, this means that we have to work with larger immediates. This patch proposes to use the values -1 and -2 instead, without caring about pointer alignment. (Non-roundtrip) integer to pointer casts are implementation-defined in C++, but the general implementer consensus (including Clang) is that raw pointers do not carry alignment requirements, only memory accesses do. We already have lots of places that rely on this using variations on `reinterpret_cast<T*>(-1)`, so it seems odd to insist on properly aligned pointers in this one place. It is necessary to adjust a few other places after this change, which currently assume that `DenseMapInfo<void *>` returns a highly-aligned pointer. This is a small improvement for both compile-time and clang binary size.
1 parent 364c830 commit c5c615c

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

NanobindUtils.h

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

0 commit comments

Comments
 (0)