From 6622c1dc61fbe650a0d8e9cd42446e9e5293ea4b Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Tue, 18 Jun 2024 14:57:41 +0200 Subject: [PATCH] Introduce GT_PROMISE for __builtin_assume (#1785) Credits to @lukasm91 for hinting to `__builtin_assume` in `deref`. On more recent compilers (than 11.2 what we have in CI on daint), will improve codegen to the level of #1779, but is save. --------- Co-authored-by: Felix Thaler --- include/gridtools/common/defs.hpp | 20 +++++++++++++++++++- include/gridtools/fn/unstructured.hpp | 3 ++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/include/gridtools/common/defs.hpp b/include/gridtools/common/defs.hpp index b46527f53..559dd2253 100644 --- a/include/gridtools/common/defs.hpp +++ b/include/gridtools/common/defs.hpp @@ -8,6 +8,7 @@ * SPDX-License-Identifier: BSD-3-Clause */ #pragma once +#include namespace gridtools { using int_t = int; @@ -26,6 +27,22 @@ namespace gridtools { #endif #endif +#if defined(__has_builtin) +#if __has_builtin(__builtin_assume) +#define GT_ASSUME(x) __builtin_assume(x) +#else +#define GT_ASSUME(x) +#endif +#else +#define GT_ASSUME(x) +#endif + +#ifdef NDEBUG +#define GT_PROMISE(x) GT_ASSUME(x) +#else +#define GT_PROMISE(x) assert(x) +#endif + #ifdef __cpp_consteval #define GT_CONSTEVAL consteval #else @@ -53,7 +70,8 @@ namespace gridtools { #endif #if defined(__NVCC__) && (__CUDACC_VER_MAJOR__ == 12 && __CUDACC_VER_MINOR__ >= 1 && __CUDACC_VER_MINOR__ <= 4) -// enables workaround for CTAD/constexpr issues in CUDA 12.1, 12.2, 12.3, 12.4 (https://github.com/GridTools/gridtools/issues/1766) +// enables workaround for CTAD/constexpr issues in CUDA 12.1, 12.2, 12.3, 12.4 +// (https://github.com/GridTools/gridtools/issues/1766) #define GT_NVCC_WORKAROUND_1766 1 #else #define GT_NVCC_WORKAROUND_1766 0 diff --git a/include/gridtools/fn/unstructured.hpp b/include/gridtools/fn/unstructured.hpp index 136930c19..9844185a0 100644 --- a/include/gridtools/fn/unstructured.hpp +++ b/include/gridtools/fn/unstructured.hpp @@ -11,6 +11,7 @@ #include +#include "../common/defs.hpp" #include "../common/hymap.hpp" #include "../meta/logical.hpp" #include "../sid/concept.hpp" @@ -76,7 +77,7 @@ namespace gridtools::fn { template GT_FUNCTION constexpr auto deref(iterator const &it) { - assert(can_deref(it)); + GT_PROMISE(can_deref(it)); decltype(auto) stride = host_device::at_key(sid::get_stride(it.m_strides)); return *sid::shifted(it.m_ptr, stride, it.m_index); }