Skip to content

[RFC] Add WEAKORDER to ComparisonType and Deprecate SIGNED/UNSIGNED - #3003

Open
akuegel wants to merge 7 commits into
openxla:mainfrom
akuegel:akuegel-rfc
Open

[RFC] Add WEAKORDER to ComparisonType and Deprecate SIGNED/UNSIGNED#3003
akuegel wants to merge 7 commits into
openxla:mainfrom
akuegel:akuegel-rfc

Conversation

@akuegel

@akuegel akuegel commented Sep 1, 2026

Copy link
Copy Markdown
Member

This PR adds an RFC proposing to extend ComparisonType in StableHLO with WEAKORDER comparison ordering and deprecate the redundant SIGNED and UNSIGNED attributes.

Summary

  1. Add WEAKORDER to ComparisonType: Supports NumPy/Python total ordering semantics (-0.0 == +0.0, NaNs sorted to the end). This eliminates the 7+ op canonicalization boilerplate in frontend sort comparators (such as JAX) and avoids fragile compiler AST pattern matching.
  2. Deprecate SIGNED and UNSIGNED: In StableHLO, operand types explicitly encode signedness (si32, ui32, i1), and integer comparisons are inherently total order. SIGNED and UNSIGNED are legacy artifacts of XLA internals and should be deprecated in favor of NOTYPE (or omitting the attribute).
  3. Alternatives Considered: Evaluates replacing compare_type with comparison_order (PARTIAL, TOTAL, WEAK) to align with XLA's internal migration, but chooses in-place ComparisonType extension to avoid syntax/API churn for existing StableHLO users. This PR introduces the RFC markdown document in rfcs/ for community review and feedback prior to implementation.

CC @GleasonK @phawkins

This PR adds an RFC proposing to extend `ComparisonType` in StableHLO with
`NUMPY` comparison ordering and deprecate the redundant `SIGNED` and `UNSIGNED`
attributes.
### Summary
1. **Add `NUMPY` to `ComparisonType`**: Supports NumPy/Python total ordering
   semantics (-0.0 == +0.0, NaNs sorted to the end). This eliminates the 7+ op
   canonicalization boilerplate in frontend sort comparators (such as JAX) and
   avoids fragile compiler AST pattern matching for GPU/TPU radix sort
   (b/376918731).
2. **Deprecate `SIGNED` and `UNSIGNED`**: In StableHLO, operand types explicitly
   encode signedness (`si32`, `ui32`, `i1`), and integer comparisons are
   inherently total order. `SIGNED` and `UNSIGNED` are legacy artifacts of
   XLA internals and should be deprecated in favor of `NOTYPE` (or omitting the
   attribute).
3. **Alternatives Considered**: Evaluates replacing `compare_type` with
   `comparison_order` (PARTIAL, TOTAL, NUMPY) to align with XLA's internal
   migration (cl/974361549), but chooses in-place `ComparisonType` extension
   to avoid syntax/API churn for existing StableHLO users.
This PR introduces the RFC markdown document in `rfcs/` for community review
and feedback prior to implementation.

CC @GleasonK @phawkins
@akuegel akuegel changed the title Create 20260901-numpy-comparison-type.md [RFC] Add NUMPY to ComparisonType and Deprecate SIGNED/UNSIGNED Sep 1, 2026
@akuegel
akuegel requested a review from GleasonK September 1, 2026 14:45
@GleasonK
GleasonK requested a review from abhigunj September 1, 2026 14:48
@abhigunj

abhigunj commented Sep 2, 2026

Copy link
Copy Markdown
Member

Thank you for the RFC. Motivation and Overall LGTM.

Do we have a better name for enum value and avoid using NUMPY reference. If it is not the standard (which seemes to the case), we are bringing framework library context into the StableHLO, framework agnostic layer.

@abhigunj
abhigunj requested a review from pifon2a September 2, 2026 18:35
@abhigunj abhigunj added the RFC label Sep 2, 2026
@abhigunj
abhigunj requested a review from amitsabne1 September 2, 2026 18:36
@abhigunj

abhigunj commented Sep 2, 2026

Copy link
Copy Markdown
Member

cc @hawkinsp for visibility. Somehow I can't add as a reviewer. LGTM comment will work!

@abhigunj
abhigunj requested a review from mkuperst September 2, 2026 18:43

@hawkinsp hawkinsp 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.

The change itself seems like a fine idea. Just a naming nit...

Comment thread rfcs/20260901-numpy-comparison-type.md Outdated

## Motivation & Background

### 1. The Need for NUMPY Comparison Order

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.

One suggestion: don't call this "numpy" comparison, because that will get confusing if NumPy ever changes it...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, that makes sense. I changed it to WEAKORDER now. Is that ok?

Like suggested in the reviews, NUMPY is not a great name as it ties to a library that might change its implementation.
Now I am using WEAKORDER instead.
@akuegel akuegel changed the title [RFC] Add NUMPY to ComparisonType and Deprecate SIGNED/UNSIGNED [RFC] Add WEAKORDER to ComparisonType and Deprecate SIGNED/UNSIGNED Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants