Skip to content

Make matrix layout invariants compile-time#29643

Open
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/compile-time-invariants
Open

Make matrix layout invariants compile-time#29643
GopalakrishnanN wants to merge 3 commits into
mainfrom
GopalakrishnanN/compile-time-invariants

Conversation

@GopalakrishnanN

Copy link
Copy Markdown
Contributor

Description

  • Make fixed-rank Position arithmetic, factories, and row/column layout mapping usable in constant expressions.
  • Enforce positive coordinate ranks, same-rank conversions, and nonnegative static matrix dimensions at compile time.
  • Fix the three-dimensional make_Position overload to construct Position<3> instead of converting from Position<2>.
  • Add compile-time assertions for coordinate operations and layout mappings, plus a runtime smoke test for dynamic coordinates.

Motivation and Context

The matrix layout helpers describe fixed-rank metadata, but their invariants and arithmetic were previously evaluated at runtime. In addition, the three-dimensional factory constructed a two-dimensional position and converted it to rank three, which could read a nonexistent third element from the source.

Moving these operations and constraints to compile time catches invalid layouts during compilation while preserving the existing runtime API and generated layout arithmetic.

Validation:

  • .venv\Scripts\lintrunner.exe -a onnxruntime/core/util/matrix_layout.h onnxruntime/test/common/matrix_layout_test.cc
  • cmake --build build/Windows/RelWithDebInfo --config RelWithDebInfo --target onnxruntime_test_all --parallel 8
  • onnxruntime_test_all.exe --gtest_filter="MatrixLayoutTest.*" (1 test passed)

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 strengthens core/util/matrix_layout.h by making fixed-rank coordinate/layout helpers usable in constant expressions and by moving key invariants (positive ranks, nonnegative static dimensions, same-rank conversions) to compile-time checks. It also fixes the make_Position 3D factory to actually construct a Position<3> and adds compile-time + runtime tests to validate the behavior.

Changes:

  • Make Position, MatrixShape, and row/column-major layout helpers constexpr where applicable, and add compile-time invariant checks (static_assert).
  • Fix make_Position(T,T,T) to return Position<3, T, L> directly (avoiding the prior rank-mismatch conversion path).
  • Add compile-time assertions and a small runtime smoke test covering layout mapping for runtime coordinates.

Reviewed changes

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

File Description
onnxruntime/core/util/matrix_layout.h Converts core coordinate/layout utilities to constexpr and enforces key invariants at compile time; fixes 3D make_Position.
onnxruntime/test/common/matrix_layout_test.cc Adds compile-time assertions for Position/layout behavior plus a runtime coordinate mapping smoke test.

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