Skip to content

[AutoDiff] NFC: Move some 'IndexSubset' method impls to their own file. #27627

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/AST/IndexSubset.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- IndexSubset.h - Fixed-size subset of indices ---------------------===//
//===---------- IndexSubset.h - Fixed-size subset of indices --------------===//
//
// This source file is part of the Swift.org open source project
//
Expand Down Expand Up @@ -82,8 +82,8 @@ class IndexSubset : public llvm::FoldingSetNode {
}

explicit IndexSubset(const SmallBitVector &indices)
: capacity((unsigned)indices.size()),
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
: capacity((unsigned)indices.size()),
numBitWords(getNumBitWordsNeededForCapacity(capacity)) {
std::uninitialized_fill_n(getBitWordsData(), numBitWords, 0);
for (auto i : indices.set_bits()) {
unsigned bitWordIndex, offset;
Expand Down
114 changes: 1 addition & 113 deletions lib/AST/AutoDiff.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//===--- AutoDiff.cpp - Swift Differentiable Programming ------------------===//
//===--------- AutoDiff.cpp - Swift Differentiable Programming ------------===//
//
// This source file is part of the Swift.org open source project
//
Expand Down Expand Up @@ -178,118 +178,6 @@ SILLinkage autodiff::getAutoDiffDerivativeFunctionLinkage(
return SILLinkage::Hidden;
}

IndexSubset *
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
if (string.size() < 0) return nullptr;
unsigned capacity = string.size();
llvm::SmallBitVector indices(capacity);
for (unsigned i : range(capacity)) {
if (string[i] == 'S')
indices.set(i);
else if (string[i] != 'U')
return nullptr;
}
return get(ctx, indices);
}

std::string IndexSubset::getString() const {
std::string result;
result.reserve(capacity);
for (unsigned i : range(capacity))
result += contains(i) ? 'S' : 'U';
return result;
}

bool IndexSubset::isSubsetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (auto index : range(numBitWords))
if (getBitWord(index) & ~other->getBitWord(index))
return false;
return true;
}

bool IndexSubset::isSupersetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (auto index : range(numBitWords))
if (~getBitWord(index) & other->getBitWord(index))
return false;
return true;
}

IndexSubset *IndexSubset::adding(unsigned index,
ASTContext &ctx) const {
assert(index < getCapacity());
if (contains(index))
return const_cast<IndexSubset *>(this);
SmallBitVector newIndices(capacity);
bool inserted = false;
for (auto curIndex : getIndices()) {
if (!inserted && curIndex > index) {
newIndices.set(index);
inserted = true;
}
newIndices.set(curIndex);
}
return get(ctx, newIndices);
}

IndexSubset *IndexSubset::extendingCapacity(
ASTContext &ctx, unsigned newCapacity) const {
assert(newCapacity >= capacity);
if (newCapacity == capacity)
return const_cast<IndexSubset *>(this);
SmallBitVector indices(newCapacity);
for (auto index : getIndices())
indices.set(index);
return IndexSubset::get(ctx, indices);
}

int IndexSubset::findNext(int startIndex) const {
assert(startIndex < (int)capacity && "Start index cannot be past the end");
unsigned bitWordIndex = 0, offset = 0;
if (startIndex >= 0) {
auto indexAndOffset = getBitWordIndexAndOffset(startIndex);
bitWordIndex = indexAndOffset.first;
offset = indexAndOffset.second + 1;
}
for (; bitWordIndex < numBitWords; ++bitWordIndex, offset = 0) {
for (; offset < numBitsPerBitWord; ++offset) {
auto index = bitWordIndex * numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index >= capacity)
return capacity;
if (bitWord & ((BitWord)1 << offset))
return index;
}
}
return capacity;
}

int IndexSubset::findPrevious(int endIndex) const {
assert(endIndex >= 0 && "End index cannot be before the start");
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
if (endIndex < (int)capacity) {
auto indexAndOffset = getBitWordIndexAndOffset(endIndex);
bitWordIndex = (int)indexAndOffset.first;
offset = (int)indexAndOffset.second - 1;
}
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
for (; offset < (int)numBitsPerBitWord; --offset) {
auto index = bitWordIndex * (int)numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index < 0)
return -1;
if (bitWord & ((BitWord)1 << offset))
return index;
}
}
return -1;
}

Type VectorSpace::getType() const {
switch (kind) {
case Kind::Vector:
Expand Down
1 change: 1 addition & 0 deletions lib/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_swift_host_library(swiftAST STATIC
GenericSignatureBuilder.cpp
Identifier.cpp
ImportCache.cpp
IndexSubset.cpp
InlinableText.cpp
LayoutConstraint.cpp
Module.cpp
Expand Down
126 changes: 126 additions & 0 deletions lib/AST/IndexSubset.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//===-------- IndexSubset.cpp - Swift Differentiable Programming ----------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "swift/AST/IndexSubset.h"

using namespace swift;

IndexSubset *
IndexSubset::getFromString(ASTContext &ctx, StringRef string) {
if (string.size() < 0) return nullptr;
unsigned capacity = string.size();
llvm::SmallBitVector indices(capacity);
for (unsigned i : range(capacity)) {
if (string[i] == 'S')
indices.set(i);
else if (string[i] != 'U')
return nullptr;
}
return get(ctx, indices);
}

std::string IndexSubset::getString() const {
std::string result;
result.reserve(capacity);
for (unsigned i : range(capacity))
result += contains(i) ? 'S' : 'U';
return result;
}

bool IndexSubset::isSubsetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (auto index : range(numBitWords))
if (getBitWord(index) & ~other->getBitWord(index))
return false;
return true;
}

bool IndexSubset::isSupersetOf(IndexSubset *other) const {
assert(capacity == other->capacity);
for (auto index : range(numBitWords))
if (~getBitWord(index) & other->getBitWord(index))
return false;
return true;
}

IndexSubset *IndexSubset::adding(unsigned index, ASTContext &ctx) const {
assert(index < getCapacity());
if (contains(index))
return const_cast<IndexSubset *>(this);
SmallBitVector newIndices(capacity);
bool inserted = false;
for (auto curIndex : getIndices()) {
if (!inserted && curIndex > index) {
newIndices.set(index);
inserted = true;
}
newIndices.set(curIndex);
}
return get(ctx, newIndices);
}

IndexSubset *IndexSubset::extendingCapacity(
ASTContext &ctx, unsigned newCapacity) const {
assert(newCapacity >= capacity);
if (newCapacity == capacity)
return const_cast<IndexSubset *>(this);
SmallBitVector indices(newCapacity);
for (auto index : getIndices())
indices.set(index);
return IndexSubset::get(ctx, indices);
}

int IndexSubset::findNext(int startIndex) const {
assert(startIndex < (int)capacity && "Start index cannot be past the end");
unsigned bitWordIndex = 0, offset = 0;
if (startIndex >= 0) {
auto indexAndOffset = getBitWordIndexAndOffset(startIndex);
bitWordIndex = indexAndOffset.first;
offset = indexAndOffset.second + 1;
}
for (; bitWordIndex < numBitWords; ++bitWordIndex, offset = 0) {
for (; offset < numBitsPerBitWord; ++offset) {
auto index = bitWordIndex * numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index >= capacity)
return capacity;
if (bitWord & ((BitWord)1 << offset))
return index;
}
}
return capacity;
}

int IndexSubset::findPrevious(int endIndex) const {
assert(endIndex >= 0 && "End index cannot be before the start");
int bitWordIndex = numBitWords - 1, offset = numBitsPerBitWord - 1;
if (endIndex < (int)capacity) {
auto indexAndOffset = getBitWordIndexAndOffset(endIndex);
bitWordIndex = (int)indexAndOffset.first;
offset = (int)indexAndOffset.second - 1;
}
for (; bitWordIndex >= 0; --bitWordIndex, offset = numBitsPerBitWord - 1) {
for (; offset < (int)numBitsPerBitWord; --offset) {
auto index = bitWordIndex * (int)numBitsPerBitWord + offset;
auto bitWord = getBitWord(bitWordIndex);
if (!bitWord)
break;
if (index < 0)
return -1;
if (bitWord & ((BitWord)1 << offset))
return index;
}
}
return -1;
}