Skip to content

Commit

Permalink
[pstl] Replace direct use of assert() with _PSTL_ASSERT
Browse files Browse the repository at this point in the history
Standard libraries may (libstdc++ in particular) forbid direct use of
assert()/<cassert> in library code.

Differential Revision: https://reviews.llvm.org/D60249
  • Loading branch information
rodgert authored and ldionne committed Nov 2, 2020
1 parent ae9231c commit 2e15f4a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
10 changes: 5 additions & 5 deletions pstl/include/pstl/internal/algorithm_impl.h
Expand Up @@ -2757,8 +2757,8 @@ __pattern_includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _Forwa
return !__internal::__parallel_or(
std::forward<_ExecutionPolicy>(__exec), __first2, __last2,
[__first1, __last1, __first2, __last2, &__comp](_ForwardIterator2 __i, _ForwardIterator2 __j) {
assert(__j > __i);
//assert(__j - __i > 1);
_PSTL_ASSERT(__j > __i);
//_PSTL_ASSERT(__j - __i > 1);

//1. moving boundaries to "consume" subsequence of equal elements
auto __is_equal = [&__comp](_ForwardIterator2 __a, _ForwardIterator2 __b) -> bool {
Expand All @@ -2782,8 +2782,8 @@ __pattern_includes(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _Forwa
//2. testing is __a subsequence of the second range included into the first range
auto __b = std::lower_bound(__first1, __last1, *__i, __comp);

assert(!__comp(*(__last1 - 1), *__b));
assert(!__comp(*(__j - 1), *__i));
_PSTL_ASSERT(!__comp(*(__last1 - 1), *__b));
_PSTL_ASSERT(!__comp(*(__j - 1), *__i));
return !std::includes(__b, __last1, __i, __j, __comp);
});
});
Expand Down Expand Up @@ -2971,7 +2971,7 @@ __parallel_set_union_op(_ExecutionPolicy&& __exec, _ForwardIterator1 __first1, _
}

const auto __m2 = __left_bound_seq_2 - __first2;
assert(__m1 == 0 || __m2 == 0);
_PSTL_ASSERT(__m1 == 0 || __m2 == 0);
if (__m2 > __set_algo_cut_off)
{
auto __res_or = __result;
Expand Down
4 changes: 2 additions & 2 deletions pstl/include/pstl/internal/numeric_impl.h
Expand Up @@ -304,7 +304,7 @@ _ForwardIterator2
__brick_adjacent_difference(_ForwardIterator1 __first, _ForwardIterator1 __last, _ForwardIterator2 __d_first,
BinaryOperation __op, /*is_vector=*/std::true_type) noexcept
{
assert(__first != __last);
_PSTL_ASSERT(__first != __last);

typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;
Expand Down Expand Up @@ -333,7 +333,7 @@ __pattern_adjacent_difference(_ExecutionPolicy&& __exec, _ForwardIterator1 __fir
_ForwardIterator2 __d_first, _BinaryOperation __op, _IsVector __is_vector,
/*is_parallel=*/std::true_type)
{
assert(__first != __last);
_PSTL_ASSERT(__first != __last);
typedef typename std::iterator_traits<_ForwardIterator1>::reference _ReferenceType1;
typedef typename std::iterator_traits<_ForwardIterator2>::reference _ReferenceType2;

Expand Down
51 changes: 25 additions & 26 deletions pstl/include/pstl/internal/parallel_backend_tbb.h
Expand Up @@ -10,7 +10,6 @@
#ifndef _PSTL_PARALLEL_BACKEND_TBB_H
#define _PSTL_PARALLEL_BACKEND_TBB_H

#include <cassert>
#include <algorithm>
#include <type_traits>

Expand Down Expand Up @@ -529,7 +528,7 @@ class __task : public tbb::detail::d1::task
__task*
allocate_func_task(_Fn&& __f)
{
assert(_M_execute_data != nullptr);
_PSTL_ASSERT(_M_execute_data != nullptr);
tbb::detail::d1::small_object_allocator __alloc{};
auto __t =
__alloc.new_object<__func_task<typename std::decay<_Fn>::type>>(*_M_execute_data, std::forward<_Fn>(__f));
Expand Down Expand Up @@ -574,7 +573,7 @@ class __task : public tbb::detail::d1::task
make_additional_child_of(__task* __parent, _Fn&& __f)
{
auto __t = make_child_of(__parent, std::forward<_Fn>(__f));
assert(__parent->_M_refcount.load(std::memory_order_relaxed) > 0);
_PSTL_ASSERT(__parent->_M_refcount.load(std::memory_order_relaxed) > 0);
++__parent->_M_refcount;
return __t;
}
Expand All @@ -595,7 +594,7 @@ class __task : public tbb::detail::d1::task
inline void
spawn(__task* __t)
{
assert(_M_execute_data != nullptr);
_PSTL_ASSERT(_M_execute_data != nullptr);
tbb::detail::d1::spawn(*__t, *_M_execute_data->context);
}

Expand Down Expand Up @@ -648,11 +647,11 @@ class __func_task : public __task

this->~__func_task();

assert(__parent != nullptr);
assert(__parent->_M_refcount.load(std::memory_order_relaxed) > 0);
_PSTL_ASSERT(__parent != nullptr);
_PSTL_ASSERT(__parent->_M_refcount.load(std::memory_order_relaxed) > 0);
if (--__parent->_M_refcount == 0)
{
assert(__next == nullptr);
_PSTL_ASSERT(__next == nullptr);
__alloc.deallocate(this, *__ed);
return __parent;
}
Expand Down Expand Up @@ -864,28 +863,28 @@ class __merge_func
{
const auto __nx = (_M_xe - _M_xs);
const auto __ny = (_M_ye - _M_ys);
assert(__nx > 0 && __ny > 0);
_PSTL_ASSERT(__nx > 0 && __ny > 0);

assert(_x_orig == _y_orig);
assert(!is_partial());
_PSTL_ASSERT(_x_orig == _y_orig);
_PSTL_ASSERT(!is_partial());

if (_x_orig)
{
assert(std::is_sorted(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_comp));
assert(std::is_sorted(_M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_comp));
_PSTL_ASSERT(std::is_sorted(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_comp));
_PSTL_ASSERT(std::is_sorted(_M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_comp));
return !_M_comp(*(_M_x_beg + _M_ys), *(_M_x_beg + _M_xe - 1));
}

assert(std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp));
assert(std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp));
_PSTL_ASSERT(std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp));
_PSTL_ASSERT(std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp));
return !_M_comp(*(_M_z_beg + _M_zs + __nx), *(_M_z_beg + _M_zs + __nx - 1));
}
void
move_x_range()
{
const auto __nx = (_M_xe - _M_xs);
const auto __ny = (_M_ye - _M_ys);
assert(__nx > 0 && __ny > 0);
_PSTL_ASSERT(__nx > 0 && __ny > 0);

if (_x_orig)
__move_range_construct()(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_z_beg + _M_zs);
Expand Down Expand Up @@ -916,7 +915,7 @@ class __merge_func
__task*
merge_ranges(__task* __self)
{
assert(_x_orig == _y_orig); //two merged subrange must be lie into the same buffer
_PSTL_ASSERT(_x_orig == _y_orig); //two merged subrange must be lie into the same buffer

const auto __nx = (_M_xe - _M_xs);
const auto __ny = (_M_ye - _M_ys);
Expand All @@ -932,15 +931,15 @@ class __merge_func
_M_leaf_merge(_M_x_beg + _M_xs, _M_x_beg + _M_xe, _M_x_beg + _M_ys, _M_x_beg + _M_ye, _M_z_beg + _M_zs,
_M_comp, __move_value_construct(), __move_value_construct(), __move_range_construct(),
__move_range_construct());
assert(parent_merge(__self)); //not root merging task
_PSTL_ASSERT(parent_merge(__self)); //not root merging task
}
//merge to "origin"
else
{
assert(_x_orig == _y_orig);
_PSTL_ASSERT(_x_orig == _y_orig);

assert(is_partial() || std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp));
assert(is_partial() || std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp));
_PSTL_ASSERT(is_partial() || std::is_sorted(_M_z_beg + _M_xs, _M_z_beg + _M_xe, _M_comp));
_PSTL_ASSERT(is_partial() || std::is_sorted(_M_z_beg + _M_ys, _M_z_beg + _M_ye, _M_comp));

const auto __nx = (_M_xe - _M_xs);
const auto __ny = (_M_ye - _M_ys);
Expand All @@ -957,8 +956,8 @@ class __merge_func
__task*
process_ranges(__task* __self)
{
assert(_x_orig == _y_orig);
assert(!_split);
_PSTL_ASSERT(_x_orig == _y_orig);
_PSTL_ASSERT(!_split);

auto p = parent_merge(__self);

Expand Down Expand Up @@ -1004,7 +1003,7 @@ class __merge_func
__task*
split_merging(__task* __self)
{
assert(_x_orig == _y_orig);
_PSTL_ASSERT(_x_orig == _y_orig);
const auto __nx = (_M_xe - _M_xs);
const auto __ny = (_M_ye - _M_ys);

Expand Down Expand Up @@ -1076,8 +1075,8 @@ operator()(__task* __self)
{
const _SizeType __nx = (_M_xe - _M_xs);
const _SizeType __ny = (_M_ye - _M_ys);
assert(__nx > 0);
assert(__nx > 0);
_PSTL_ASSERT(__nx > 0);
_PSTL_ASSERT(__nx > 0);

if (__nx < __ny)
move_x_range();
Expand Down Expand Up @@ -1133,7 +1132,7 @@ __stable_sort_func<_RandomAccessIterator1, _RandomAccessIterator2, _Compare, _Le
if (__n <= __sort_cut_off)
{
_M_leaf_sort(_M_xs, _M_xe, _M_comp);
assert(!_M_root);
_PSTL_ASSERT(!_M_root);
return nullptr;
}

Expand Down
3 changes: 1 addition & 2 deletions pstl/include/pstl/internal/parallel_backend_utils.h
Expand Up @@ -12,7 +12,6 @@

#include <iterator>
#include <utility>
#include <cassert>
#include "utils.h"

#include "pstl_config.h"
Expand Down Expand Up @@ -58,7 +57,7 @@ struct __serial_move_merge
constexpr bool __same_move_seq = std::is_same<_MoveSequenceX, _MoveSequenceY>::value;

auto __n = _M_nmerge;
assert(__n > 0);
_PSTL_ASSERT(__n > 0);

auto __nx = __xe - __xs;
//auto __ny = __ye - __ys;
Expand Down
5 changes: 5 additions & 0 deletions pstl/include/pstl/internal/pstl_config.h
Expand Up @@ -31,6 +31,11 @@
# define _PSTL_USAGE_WARNINGS 0
#endif

#if !defined(_PSTL_ASSERT)
# include <cassert>
# define _PSTL_ASSERT(pred) (assert((pred)))
#endif

// Portability "#pragma" definition
#ifdef _MSC_VER
# define _PSTL_PRAGMA(x) __pragma(x)
Expand Down
Expand Up @@ -14,7 +14,6 @@
#include <execution>
#include <algorithm>
#include <set>
#include <cassert>
#include <cmath>

#include "support/utils.h"
Expand Down

0 comments on commit 2e15f4a

Please sign in to comment.