Skip to content

Commit 9ff5005

Browse files
generatedunixname89002005232357facebook-github-bot
generatedunixname89002005232357
authored andcommitted
Revert D61236418
Summary: This diff reverts D61236418 unicorn continuous tests are down after this and asan canaries are crashlooping Differential Revision: D61514775 fbshipit-source-id: 96c61ccc8fd1af1fdc27c28271bb3f2e2e06d6b5
1 parent 96d03f6 commit 9ff5005

File tree

2 files changed

+33
-40
lines changed

2 files changed

+33
-40
lines changed

folly/container/detail/F14Table.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ namespace detail {
3030
// everywhere or disabled everywhere.
3131
void F14LinkCheck<getF14IntrinsicsMode()>::check() noexcept {}
3232

33+
#if FOLLY_F14_VECTOR_INTRINSICS_AVAILABLE
34+
EmptyTagVectorType kEmptyTagVector = {};
35+
#endif
36+
3337
//// Debug and ASAN stuff
3438

3539
bool tlsPendingSafeInserts(std::ptrdiff_t delta) {

folly/container/detail/F14Table.h

+29-40
Original file line numberDiff line numberDiff line change
@@ -474,25 +474,11 @@ using TagVector = __uint128_t;
474474
constexpr std::size_t kRequiredVectorAlignment =
475475
constexpr_max(std::size_t{16}, alignof(max_align_t));
476476

477-
struct alignas(kRequiredVectorAlignment) F14EmptyTagVector {
478-
std::array<uint8_t, sizeof(void*)> tags_{};
479-
FOLLY_CONSTEVAL F14EmptyTagVector() noexcept {
480-
for (auto& t : tags_) {
481-
t = 1;
482-
}
483-
}
484-
};
477+
using EmptyTagVectorType = std::aligned_storage_t<
478+
sizeof(TagVector) + kRequiredVectorAlignment,
479+
alignof(max_align_t)>;
485480

486-
FOLLY_EXPORT inline F14EmptyTagVector& getF14EmptyTagVector() noexcept {
487-
static constexpr F14EmptyTagVector instance;
488-
auto const raw = reinterpret_cast<uintptr_t>(&instance);
489-
FOLLY_SAFE_DCHECK(
490-
(raw % kRequiredVectorAlignment) == 0,
491-
raw,
492-
" not aligned to ",
493-
kRequiredVectorAlignment);
494-
return const_cast<F14EmptyTagVector&>(instance);
495-
}
481+
FOLLY_EXPORT extern EmptyTagVectorType kEmptyTagVector;
496482

497483
template <typename ItemType>
498484
struct alignas(kRequiredVectorAlignment) F14Chunk {
@@ -538,14 +524,20 @@ struct alignas(kRequiredVectorAlignment) F14Chunk {
538524

539525
std::array<aligned_storage_for_t<Item>, kAllocatedCapacity> rawItems_;
540526

541-
FOLLY_EXPORT static F14Chunk* getSomeEmptyInstance() noexcept {
542-
return &reinterpret_cast<F14Chunk&>(getF14EmptyTagVector());
543-
}
544-
545-
bool isEmptyInstance() const noexcept {
546-
F14EmptyTagVector self;
547-
std::memcpy(&self, this, sizeof(self));
548-
return self.tags_ == F14EmptyTagVector{}.tags_;
527+
static F14Chunk* emptyInstance() {
528+
auto raw = reinterpret_cast<char*>(&kEmptyTagVector);
529+
if constexpr (kRequiredVectorAlignment > alignof(max_align_t)) {
530+
auto delta = kRequiredVectorAlignment -
531+
(reinterpret_cast<uintptr_t>(raw) % kRequiredVectorAlignment);
532+
raw += delta;
533+
}
534+
auto rv = reinterpret_cast<F14Chunk*>(raw);
535+
FOLLY_SAFE_DCHECK(
536+
(reinterpret_cast<uintptr_t>(rv) % kRequiredVectorAlignment) == 0,
537+
reinterpret_cast<uintptr_t>(rv),
538+
" not aligned to ",
539+
kRequiredVectorAlignment);
540+
return rv;
549541
}
550542

551543
void clear() {
@@ -585,7 +577,7 @@ struct alignas(kRequiredVectorAlignment) F14Chunk {
585577

586578
void setCapacityScale(std::size_t scale) {
587579
FOLLY_SAFE_DCHECK(
588-
!isEmptyInstance() && scale > 0 &&
580+
this != emptyInstance() && scale > 0 &&
589581
scale < (std::size_t{1} << kCapacityScaleBits),
590582
"");
591583
if constexpr (kCapacityScaleBits == 4) {
@@ -618,7 +610,8 @@ struct alignas(kRequiredVectorAlignment) F14Chunk {
618610
std::size_t tag(std::size_t index) const { return tags_[index]; }
619611

620612
void setTag(std::size_t index, std::size_t tag) {
621-
FOLLY_SAFE_DCHECK(!isEmptyInstance() && tag >= 0x80 && tag <= 0xff, "");
613+
FOLLY_SAFE_DCHECK(
614+
this != emptyInstance() && tag >= 0x80 && tag <= 0xff, "");
622615
FOLLY_SAFE_CHECK(tags_[index] == 0, "");
623616
tags_[index] = static_cast<uint8_t>(tag);
624617
}
@@ -741,9 +734,8 @@ struct alignas(kRequiredVectorAlignment) F14Chunk {
741734
}
742735

743736
bool occupied(std::size_t index) const {
744-
auto const tag = tags_[index];
745-
FOLLY_SAFE_DCHECK((tag & ~1) == 0 || (tag & 0x80) != 0, "");
746-
return to_signed(tag) < 0;
737+
FOLLY_SAFE_DCHECK(tags_[index] == 0 || (tags_[index] & 0x80) != 0, "");
738+
return tags_[index] != 0;
747739
}
748740

749741
Item* itemAddr(std::size_t i) const {
@@ -1243,7 +1235,7 @@ class F14Table : public Policy {
12431235
private:
12441236
//////// begin fields
12451237

1246-
ChunkPtr chunks_{Chunk::getSomeEmptyInstance()};
1238+
ChunkPtr chunks_{Chunk::emptyInstance()};
12471239
SizeAndChunkShiftAndPackedBegin<ItemIter, kEnableItemIteration>
12481240
sizeAndChunkShiftAndPackedBegin_;
12491241

@@ -2060,8 +2052,7 @@ class F14Table : public Policy {
20602052
void initialReserve(std::size_t desiredCapacity) {
20612053
FOLLY_SAFE_DCHECK(size() == 0, "");
20622054
FOLLY_SAFE_DCHECK(chunkShift() == 0, "");
2063-
FOLLY_SAFE_DCHECK(!!chunks_);
2064-
FOLLY_SAFE_DCHECK(chunks_->isEmptyInstance(), "");
2055+
FOLLY_SAFE_DCHECK(chunks_ == Chunk::emptyInstance(), "");
20652056
if (desiredCapacity == 0) {
20662057
return;
20672058
}
@@ -2279,8 +2270,7 @@ class F14Table : public Policy {
22792270
// We want to support the pattern
22802271
// map.reserve(map.size() + 2); auto& r1 = map[k1]; auto& r2 = map[k2];
22812272
debugModeOnReserve(capacity);
2282-
FOLLY_SAFE_DCHECK(!!chunks_);
2283-
if (chunks_->isEmptyInstance()) {
2273+
if (chunks_ == Chunk::emptyInstance()) {
22842274
initialReserve(capacity);
22852275
} else {
22862276
reserveImpl(capacity);
@@ -2364,8 +2354,7 @@ class F14Table : public Policy {
23642354
private:
23652355
template <bool Reset>
23662356
void clearImpl() noexcept {
2367-
FOLLY_SAFE_DCHECK(!!chunks_);
2368-
if (chunks_->isEmptyInstance()) {
2357+
if (chunks_ == Chunk::emptyInstance()) {
23692358
FOLLY_SAFE_DCHECK(empty() && bucket_count() == 0, "");
23702359
return;
23712360
}
@@ -2419,7 +2408,7 @@ class F14Table : public Policy {
24192408
std::size_t rawSize =
24202409
chunkAllocSize(chunkCount(), chunks_->capacityScale());
24212410

2422-
chunks_ = Chunk::getSomeEmptyInstance();
2411+
chunks_ = Chunk::emptyInstance();
24232412
sizeAndChunkShiftAndPackedBegin_.setChunkCount(1);
24242413

24252414
this->afterReset(origSize, origCapacity, rawAllocation, rawSize);
@@ -2577,7 +2566,7 @@ class F14Table : public Policy {
25772566
}
25782567

25792568
FOLLY_SAFE_DCHECK(
2580-
(chunks_->isEmptyInstance()) == (bucket_count() == 0), "");
2569+
(chunks_ == Chunk::emptyInstance()) == (bucket_count() == 0), "");
25812570

25822571
std::size_t n1 = 0;
25832572
std::size_t n2 = 0;

0 commit comments

Comments
 (0)