Skip to content

Validate CPU kernel registrations at compile time#29649

Open
GopalakrishnanN wants to merge 4 commits into
mainfrom
GopalakrishnanN/constexpr-registration-descriptors
Open

Validate CPU kernel registrations at compile time#29649
GopalakrishnanN wants to merge 4 commits into
mainfrom
GopalakrishnanN/constexpr-registration-descriptors

Conversation

@GopalakrishnanN

Copy link
Copy Markdown
Contributor

Description

  • Mark the ONNX, optional FP16, and ONNX-ML CPU kernel factory tables constexpr.
  • Add a consteval invariant that requires the disabled BuildKernelCreateInfo<void> entry first and rejects null factory pointers.
  • Preserve reduced-operator behavior: disabled registrations may legitimately repeat the non-null disabled entry.
  • Add independent compile-time tests for valid, missing-sentinel, null-factory, and null-sentinel tables, plus a runtime check that the real CPU registry initializes non-empty.

Motivation and Context

CPU kernel registration is a fixed build-time descriptor set, but malformed tables were only discoverable while constructing the runtime registry. These checks make sentinel placement and factory-pointer validity compile-time requirements across the normal, reduced-ops, optional FP16, and optional ONNX-ML table variants.

The existing compact runtime registration loops are intentionally unchanged. A templated shared loop increased the provider library by 5,006 bytes, and an inline span version increased it by 33,970 bytes; keeping the loops produced the smallest measured result.

Measurement

Windows RelWithDebInfo, MSVC 19.51, touched-file rebuild of cpu_execution_provider.cc:

Artifact Before After Delta
cpu_execution_provider.obj 2,033,704 B 2,033,836 B +132 B
onnxruntime_providers.lib 678,492,398 B 678,492,530 B +132 B

The single baseline rebuild was 8.55 s; final post-change samples were 6.78 s, 6.54 s, and 6.44 s. Timing is noisy, so this PR does not claim a compile-time improvement.

Testing

  • cmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8
  • onnxruntime_test_all.exe --gtest_filter=CpuKernelRegistrationTest.*:KernelRegistryTests.* from the test output directory: 9 tests from 2 suites passed.
  • lintrunner -a on all three changed files: no lint issues.

Copilot AI 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.

Pull request overview

This PR makes malformed CPU kernel factory tables a compile-time error by adding a consteval validation helper and applying it to the CPU EP’s ONNX, optional FP16, and ONNX-ML registration tables. It also adds compile-time unit tests for the validation logic plus a small runtime sanity test that the real CPU kernel registry is non-empty.

Changes:

  • Add cpu::registration_internal::IsKernelRegistrationTableValid (consteval) to enforce that the disabled BuildKernelCreateInfo<void> entry is first and that no table entries are null.
  • Mark the CPU EP’s ONNX / FP16 / ONNX-ML function_table arrays as constexpr and add static_assert validation for each.
  • Add unit tests covering valid/invalid tables and a runtime check that the default CPU kernel registry initializes non-empty.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
onnxruntime/test/framework/cpu_kernel_registration_test.cc Adds compile-time table-validation tests plus a runtime non-empty registry check.
onnxruntime/core/providers/cpu/cpu_kernel_registration.h Introduces a consteval helper to validate kernel registration tables at compile time.
onnxruntime/core/providers/cpu/cpu_execution_provider.cc Makes the registration tables constexpr and enforces compile-time invariants via static_assert.

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.

2 participants