@@ -87,247 +87,6 @@ class ParsedAutoDiffParameter {
87
87
}
88
88
};
89
89
90
- // / An efficient index subset data structure, uniqued in `ASTContext`.
91
- // / Stores a bit vector representing set indices and a total capacity.
92
- class AutoDiffIndexSubset : public llvm ::FoldingSetNode {
93
- public:
94
- typedef uint64_t BitWord;
95
-
96
- static constexpr unsigned bitWordSize = sizeof (BitWord);
97
- static constexpr unsigned numBitsPerBitWord = bitWordSize * 8 ;
98
-
99
- static std::pair<unsigned , unsigned >
100
- getBitWordIndexAndOffset (unsigned index) {
101
- auto bitWordIndex = index / numBitsPerBitWord;
102
- auto bitWordOffset = index % numBitsPerBitWord;
103
- return {bitWordIndex, bitWordOffset};
104
- }
105
-
106
- static unsigned getNumBitWordsNeededForCapacity (unsigned capacity) {
107
- if (capacity == 0 ) return 0 ;
108
- return capacity / numBitsPerBitWord + 1 ;
109
- }
110
-
111
- private:
112
- // / The total capacity of the index subset, which is `1` less than the largest
113
- // / index.
114
- unsigned capacity;
115
- // / The number of bit words in the index subset.
116
- unsigned numBitWords;
117
-
118
- BitWord *getBitWordsData () {
119
- return reinterpret_cast <BitWord *>(this + 1 );
120
- }
121
-
122
- const BitWord *getBitWordsData () const {
123
- return reinterpret_cast <const BitWord *>(this + 1 );
124
- }
125
-
126
- ArrayRef<BitWord> getBitWords () const {
127
- return {getBitWordsData (), getNumBitWords ()};
128
- }
129
-
130
- BitWord getBitWord (unsigned i) const {
131
- return getBitWordsData ()[i];
132
- }
133
-
134
- BitWord &getBitWord (unsigned i) {
135
- return getBitWordsData ()[i];
136
- }
137
-
138
- MutableArrayRef<BitWord> getMutableBitWords () {
139
- return {const_cast <BitWord *>(getBitWordsData ()), getNumBitWords ()};
140
- }
141
-
142
- explicit AutoDiffIndexSubset (const SmallBitVector &indices)
143
- : capacity((unsigned )indices.size()),
144
- numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
145
- std::uninitialized_fill_n (getBitWordsData (), numBitWords, 0 );
146
- for (auto i : indices.set_bits ()) {
147
- unsigned bitWordIndex, offset;
148
- std::tie (bitWordIndex, offset) = getBitWordIndexAndOffset (i);
149
- getBitWord (bitWordIndex) |= (1 << offset);
150
- }
151
- }
152
-
153
- public:
154
- AutoDiffIndexSubset () = delete ;
155
- AutoDiffIndexSubset (const AutoDiffIndexSubset &) = delete ;
156
- AutoDiffIndexSubset &operator =(const AutoDiffIndexSubset &) = delete ;
157
-
158
- // Defined in ASTContext.cpp.
159
- static AutoDiffIndexSubset *get (ASTContext &ctx,
160
- const SmallBitVector &indices);
161
-
162
- static AutoDiffIndexSubset *get (ASTContext &ctx, unsigned capacity,
163
- ArrayRef<unsigned > indices) {
164
- SmallBitVector indicesBitVec (capacity, false );
165
- for (auto index : indices)
166
- indicesBitVec.set (index);
167
- return AutoDiffIndexSubset::get (ctx, indicesBitVec);
168
- }
169
-
170
- static AutoDiffIndexSubset *getDefault (ASTContext &ctx, unsigned capacity,
171
- bool includeAll = false ) {
172
- return get (ctx, SmallBitVector (capacity, includeAll));
173
- }
174
-
175
- static AutoDiffIndexSubset *getFromRange (ASTContext &ctx, unsigned capacity,
176
- unsigned start, unsigned end) {
177
- assert (start < capacity);
178
- assert (end <= capacity);
179
- SmallBitVector bitVec (capacity);
180
- bitVec.set (start, end);
181
- return get (ctx, bitVec);
182
- }
183
-
184
- // / Creates an index subset corresponding to the given string generated by
185
- // / `getString()`. If the string is invalid, returns nullptr.
186
- static AutoDiffIndexSubset *getFromString (ASTContext &ctx, StringRef string);
187
-
188
- // / Returns the number of bit words used to store the index subset.
189
- // Note: Use `getCapacity()` to get the total index subset capacity.
190
- // This is public only for unit testing
191
- // (in unittests/AST/SILAutoDiffIndices.cpp).
192
- unsigned getNumBitWords () const {
193
- return numBitWords;
194
- }
195
-
196
- // / Returns the capacity of the index subset.
197
- unsigned getCapacity () const {
198
- return capacity;
199
- }
200
-
201
- // / Returns a textual string description of these indices.
202
- // /
203
- // / It has the format `[SU]+`, where the total number of characters is equal
204
- // / to the capacity, and where "S" means that the corresponding index is
205
- // / contained and "U" means that the corresponding index is not.
206
- std::string getString () const ;
207
-
208
- class iterator ;
209
-
210
- iterator begin () const {
211
- return iterator (this );
212
- }
213
-
214
- iterator end () const {
215
- return iterator (this , (int )capacity);
216
- }
217
-
218
- iterator_range<iterator> getIndices () const {
219
- return make_range (begin (), end ());
220
- }
221
-
222
- unsigned getNumIndices () const {
223
- return (unsigned )std::distance (begin (), end ());
224
- }
225
-
226
- SmallBitVector getBitVector () const {
227
- SmallBitVector indicesBitVec (capacity, false );
228
- for (auto index : getIndices ())
229
- indicesBitVec.set (index);
230
- return indicesBitVec;
231
- }
232
-
233
- bool contains (unsigned index) const {
234
- unsigned bitWordIndex, offset;
235
- std::tie (bitWordIndex, offset) = getBitWordIndexAndOffset (index);
236
- return getBitWord (bitWordIndex) & (1 << offset);
237
- }
238
-
239
- bool isEmpty () const {
240
- return llvm::all_of (getBitWords (), [](BitWord bw) { return !(bool )bw; });
241
- }
242
-
243
- bool equals (AutoDiffIndexSubset *other) const {
244
- return capacity == other->getCapacity () &&
245
- getBitWords ().equals (other->getBitWords ());
246
- }
247
-
248
- bool isSubsetOf (AutoDiffIndexSubset *other) const ;
249
- bool isSupersetOf (AutoDiffIndexSubset *other) const ;
250
-
251
- AutoDiffIndexSubset *adding (unsigned index, ASTContext &ctx) const ;
252
- AutoDiffIndexSubset *extendingCapacity (ASTContext &ctx,
253
- unsigned newCapacity) const ;
254
-
255
- void Profile (llvm::FoldingSetNodeID &id) const {
256
- id.AddInteger (capacity);
257
- for (auto index : getIndices ())
258
- id.AddInteger (index);
259
- }
260
-
261
- void print (llvm::raw_ostream &s = llvm::outs()) const {
262
- s << ' {' ;
263
- interleave (range (capacity), [this , &s](unsigned i) { s << contains (i); },
264
- [&s] { s << " , " ; });
265
- s << ' }' ;
266
- }
267
-
268
- void dump (llvm::raw_ostream &s = llvm::errs()) const {
269
- s << " (autodiff_index_subset capacity=" << capacity << " indices=(" ;
270
- interleave (getIndices (), [&s](unsigned i) { s << i; },
271
- [&s] { s << " , " ; });
272
- s << " ))" ;
273
- }
274
-
275
- int findNext (int startIndex) const ;
276
- int findFirst () const { return findNext (-1 ); }
277
- int findPrevious (int endIndex) const ;
278
- int findLast () const { return findPrevious (capacity); }
279
-
280
- class iterator {
281
- public:
282
- typedef unsigned value_type;
283
- typedef unsigned difference_type;
284
- typedef unsigned * pointer;
285
- typedef unsigned & reference;
286
- typedef std::forward_iterator_tag iterator_category;
287
-
288
- private:
289
- const AutoDiffIndexSubset *parent;
290
- int current = 0 ;
291
-
292
- void advance () {
293
- assert (current != -1 && " Trying to advance past end." );
294
- current = parent->findNext (current);
295
- }
296
-
297
- public:
298
- iterator (const AutoDiffIndexSubset *parent, int current)
299
- : parent(parent), current(current) {}
300
- explicit iterator (const AutoDiffIndexSubset *parent)
301
- : iterator(parent, parent->findFirst ()) {}
302
- iterator (const iterator &) = default;
303
-
304
- iterator operator ++(int ) {
305
- auto prev = *this ;
306
- advance ();
307
- return prev;
308
- }
309
-
310
- iterator &operator ++() {
311
- advance ();
312
- return *this ;
313
- }
314
-
315
- unsigned operator *() const { return current; }
316
-
317
- bool operator ==(const iterator &other) const {
318
- assert (parent == other.parent &&
319
- " Comparing iterators from different AutoDiffIndexSubsets" );
320
- return current == other.current ;
321
- }
322
-
323
- bool operator !=(const iterator &other) const {
324
- assert (parent == other.parent &&
325
- " Comparing iterators from different AutoDiffIndexSubsets" );
326
- return current != other.current ;
327
- }
328
- };
329
- };
330
-
331
90
} // end namespace swift
332
91
333
92
#endif // SWIFT_AST_AUTODIFF_H
0 commit comments