Skip to content

Commit

Permalink
Add assertions to prevent infinite loop in least_pot_bound in case …
Browse files Browse the repository at this point in the history
…of negative snode size (#567)
  • Loading branch information
xumingkuan authored Mar 6, 2020
1 parent 3475023 commit 51bd159
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 0 deletions.
3 changes: 3 additions & 0 deletions taichi/common/bit.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ TI_FORCE_INLINE constexpr T product(const std::array<T, N> arr) {
}

constexpr std::size_t least_pot_bound(std::size_t v) {
if (v > std::numeric_limits<std::size_t>::max() / 2 + 1) {
TI_ERROR("v({}) too large", v)
}
std::size_t ret = 1;
while (ret < v) {
ret *= 2;
Expand Down
1 change: 1 addition & 0 deletions taichi/ir/snode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ SNode &SNode::create_node(std::vector<Index> indices,
new_node.n = 1;
for (int i = 0; i < sizes.size(); i++) {
auto s = sizes[i];
TI_ASSERT(sizes[i] > 0);
if (!bit::is_power_of_two(s)) {
auto promoted_s = bit::least_pot_bound(s);
TI_DEBUG("Non-power-of-two node size {} promoted to {}.", s, promoted_s);
Expand Down

0 comments on commit 51bd159

Please sign in to comment.