Skip to content

Use C++20 likely/unlikely instead of __builtin_expect - #1714

Merged
aranadive merged 5 commits into
ai-dynamo:mainfrom
iyastreb:nixl-likely
Jun 3, 2026
Merged

aranadive merged 5 commits into
ai-dynamo:mainfrom
iyastreb:nixl-likely

Conversation

@iyastreb

@iyastreb iyastreb commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

What?

Use C++20 likely/unlikely instead of __builtin_expect

Why?

Prefer C++20 standard constructs

Summary by CodeRabbit

  • Refactor
    • Modernized internal branch annotations across core and backend components. No functional or API changes; error handling and behavior are unchanged. No end‑user impact.

@github-actions

github-actions Bot commented Jun 2, 2026

Copy link
Copy Markdown

👋 Hi iyastreb! Thank you for contributing to ai-dynamo/nixl.

Your PR reviewers will review your contribution then trigger the CI to test your changes.

🚀

@coderabbitai

coderabbitai Bot commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3f1f328e-62b6-4ce6-8b77-bed666f2395d

📥 Commits

Reviewing files that changed from the base of the PR and between 221ed78 and c03dc9f.

📒 Files selected for processing (1)
  • src/infra/nixl_memory_section.cpp

📝 Walkthrough

Walkthrough

This PR replaces scattered uses of __builtin_expect with standard C++ [[likely]]/[[unlikely]] attributes across worker, core agent, memory section, and UCX backend code, preserving existing control flow and error-handling behavior.

Changes

Branch Prediction Hint Modernization

Layer / File(s) Summary
Worker execution loop branch hints
benchmark/nixlbench/src/worker/nixl/nixl_worker.cpp
execTransferLoop termination checks, slot setup, completion loop, status queries, recycle/resubmit, and execTransfer result check now use [[unlikely]]/[[likely]] annotations; cleanup and return semantics unchanged.
Core and memory module branch hints
src/core/nixl_agent.cpp, src/infra/nixl_memory_section.cpp
Descriptor-length mismatch in createXferReq and ordering/lookup checks in nixlMemSection::populate now use [[unlikely]]; error paths and returns preserved.
UCX backend branch prediction updates
src/plugins/ucx/ucx_backend.cpp
Backend request status, EP-selection mismatch, existing-request free path, and checkXfer notification early exits converted to [[likely]]/[[unlikely]] attributes without changing behavior.
UCX status enum switch optimization
src/plugins/ucx/ucx_enums.cpp
ucsToNixlStatus switch now marks case UCS_OK as [[likely]] instead of using __builtin_expect around the switch expression; mappings unchanged.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • ai-dynamo/nixl#1474: Overlaps changes in benchmark/nixlbench/src/worker/nixl/nixl_worker.cpp around transfer completion and resubmission flows.
  • ai-dynamo/nixl#1571: Related C++20 feature adoption (branch-prediction attributes) and build configuration updates.

Suggested reviewers

  • ovidiusm
  • brminich
  • gleon99

🐰 I hopped through code both near and far,
Replaced old hints with a modern star,
Likely here and unlikely there,
The logic stands — unchanged and fair,
A tiny rabbit’s tidy PR.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 12.50% 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 clearly and concisely summarizes the main change: replacing __builtin_expect with C++20 likely/unlikely attributes across multiple files.
Description check ✅ Passed The description includes both required 'What?' and 'Why?' sections with adequate detail. The 'What?' explains the specific change, and 'Why?' provides clear justification (preferring C++20 standard constructs). The optional 'How?' section is not required.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 Infer (1.2.0)
src/infra/nixl_memory_section.cpp

src/infra/nixl_memory_section.cpp:20:10: fatal error: 'nixl.h' file not found
20 | #include "nixl.h"
| ^~~~~~~~
1 error generated.
src/infra/nixl_memory_section.cpp:57:72-106:1: ERROR translating statement 'CompoundStmt'
Aborting translation of method 'nixlMemSection::populate' in file 'src/infra/nixl_memory_section.cpp': "Assert_failure src/clang/cAst_utils.ml:249:53"
Uncaught Internal Error: "Assert_failure src/clang/cAst_utils.ml:249:53"
Error backtrace:
Raised at ClangFrontend__CAst_utils.get_decl_from_typ_ptr in file "src/clang/cAst_utils.ml", line 249, characters 53-65
Called from ClangFrontend__CTrans.CTrans_funct.get_destructor_decl_ref in file "src/clang/cTrans.ml", line 658, characters 12-59
Called from ClangFrontend__CTrans.CTrans_funct.destructor_calls.(fun) in file "src/clang/cTrans.ml", line 2048, characters 12-69
Called from Base__List.rev_filter_map.loop in file "src/list.ml", line 944, characters 13-17
Called from Base__List.filter_map in file "src/li

... [truncated 2200 characters] ...

line 54, characters 4-52
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl.add_method_if_create_procdesc in file "src/clang/cFrontend_decl.ml" (inlined), line 123, characters 16-158
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_method_decl in file "src/clang/cFrontend_decl.ml", line 126, characters 17-97
Called from ClangFrontend__CFrontend_decl.CFrontend_decl_funct.process_methods in file "src/clang/cFrontend_decl.ml" (inlined), line 270, characters 8-122
Called from Stdlib__List.iter in file "list.ml" (inlined), line 110, characters 12-15
Called from Stdlib__List.iter in file "list.ml" (inlined), line 108, characters 13-64
Called from Base__List0.iter in file "src/list0.ml" (inlined), line 25, characters 16-35
Called from ClangFr


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/plugins/ucx/ucx_backend.cpp`:
- Line 158: The branch hint on the conditional checking "ret == NIXL_SUCCESS" is
inverted; change the attribute from [[unlikely]] to [[likely]] so the
successful-fast-path is optimized. Locate the if-condition that tests ret
against NIXL_SUCCESS in src/plugins/ucx/ucx_backend.cpp (the loop handling
completion of requests) and replace [[unlikely]] with [[likely]] for the if(ret
== NIXL_SUCCESS) check to match the comment and expected hot path.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 581813b1-2650-454c-bdf9-976a81014d99

📥 Commits

Reviewing files that changed from the base of the PR and between 85608c9 and c375a65.

📒 Files selected for processing (5)
  • benchmark/nixlbench/src/worker/nixl/nixl_worker.cpp
  • src/core/nixl_agent.cpp
  • src/infra/nixl_memory_section.cpp
  • src/plugins/ucx/ucx_backend.cpp
  • src/plugins/ucx/ucx_enums.cpp

Comment thread src/plugins/ucx/ucx_backend.cpp Outdated
@iyastreb

iyastreb commented Jun 2, 2026

Copy link
Copy Markdown
Contributor Author

/build

Comment thread src/plugins/ucx/ucx_backend.cpp
@iyastreb

iyastreb commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

/build

@aranadive
aranadive enabled auto-merge (squash) June 3, 2026 05:57
@iyastreb

iyastreb commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

/build

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/infra/nixl_memory_section.cpp (1)

86-99: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Review: [[likely]]/[[unlikely]] are valid here (project uses C++20); update docs/CodeStyle.md instead.

  • Root meson.build sets cpp_std=c++20, so [[likely]]/[[unlikely]] will compile for src/**/*.cpp in this repo.
  • docs/CodeStyle.md still states “C++17” for src/**/*.cpp; if the intended standard is C++20, update the guideline to match.
  • src/infra/nixl_memory_section.cpp line 184 has no [[likely]]/[[unlikely]]—it’s a normalizeSecDesc assignment.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/infra/nixl_memory_section.cpp` around lines 86 - 99, The code uses C++20
attributes ([[likely]]/[[unlikely]]) and Meson already sets cpp_std=c++20 in the
build, so update the docs/CodeStyle.md to reflect C++20 (not C++17); edit the
guideline text that mentions the language standard and any examples to state
C++20 and note that attributes like [[likely]]/[[unlikely]] are allowed (no code
change needed in functions using base.getCoveringIndex, s_index, query, or
normalizeSecDesc).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/infra/nixl_memory_section.cpp`:
- Line 184: The diff shows a no-op change on the line calling
normalizeSecDesc(lp = normalizeSecDesc(mem, nixl_mem)) — either revert the
unintentional whitespace/formatting change so the line returns exactly to its
previous content, or if the change was intentional, replace the line with the
intended modification and add a clarifying comment above it; locate the call to
normalizeSecDesc and the variable lp (and references to mem and nixl_mem) and
either restore the original exact text or commit the real change plus a brief
explanatory comment so the PR diff matches intent.

---

Outside diff comments:
In `@src/infra/nixl_memory_section.cpp`:
- Around line 86-99: The code uses C++20 attributes ([[likely]]/[[unlikely]])
and Meson already sets cpp_std=c++20 in the build, so update the
docs/CodeStyle.md to reflect C++20 (not C++17); edit the guideline text that
mentions the language standard and any examples to state C++20 and note that
attributes like [[likely]]/[[unlikely]] are allowed (no code change needed in
functions using base.getCoveringIndex, s_index, query, or normalizeSecDesc).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3f1f328e-62b6-4ce6-8b77-bed666f2395d

📥 Commits

Reviewing files that changed from the base of the PR and between 221ed78 and c03dc9f.

📒 Files selected for processing (1)
  • src/infra/nixl_memory_section.cpp

@coderabbitai coderabbitai Bot left a comment

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.

Caution

Inline review comments failed to post. This is likely due to GitHub's internal server error or limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/infra/nixl_memory_section.cpp (1)

86-99: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Review: [[likely]]/[[unlikely]] are valid here (project uses C++20); update docs/CodeStyle.md instead.

  • Root meson.build sets cpp_std=c++20, so [[likely]]/[[unlikely]] will compile for src/**/*.cpp in this repo.
  • docs/CodeStyle.md still states “C++17” for src/**/*.cpp; if the intended standard is C++20, update the guideline to match.
  • src/infra/nixl_memory_section.cpp line 184 has no [[likely]]/[[unlikely]]—it’s a normalizeSecDesc assignment.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/infra/nixl_memory_section.cpp` around lines 86 - 99, The code uses C++20
attributes ([[likely]]/[[unlikely]]) and Meson already sets cpp_std=c++20 in the
build, so update the docs/CodeStyle.md to reflect C++20 (not C++17); edit the
guideline text that mentions the language standard and any examples to state
C++20 and note that attributes like [[likely]]/[[unlikely]] are allowed (no code
change needed in functions using base.getCoveringIndex, s_index, query, or
normalizeSecDesc).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/infra/nixl_memory_section.cpp`:
- Line 184: The diff shows a no-op change on the line calling
normalizeSecDesc(lp = normalizeSecDesc(mem, nixl_mem)) — either revert the
unintentional whitespace/formatting change so the line returns exactly to its
previous content, or if the change was intentional, replace the line with the
intended modification and add a clarifying comment above it; locate the call to
normalizeSecDesc and the variable lp (and references to mem and nixl_mem) and
either restore the original exact text or commit the real change plus a brief
explanatory comment so the PR diff matches intent.

---

Outside diff comments:
In `@src/infra/nixl_memory_section.cpp`:
- Around line 86-99: The code uses C++20 attributes ([[likely]]/[[unlikely]])
and Meson already sets cpp_std=c++20 in the build, so update the
docs/CodeStyle.md to reflect C++20 (not C++17); edit the guideline text that
mentions the language standard and any examples to state C++20 and note that
attributes like [[likely]]/[[unlikely]] are allowed (no code change needed in
functions using base.getCoveringIndex, s_index, query, or normalizeSecDesc).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 3f1f328e-62b6-4ce6-8b77-bed666f2395d

📥 Commits

Reviewing files that changed from the base of the PR and between 221ed78 and c03dc9f.

📒 Files selected for processing (1)
  • src/infra/nixl_memory_section.cpp
🛑 Comments failed to post (1)
src/infra/nixl_memory_section.cpp (1)

184-184: ⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Line marked as changed but no visible modification.

Line 184 is annotated as changed (~) but contains no visible [[likely]]/[[unlikely]] modification or __builtin_expect replacement. This appears inconsistent with the PR objective.

If this is an unintentional change (whitespace, formatting), it should be reverted to keep the diff focused. If intentional, please clarify the change.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/infra/nixl_memory_section.cpp` at line 184, The diff shows a no-op change
on the line calling normalizeSecDesc(lp = normalizeSecDesc(mem, nixl_mem)) —
either revert the unintentional whitespace/formatting change so the line returns
exactly to its previous content, or if the change was intentional, replace the
line with the intended modification and add a clarifying comment above it;
locate the call to normalizeSecDesc and the variable lp (and references to mem
and nixl_mem) and either restore the original exact text or commit the real
change plus a brief explanatory comment so the PR diff matches intent.

@iyastreb

iyastreb commented Jun 3, 2026

Copy link
Copy Markdown
Contributor Author

/build

@aranadive
aranadive merged commit 328e4ac into ai-dynamo:main Jun 3, 2026
24 checks passed
@iyastreb
iyastreb deleted the nixl-likely branch June 3, 2026 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants