Skip to content

Commit 8e0d23b

Browse files
fixup! Avoid while (true) during index generation
1 parent ee8b774 commit 8e0d23b

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

llvm/lib/Support/TrieHashIndexGenerator.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,19 @@ struct IndexGenerator {
4545

4646
// Get the index of the object in the next level of trie.
4747
size_t next() {
48-
size_t Index;
4948
if (!StartBit) {
5049
// Compute index for root when StartBit is not set.
5150
StartBit = 0;
52-
Index = getIndex(Bytes, *StartBit, NumRootBits);
53-
} else {
51+
return getIndex(Bytes, *StartBit, NumRootBits);
52+
}
53+
if (*StartBit < Bytes.size() * 8) {
5454
// Compute index for sub-trie.
5555
*StartBit += *StartBit ? NumSubtrieBits : NumRootBits;
5656
assert((*StartBit - NumRootBits) % NumSubtrieBits == 0);
57-
Index = getIndex(Bytes, *StartBit, NumSubtrieBits);
57+
return getIndex(Bytes, *StartBit, NumSubtrieBits);
5858
}
59-
return Index;
59+
// All the bits are consumed.
60+
return end();
6061
}
6162

6263
// Provide a hint to speed up the index generation by providing the
@@ -79,6 +80,8 @@ struct IndexGenerator {
7980
return getIndex(CollidingBits, *StartBit, NumSubtrieBits);
8081
}
8182

83+
size_t end() const { return SIZE_MAX; }
84+
8285
// Compute the index for the object from its hash, current start bits, and
8386
// the number of bits used for current level.
8487
static size_t getIndex(ArrayRef<uint8_t> Bytes, size_t StartBit,

llvm/lib/Support/TrieRawHashMap.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ ThreadSafeTrieRawHashMapBase::find(ArrayRef<uint8_t> Hash) const {
224224
TrieSubtrie *S = &Impl->Root;
225225
IndexGenerator IndexGen{NumRootBits, NumSubtrieBits, Hash};
226226
size_t Index = IndexGen.next();
227-
while (true) {
227+
while (Index != IndexGen.end()) {
228228
// Try to set the content.
229229
TrieNode *Existing = S->get(Index);
230230
if (!Existing)
@@ -239,6 +239,7 @@ ThreadSafeTrieRawHashMapBase::find(ArrayRef<uint8_t> Hash) const {
239239
Index = IndexGen.next();
240240
S = cast<TrieSubtrie>(Existing);
241241
}
242+
llvm_unreachable("failed to locate the node after consuming all hash bytes");
242243
}
243244

244245
ThreadSafeTrieRawHashMapBase::PointerBase ThreadSafeTrieRawHashMapBase::insert(
@@ -258,7 +259,7 @@ ThreadSafeTrieRawHashMapBase::PointerBase ThreadSafeTrieRawHashMapBase::insert(
258259
Index = IndexGen.next();
259260
}
260261

261-
while (true) {
262+
while (Index != IndexGen.end()) {
262263
// Load the node from the slot, allocating and calling the constructor if
263264
// the slot is empty.
264265
bool Generated = false;
@@ -292,8 +293,8 @@ ThreadSafeTrieRawHashMapBase::PointerBase ThreadSafeTrieRawHashMapBase::insert(
292293
return PointerBase(ExistingContent.getValuePointer());
293294

294295
// Sink the existing content as long as the indexes match.
295-
while (true) {
296-
size_t NextIndex = IndexGen.next();
296+
size_t NextIndex = IndexGen.next();
297+
while (NextIndex != IndexGen.end()) {
297298
size_t NewIndexForExistingContent =
298299
IndexGen.getCollidingBits(ExistingContent.getHash());
299300
S = S->sink(Index, ExistingContent, IndexGen.getNumBits(),
@@ -306,8 +307,11 @@ ThreadSafeTrieRawHashMapBase::PointerBase ThreadSafeTrieRawHashMapBase::insert(
306307
// Found the difference.
307308
if (NextIndex != NewIndexForExistingContent)
308309
break;
310+
311+
NextIndex = IndexGen.next();
309312
}
310313
}
314+
llvm_unreachable("failed to insert the node after consuming all hash bytes");
311315
}
312316

313317
ThreadSafeTrieRawHashMapBase::ThreadSafeTrieRawHashMapBase(

0 commit comments

Comments
 (0)