Skip to content

Commit cdf3234

Browse files
authored
[AutoDiff] Refactor 'AutoDiffIndexSubset' to take a 'SmallBitVector' in the base factory method. (#25058)
Change the base implementation of `AutoDiffIndexSubset::get` to take a `const SmallBitVector &` instead of an `ArrayRef<unsigned>` because bit vectors are far more efficient.
1 parent 4ebb027 commit cdf3234

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

include/swift/AST/AutoDiff.h

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
276276
return {const_cast<BitWord *>(getBitWordsData()), getNumBitWords()};
277277
}
278278

279-
explicit AutoDiffIndexSubset(unsigned capacity, ArrayRef<unsigned> indices)
280-
: capacity(capacity),
279+
explicit AutoDiffIndexSubset(const SmallBitVector &indices)
280+
: capacity((unsigned)indices.size()),
281281
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
282282
std::uninitialized_fill_n(getBitWordsData(), numBitWords, 0);
283-
for (auto i : indices) {
283+
for (auto i : indices.set_bits()) {
284284
unsigned bitWordIndex, offset;
285285
std::tie(bitWordIndex, offset) = getBitWordIndexAndOffset(i);
286286
getBitWord(bitWordIndex) |= (1 << offset);
@@ -294,22 +294,28 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
294294

295295
// Defined in ASTContext.h.
296296
static AutoDiffIndexSubset *get(ASTContext &ctx,
297-
unsigned capacity,
298-
ArrayRef<unsigned> indices);
297+
const SmallBitVector &indices);
299298

300-
static AutoDiffIndexSubset *getDefault(ASTContext &ctx,
301-
unsigned capacity,
299+
static AutoDiffIndexSubset *get(ASTContext &ctx, unsigned capacity,
300+
ArrayRef<unsigned> indices) {
301+
SmallBitVector indicesBitVec(capacity, false);
302+
for (auto index : indices)
303+
indicesBitVec.set(index);
304+
return AutoDiffIndexSubset::get(ctx, indicesBitVec);
305+
}
306+
307+
static AutoDiffIndexSubset *getDefault(ASTContext &ctx, unsigned capacity,
302308
bool includeAll = false) {
303-
if (includeAll)
304-
return getFromRange(ctx, capacity, IntRange<>(capacity));
305-
return get(ctx, capacity, {});
309+
return get(ctx, SmallBitVector(capacity, includeAll));
306310
}
307311

308-
static AutoDiffIndexSubset *getFromRange(ASTContext &ctx,
309-
unsigned capacity,
310-
IntRange<> range) {
311-
return get(ctx, capacity,
312-
SmallVector<unsigned, 8>(range.begin(), range.end()));
312+
static AutoDiffIndexSubset *getFromRange(ASTContext &ctx, unsigned capacity,
313+
unsigned start, unsigned end) {
314+
assert(start < capacity);
315+
assert(end <= capacity);
316+
SmallBitVector bitVec(capacity);
317+
bitVec.set(start, end);
318+
return get(ctx, bitVec);
313319
}
314320

315321
unsigned getNumBitWords() const {

lib/AST/ASTContext.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4521,21 +4521,13 @@ AutoDiffParameterIndices::get(llvm::SmallBitVector indices, ASTContext &C) {
45214521
}
45224522

45234523
AutoDiffIndexSubset *
4524-
AutoDiffIndexSubset::get(ASTContext &ctx, unsigned capacity,
4525-
ArrayRef<unsigned> indices) {
4524+
AutoDiffIndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
45264525
auto &foldingSet = ctx.getImpl().AutoDiffIndexSubsets;
45274526
llvm::FoldingSetNodeID id;
4527+
unsigned capacity = indices.size();
45284528
id.AddInteger(capacity);
4529-
#ifndef NDEBUG
4530-
int last = -1;
4531-
#endif
4532-
for (unsigned index : indices) {
4533-
#ifndef NDEBUG
4534-
assert((int)index > last && "Indices must be ascending");
4535-
last = (int)index;
4536-
#endif
4529+
for (unsigned index : indices.set_bits())
45374530
id.AddInteger(index);
4538-
}
45394531
void *insertPos = nullptr;
45404532
auto *existing = foldingSet.FindNodeOrInsertPos(id, insertPos);
45414533
if (existing)
@@ -4544,7 +4536,7 @@ AutoDiffIndexSubset::get(ASTContext &ctx, unsigned capacity,
45444536
getNumBitWordsNeededForCapacity(capacity);
45454537
auto *buf = reinterpret_cast<AutoDiffIndexSubset *>(
45464538
ctx.Allocate(sizeToAlloc, alignof(AutoDiffIndexSubset)));
4547-
auto *newNode = new (buf) AutoDiffIndexSubset(capacity, indices);
4539+
auto *newNode = new (buf) AutoDiffIndexSubset(indices);
45484540
foldingSet.InsertNode(newNode, insertPos);
45494541
return newNode;
45504542
}

lib/AST/AutoDiff.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,28 +385,27 @@ AutoDiffIndexSubset *AutoDiffIndexSubset::adding(unsigned index,
385385
assert(index < getCapacity());
386386
if (contains(index))
387387
return const_cast<AutoDiffIndexSubset *>(this);
388-
SmallVector<unsigned, 8> newIndices;
389-
newIndices.reserve(capacity + 1);
388+
SmallBitVector newIndices(capacity);
390389
bool inserted = false;
391390
for (auto curIndex : getIndices()) {
392391
if (!inserted && curIndex > index) {
393-
newIndices.push_back(index);
392+
newIndices.set(index);
394393
inserted = true;
395394
}
396-
newIndices.push_back(curIndex);
395+
newIndices.set(curIndex);
397396
}
398-
return get(ctx, capacity, newIndices);
397+
return get(ctx, newIndices);
399398
}
400399

401400
AutoDiffIndexSubset *AutoDiffIndexSubset::extendingCapacity(
402401
ASTContext &ctx, unsigned newCapacity) const {
403402
assert(newCapacity >= capacity);
404403
if (newCapacity == capacity)
405404
return const_cast<AutoDiffIndexSubset *>(this);
406-
SmallVector<unsigned, 8> indices;
405+
SmallBitVector indices(newCapacity);
407406
for (auto index : getIndices())
408-
indices.push_back(index);
409-
return AutoDiffIndexSubset::get(ctx, newCapacity, indices);
407+
indices.set(index);
408+
return AutoDiffIndexSubset::get(ctx, indices);
410409
}
411410

412411
int AutoDiffIndexSubset::findNext(int startIndex) const {

0 commit comments

Comments
 (0)