Skip to content

Document ODR violations in KOKKOS_IF_ON_HOST/DEVICE constexpr usage#813

Open
Copilot wants to merge 7 commits into
mainfrom
copilot/extend-constexpr-context-documentation
Open

Document ODR violations in KOKKOS_IF_ON_HOST/DEVICE constexpr usage#813
Copilot wants to merge 7 commits into
mainfrom
copilot/extend-constexpr-context-documentation

Conversation

Copy link
Copy Markdown

Copilot AI commented Feb 17, 2026

Extend constexpr Context Documentation for KOKKOS_IF_ON_HOST/DEVICE ✅

Summary of Changes:

Extended the "constexpr Context" section in the documentation of KOKKOS_IF_ON_HOST and KOKKOS_IF_ON_DEVICE macros with comprehensive explanation of ODR violations based on developer discussions.

Completed Work:

  • Explore repository structure and locate documentation file
  • Understand existing "constexpr Context" section
  • Extend the "constexpr Context" section with detailed explanations about ODR violations
  • Add code examples showing problematic patterns
  • Add code examples showing correct alternatives
  • Build documentation to verify no syntax errors
  • Review final changes and verify quality
  • Fix template specialization example to use execution space instead of memory space
  • Remove platform-specific size assumptions from examples
  • Final code review and verification
  • Address review feedback: clarify lambda section title and emphasize template specialization preference

Documentation Enhancements:

  1. Strengthened warning: Changed from "cannot" to "must not" with emphasis on ODR violations
  2. Added "Why This Is Problematic" section: Explains the difference between constexpr and runtime contexts
  3. Added analogy: Comparison to #ifdef __AVX2__ preprocessor usage across translation units
  4. Added "Examples of ODR Violations" section: Two concrete problematic patterns:
    • Using in constexpr function affecting struct size
    • Lambda capture dependency issues
  5. Added "Correct Alternatives" section: Three practical solutions:
    • Remove constexpr for runtime functions
    • Move KOKKOS_IF_ON_* to calling context
    • Use template specialization on execution spaces (Preferred)

Build Verification:

  • Documentation builds successfully with Sphinx
  • Generated HTML verified for correctness

💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 3 commits February 17, 2026 18:51
… examples

Co-authored-by: crtrott <9490481+crtrott@users.noreply.github.com>
… memory space

Co-authored-by: crtrott <9490481+crtrott@users.noreply.github.com>
Co-authored-by: crtrott <9490481+crtrott@users.noreply.github.com>
Copilot AI changed the title [WIP] Update documentation for constexpr Context in KOKKOS_IF_ON_HOST Document ODR violations in KOKKOS_IF_ON_HOST/DEVICE constexpr usage Feb 17, 2026
Copilot AI requested a review from crtrott February 17, 2026 18:55
In this example, the ``Functor`` struct would have different sizes on the host
and device, causing serious memory corruption issues when passed between them.

**Problematic: Lambda captures with** ``constexpr`` **usage**
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
**Problematic: Lambda captures with** ``constexpr`` **usage**
**Problematic: Lambda capture dependency

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.

Updated to "Lambda capture dependency" in commit c160bf9.

Comment thread docs/source/API/core/macros-special/if_on_host_or_device.rst Outdated
Comment thread docs/source/API/core/macros-special/if_on_host_or_device.rst Outdated
… specialization preference

Co-authored-by: crtrott <9490481+crtrott@users.noreply.github.com>
Copilot AI requested a review from crtrott February 17, 2026 19:12
Comment on lines +109 to +126
``#ifdef __AVX2__`` in different translation units.

This is analogous to the following problematic pattern:

.. code-block:: cpp

// DO NOT DO THIS - ODR violation
static constexpr int foo() {
#ifdef __AVX2__
return 4;
#else
return 2;
#endif
}

If you compile this code in two different translation units with different
compiler flags (one with AVX2 and one without) and then link them together, you
have an ODR violation because the same function has different definitions.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we need this example? I think the next example makes it clear enough?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I kinda do like to connect this to something not involving host/device compilation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

but the explanation is about different compilation units with different flags...which if you know how some compilers do device code is helpful, but if you are oblivious to that, the question is: why shouldn't I do that?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also find the avx example very confusing.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Per review let's not get into that compile flag mess here.

and template instantiations. When ``KOKKOS_IF_ON_HOST`` and
``KOKKOS_IF_ON_DEVICE`` are used in ``constexpr`` contexts, they cause the same
function or variable to have different compile-time values on the host versus
the device—similar to using architecture-specific preprocessor macros like
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
the device—similar to using architecture-specific preprocessor macros like
the device to using architecture-specific preprocessor macros like

I get what it's getting to, but I feel like just the device is sufficient.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think a comma or a parenthesis should be added. It looks like the device-similar to ... was a kind of device (similar to ....

))
}

**Alternative 3 (Preferred): Use template specialization**
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think the Preferred method should be placed first as the Alternative 1.

@crtrott crtrott marked this pull request as ready for review February 25, 2026 17:34
(constant expression).
These macros **must not** be used in a context that requires a ``constexpr``
(constant expression). Using ``KOKKOS_IF_ON_HOST`` or ``KOKKOS_IF_ON_DEVICE``
within ``constexpr`` functions or to initialize ``constexpr`` variables leads to
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
within ``constexpr`` functions or to initialize ``constexpr`` variables leads to
within ``constexpr`` functions or to initialize ``constexpr`` variables can lead to

These macros **must not** be used in a context that requires a ``constexpr``
(constant expression). Using ``KOKKOS_IF_ON_HOST`` or ``KOKKOS_IF_ON_DEVICE``
within ``constexpr`` functions or to initialize ``constexpr`` variables leads to
**One Definition Rule (ODR) violations** and undefined behavior.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
**One Definition Rule (ODR) violations** and undefined behavior.
**One Definition Rule (ODR) violations** and cause undefined behavior.

Comment on lines +109 to +126
``#ifdef __AVX2__`` in different translation units.

This is analogous to the following problematic pattern:

.. code-block:: cpp

// DO NOT DO THIS - ODR violation
static constexpr int foo() {
#ifdef __AVX2__
return 4;
#else
return 2;
#endif
}

If you compile this code in two different translation units with different
compiler flags (one with AVX2 and one without) and then link them together, you
have an ODR violation because the same function has different definitions.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Per review let's not get into that compile flag mess here.

crtrott added 2 commits March 9, 2026 16:52
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Signed-off-by: Christian Trott <crtrott@sandia.gov>
Comment on lines +131 to +147
**Problematic: Lambda capture dependency**

.. code-block:: cpp

// DO NOT DO THIS - causes ODR violation
void foo() {
int a = 0;
double b = 1.0;
auto lambda = KOKKOS_LAMBDA(int) {
KOKKOS_IF_ON_HOST((printf("%i\n", a);)) // Captures 'a'
KOKKOS_IF_ON_DEVICE((printf("%lf\n", b);)) // Captures 'b'
};
// Lambda has different size on host vs device due to different captures
}

The lambda object has different sizes on host and device because of the
different captures, violating the ODR.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So this problematic example never gets resolved, unlike the one above? If so, I think this example should be first, then the one that gets resolved should be second and it should be clear that the "Correct Alternatives" section is a subsection of that example.

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.

7 participants