Skip to content

Compile RMM with -Wsign-conversion#2308

Merged
rapids-bot[bot] merged 13 commits intorapidsai:mainfrom
Matt711:fix-sign-conversion-warnings
Mar 17, 2026
Merged

Compile RMM with -Wsign-conversion#2308
rapids-bot[bot] merged 13 commits intorapidsai:mainfrom
Matt711:fix-sign-conversion-warnings

Conversation

@Matt711
Copy link
Copy Markdown
Contributor

@Matt711 Matt711 commented Mar 16, 2026

Description

Compiles rmm with -Wsign-conversion -Werror and fixes the failures

Checklist

  • I am familiar with the Contributing Guidelines.
  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

…resource.hpp

Downstream projects compiling with -Wsign-conversion -Werror failed to
build when including rmm headers due to implicit int -> size_t conversions:

- detail/format.hpp: index declared as int but passed to std::array::at()
- mr/detail/stream_ordered_memory_resource.hpp: get_num_cuda_devices() and
  device_id.value() (both int) used where size_t is expected

Adds rmm::detail::safe_cast<To>() which asserts the value is non-negative
before widening to an unsigned type, and uses it at the two call sites in
stream_ordered_memory_resource.hpp. Changes index in format_bytes directly
to std::size_t since it only ever increments from zero.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Matt711 Matt711 self-assigned this Mar 16, 2026
@Matt711 Matt711 requested a review from a team as a code owner March 16, 2026 15:52
@Matt711 Matt711 requested a review from rongou March 16, 2026 15:52
@Matt711 Matt711 added the bug Something isn't working label Mar 16, 2026
@Matt711 Matt711 requested a review from harrism March 16, 2026 15:52
@Matt711 Matt711 added the non-breaking Non-breaking change label Mar 16, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 16, 2026

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Replaced implicit int→std::size_t conversions with explicit std::size_t/static_casts across headers, tests, and utilities; added -Wsign-conversion to several CMake flag sets; minor formatting and SPDX year updates.

Changes

Cohort / File(s) Summary
Build flags
cpp/CMakeLists.txt, cpp/benchmarks/CMakeLists.txt, cpp/tests/CMakeLists.txt
Add -Wsign-conversion to CXX/CUDA flags; apply per-target compile_options to silence the warning for specific benchmark/test targets.
Core headers (sign-conversion fixes)
cpp/include/rmm/detail/format.hpp, cpp/include/rmm/mr/detail/stream_ordered_memory_resource.hpp
Change local index types to std::size_t / add static_cast<std::size_t> for device-count and indexing to remove implicit signed→unsigned conversions; SPDX year update in format.hpp.
Allocators / internals
cpp/include/rmm/mr/fixed_size_memory_resource.hpp, cpp/include/rmm/mr/statistics_resource_adaptor.hpp
Change block/index lambda parameter types to std::size_t; cast sizes to int64_t when updating statistics counters.
Bench utilities
cpp/benchmarks/utilities/log_parser.hpp
Parse pointer hex strings into uintptr_t via explicit cast (replace direct std::stoll→pointer assignment).
Tests — index/iterator fixes
cpp/tests/mr/mr_ref_test.hpp, cpp/tests/mr/mr_ref_test_mt.hpp, cpp/tests/mr/tracking_mr_tests.cpp, cpp/tests/prefetch_tests.cpp
Use std::size_t or explicit casts (std::size_t, std::ptrdiff_t) for indices and iterator arithmetic to silence sign-conversion warnings; small SPDX year updates.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • Drop usage of thrust iterators #2254: Changes in cpp/include/rmm/mr/fixed_size_memory_resource.hpp related to block-index handling — closely related to the index-type changes here.

Suggested labels

improvement

Suggested reviewers

  • rongou
  • harrism
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 30.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Compile RMM with -Wsign-conversion' clearly and concisely describes the main objective of the PR.
Description check ✅ Passed The description explains that the PR compiles RMM with -Wsign-conversion -Werror and fixes failures, directly relating to the changeset.
Linked Issues check ✅ Passed The PR fixes all sign-conversion issues identified in #2307: detail/format.hpp (int→size_t), stream_ordered_memory_resource.hpp (two locations), and adds -Wsign-conversion flags throughout CMakeLists files to enforce compliance.
Out of Scope Changes check ✅ Passed All changes in the PR are directly related to addressing sign-conversion warnings: type casts for index/size parameters, CMakeLists enforcement flags, and copyright year updates are all in scope.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
cpp/include/rmm/detail/safe_cast.hpp (1)

14-30: Documentation claims broader checking than implementation provides.

The docstring states this is a "Checked narrowing/sign-converting cast" that asserts the value is "representable in To", but the implementation only checks for negative values when converting signed→unsigned. Narrowing conversions (e.g., int64_tint32_t) are not validated and could silently overflow.

For the current PR use cases (widening intstd::size_t), this is sufficient. Consider either:

  1. Updating the docstring to reflect the actual scope (sign-converting only), or
  2. Adding a narrowing check for completeness.
Option 1: Update documentation to match implementation
 /**
- * `@brief` Checked narrowing/sign-converting cast.
+ * `@brief` Checked sign-converting cast for widening integral conversions.
  *
- * Casts `value` to type `To`, asserting at runtime that the value is
- * representable in `To`. In release builds the assertion compiles away when
+ * Casts `value` to type `To`, asserting at runtime that signed values are
+ * non-negative when converting to unsigned types. In release builds the assertion compiles away when
  * the compiler can prove it is always true.
  */
Option 2: Add narrowing check for full representability
 template <typename To, typename From>
 [[nodiscard]] constexpr To safe_cast(From value)
 {
   static_assert(std::is_integral_v<From> && std::is_integral_v<To>,
                 "safe_cast is only defined for integral types");
   if constexpr (std::is_signed_v<From> && std::is_unsigned_v<To>) {
     RMM_EXPECTS(value >= From{0}, "safe_cast: negative value cannot be represented as unsigned");
   }
+  if constexpr (sizeof(From) > sizeof(To) || 
+                (std::is_signed_v<From> == std::is_signed_v<To> && sizeof(From) == sizeof(To))) {
+    // Potentially narrowing; check bounds
+    RMM_EXPECTS(value <= static_cast<From>(std::numeric_limits<To>::max()) &&
+                value >= static_cast<From>(std::numeric_limits<To>::min()),
+                "safe_cast: value out of range for target type");
+  }
   return static_cast<To>(value);
 }

Note: Option 2 would require adding #include <limits>.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@cpp/include/rmm/detail/safe_cast.hpp` around lines 14 - 30, The docstring
overstates checks—implement full representability checks in safe_cast instead of
only signed→unsigned negative checks: include <limits> and inside template
safe_cast add constexpr branches that for narrowing conversions (when sizeof(To)
< sizeof(From) or differing signedness) verify value <=
std::numeric_limits<To>::max() and value >= std::numeric_limits<To>::min() as
applicable, using RMM_EXPECTS for failures; retain the existing negative check
for signed→unsigned but expand logic to cover signed→signed, unsigned→unsigned,
and unsigned→signed by comparing against numeric_limits<To>::min()/max()
(convert bounds to From for comparison) so all integral representability is
asserted before static_cast in safe_cast.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@cpp/include/rmm/detail/safe_cast.hpp`:
- Around line 14-30: The docstring overstates checks—implement full
representability checks in safe_cast instead of only signed→unsigned negative
checks: include <limits> and inside template safe_cast add constexpr branches
that for narrowing conversions (when sizeof(To) < sizeof(From) or differing
signedness) verify value <= std::numeric_limits<To>::max() and value >=
std::numeric_limits<To>::min() as applicable, using RMM_EXPECTS for failures;
retain the existing negative check for signed→unsigned but expand logic to cover
signed→signed, unsigned→unsigned, and unsigned→signed by comparing against
numeric_limits<To>::min()/max() (convert bounds to From for comparison) so all
integral representability is asserted before static_cast in safe_cast.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 160181ea-f492-4399-bdc1-dd581340a724

📥 Commits

Reviewing files that changed from the base of the PR and between 22d812c and d23657c.

📒 Files selected for processing (3)
  • cpp/include/rmm/detail/format.hpp
  • cpp/include/rmm/detail/safe_cast.hpp
  • cpp/include/rmm/mr/detail/stream_ordered_memory_resource.hpp

Matt711 and others added 2 commits March 16, 2026 09:02
Replace C++20 requires clause and std::in_range with a C++17-compatible
implementation using std::numeric_limits and if constexpr.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…ource.hpp

Remove safe_cast.hpp and replace with static_cast<std::size_t> at the two
affected call sites.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@bdice bdice left a comment

Choose a reason for hiding this comment

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

@Matt711 Please update the PR description and then feel free to trigger a merge.

Copy link
Copy Markdown
Collaborator

@bdice bdice left a comment

Choose a reason for hiding this comment

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

I almost forgot to ask:

Please update these to enforce this requirement, too:

rmm/cpp/CMakeLists.txt

Lines 127 to 129 in 22d812c

set(RMM_CXX_FLAGS -Wall -Werror -Wextra -Wno-unknown-pragmas -Wno-error=deprecated-declarations)
set(RMM_CUDA_FLAGS -Werror=all-warnings
-Xcompiler=-Wall,-Werror,-Wextra,-Wno-error=deprecated-declarations)

set(RMM_TESTS_CXX_FLAGS -Wall -Werror -Wextra -Wno-unknown-pragmas)
set(RMM_TESTS_CUDA_FLAGS -Werror=all-warnings -Xcompiler=-Wall,-Werror,-Wextra)

set(RMM_BENCHMARKS_CXX_FLAGS -Wall -Werror -Wextra -Wno-unknown-pragmas)
set(RMM_BENCHMARKS_CUDA_FLAGS -Werror=all-warnings -Xcompiler=-Wall,-Werror,-Wextra)

@Matt711
Copy link
Copy Markdown
Contributor Author

Matt711 commented Mar 16, 2026

I almost forgot to ask:

Please update these to enforce this requirement, too:

set(RMM_TESTS_CXX_FLAGS -Wall -Werror -Wextra -Wno-unknown-pragmas)
set(RMM_TESTS_CUDA_FLAGS -Werror=all-warnings -Xcompiler=-Wall,-Werror,-Wextra)

set(RMM_BENCHMARKS_CXX_FLAGS -Wall -Werror -Wextra -Wno-unknown-pragmas)
set(RMM_BENCHMARKS_CUDA_FLAGS -Werror=all-warnings -Xcompiler=-Wall,-Werror,-Wextra)

Yup working on that now. There are more bugs to fix

@Matt711 Matt711 changed the title Fix -Wsign-conversion errors in format.hpp and stream_ordered_memory_resource.hpp Compile RMM with -Wsign-conversion Mar 16, 2026
@bdice bdice changed the title Compile RMM with -Wsign-conversion Compile RMM with -Wsign-conversion Mar 16, 2026
Enables -Wsign-conversion -Werror across the library, tests, and
benchmarks. Fixes all violations in rmm-owned code:

- fixed_size_memory_resource.hpp: block_gen lambda index int -> size_t
- statistics_resource_adaptor.hpp: cast bytes to int64_t for counter +=/-=
- mr_ref_test.hpp: fix index type and std::next difference_type cast
- mr_ref_test_mt.hpp: cast num_devices to size_t for vector::reserve
- tracking_mr_tests.cpp: cast loop index for vector subscript
- prefetch_tests.cpp: cast size_t to ptrdiff_t for std::next
- log_parser.hpp: cast stoll result to uintptr_t

rapidcsv.h and logger_tests.cpp (which includes it via log_parser.hpp)
are suppressed per-target as rapidcsv.h is vendored third-party code.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Matt711 Matt711 requested a review from a team as a code owner March 16, 2026 18:04
Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@cpp/benchmarks/utilities/log_parser.hpp`:
- Line 149: The parse_pointer lambda currently uses std::stoll to parse hex
pointer strings into uintptr_t which can overflow for high-bit values; change
the call to std::stoull so unsigned parsing is used (keep the "(nil)" check) and
cast the result to uintptr_t (e.g. replace the std::stoll call with std::stoull
while leaving the ternary and static_cast<uintptr_t> intact) in the
parse_pointer/ptr assignment.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: f73316ca-0937-450e-a46a-c5cb11da26d9

📥 Commits

Reviewing files that changed from the base of the PR and between 5e327b5 and d001da1.

📒 Files selected for processing (10)
  • cpp/CMakeLists.txt
  • cpp/benchmarks/CMakeLists.txt
  • cpp/benchmarks/utilities/log_parser.hpp
  • cpp/include/rmm/mr/fixed_size_memory_resource.hpp
  • cpp/include/rmm/mr/statistics_resource_adaptor.hpp
  • cpp/tests/CMakeLists.txt
  • cpp/tests/mr/mr_ref_test.hpp
  • cpp/tests/mr/mr_ref_test_mt.hpp
  • cpp/tests/mr/tracking_mr_tests.cpp
  • cpp/tests/prefetch_tests.cpp

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is vendored -- no need to review its diff.

Copy link
Copy Markdown
Collaborator

@bdice bdice left a comment

Choose a reason for hiding this comment

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

Thanks @Matt711! I got this fixed up with a few more changes in benchmarks. I'm going to merge this once CI passes, so that I can merge main into staging and continue my work on the CCCL MR refactoring.

@bdice
Copy link
Copy Markdown
Collaborator

bdice commented Mar 17, 2026

/merge

@rapids-bot rapids-bot bot merged commit 9b91ecc into rapidsai:main Mar 17, 2026
83 checks passed
@github-project-automation github-project-automation bot moved this from Review to Done in RMM Project Board Mar 17, 2026
@bdice bdice mentioned this pull request Mar 17, 2026
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working non-breaking Non-breaking change

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] implicit int -> size_t conversions in rmm headers break downstream builds with -Wsign-conversion -Werror

2 participants