1
- // ===--- AutoDiff .h - Swift Differentiable Programming ------ --------------===//
1
+ // ===---------- IndexSubset .h - Fixed-size subset of indices --------------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
10
10
//
11
11
// ===----------------------------------------------------------------------===//
12
12
//
13
- // This file defines AST support for the experimental differentiable
14
- // programming feature.
13
+ // This file defines the `IndexSubset` class and support logic.
15
14
//
16
15
// ===----------------------------------------------------------------------===//
17
16
17
+ #ifndef SWIFT_AST_INDEXSUBSET_H
18
+ #define SWIFT_AST_INDEXSUBSET_H
18
19
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"
24
21
#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"
25
27
26
28
namespace swift {
27
29
30
+ class ASTContext ;
31
+
28
32
// / An efficient index subset data structure, uniqued in `ASTContext`.
29
33
// / Stores a bit vector representing set indices and a total capacity.
30
- class AutoDiffIndexSubset : public llvm ::FoldingSetNode {
34
+ class IndexSubset : public llvm ::FoldingSetNode {
31
35
public:
32
36
typedef uint64_t BitWord;
33
37
@@ -45,7 +49,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
45
49
if (capacity == 0 ) return 0 ;
46
50
return capacity / numBitsPerBitWord + 1 ;
47
51
}
48
-
52
+
49
53
private:
50
54
// / The total capacity of the index subset, which is `1` less than the largest
51
55
// / index.
@@ -77,7 +81,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
77
81
return {const_cast <BitWord *>(getBitWordsData ()), getNumBitWords ()};
78
82
}
79
83
80
- explicit AutoDiffIndexSubset (const SmallBitVector &indices)
84
+ explicit IndexSubset (const SmallBitVector &indices)
81
85
: capacity((unsigned )indices.size()),
82
86
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
83
87
std::uninitialized_fill_n (getBitWordsData (), numBitWords, 0 );
@@ -89,39 +93,38 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
89
93
}
90
94
91
95
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 ;
95
99
96
100
// Defined in ASTContext.cpp.
97
- static AutoDiffIndexSubset *get (ASTContext &ctx,
98
- const SmallBitVector &indices);
101
+ static IndexSubset *get (ASTContext &ctx, const SmallBitVector &indices);
99
102
100
- static AutoDiffIndexSubset *get (ASTContext &ctx, unsigned capacity,
101
- ArrayRef<unsigned > indices) {
103
+ static IndexSubset *get (ASTContext &ctx, unsigned capacity,
104
+ ArrayRef<unsigned > indices) {
102
105
SmallBitVector indicesBitVec (capacity, false );
103
106
for (auto index : indices)
104
107
indicesBitVec.set (index);
105
- return AutoDiffIndexSubset ::get (ctx, indicesBitVec);
108
+ return IndexSubset ::get (ctx, indicesBitVec);
106
109
}
107
110
108
- static AutoDiffIndexSubset *getDefault (ASTContext &ctx, unsigned capacity,
109
- bool includeAll = false ) {
111
+ static IndexSubset *getDefault (ASTContext &ctx, unsigned capacity,
112
+ bool includeAll = false ) {
110
113
return get (ctx, SmallBitVector (capacity, includeAll));
111
114
}
112
115
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) {
115
118
assert (start < capacity);
116
119
assert (end <= capacity);
117
120
SmallBitVector bitVec (capacity);
118
121
bitVec.set (start, end);
119
122
return get (ctx, bitVec);
120
123
}
121
-
124
+
122
125
// / Creates an index subset corresponding to the given string generated by
123
126
// / `getString()`. If the string is invalid, returns nullptr.
124
- static AutoDiffIndexSubset *getFromString (ASTContext &ctx, StringRef string);
127
+ static IndexSubset *getFromString (ASTContext &ctx, StringRef string);
125
128
126
129
// / Returns the number of bit words used to store the index subset.
127
130
// Note: Use `getCapacity()` to get the total index subset capacity.
@@ -148,11 +151,11 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
148
151
iterator begin () const {
149
152
return iterator (this );
150
153
}
151
-
154
+
152
155
iterator end () const {
153
156
return iterator (this , (int )capacity);
154
157
}
155
-
158
+
156
159
// / Returns an iterator range of indices in the index subset.
157
160
iterator_range<iterator> getIndices () const {
158
161
return make_range (begin (), end ());
@@ -162,7 +165,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
162
165
unsigned getNumIndices () const {
163
166
return (unsigned )std::distance (begin (), end ());
164
167
}
165
-
168
+
166
169
SmallBitVector getBitVector () const {
167
170
SmallBitVector indicesBitVec (capacity, false );
168
171
for (auto index : getIndices ())
@@ -179,18 +182,18 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
179
182
bool isEmpty () const {
180
183
return llvm::all_of (getBitWords (), [](BitWord bw) { return !(bool )bw; });
181
184
}
182
-
183
- bool equals (AutoDiffIndexSubset *other) const {
185
+
186
+ bool equals (IndexSubset *other) const {
184
187
return capacity == other->getCapacity () &&
185
- getBitWords ().equals (other->getBitWords ());
188
+ getBitWords ().equals (other->getBitWords ());
186
189
}
187
190
188
- bool isSubsetOf (AutoDiffIndexSubset *other) const ;
189
- bool isSupersetOf (AutoDiffIndexSubset *other) const ;
191
+ bool isSubsetOf (IndexSubset *other) const ;
192
+ bool isSupersetOf (IndexSubset *other) const ;
190
193
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 ;
194
197
195
198
void Profile (llvm::FoldingSetNodeID &id) const {
196
199
id.AddInteger (capacity);
@@ -206,7 +209,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
206
209
}
207
210
208
211
void dump (llvm::raw_ostream &s = llvm::errs()) const {
209
- s << " (autodiff_index_subset capacity=" << capacity << " indices=(" ;
212
+ s << " (index_subset capacity=" << capacity << " indices=(" ;
210
213
interleave (getIndices (), [&s](unsigned i) { s << i; },
211
214
[&s] { s << " , " ; });
212
215
s << " ))" ;
@@ -226,7 +229,7 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
226
229
typedef std::forward_iterator_tag iterator_category;
227
230
228
231
private:
229
- const AutoDiffIndexSubset *parent;
232
+ const IndexSubset *parent;
230
233
int current = 0 ;
231
234
232
235
void advance () {
@@ -235,10 +238,10 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
235
238
}
236
239
237
240
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 ()) {}
242
245
iterator (const iterator &) = default;
243
246
244
247
iterator operator ++(int ) {
@@ -256,18 +259,18 @@ class AutoDiffIndexSubset : public llvm::FoldingSetNode {
256
259
257
260
bool operator ==(const iterator &other) const {
258
261
assert (parent == other.parent &&
259
- " Comparing iterators from different AutoDiffIndexSubsets " );
262
+ " Comparing iterators from different IndexSubsets " );
260
263
return current == other.current ;
261
264
}
262
265
263
266
bool operator !=(const iterator &other) const {
264
267
assert (parent == other.parent &&
265
- " Comparing iterators from different AutoDiffIndexSubsets " );
268
+ " Comparing iterators from different IndexSubsets " );
266
269
return current != other.current ;
267
270
}
268
271
};
269
272
};
270
273
271
- } // end namespace swift
274
+ }
272
275
273
- #endif // SWIFT_AST_AUTODIFF_H
276
+ #endif // SWIFT_AST_INDEXSUBSET_H
0 commit comments