Skip to content

Commit 9765858

Browse files
committed
Rename to 'IndexSubset' and move it to its own file.
1 parent fd3a880 commit 9765858

File tree

8 files changed

+394
-285
lines changed

8 files changed

+394
-285
lines changed

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ namespace swift {
109109
class TypeAliasDecl;
110110
class VarDecl;
111111
class UnifiedStatsReporter;
112-
class AutoDiffIndexSubset;
112+
class IndexSubset;
113113

114114
enum class KnownProtocolKind : uint8_t;
115115

include/swift/AST/AutoDiff.h renamed to include/swift/AST/IndexSubset.h

Lines changed: 49 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===--- AutoDiff.h - Swift Differentiable Programming --------------------===//
1+
//===---------- IndexSubset.h - Fixed-size subset of indices --------------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
@@ -10,24 +10,28 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212
//
13-
// This file defines AST support for the experimental differentiable
14-
// programming feature.
13+
// This file defines the `IndexSubset` class and support logic.
1514
//
1615
//===----------------------------------------------------------------------===//
1716

17+
#ifndef SWIFT_AST_INDEXSUBSET_H
18+
#define SWIFT_AST_INDEXSUBSET_H
1819

19-
#ifndef SWIFT_AST_AUTODIFF_H
20-
#define SWIFT_AST_AUTODIFF_H
21-
22-
#include "ASTContext.h"
23-
#include "llvm/ADT/SmallBitVector.h"
20+
#include "swift/Basic/LLVM.h"
2421
#include "swift/Basic/Range.h"
22+
#include "swift/Basic/STLExtras.h"
23+
#include "llvm/ADT/ArrayRef.h"
24+
#include "llvm/ADT/FoldingSet.h"
25+
#include "llvm/ADT/SmallBitVector.h"
26+
#include "llvm/Support/raw_ostream.h"
2527

2628
namespace swift {
2729

30+
class ASTContext;
31+
2832
/// An efficient index subset data structure, uniqued in `ASTContext`.
2933
/// Stores a bit vector representing set indices and a total capacity.
30-
class AutoDiffIndexSubset : public llvm::FoldingSetNode {
34+
class IndexSubset : public llvm::FoldingSetNode {
3135
public:
3236
typedef uint64_t BitWord;
3337

@@ -45,7 +49,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
4549
if (capacity == 0) return 0;
4650
return capacity / numBitsPerBitWord + 1;
4751
}
48-
52+
4953
private:
5054
/// The total capacity of the index subset, which is `1` less than the largest
5155
/// index.
@@ -77,7 +81,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
7781
return {const_cast<BitWord *>(getBitWordsData()), getNumBitWords()};
7882
}
7983

80-
explicit AutoDiffIndexSubset(const SmallBitVector &indices)
84+
explicit IndexSubset(const SmallBitVector &indices)
8185
: capacity((unsigned)indices.size()),
8286
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
8387
std::uninitialized_fill_n(getBitWordsData(), numBitWords, 0);
@@ -89,39 +93,38 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
8993
}
9094

9195
public:
92-
AutoDiffIndexSubset() = delete;
93-
AutoDiffIndexSubset(const AutoDiffIndexSubset &) = delete;
94-
AutoDiffIndexSubset &operator=(const AutoDiffIndexSubset &) = delete;
96+
IndexSubset() = delete;
97+
IndexSubset(const IndexSubset &) = delete;
98+
IndexSubset &operator=(const IndexSubset &) = delete;
9599

96100
// Defined in ASTContext.cpp.
97-
static AutoDiffIndexSubset *get(ASTContext &ctx,
98-
const SmallBitVector &indices);
101+
static IndexSubset *get(ASTContext &ctx, const SmallBitVector &indices);
99102

100-
static AutoDiffIndexSubset *get(ASTContext &ctx, unsigned capacity,
101-
ArrayRef<unsigned> indices) {
103+
static IndexSubset *get(ASTContext &ctx, unsigned capacity,
104+
ArrayRef<unsigned> indices) {
102105
SmallBitVector indicesBitVec(capacity, false);
103106
for (auto index : indices)
104107
indicesBitVec.set(index);
105-
return AutoDiffIndexSubset::get(ctx, indicesBitVec);
108+
return IndexSubset::get(ctx, indicesBitVec);
106109
}
107110

108-
static AutoDiffIndexSubset *getDefault(ASTContext &ctx, unsigned capacity,
109-
bool includeAll = false) {
111+
static IndexSubset *getDefault(ASTContext &ctx, unsigned capacity,
112+
bool includeAll = false) {
110113
return get(ctx, SmallBitVector(capacity, includeAll));
111114
}
112115

113-
static AutoDiffIndexSubset *getFromRange(ASTContext &ctx, unsigned capacity,
114-
unsigned start, unsigned end) {
116+
static IndexSubset *getFromRange(ASTContext &ctx, unsigned capacity,
117+
unsigned start, unsigned end) {
115118
assert(start < capacity);
116119
assert(end <= capacity);
117120
SmallBitVector bitVec(capacity);
118121
bitVec.set(start, end);
119122
return get(ctx, bitVec);
120123
}
121-
124+
122125
/// Creates an index subset corresponding to the given string generated by
123126
/// `getString()`. If the string is invalid, returns nullptr.
124-
static AutoDiffIndexSubset *getFromString(ASTContext &ctx, StringRef string);
127+
static IndexSubset *getFromString(ASTContext &ctx, StringRef string);
125128

126129
/// Returns the number of bit words used to store the index subset.
127130
// Note: Use `getCapacity()` to get the total index subset capacity.
@@ -148,11 +151,11 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
148151
iterator begin() const {
149152
return iterator(this);
150153
}
151-
154+
152155
iterator end() const {
153156
return iterator(this, (int)capacity);
154157
}
155-
158+
156159
/// Returns an iterator range of indices in the index subset.
157160
iterator_range<iterator> getIndices() const {
158161
return make_range(begin(), end());
@@ -162,7 +165,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
162165
unsigned getNumIndices() const {
163166
return (unsigned)std::distance(begin(), end());
164167
}
165-
168+
166169
SmallBitVector getBitVector() const {
167170
SmallBitVector indicesBitVec(capacity, false);
168171
for (auto index : getIndices())
@@ -179,18 +182,18 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
179182
bool isEmpty() const {
180183
return llvm::all_of(getBitWords(), [](BitWord bw) { return !(bool)bw; });
181184
}
182-
183-
bool equals(AutoDiffIndexSubset *other) const {
185+
186+
bool equals(IndexSubset *other) const {
184187
return capacity == other->getCapacity() &&
185-
getBitWords().equals(other->getBitWords());
188+
getBitWords().equals(other->getBitWords());
186189
}
187190

188-
bool isSubsetOf(AutoDiffIndexSubset *other) const;
189-
bool isSupersetOf(AutoDiffIndexSubset *other) const;
191+
bool isSubsetOf(IndexSubset *other) const;
192+
bool isSupersetOf(IndexSubset *other) const;
190193

191-
AutoDiffIndexSubset *adding(unsigned index, ASTContext &ctx) const;
192-
AutoDiffIndexSubset *extendingCapacity(ASTContext &ctx,
193-
unsigned newCapacity) const;
194+
IndexSubset *adding(unsigned index, ASTContext &ctx) const;
195+
IndexSubset *extendingCapacity(ASTContext &ctx,
196+
unsigned newCapacity) const;
194197

195198
void Profile(llvm::FoldingSetNodeID &id) const {
196199
id.AddInteger(capacity);
@@ -206,7 +209,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
206209
}
207210

208211
void dump(llvm::raw_ostream &s = llvm::errs()) const {
209-
s << "(autodiff_index_subset capacity=" << capacity << " indices=(";
212+
s << "(index_subset capacity=" << capacity << " indices=(";
210213
interleave(getIndices(), [&s](unsigned i) { s << i; },
211214
[&s] { s << ", "; });
212215
s << "))";
@@ -226,7 +229,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
226229
typedef std::forward_iterator_tag iterator_category;
227230

228231
private:
229-
const AutoDiffIndexSubset *parent;
232+
const IndexSubset *parent;
230233
int current = 0;
231234

232235
void advance() {
@@ -235,10 +238,10 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
235238
}
236239

237240
public:
238-
iterator(const AutoDiffIndexSubset *parent, int current)
239-
: parent(parent), current(current) {}
240-
explicit iterator(const AutoDiffIndexSubset *parent)
241-
: iterator(parent, parent->findFirst()) {}
241+
iterator(const IndexSubset *parent, int current)
242+
: parent(parent), current(current) {}
243+
explicit iterator(const IndexSubset *parent)
244+
: iterator(parent, parent->findFirst()) {}
242245
iterator(const iterator &) = default;
243246

244247
iterator operator++(int) {
@@ -256,18 +259,18 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
256259

257260
bool operator==(const iterator &other) const {
258261
assert(parent == other.parent &&
259-
"Comparing iterators from different AutoDiffIndexSubsets");
262+
"Comparing iterators from different IndexSubsets");
260263
return current == other.current;
261264
}
262265

263266
bool operator!=(const iterator &other) const {
264267
assert(parent == other.parent &&
265-
"Comparing iterators from different AutoDiffIndexSubsets");
268+
"Comparing iterators from different IndexSubsets");
266269
return current != other.current;
267270
}
268271
};
269272
};
270273

271-
} // end namespace swift
274+
}
272275

273-
#endif // SWIFT_AST_AUTODIFF_H
276+
#endif // SWIFT_AST_INDEXSUBSET_H

lib/AST/ASTContext.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -428,8 +428,8 @@ FOR_KNOWN_FOUNDATION_TYPES(CACHE_FOUNDATION_DECL)
428428
llvm::FoldingSet<DeclName::CompoundDeclName> CompoundNames;
429429
llvm::DenseMap<UUID, OpenedArchetypeType *> OpenedExistentialArchetypes;
430430

431-
/// For uniquifying `AutoDiffIndexSubset` allocations.
432-
llvm::FoldingSet<AutoDiffIndexSubset> AutoDiffIndexSubsets;
431+
/// For uniquifying `IndexSubset` allocations.
432+
llvm::FoldingSet<IndexSubset> IndexSubsets;
433433

434434
/// A cache of information about whether particular nominal types
435435
/// are representable in a foreign language.
@@ -4621,9 +4621,9 @@ void VarDecl::setOriginalWrappedProperty(VarDecl *originalProperty) {
46214621
ctx.getImpl().OriginalWrappedProperties[this] = originalProperty;
46224622
}
46234623

4624-
AutoDiffIndexSubset *
4625-
AutoDiffIndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
4626-
auto &foldingSet = ctx.getImpl().AutoDiffIndexSubsets;
4624+
IndexSubset *
4625+
IndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
4626+
auto &foldingSet = ctx.getImpl().IndexSubsets;
46274627
llvm::FoldingSetNodeID id;
46284628
unsigned capacity = indices.size();
46294629
id.AddInteger(capacity);
@@ -4633,11 +4633,11 @@ AutoDiffIndexSubset::get(ASTContext &ctx, const SmallBitVector &indices) {
46334633
auto *existing = foldingSet.FindNodeOrInsertPos(id, insertPos);
46344634
if (existing)
46354635
return existing;
4636-
auto sizeToAlloc = sizeof(AutoDiffIndexSubset) +
4636+
auto sizeToAlloc = sizeof(IndexSubset) +
46374637
getNumBitWordsNeededForCapacity(capacity);
4638-
auto *buf = reinterpret_cast<AutoDiffIndexSubset *>(
4639-
ctx.Allocate(sizeToAlloc, alignof(AutoDiffIndexSubset)));
4640-
auto *newNode = new (buf) AutoDiffIndexSubset(indices);
4638+
auto *buf = reinterpret_cast<IndexSubset *>(
4639+
ctx.Allocate(sizeToAlloc, alignof(IndexSubset)));
4640+
auto *newNode = new (buf) IndexSubset(indices);
46414641
foldingSet.InsertNode(newNode, insertPos);
46424642
return newNode;
46434643
}

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ add_swift_host_library(swiftAST STATIC
2929
ASTVerifier.cpp
3030
ASTWalker.cpp
3131
Attr.cpp
32-
AutoDiff.cpp
32+
IndexSubset.cpp
3333
Availability.cpp
3434
AvailabilitySpec.cpp
3535
Builtins.cpp

lib/AST/AutoDiff.cpp renamed to lib/AST/IndexSubset.cpp

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,21 @@
1-
//===--- AutoDiff.cpp - Swift Differentiable Programming ------------------===//
1+
//===-------- IndexSubset.cpp - Swift Differentiable Programming ----------===//
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#include "swift/AST/AutoDiff.h"
14-
#include "swift/AST/Module.h"
15-
#include "swift/AST/Types.h"
16-
#include "swift/SIL/SILLinkage.h"
17-
#include "swift/Basic/LLVM.h"
18-
#include "swift/Basic/Range.h"
19-
#include "llvm/ADT/STLExtras.h"
20-
#include "llvm/ADT/StringSwitch.h"
13+
#include "swift/AST/IndexSubset.h"
2114

2215
using namespace swift;
2316

24-
AutoDiffIndexSubset *
25-
AutoDiffIndexSubset::getFromString(ASTContext &ctx, StringRef string) {
17+
IndexSubset *
18+
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
2619
if (string.size() < 0) return nullptr;
2720
unsigned capacity = string.size();
2821
llvm::SmallBitVector indices(capacity);
@@ -35,35 +28,34 @@ AutoDiffIndexSubset::getFromString(ASTContext &ctx, StringRef string) {
3528
return get(ctx, indices);
3629
}
3730

38-
std::string AutoDiffIndexSubset::getString() const {
31+
std::string IndexSubset::getString() const {
3932
std::string result;
4033
result.reserve(capacity);
4134
for (unsigned i : range(capacity))
4235
result += contains(i) ? 'S' : 'U';
4336
return result;
4437
}
4538

46-
bool AutoDiffIndexSubset::isSubsetOf(AutoDiffIndexSubset *other) const {
39+
bool IndexSubset::isSubsetOf(IndexSubset *other) const {
4740
assert(capacity == other->capacity);
4841
for (auto index : range(numBitWords))
4942
if (getBitWord(index) & ~other->getBitWord(index))
5043
return false;
5144
return true;
5245
}
5346

54-
bool AutoDiffIndexSubset::isSupersetOf(AutoDiffIndexSubset *other) const {
47+
bool IndexSubset::isSupersetOf(IndexSubset *other) const {
5548
assert(capacity == other->capacity);
5649
for (auto index : range(numBitWords))
5750
if (~getBitWord(index) & other->getBitWord(index))
5851
return false;
5952
return true;
6053
}
6154

62-
AutoDiffIndexSubset *AutoDiffIndexSubset::adding(unsigned index,
63-
ASTContext &ctx) const {
55+
IndexSubset *IndexSubset::adding(unsigned index, ASTContext &ctx) const {
6456
assert(index < getCapacity());
6557
if (contains(index))
66-
return const_cast<AutoDiffIndexSubset *>(this);
58+
return const_cast<IndexSubset *>(this);
6759
SmallBitVector newIndices(capacity);
6860
bool inserted = false;
6961
for (auto curIndex : getIndices()) {
@@ -76,18 +68,18 @@ AutoDiffIndexSubset *AutoDiffIndexSubset::adding(unsigned index,
7668
return get(ctx, newIndices);
7769
}
7870

79-
AutoDiffIndexSubset *AutoDiffIndexSubset::extendingCapacity(
71+
IndexSubset *IndexSubset::extendingCapacity(
8072
ASTContext &ctx, unsigned newCapacity) const {
8173
assert(newCapacity >= capacity);
8274
if (newCapacity == capacity)
83-
return const_cast<AutoDiffIndexSubset *>(this);
75+
return const_cast<IndexSubset *>(this);
8476
SmallBitVector indices(newCapacity);
8577
for (auto index : getIndices())
8678
indices.set(index);
87-
return AutoDiffIndexSubset::get(ctx, indices);
79+
return IndexSubset::get(ctx, indices);
8880
}
8981

90-
int AutoDiffIndexSubset::findNext(int startIndex) const {
82+
int IndexSubset::findNext(int startIndex) const {
9183
assert(startIndex < (int)capacity && "Start index cannot be past the end");
9284
unsigned bitWordIndex = 0, offset = 0;
9385
if (startIndex >= 0) {
@@ -110,7 +102,7 @@ int AutoDiffIndexSubset::findNext(int startIndex) const {
110102
return capacity;
111103
}
112104

113-
int AutoDiffIndexSubset::findPrevious(int endIndex) const {
105+
int IndexSubset::findPrevious(int endIndex) const {
114106
assert(endIndex >= 0 && "End index cannot be before the start");
115107
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
116108
if (endIndex < (int)capacity) {

0 commit comments

Comments
 (0)