From 4c596b663365761af4f9f074003277dee025ded5 Mon Sep 17 00:00:00 2001 From: Bjoern Knafla Date: Tue, 3 Sep 2019 18:59:06 +0100 Subject: [PATCH 1/2] [SYCL][PI] Align SYCL unittests CMake with LLVM LLVM has two CMake options to include/build unittests: * `LLVM_INCLUDE_TESTS` to generate CMake targets for all kind of testing, which is `ON` by default. * `LLVM_BUILD_TESTS` to build the included unittests CMake targets, which is `OFF` by default. Other LLVM projects introduce a `_INCLUDE_TESTS` option to control if CMake targets should be created for their different tests. If their unittests are build by default is controlled by the `LLVM_BUILD_TESTS` option. This commit aligns SYCL testing and unittesting with other LLVM projects, e.g.: * introduces a `SYCL_INCLUDE_TESTS` option, * renames the SYCL unittest suite to `SYCLUnitTests`, and * introduces the `add_sycl_unittest` CMake function to simplify adding SYCL-specific unittests. * Aligns naming of CMake unittest folder with Clang. * Copies Clang unittest LIT infrastructure to SYCL unittests. * Renames LIT testsuite names of SYCL to "SYCL" and * names the unittests in LIT as "SYCL-Unit". * Adapts other LLVM projects LIT config files naming scheme. * Cleans up CMake variables used by LIT config `.in` files and aligns variable names stronger with other LLVM projects. * Makes the SYCL unittests part of the `check-sycl` target, i.e., builds them on demand. * Adds a CMake target to build/run every SYLC LIT test including unit tests with `check-sycl-unit`. Signed-off-by: Bjoern Knafla --- buildbot/configure.py | 3 +- sycl/CMakeLists.txt | 20 ++++++- sycl/test/CMakeLists.txt | 33 ++++++++--- sycl/test/Unit/lit.cfg.py | 59 +++++++++++++++++++ sycl/test/Unit/lit.site.cfg.py.in | 29 +++++++++ sycl/test/{lit.cfg => lit.cfg.py} | 4 +- .../{lit.site.cfg.in => lit.site.cfg.py.in} | 5 +- sycl/unittests/CMakeLists.txt | 33 ++++++++--- sycl/unittests/pi/CMakeLists.txt | 11 +--- 9 files changed, 164 insertions(+), 33 deletions(-) create mode 100644 sycl/test/Unit/lit.cfg.py create mode 100644 sycl/test/Unit/lit.site.cfg.py.in rename sycl/test/{lit.cfg => lit.cfg.py} (98%) rename sycl/test/{lit.site.cfg.in => lit.site.cfg.py.in} (77%) diff --git a/buildbot/configure.py b/buildbot/configure.py index fbc1160715d98..63038c97285e9 100644 --- a/buildbot/configure.py +++ b/buildbot/configure.py @@ -26,13 +26,14 @@ def do_configure(args): "-DLLVM_EXTERNAL_PROJECTS=sycl;llvm-spirv", "-DLLVM_EXTERNAL_SYCL_SOURCE_DIR={}".format(sycl_dir), "-DLLVM_EXTERNAL_LLVM_SPIRV_SOURCE_DIR={}".format(spirv_dir), - "-DLLVM_ENABLE_PROJECTS=clang;llvm-spirv;sycl", + "-DLLVM_ENABLE_PROJECTS=clang;sycl;llvm-spirv", "-DOpenCL_INCLUDE_DIR={}".format(ocl_header_dir), "-DOpenCL_LIBRARY={}".format(icd_loader_lib), "-DLLVM_BUILD_TOOLS=OFF", "-DSYCL_ENABLE_WERROR=ON", "-DLLVM_ENABLE_ASSERTIONS=ON", "-DCMAKE_INSTALL_PREFIX={}".format(install_dir), + "-DSYCL_INCLUDE_TESTS=ON", # Explicitly include all kinds of SYCL tests. llvm_dir] print(cmake_cmd) diff --git a/sycl/CMakeLists.txt b/sycl/CMakeLists.txt index 6dae4f8be5d15..6d3ef64670a09 100644 --- a/sycl/CMakeLists.txt +++ b/sycl/CMakeLists.txt @@ -159,9 +159,23 @@ add_custom_target( sycl-toolchain COMMENT "Building SYCL compiler toolchain..." ) -add_subdirectory( test ) -add_subdirectory( unittests ) -add_subdirectory( tools ) +if (NOT DEFINED LLVM_INCLUDE_TESTS) + set(LLVM_INCLUDE_TESTS ON) +endif() + +option(SYCL_INCLUDE_TESTS + "Generate build targets for the SYCL unit tests." + ${LLVM_INCLUDE_TESTS}) + +add_subdirectory(tools) + +if(SYCL_INCLUDE_TESTS) + if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/unittest/googletest/include/gtest/gtest.h) + add_subdirectory(unittests) + list(APPEND SYCL_TEST_DEPS SYCLUnitTests) + endif() + add_subdirectory(test) +endif() # Package deploy support # Listed here are component names contributing the package diff --git a/sycl/test/CMakeLists.txt b/sycl/test/CMakeLists.txt index 57bc59f5ebfa9..e9c161793c837 100644 --- a/sycl/test/CMakeLists.txt +++ b/sycl/test/CMakeLists.txt @@ -4,22 +4,41 @@ set(LLVM_TOOLS_DIR "${LLVM_BINARY_DIR}/bin/") set(CLANG_IN_BUILD "${LLVM_BINARY_DIR}/bin/clang") set(CLANGXX_IN_BUILD "${LLVM_BINARY_DIR}/bin/clang++") -set(SYCL_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) -set(SYCL_TESTS_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) -set(SYCL_TESTS_BINARY_DIR ${SYCL_BINARY_DIR}/test) +get_target_property(SYCL_BINARY_DIR sycl-toolchain BINARY_DIR) +get_target_property(SYCL_SOURCE_DIR sycl-toolchain SOURCE_DIR) + +set(SYCL_INCLUDE "${dst_dir}") set(RT_TEST_ARGS ${RT_TEST_ARGS} "-v") configure_lit_site_cfg( - ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.in - ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg + ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py + MAIN_CONFIG + ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py + ) + +configure_lit_site_cfg( + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.site.cfg.py.in + ${CMAKE_CURRENT_BINARY_DIR}/Unit/lit.site.cfg.py MAIN_CONFIG - ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg + ${CMAKE_CURRENT_SOURCE_DIR}/Unit/lit.cfg.py + ) + +list(APPEND SYCL_TEST_DEPS + sycl-toolchain + FileCheck + get_device_count_by_type + llvm-config ) add_lit_testsuite(check-sycl "Running the SYCL regression tests" ${CMAKE_CURRENT_BINARY_DIR} ARGS ${RT_TEST_ARGS} - DEPENDS sycl-toolchain FileCheck get_device_count_by_type llvm-config + DEPENDS ${SYCL_TEST_DEPS} ) set_target_properties(check-sycl PROPERTIES FOLDER "SYCL tests") + +add_lit_testsuites(SYCL ${CMAKE_CURRENT_SOURCE_DIR} + DEPENDS ${SYCL_TEST_DEPS} + ) diff --git a/sycl/test/Unit/lit.cfg.py b/sycl/test/Unit/lit.cfg.py new file mode 100644 index 0000000000000..58d72af9b2627 --- /dev/null +++ b/sycl/test/Unit/lit.cfg.py @@ -0,0 +1,59 @@ +# -*- Python -*- + +# Configuration file for the 'lit' test runner. + +import os +import platform +import subprocess + +import lit.formats +import lit.util + +# name: The name of this test suite. +config.name = 'SYCL-Unit' + +# suffixes: A list of file extensions to treat as test files. +config.suffixes = [] + +# test_source_root: The root path where tests are located. +# test_exec_root: The root path where tests should be run. +config.test_exec_root = os.path.join(config.sycl_obj_root, 'unittests') +config.test_source_root = config.test_exec_root + +# testFormat: The test format to use to interpret tests. +config.test_format = lit.formats.GoogleTest(config.llvm_build_mode, 'Tests') + +# Propagate the temp directory. Windows requires this because it uses \Windows\ +# if none of these are present. +if 'TMP' in os.environ: + config.environment['TMP'] = os.environ['TMP'] +if 'TEMP' in os.environ: + config.environment['TEMP'] = os.environ['TEMP'] + +# Propagate path to symbolizer for ASan/MSan. +for symbolizer in ['ASAN_SYMBOLIZER_PATH', 'MSAN_SYMBOLIZER_PATH']: + if symbolizer in os.environ: + config.environment[symbolizer] = os.environ[symbolizer] + +def find_shlibpath_var(): + if platform.system() in ['Linux', 'FreeBSD', 'NetBSD', 'SunOS']: + yield 'LD_LIBRARY_PATH' + elif platform.system() == 'Darwin': + yield 'DYLD_LIBRARY_PATH' + elif platform.system() == 'Windows': + yield 'PATH' + elif platform.system() == 'AIX': + yield 'LIBPATH' + +for shlibpath_var in find_shlibpath_var(): + # in stand-alone builds, shlibdir is clang's build tree + # while llvm_libs_dir is installed LLVM (and possibly older clang) + shlibpath = os.path.pathsep.join( + (config.shlibdir, + config.llvm_libs_dir, + config.environment.get(shlibpath_var, ''))) + config.environment[shlibpath_var] = shlibpath + break +else: + lit_config.warning("unable to inject shared library path on '{}'" + .format(platform.system())) diff --git a/sycl/test/Unit/lit.site.cfg.py.in b/sycl/test/Unit/lit.site.cfg.py.in new file mode 100644 index 0000000000000..0da0e447a2866 --- /dev/null +++ b/sycl/test/Unit/lit.site.cfg.py.in @@ -0,0 +1,29 @@ +@LIT_SITE_CFG_IN_HEADER@ + +import sys + +config.llvm_src_root = "@LLVM_SOURCE_DIR@" +config.llvm_obj_root = "@LLVM_BINARY_DIR@" +config.llvm_tools_dir = "@LLVM_TOOLS_DIR@" +config.llvm_libs_dir = "@LLVM_LIBS_DIR@" +config.llvm_build_mode = "@LLVM_BUILD_MODE@" +config.clang_obj_root = "@CLANG_BINARY_DIR@" +config.enable_shared = @ENABLE_SHARED@ +config.shlibdir = "@SHLIBDIR@" +config.target_triple = "@TARGET_TRIPLE@" +config.sycl_obj_root = "@SYCL_BINARY_DIR@" + +# Support substitution of the tools_dir, libs_dirs, and build_mode with user +# parameters. This is used when we can't determine the tool dir at +# configuration time. +try: + config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params + config.llvm_libs_dir = config.llvm_libs_dir % lit_config.params + config.llvm_build_mode = config.llvm_build_mode % lit_config.params +except KeyError: + e = sys.exc_info()[1] + key, = e.args + lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key)) + +# Let the main config do the real work. +lit_config.load_config(config, "@SYCL_SOURCE_DIR@/test/Unit/lit.cfg.py") diff --git a/sycl/test/lit.cfg b/sycl/test/lit.cfg.py similarity index 98% rename from sycl/test/lit.cfg rename to sycl/test/lit.cfg.py index 9e81414bea198..e5058a803c59e 100644 --- a/sycl/test/lit.cfg +++ b/sycl/test/lit.cfg.py @@ -14,7 +14,7 @@ # Configuration file for the 'lit' test runner. # name: The name of this test suite. -config.name = 'SYCLUnitTests' +config.name = 'SYCL' # testFormat: The test format to use to interpret tests. # @@ -31,7 +31,7 @@ config.test_source_root = os.path.dirname(__file__) # test_exec_root: The root path where tests should be run. -config.test_exec_root = os.path.join(config.sycl_dir, 'test') +config.test_exec_root = os.path.join(config.sycl_obj_root, 'test') if platform.system() == "Linux": # Propagate 'LD_LIBRARY_PATH' through the environment. diff --git a/sycl/test/lit.site.cfg.in b/sycl/test/lit.site.cfg.py.in similarity index 77% rename from sycl/test/lit.site.cfg.in rename to sycl/test/lit.site.cfg.py.in index dd9b508d6e650..6156fc2670c2b 100644 --- a/sycl/test/lit.site.cfg.in +++ b/sycl/test/lit.site.cfg.py.in @@ -11,12 +11,11 @@ config.llvm_build_bins_dir = "@LLVM_BUILD_BINARY_DIRS@" config.llvm_binary_dir = "@LLVM_BINARY_DIR@" config.opencl_include = "@OPENCL_INCLUDE@" config.sycl_include = "@SYCL_INCLUDE@" +config.sycl_obj_root = "@SYCL_BINARY_DIR@" -config.sycl_tests_binary_dir = "@SYCL_TESTS_BINARY_DIR@" -config.sycl_dir = "@SYCL_BINARY_DIR@" import lit.llvm lit.llvm.initialize(lit_config, config) # Let the main config do the real work. -lit_config.load_config(config, "@SYCL_TESTS_ROOT_DIR@/lit.cfg") +lit_config.load_config(config, "@SYCL_SOURCE_DIR@/test/lit.cfg.py") diff --git a/sycl/unittests/CMakeLists.txt b/sycl/unittests/CMakeLists.txt index c2153a4877c5b..f903f7970eb11 100644 --- a/sycl/unittests/CMakeLists.txt +++ b/sycl/unittests/CMakeLists.txt @@ -1,12 +1,29 @@ -add_custom_target(PiUnitTests) -set_target_properties(PiUnitTests PROPERTIES FOLDER "Tests") +add_custom_target(SYCLUnitTests) +set_target_properties(SYCLUnitTests PROPERTIES FOLDER "SYCL tests") -function(add_llvm_unittest test_dirname) - add_unittest(PiUnitTests ${test_dirname} ${ARGN}) -endfunction() +# add_sycl_unittest(test_dirname file1.cpp, file2.cpp ...) +# +# Will compile the list of files together and link against SYCL. +# Produces a binary names `basename(test_dirname)`. +function(add_sycl_unittest test_dirname) + # Enable exception handling for these unit tests + set(LLVM_REQUIRES_EH 1) -function(add_llvm_unittest_with_input_files test_dirname) - add_unittest_with_input_files(PiUnitTests ${test_dirname} ${ARGN}) + add_unittest(SYCLUnitTests ${test_dirname} ${ARGN}) + target_link_libraries(${test_dirname} + PRIVATE + sycl + LLVMTestingSupport + OpenCL-Headers + ) + target_include_directories(${test_dirname} PRIVATE SYSTEM ${sycl_inc_dir}) + # LLVM gtest uses LLVM utilities that require C++-14 + # CXX_STANDARD_REQUIRED makes CXX_STANDARD a hard requirement. + set_target_properties(${test_dirname} + PROPERTIES + CXX_STANDARD 14 + CXX_STANDARD_REQUIRED ON + ) endfunction() -add_subdirectory(pi) \ No newline at end of file +add_subdirectory(pi) diff --git a/sycl/unittests/pi/CMakeLists.txt b/sycl/unittests/pi/CMakeLists.txt index 9c50717ccfd5c..d90f4dd695c69 100644 --- a/sycl/unittests/pi/CMakeLists.txt +++ b/sycl/unittests/pi/CMakeLists.txt @@ -1,12 +1,5 @@ -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_EXTENSIONS OFF) -# Enable exception handling for these unit tests -set(LLVM_REQUIRES_EH 1) -add_llvm_unittest(PiTests +add_sycl_unittest(PiTests PlatformTest.cpp -) - -add_dependencies(PiTests sycl) -target_link_libraries(PiTests PRIVATE sycl LLVMTestingSupport) \ No newline at end of file + ) From f08659919cd05d5cebdea995e369869bfbd38286 Mon Sep 17 00:00:00 2001 From: Bjoern Knafla Date: Tue, 3 Sep 2019 19:18:37 +0100 Subject: [PATCH 2/2] [SYCL][PI] Treat SYCL headers as system headers SYCL examples, e.g., in the Khronos specs, include the SYCL header as system headers. Adapt includes in the `CL/sycl/detail/pi.hpp` header to this style. Signed-off-by: Bjoern Knafla --- sycl/unittests/pi/PlatformTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sycl/unittests/pi/PlatformTest.cpp b/sycl/unittests/pi/PlatformTest.cpp index 6f0ad0b5fcba1..95e3d0fb77fb9 100644 --- a/sycl/unittests/pi/PlatformTest.cpp +++ b/sycl/unittests/pi/PlatformTest.cpp @@ -6,8 +6,8 @@ // //===----------------------------------------------------------------------===// -#include "CL/sycl/detail/pi.hpp" #include +#include #include #include