Skip to content

Commit

Permalink
Fix GT_ASSUME for NVCC and Enable GT_ASSUME on Recent GCC Versions (#…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
fthaler authored and havogt committed Sep 30, 2024
1 parent 7338ed4 commit be00005
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
16 changes: 12 additions & 4 deletions include/gridtools/common/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions tests/unit_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 28 additions & 0 deletions tests/unit_tests/test_defs.cpp
Original file line number Diff line number Diff line change
@@ -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 <string_view>

#include <boost/preprocessor/stringize.hpp>

#include <gridtools/common/defs.hpp>

#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

0 comments on commit be00005

Please sign in to comment.