From be000059251f574c3c34bf0641d285fd1e99d8e4 Mon Sep 17 00:00:00 2001 From: Felix Thaler Date: Mon, 8 Jul 2024 13:46:10 +0200 Subject: [PATCH] Fix GT_ASSUME for NVCC and Enable GT_ASSUME on Recent GCC Versions (#1789) - Fixes non-functional `GT_ASSUME` on NVCC due to `__has_builtin(__builtin_assume)` returning 0. - Adds a test to check correctly defined device-side `GT_ASSUME` on NVCC. - Enables `GT_ASSUME` based on C++-23 `[[assume(x)]]` on recent versions of GCC. --- include/gridtools/common/defs.hpp | 16 ++++++++++++---- tests/unit_tests/CMakeLists.txt | 1 + tests/unit_tests/test_defs.cpp | 28 ++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tests/unit_tests/test_defs.cpp diff --git a/include/gridtools/common/defs.hpp b/include/gridtools/common/defs.hpp index 559dd2253..e901679bc 100644 --- a/include/gridtools/common/defs.hpp +++ b/include/gridtools/common/defs.hpp @@ -27,13 +27,21 @@ namespace gridtools { #endif #endif -#if defined(__has_builtin) +#if defined(__NVCC__) && defined(__CUDA_ARCH__) && \ + (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 2 || __CUDACC_VER_MAJOR__ > 11) +#define GT_ASSUME(x) __builtin_assume(x) +#endif +#if !defined(GT_ASSUME) && defined(__has_builtin) #if __has_builtin(__builtin_assume) #define GT_ASSUME(x) __builtin_assume(x) -#else -#define GT_ASSUME(x) #endif -#else +#if !defined(GT_ASSUME) && defined(__cpp_attributes) +#if __has_cpp_attribute(assume) +#define GT_ASSUME(x) [[assume(x)]] +#endif +#endif +#endif +#ifndef GT_ASSUME #define GT_ASSUME(x) #endif diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 6ed9874d9..df2962f68 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -36,6 +36,7 @@ function(gridtools_add_unit_test tgt) endfunction() gridtools_check_compilation(test_meta test_meta.cpp) +gridtools_check_compilation(test_defs test_defs.cpp) add_subdirectory(common) add_subdirectory(sid) diff --git a/tests/unit_tests/test_defs.cpp b/tests/unit_tests/test_defs.cpp new file mode 100644 index 000000000..0cd1b9ed9 --- /dev/null +++ b/tests/unit_tests/test_defs.cpp @@ -0,0 +1,28 @@ +/* + * GridTools + * + * Copyright (c) 2014-2023, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include + +#include + +#if defined(__NVCC__) && defined(__CUDA_ARCH__) && \ + (__CUDACC_VER_MAJOR__ == 11 && __CUDACC_VER_MINOR__ >= 2 || __CUDACC__VER_MAJOR__ > 11) + +#if !defined(GT_ASSUME) +#error "GT_ASSUME is undefined" +#else +static_assert(std::string_view(BOOST_PP_STRINGIZE(GT_ASSUME(x))) == + std::string_view(BOOST_PP_STRINGIZE(__builtin_assume(x))), + BOOST_PP_STRINGIZE(GT_ASSUME(x)) " != " BOOST_PP_STRINGIZE(__builtin_assume(x))); +#endif + +#endif