Skip to content

Add clang-format to pre-commit hooks - #3133

Open
mdboom wants to merge 3 commits into
NVIDIA:mainfrom
mdboom:clang-format
Open

mdboom wants to merge 3 commits into
NVIDIA:mainfrom
mdboom:clang-format

Conversation

@mdboom

@mdboom mdboom commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

This adds a pre-commit hook for clang-format, and then updates our C, C++ and CUDA code to follow that convention.

To be familiar for other developers of Python/C extensions, the formatting rules are based on PEP7, but deviating in 2 important ways:

  • 120 line limit
  • Line wrapping of function calls and declarations follows the black/ruff convention, where it either fits on one line, or it is line wrapped with a single argument per line. IMHO, line wrapping to fit columns is a confusing convention that causes poor diffs. This doesn't contradict PEP7, it merely extends it a little bit.

Follow-on

After this is merged, I plan to continue with clang-tidy as well.

Also after this is merged, we should add the merge commit's hash to .git-blame-ignore-revs so that git blame will skip over it (this is merely reformatting and generally would not be useful to a git blame query).

@mdboom mdboom self-assigned this Oct 16, 2026
@mdboom
mdboom requested a review from Andy-Jost October 16, 2026 14:19
@github-actions github-actions Bot added the cuda.core Everything related to the cuda.core module label Oct 16, 2026
@mdboom
mdboom requested a review from rparolin October 16, 2026 14:19
@mdboom mdboom added the enhancement Any code-related improvements label Oct 16, 2026
@mdboom mdboom added this to the cuda.core next milestone Oct 16, 2026
@coderabbitai

coderabbitai Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/cuda-python/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 92c7a6bc-bea5-443f-a6b6-5edd0a1b2515
📥 Commits

Reviewing files that changed from the base of the PR and between 4e9e33d and a86e93a.

📒 Files selected for processing: 13
  • .clang-format
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_ctx_device.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_event.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_launch.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_memory.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_stream.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_support.hpp
  • cuda_core/cuda/core/_cpp/rt/context.cpp
  • cuda_core/cuda/core/_cpp/rt/context_scope.hpp
  • cuda_core/cuda/core/_cpp/rt/event.cpp
  • cuda_core/cuda/core/_cpp/rt/memory.cpp
  • cuda_core/cuda/core/_cpp/rt/stream.cpp
  • cuda_core/cuda/core/_cpp/rt/texture.cpp
🚧 Files skipped from review as they are similar to previous changes: 12
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_memory.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_event.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_ctx_device.cpp
  • cuda_core/cuda/core/_cpp/rt/event.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_stream.cpp
  • cuda_core/cuda/core/_cpp/rt/texture.cpp
  • cuda_core/cuda/core/_cpp/rt/context_scope.hpp
  • cuda_core/cuda/core/_cpp/rt/stream.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_launch.cpp
  • benchmarks/cuda_bindings/benchmarks/cpp/bench_support.hpp
  • cuda_core/cuda/core/_cpp/rt/context.cpp
  • cuda_core/cuda/core/_cpp/rt/memory.cpp

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 10 remain after this review.

📝 Summary

Summary by CodeRabbit

  • Chores
    • Standardized C, C++ and CUDA code formatting across the project.
    • Added contributor guidance and automatic formatting checks for supported source files.

Adds a pinned clang-format hook and repository formatting guidance. The configured style is applied to C++ and CUDA benchmark and runtime sources. The summaries report no changes to behavior, signatures, or benchmark operations.

Changes: C++ and CUDA formatting

Layer / File(s) Summary
Formatting policy and setup
.clang-format, .pre-commit-config.yaml, CONTRIBUTING.md
Adds the repository clang-format configuration, pins the formatter hook to version 23.1.3, and documents formatting instructions for C, C++, and CUDA files.
Benchmark source formatting
benchmarks/cuda_bindings/benchmarks/cpp/*
Reformats benchmark implementations and shared helpers. Benchmark operations, arguments, error handling, and output behavior remain unchanged.
Runtime API and shared contracts
cuda_core/cuda/core/_cpp/rt/api.hpp, cuda_core/cuda/core/_cpp/rt/{context_scope,driver_api,error,internal,types,versions}.*
Reformats runtime declarations and shared types. Signatures and documented behavior remain unchanged.
Runtime handle and resource implementations
cuda_core/cuda/core/_cpp/rt/{context,event,graph,graph_exec,memory,program,stream,texture,virtual_memory}.*
Reformats runtime implementation code. Control flow, ownership, cleanup, and error handling remain unchanged.
Python integration and CUDA helpers
cuda_core/cuda/core/_cpp/rt/py*, cuda_core/cuda/core/_include/{layout,utility}.hpp, cuda_core/tests/test_binaries/saxpy.cu
Reformats Python-coupled code, native-handle and layout helpers, and the SAXPY CUDA source. Signatures and behavior remain unchanged.

Suggested reviewers: danielfrg

Priority: ⬇️ Low

Change: Other

Merge Risk: ⚪ Minimal · up to a86e9

The PR adds formatting tooling and reformats C++ and CUDA sources. No actionable behavior or formatting-contract issue is established in the supplied context, so no PR-specific merge risk remains beyond normal checks.

- [ ] Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

@github-actions

Copy link
Copy Markdown
Contributor

suite.run("ctx_device.ctx_set_current", [&]() {
check_cu(cuCtxSetCurrent(ctx), "cuCtxSetCurrent failed");
});
suite.run("ctx_device.ctx_set_current", [&]() { check_cu(cuCtxSetCurrent(ctx), "cuCtxSetCurrent failed"); });

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 where the line limit should inform the formatter to break the lambda across multiple lines the way it was before. Take a look at the clang-format settings I proposed in the issue ticket.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This doesn't exceed the line limit of 120. I think what you may really be asking for is to always break inside of the {}?

for (int i = 0; i < 16; ++i)
packed_16[i] = &int_ptrs[i];
void *packed_16[16];
for (int i = 0; i < 16; ++i) packed_16[i] = &int_ptrs[i];

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 another example where the format should break to a new line.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think the intention of the rule here is that single-line for loops are on the same line to prevent this kind of confusion:

for (int i = 0; i < 16; ++i) 
    packed_16[i] = &int_ptrs[i];
    packed_16[i] = &int_ptrs[i];

So the fix is to add explicit {}.

@rparolin rparolin left a comment

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 largely looks fine, but I would fix the lambda's so they are not all slammed onto a single line.

@mdboom
mdboom requested a review from rparolin October 16, 2026 15:59

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

cuda.core Everything related to the cuda.core module enhancement Any code-related improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants