[libc++] Integrate the PSTL into libc++
authorLouis Dionne <ldionne@apple.com>
Fri, 19 Jul 2019 17:02:42 +0000 (17:02 +0000)
committerLouis Dionne <ldionne@apple.com>
Fri, 19 Jul 2019 17:02:42 +0000 (17:02 +0000)
Summary:
This commit allows specifying LIBCXX_ENABLE_PARALLEL_ALGORITHMS when
configuring libc++ in CMake. When that option is enabled, libc++ will
assume that the PSTL can be found somewhere on the CMake module path,
and it will provide the C++17 parallel algorithms based on the PSTL
(that is assumed to be available).

The commit also adds support for running the PSTL tests as part of
the libc++ test suite.

Reviewers: rodgert, EricWF

Subscribers: mgorny, christof, jkorous, dexonsmith, libcxx-commits, mclow.lists, EricWF

Tags: #libc

Differential Revision: https://reviews.llvm.org/D60480

llvm-svn: 366593

17 files changed:
libcxx/CMakeLists.txt
libcxx/include/CMakeLists.txt
libcxx/include/__config_site.in
libcxx/include/algorithm
libcxx/include/execution [new file with mode: 0644]
libcxx/include/memory
libcxx/include/module.modulemap
libcxx/include/numeric
libcxx/src/CMakeLists.txt
libcxx/test/CMakeLists.txt
libcxx/test/libcxx/double_include.sh.cpp
libcxx/test/lit.site.cfg.in
libcxx/test/std/pstl [new symlink]
libcxx/utils/libcxx/test/config.py
libcxxabi/src/CMakeLists.txt
llvm/projects/CMakeLists.txt
pstl/test/std/lit.local.cfg [new file with mode: 0644]

index 1f32bb1..78b2f37 100644 (file)
@@ -80,6 +80,7 @@ endif()
 option(LIBCXX_ENABLE_FILESYSTEM "Build filesystem as part of the main libc++ library"
     ${ENABLE_FILESYSTEM_DEFAULT})
 option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
+option(LIBCXX_ENABLE_PARALLEL_ALGORITHMS "Enable the parallel algorithms library. This requires the PSTL to be available." OFF)
 
 # Benchmark options -----------------------------------------------------------
 option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependencies" ON)
@@ -745,6 +746,7 @@ config_define_if(LIBCXX_HAS_WIN32_THREAD_API _LIBCPP_HAS_THREAD_API_WIN32)
 config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
 config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC)
 config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
+config_define_if_not(LIBCXX_ENABLE_PARALLEL_ALGORITHMS _LIBCPP_HAS_NO_PARALLEL_ALGORITHMS)
 
 if (LIBCXX_ABI_DEFINES)
   set(abi_defines)
index 7cbf82c..e12068a 100644 (file)
@@ -62,6 +62,7 @@ set(files
   deque
   errno.h
   exception
+  execution
   experimental/__config
   experimental/__memory
   experimental/algorithm
index ffbd372..2bd3d8c 100644 (file)
@@ -29,6 +29,7 @@
 #cmakedefine _LIBCPP_NO_VCRUNTIME
 #cmakedefine01 _LIBCPP_HAS_MERGED_TYPEINFO_NAMES_DEFAULT
 #cmakedefine _LIBCPP_ABI_NAMESPACE @_LIBCPP_ABI_NAMESPACE@
+#cmakedefine _LIBCPP_HAS_NO_PARALLEL_ALGORITHMS
 
 @_LIBCPP_ABI_DEFINES@
 
index 0d78626..9ff29d7 100644 (file)
@@ -5678,4 +5678,8 @@ _LIBCPP_END_NAMESPACE_STD
 
 _LIBCPP_POP_MACROS
 
+#if !defined(_LIBCPP_HAS_NO_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
+#   include <pstl/internal/glue_algorithm_impl.h>
+#endif
+
 #endif  // _LIBCPP_ALGORITHM
diff --git a/libcxx/include/execution b/libcxx/include/execution
new file mode 100644 (file)
index 0000000..c1346db
--- /dev/null
@@ -0,0 +1,19 @@
+// -*- C++ -*-
+//===------------------------- execution ---------------------------------===//
+//
+// 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 _LIBCPP_EXECUTION
+#define _LIBCPP_EXECUTION
+
+#include <__config>
+
+#if !defined(_LIBCPP_HAS_NO_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
+#   include <pstl/internal/glue_execution_defs.h>
+#endif
+
+#endif // _LIBCPP_EXECUTION
index d9222b3..744b864 100644 (file)
@@ -5590,4 +5590,8 @@ _LIBCPP_END_NAMESPACE_STD
 
 _LIBCPP_POP_MACROS
 
+#if !defined(_LIBCPP_HAS_NO_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
+#   include <pstl/internal/glue_memory_impl.h>
+#endif
+
 #endif  // _LIBCPP_MEMORY
index bbfe90e..31d39dd 100644 (file)
@@ -275,6 +275,10 @@ module std [system] {
     header "exception"
     export *
   }
+  module execution {
+    header "execution"
+    export *
+  }
   module filesystem {
     header "filesystem"
     export *
index 2118704..a815e5c 100644 (file)
@@ -586,4 +586,8 @@ _LIBCPP_END_NAMESPACE_STD
 
 _LIBCPP_POP_MACROS
 
+#if !defined(_LIBCPP_HAS_NO_PARALLEL_ALGORITHMS) && _LIBCPP_STD_VER >= 17
+#   include <pstl/internal/glue_numeric_impl.h>
+#endif
+
 #endif  // _LIBCPP_NUMERIC
index 31cd243..e7d1106 100644 (file)
@@ -196,6 +196,11 @@ function(cxx_link_system_libraries target)
   endif()
 endfunction()
 
+find_package(ParallelSTL QUIET)
+if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS AND NOT TARGET pstl::ParallelSTL)
+  message(FATAL_ERROR "Could not find ParallelSTL")
+endif()
+
 function(cxx_set_common_defines name)
   if(LIBCXX_CXX_ABI_HEADER_TARGET)
     add_dependencies(${name} ${LIBCXX_CXX_ABI_HEADER_TARGET})
@@ -222,6 +227,10 @@ function(cxx_set_common_defines name)
                                  # in printf, scanf.
                                  _CRT_STDIO_ISO_WIDE_SPECIFIERS)
   endif()
+
+  if (LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
+    target_link_libraries(${name} PUBLIC pstl::ParallelSTL)
+  endif()
 endfunction()
 
 split_list(LIBCXX_COMPILE_FLAGS)
index 408ab62..8a2114f 100644 (file)
@@ -40,6 +40,7 @@ pythonize_bool(LIBCXX_HAS_ATOMIC_LIB)
 pythonize_bool(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
 pythonize_bool(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
 pythonize_bool(LIBCXX_DEBUG_BUILD)
+pythonize_bool(LIBCXX_ENABLE_PARALLEL_ALGORITHMS)
 
 # By default, for non-standalone builds, libcxx and libcxxabi share a library
 # directory.
index 2ee444a..5a1ee32 100644 (file)
@@ -63,6 +63,7 @@
 #include <deque>
 #include <errno.h>
 #include <exception>
+#include <execution>
 #include <fenv.h>
 #include <filesystem>
 #include <float.h>
index ed9a711..b5b390d 100644 (file)
@@ -33,6 +33,7 @@ config.use_libatomic            = @LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB@
 config.debug_build              = @LIBCXX_DEBUG_BUILD@
 config.libcxxabi_shared         = @LIBCXXABI_ENABLE_SHARED@
 config.cxx_ext_threads          = @LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY@
+config.pstl_root                = "@ParallelSTL_SOURCE_DIR@" if @LIBCXX_ENABLE_PARALLEL_ALGORITHMS@ else None
 
 # Let the main config do the real work.
 config.loaded_site_config = True
diff --git a/libcxx/test/std/pstl b/libcxx/test/std/pstl
new file mode 120000 (symlink)
index 0000000..27a2822
--- /dev/null
@@ -0,0 +1 @@
+../../../pstl/test/std
\ No newline at end of file
index e263e9d..e694ea1 100644 (file)
@@ -580,6 +580,13 @@ class Configuration(object):
         support_path = os.path.join(self.libcxx_src_root, 'test/support')
         self.cxx.compile_flags += ['-I' + support_path]
 
+        # Add includes for the PSTL headers
+        pstl_root = self.get_lit_conf('pstl_root')
+        if pstl_root is not None:
+            self.cxx.compile_flags += ['-I' + os.path.join(pstl_root, 'include')]
+            self.cxx.compile_flags += ['-I' + os.path.join(pstl_root, 'test')]
+            self.config.available_features.add('parallel-algorithms')
+
         # FIXME(EricWF): variant_size.pass.cpp requires a slightly larger
         # template depth with older Clang versions.
         self.cxx.addFlagIfSupported('-ftemplate-depth=270')
index 45d4d02..2eea6c7 100644 (file)
@@ -147,6 +147,11 @@ if (LLVM_ENABLE_MODULES)
   string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
 endif()
 
+find_package(ParallelSTL QUIET)
+if (NOT TARGET pstl::ParallelSTL)
+  message(STATUS "Could not find ParallelSTL, libc++abi will not attempt to use it but the build may fail if the libc++ in use needs it to be available.")
+endif()
+
 if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^(armv6|armv7|armv7s)$")
   set(LIBCXXABI_USE_SJLJ_EXCEPTIONS ON)
 else()
@@ -160,6 +165,9 @@ if (LIBCXXABI_ENABLE_SHARED)
     llvm_setup_rpath(cxxabi_shared)
   endif()
   target_link_libraries(cxxabi_shared PRIVATE ${LIBCXXABI_SHARED_LIBRARIES} ${LIBCXXABI_LIBRARIES})
+  if (TARGET pstl::ParallelSTL)
+    target_link_libraries(cxxabi_shared PUBLIC pstl::ParallelSTL)
+  endif()
   set_target_properties(cxxabi_shared
                         PROPERTIES
                           CXX_EXTENSIONS
@@ -208,6 +216,9 @@ endif()
 if (LIBCXXABI_ENABLE_STATIC)
   add_library(cxxabi_static STATIC ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
   target_link_libraries(cxxabi_static PRIVATE ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
+  if (TARGET pstl::ParallelSTL)
+    target_link_libraries(cxxabi_static PUBLIC pstl::ParallelSTL)
+  endif()
   set_target_properties(cxxabi_static
                         PROPERTIES
                           CXX_EXTENSIONS
index c98a882..47ceb9e 100644 (file)
@@ -30,8 +30,8 @@ if(${LLVM_BUILD_RUNTIME})
     # Add the projects in reverse order of their dependencies so that the
     # dependent projects can see the target names of their dependencies.
     add_llvm_external_project(libunwind)
-    add_llvm_external_project(libcxxabi)
     add_llvm_external_project(pstl)
+    add_llvm_external_project(libcxxabi)
     add_llvm_external_project(libcxx)
   endif()
   if(NOT LLVM_BUILD_EXTERNAL_COMPILER_RT)
diff --git a/pstl/test/std/lit.local.cfg b/pstl/test/std/lit.local.cfg
new file mode 100644 (file)
index 0000000..6b1e2c6
--- /dev/null
@@ -0,0 +1,2 @@
+if 'parallel-algorithms' not in config.available_features:
+    config.unsupported = True