Skip to content

Use signed type for static strides in nanobind adapter - #1833

Open
edopao wants to merge 1 commit into
GridTools:masterfrom
edopao:fix/nanobind-signed-static-strides
Open

edopao wants to merge 1 commit into
GridTools:masterfrom
edopao:fix/nanobind-signed-static-strides

Conversation

@edopao

@edopao edopao commented Dec 2, 2026

Copy link
Copy Markdown

Problem

nanobind_adapter.hpp represents static strides as integral_constant<std::size_t, ...>. A compile-time shift by a negative offset along such a dimension computes stride * offset in unsigned arithmetic in sid::shift:

int_t PtrOffset = get_static_const_value<Stride>::value * Offset::value  // size_t(1) * -1 == 18446744073709551615

The value cannot be narrowed to int_t, so every compile-time shift overload is discarded and the call fails with no matching function for call to 'shift'.

This blocks gt4py from passing static unit strides on Cartesian grids: a backward neighbor access such as a(I - 1) lowers to a shift by integral_constant<int, -1>, see GridTools/gt4py#3071.

Standalone reproducer, using only GridTools headers:

#include <cstddef>
#include <gridtools/common/integral_constant.hpp>
#include <gridtools/sid/concept.hpp>
using namespace gridtools;

int main() {
    double data[3] = {10, 20, 30};
    double *ptr = &data[1];
    // fails to compile; compiles with integral_constant<std::ptrdiff_t, 1>
    sid::shift(ptr, integral_constant<std::size_t, 1>{}, integral_constant<int, -1>{});
    return *ptr == 10 ? 0 : 1;
}

Fix

Represent static strides as integral_constant<std::ptrdiff_t, ...>. Dynamic strides are unchanged.

Test

New NanobindAdapterStaticStridesNegativeShift test: builds a SID with stride_spec<1, -1>, checks that the static stride type is signed, and shifts by integral_constant<int, -1>. Without the fix it fails to compile with the error above; with the fix all nanobind adapter tests pass.

Note: I ran the tests locally on macOS with std::int64_t instead of std::intptr_t for the test strides. nanobind 2.1 takes const int64_t * strides, and std::intptr_t (long) does not convert to it on macOS. This affects the existing tests too; on Linux both are the same type.

Static strides were represented as integral_constant<std::size_t, ...>.
A compile-time shift by a negative offset along such a dimension then
evaluates stride * offset in unsigned arithmetic, which cannot be
narrowed to int_t, so no sid::shift overload is viable and compilation
fails.

Represent static strides as integral_constant<std::ptrdiff_t, ...> and
add a regression test.

See GridTools/gt4py#3071.

Co-Authored-By: Claude <noreply@anthropic.com>
@gridtoolsjenkins

Copy link
Copy Markdown
Collaborator

Hi there, this is jenkins continuous integration...
Do you want me to verify this patch?

return gridtools::integral_constant<std::size_t, SpecValue>{};
// A signed type is needed: with an unsigned static stride, a compile-time shift
// by a negative offset overflows and no `sid::shift` overload is viable.
return gridtools::integral_constant<std::ptrdiff_t, SpecValue>{};

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see above you defined array_size_t. Should I define a type like below?

#if NB_VERSION_MAJOR >= 2
        using array_stride_t = nanobind::ssize_t;
#else
        using array_stride_t = std::ptrdiff_t;
#endif

@edopao

edopao commented Dec 3, 2026

Copy link
Copy Markdown
Author

cscs-ci run default

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[next] gtfn: fails to compile negative shifts on cartesian unit-stride dimension with static strides

2 participants