Skip to content

Commit

Permalink
Introduce GT_PROMISE for __builtin_assume (GridTools#1785)
Browse files Browse the repository at this point in the history
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 GridTools#1779, but is save.

---------

Co-authored-by: Felix Thaler <felix.thaler@nummi.ch>
  • Loading branch information
havogt and fthaler authored Jun 18, 2024
1 parent ab11057 commit 6622c1d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
20 changes: 19 additions & 1 deletion include/gridtools/common/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* SPDX-License-Identifier: BSD-3-Clause
*/
#pragma once
#include <cassert>

namespace gridtools {
using int_t = int;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion include/gridtools/fn/unstructured.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <functional>

#include "../common/defs.hpp"
#include "../common/hymap.hpp"
#include "../meta/logical.hpp"
#include "../sid/concept.hpp"
Expand Down Expand Up @@ -76,7 +77,7 @@ namespace gridtools::fn {

template <class Tag, class Ptr, class Strides, class Domain>
GT_FUNCTION constexpr auto deref(iterator<Tag, Ptr, Strides, Domain> const &it) {
assert(can_deref(it));
GT_PROMISE(can_deref(it));
decltype(auto) stride = host_device::at_key<Tag>(sid::get_stride<dim::horizontal>(it.m_strides));
return *sid::shifted(it.m_ptr, stride, it.m_index);
}
Expand Down

0 comments on commit 6622c1d

Please sign in to comment.