204 changes: 204 additions & 0 deletions libcxx/include/pstl/internal/pstl_config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _PSTL_CONFIG_H
#define _PSTL_CONFIG_H

#include <__pstl_config_site>

// The version is XYYZ, where X is major, YY is minor, and Z is patch (i.e. X.YY.Z)
#define _PSTL_VERSION 16000
#define _PSTL_VERSION_MAJOR (_PSTL_VERSION / 1000)
#define _PSTL_VERSION_MINOR ((_PSTL_VERSION % 1000) / 10)
#define _PSTL_VERSION_PATCH (_PSTL_VERSION % 10)

#if !defined(_PSTL_PAR_BACKEND_SERIAL) && !defined(_PSTL_PAR_BACKEND_TBB) && !defined(_PSTL_PAR_BACKEND_OPENMP)
# error "A parallel backend must be specified"
#endif

// Check the user-defined macro for warnings
#if defined(PSTL_USAGE_WARNINGS)
# define _PSTL_USAGE_WARNINGS
#endif

#if defined(_LIBCPP_VERSION)
# include <__assert>
# define _PSTL_ASSERT(pred) _LIBCPP_ASSERT(pred, "")
#elif defined(__GLIBCXX__)
# define _PSTL_ASSERT(pred) __glibcxx_assert(pred)
#else
# include <cassert>
# define _PSTL_ASSERT(pred) (assert((pred)))
#endif

// Portability "#pragma" definition
#ifdef _MSC_VER
# define _PSTL_PRAGMA(x) __pragma(x)
#else
# define _PSTL_PRAGMA(x) _Pragma(# x)
#endif

#define _PSTL_STRING_AUX(x) #x
#define _PSTL_STRING(x) _PSTL_STRING_AUX(x)
#define _PSTL_STRING_CONCAT(x, y) x #y

#ifdef _PSTL_HIDE_FROM_ABI_PER_TU
# define _PSTL_HIDE_FROM_ABI_PUSH \
_Pragma("clang attribute push(__attribute__((internal_linkage)), apply_to=any(function,record))")
# define _PSTL_HIDE_FROM_ABI_POP _Pragma("clang attribute pop")
#else
# define _PSTL_HIDE_FROM_ABI_PUSH /* nothing */
# define _PSTL_HIDE_FROM_ABI_POP /* nothing */
#endif

// note that when ICC or Clang is in use, _PSTL_GCC_VERSION might not fully match
// the actual GCC version on the system.
#define _PSTL_GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)

#if defined(__clang__)
// according to clang documentation, version can be vendor specific
# define _PSTL_CLANG_VERSION (__clang_major__ * 10000 + __clang_minor__ * 100 + __clang_patchlevel__)
#endif

// Enable SIMD for compilers that support OpenMP 4.0
#if (defined(_OPENMP) && _OPENMP >= 201307) || \
(defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1600) || \
(!defined(__INTEL_COMPILER) && _PSTL_GCC_VERSION >= 40900) || \
defined(__clang__)
# define _PSTL_PRAGMA_SIMD _PSTL_PRAGMA(omp simd)
# define _PSTL_PRAGMA_DECLARE_SIMD _PSTL_PRAGMA(omp declare simd)
# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _PSTL_PRAGMA(omp simd reduction(PRM))
#elif !defined(_MSC_VER) //#pragma simd
# define _PSTL_PRAGMA_SIMD _PSTL_PRAGMA(simd)
# define _PSTL_PRAGMA_DECLARE_SIMD
# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM) _PSTL_PRAGMA(simd reduction(PRM))
#else //no simd
# define _PSTL_PRAGMA_SIMD
# define _PSTL_PRAGMA_DECLARE_SIMD
# define _PSTL_PRAGMA_SIMD_REDUCTION(PRM)
#endif //Enable SIMD

#if defined(__INTEL_COMPILER)
# define _PSTL_PRAGMA_FORCEINLINE _PSTL_PRAGMA(forceinline)
#else
# define _PSTL_PRAGMA_FORCEINLINE
#endif

#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900
# define _PSTL_PRAGMA_SIMD_SCAN(PRM) _PSTL_PRAGMA(omp simd reduction(inscan, PRM))
# define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan inclusive(PRM))
# define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM) _PSTL_PRAGMA(omp scan exclusive(PRM))
#else
# define _PSTL_PRAGMA_SIMD_SCAN(PRM)
# define _PSTL_PRAGMA_SIMD_INCLUSIVE_SCAN(PRM)
# define _PSTL_PRAGMA_SIMD_EXCLUSIVE_SCAN(PRM)
#endif

// Should be defined to 1 for environments with a vendor implementation of C++17 execution policies
#define _PSTL_CPP17_EXECUTION_POLICIES_PRESENT (_MSC_VER >= 1912 && _MSVC_LANG >= 201703L) || \
(_GLIBCXX_RELEASE >= 9 && __GLIBCXX__ >= 20190503 && __cplusplus >= 201703L)

#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
__cplusplus >= 201300L || \
__cpp_lib_robust_nonmodifying_seq_ops == 201304
# define _PSTL_CPP14_2RANGE_MISMATCH_EQUAL_PRESENT
#endif
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || \
__cplusplus >= 201402L || \
__cpp_lib_make_reverse_iterator == 201402
# define _PSTL_CPP14_MAKE_REVERSE_ITERATOR_PRESENT
#endif
#if (defined(_MSC_VER) && _MSC_VER >= 1900) || __cplusplus >= 201402L
# define _PSTL_CPP14_INTEGER_SEQUENCE_PRESENT
#endif
#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1700) || \
(defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918) || \
__cplusplus >= 201402L
# define _PSTL_CPP14_VARIABLE_TEMPLATES_PRESENT
#endif

#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1800
# define _PSTL_EARLYEXIT_PRESENT
# define _PSTL_MONOTONIC_PRESENT
#endif

#if (defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900) || \
(!defined(__INTEL_COMPILER) && _PSTL_GCC_VERSION >= 40900) || \
(defined(_OPENMP) && _OPENMP >= 201307)
# define _PSTL_UDR_PRESENT
#endif

#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1900 && __INTEL_COMPILER_BUILD_DATE >= 20180626
# define _PSTL_UDS_PRESENT
#endif

#if defined(_PSTL_EARLYEXIT_PRESENT)
# define _PSTL_PRAGMA_SIMD_EARLYEXIT _PSTL_PRAGMA(omp simd early_exit)
#else
# define _PSTL_PRAGMA_SIMD_EARLYEXIT
#endif

#if defined(_PSTL_MONOTONIC_PRESENT)
# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(PRM) _PSTL_PRAGMA(omp ordered simd monotonic(PRM))
# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(PRM1, PRM2) _PSTL_PRAGMA(omp ordered simd monotonic(PRM1, PRM2))
#else
# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC(PRM)
# define _PSTL_PRAGMA_SIMD_ORDERED_MONOTONIC_2ARGS(PRM1, PRM2)
#endif

// Declaration of reduction functor, where
// NAME - the name of the functor
// OP - type of the callable object with the reduction operation
// omp_in - refers to the local partial result
// omp_out - refers to the final value of the combiner operator
// omp_priv - refers to the private copy of the initial value
// omp_orig - refers to the original variable to be reduced
#define _PSTL_PRAGMA_DECLARE_REDUCTION(NAME, OP) \
_PSTL_PRAGMA(omp declare reduction(NAME:OP : omp_out(omp_in)) initializer(omp_priv = omp_orig))

#if defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 1600
# define _PSTL_PRAGMA_VECTOR_UNALIGNED _PSTL_PRAGMA(vector unaligned)
#else
# define _PSTL_PRAGMA_VECTOR_UNALIGNED
#endif

// Check the user-defined macro to use non-temporal stores
#if defined(PSTL_USE_NONTEMPORAL_STORES) && (__INTEL_COMPILER >= 1600)
# define _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED _PSTL_PRAGMA(vector nontemporal)
#else
# define _PSTL_USE_NONTEMPORAL_STORES_IF_ALLOWED
#endif

#if defined(_MSC_VER) || defined(__INTEL_COMPILER) // the preprocessors don't type a message location
# define _PSTL_PRAGMA_LOCATION __FILE__ ":" _PSTL_STRING(__LINE__) ": [Parallel STL message]: "
#else
# define _PSTL_PRAGMA_LOCATION " [Parallel STL message]: "
#endif

#define _PSTL_PRAGMA_MESSAGE_IMPL(x) _PSTL_PRAGMA(message(_PSTL_STRING_CONCAT(_PSTL_PRAGMA_LOCATION, x)))

#if defined(_PSTL_USAGE_WARNINGS)
# define _PSTL_PRAGMA_MESSAGE(x) _PSTL_PRAGMA_MESSAGE_IMPL(x)
# define _PSTL_PRAGMA_MESSAGE_POLICIES(x) _PSTL_PRAGMA_MESSAGE_IMPL(x)
#else
# define _PSTL_PRAGMA_MESSAGE(x)
# define _PSTL_PRAGMA_MESSAGE_POLICIES(x)
#endif

// broken macros
#if (defined(__GLIBCXX__) && __GLIBCXX__ < 20150716) || \
(defined(_MSC_VER) && _MSC_VER < 1800)
# define _PSTL_CPP11_STD_ROTATE_BROKEN
#endif

#if defined(__INTEL_COMPILER) && __INTEL_COMPILER == 1800
# define _PSTL_ICC_18_OMP_SIMD_BROKEN
#endif

#endif /* _PSTL_CONFIG_H */
862 changes: 862 additions & 0 deletions libcxx/include/pstl/internal/unseq_backend_simd.h

Large diffs are not rendered by default.

177 changes: 177 additions & 0 deletions libcxx/include/pstl/internal/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef _PSTL_UTILS_H
#define _PSTL_UTILS_H

#include <new>
#include <iterator>

_PSTL_HIDE_FROM_ABI_PUSH

namespace __pstl
{
namespace __internal
{

template <typename _Fp>
auto
__except_handler(_Fp __f) -> decltype(__f())
{
try
{
return __f();
}
catch (const std::bad_alloc&)
{
throw; // re-throw bad_alloc according to the standard [algorithms.parallel.exceptions]
}
catch (...)
{
std::terminate(); // Good bye according to the standard [algorithms.parallel.exceptions]
}
}

template <typename _Fp>
void
__invoke_if(std::true_type, _Fp __f)
{
__f();
}

template <typename _Fp>
void __invoke_if(std::false_type, _Fp)
{
}

template <typename _Fp>
void
__invoke_if_not(std::false_type, _Fp __f)
{
__f();
}

template <typename _Fp>
void __invoke_if_not(std::true_type, _Fp)
{
}

template <typename _F1, typename _F2>
auto
__invoke_if_else(std::true_type, _F1 __f1, _F2) -> decltype(__f1())
{
return __f1();
}

template <typename _F1, typename _F2>
auto
__invoke_if_else(std::false_type, _F1, _F2 __f2) -> decltype(__f2())
{
return __f2();
}

//! Unary operator that returns reference to its argument.
struct __no_op
{
template <typename _Tp>
_Tp&&
operator()(_Tp&& __a) const
{
return std::forward<_Tp>(__a);
}
};

template <typename _Pred>
class __reorder_pred
{
_Pred _M_pred;

public:
explicit __reorder_pred(_Pred __pred) : _M_pred(__pred) {}

template <typename _FTp, typename _STp>
bool
operator()(_FTp&& __a, _STp&& __b)
{
return _M_pred(std::forward<_STp>(__b), std::forward<_FTp>(__a));
}
};

//! Like a polymorphic lambda for pred(...,value)
template <typename _Tp, typename _Predicate>
class __equal_value_by_pred
{
const _Tp& _M_value;
_Predicate _M_pred;

public:
__equal_value_by_pred(const _Tp& __value, _Predicate __pred) : _M_value(__value), _M_pred(__pred) {}

template <typename _Arg>
bool
operator()(_Arg&& __arg)
{
return _M_pred(std::forward<_Arg>(__arg), _M_value);
}
};

//! Like a polymorphic lambda for ==value
template <typename _Tp>
class __equal_value
{
const _Tp& _M_value;

public:
explicit __equal_value(const _Tp& __value) : _M_value(__value) {}

template <typename _Arg>
bool
operator()(_Arg&& __arg) const
{
return std::forward<_Arg>(__arg) == _M_value;
}
};

//! Logical negation of ==value
template <typename _Tp>
class __not_equal_value
{
const _Tp& _M_value;

public:
explicit __not_equal_value(const _Tp& __value) : _M_value(__value) {}

template <typename _Arg>
bool
operator()(_Arg&& __arg) const
{
return !(std::forward<_Arg>(__arg) == _M_value);
}
};

template <typename _ForwardIterator, typename _Compare>
_ForwardIterator
__cmp_iterators_by_values(_ForwardIterator __a, _ForwardIterator __b, _Compare __comp)
{
if (__a < __b)
{ // we should return closer iterator
return __comp(*__b, *__a) ? __b : __a;
}
else
{
return __comp(*__a, *__b) ? __a : __b;
}
}

} // namespace __internal
} // namespace __pstl

_PSTL_HIDE_FROM_ABI_POP

#endif /* _PSTL_UTILS_H */
1 change: 1 addition & 0 deletions libcxx/test/libcxx/lint/lint_headers.sh.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def exclude_from_consideration(path):
os.path.basename(path) == '__config' or
os.path.basename(path) == '__config_site.in' or
os.path.basename(path) == 'libcxx.imp' or
os.path.basename(path).startswith('__pstl') or # TODO: Remove once PSTL integration is finished
not os.path.isfile(path)
)

Expand Down
3 changes: 3 additions & 0 deletions libcxx/test/libcxx/private_headers.verify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ for header in private_headers:
# Skip the locale API headers, since they are platform-specific and thus inherently non-modular
if 'locale_base_api' in header:
# TODO: Stop skipping PSTL headers once their integration is finished.
if header.startswith('__pstl'):
continue
print("{ifdef}#{indent}include <{header}> // {expected_error}@*:* {{{{use of private header from outside its module: '{header}'}}}}{endif}".format(
Expand Down
33 changes: 33 additions & 0 deletions libcxx/utils/data/ignore_format.txt
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,39 @@ libcxx/include/__numeric/transform_inclusive_scan.h
libcxx/include/__numeric/transform_reduce.h
libcxx/include/optional
libcxx/include/ostream
libcxx/include/pstl/internal/algorithm_fwd.h
libcxx/include/pstl/internal/algorithm_impl.h
libcxx/include/pstl/internal/execution_defs.h
libcxx/include/pstl/internal/execution_impl.h
libcxx/include/pstl/internal/glue_algorithm_defs.h
libcxx/include/pstl/internal/glue_algorithm_impl.h
libcxx/include/pstl/internal/glue_execution_defs.h
libcxx/include/pstl/internal/glue_memory_defs.h
libcxx/include/pstl/internal/glue_memory_impl.h
libcxx/include/pstl/internal/glue_numeric_defs.h
libcxx/include/pstl/internal/glue_numeric_impl.h
libcxx/include/pstl/internal/memory_impl.h
libcxx/include/pstl/internal/numeric_fwd.h
libcxx/include/pstl/internal/numeric_impl.h
libcxx/include/pstl/internal/omp/parallel_for_each.h
libcxx/include/pstl/internal/omp/parallel_for.h
libcxx/include/pstl/internal/omp/parallel_invoke.h
libcxx/include/pstl/internal/omp/parallel_merge.h
libcxx/include/pstl/internal/omp/parallel_reduce.h
libcxx/include/pstl/internal/omp/parallel_scan.h
libcxx/include/pstl/internal/omp/parallel_stable_partial_sort.h
libcxx/include/pstl/internal/omp/parallel_stable_sort.h
libcxx/include/pstl/internal/omp/parallel_transform_reduce.h
libcxx/include/pstl/internal/omp/parallel_transform_scan.h
libcxx/include/pstl/internal/omp/util.h
libcxx/include/pstl/internal/parallel_backend.h
libcxx/include/pstl/internal/parallel_backend_serial.h
libcxx/include/pstl/internal/parallel_backend_tbb.h
libcxx/include/pstl/internal/parallel_backend_utils.h
libcxx/include/pstl/internal/parallel_impl.h
libcxx/include/pstl/internal/pstl_config.h
libcxx/include/pstl/internal/unseq_backend_simd.h
libcxx/include/pstl/internal/utils.h
libcxx/include/queue
libcxx/include/__random/bernoulli_distribution.h
libcxx/include/__random/binomial_distribution.h
Expand Down
5 changes: 5 additions & 0 deletions libcxx/utils/generate_iwyu_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ def generate_map(include):
elif i == '__mbstate_t.h': continue
elif i == '__mutex_base': continue
elif i == '__node_handle': public = ['map', 'set', 'unordered_map', 'unordered_set']
elif i == '__pstl_algorithm': continue
elif i == '__pstl_config_site.in': continue
elif i == '__pstl_execution': continue
elif i == '__pstl_memory': continue
elif i == '__pstl_numeric': continue
elif i == '__split_buffer': public = ['deque', 'vector']
elif i == '__threading_support': public = ['atomic', 'mutex', 'semaphore', 'thread']
elif i == '__tree': public = ['map', 'set']
Expand Down