Skip to content

refactor: replace promote_operands with unified type promotion #237

Open
omsherikar wants to merge 4 commits intoarxlang:mainfrom
omsherikar:refactor/type-unification-135
Open

refactor: replace promote_operands with unified type promotion #237
omsherikar wants to merge 4 commits intoarxlang:mainfrom
omsherikar:refactor/type-unification-135

Conversation

@omsherikar
Copy link
Contributor

Description

Dropped fp_rank and promote_operands, introduced _unify_numeric_operands for both scalar and vector paths in BinaryOp.

solves #135

…ng#135)

Drop fp_rank and promote_operands, use _unify_numeric_operands for both scalar and vector paths in BinaryOp.
Copilot AI review requested due to automatic review settings March 17, 2026 15:21
Copy link

Copilot AI left a comment

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 refactors numeric operand promotion in LLVMLiteIRVisitor by removing the legacy fp_rank/promote_operands path and introducing a unified _unify_numeric_operands helper that handles scalar↔vector shape unification and scalar type casting consistently in BinaryOp.

Changes:

  • Removed fp_rank and promote_operands; added _unify_numeric_operands plus supporting helpers for float-width selection and scalar/vector casting.
  • Updated BinaryOp to unify numeric operands before vector/scalar op dispatch.
  • Updated and expanded tests to cover the new unification behavior (including scalar↔vector and float-rank cases).

Reviewed changes

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

File Description
src/irx/builders/llvmliteir.py Introduces unified numeric operand unification/casting and wires it into BinaryOp; removes old promotion logic.
tests/test_type_promotion.py Migrates existing promotion tests from promote_operands to _unify_numeric_operands.
tests/test_llvmlite_helpers.py Adds targeted tests for scalar↔vector unification and float-rank widening; imports is_fp_type for assertions.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if current_bits < target_bits:
return builder.fpext(value, target_ty, "fpext")
return builder.fptrunc(value, target_ty, "fptrunc")
return builder.sitofp(value, target_ty, "sitofp")
Comment on lines +872 to +873

if current_width < target_width:
@omsherikar
Copy link
Contributor Author

@yuvi the pr is ready for review

@yuvimittal
Copy link
Member

@omsherikar , CI seems to be failing

@omsherikar
Copy link
Contributor Author

@omsherikar , CI seems to be failing

Hii @yuvimittal it seems to be the env issue, its crashing I don't know why, how can I fix that? should I trigger the CI once again?

@omsherikar
Copy link
Contributor Author

@yuvimittal The CI are passing now, can you please review it?

@omsherikar omsherikar changed the title refactor: replace promote_operands with unified type promotion (#135) refactor: replace promote_operands with unified type promotion Mar 18, 2026
@yuvimittal
Copy link
Member

@omsherikar , i believe we had all the functionalities you added already in the codebase, could you explain more about what have you added

@omsherikar
Copy link
Contributor Author

@omsherikar , i believe we had all the functionalities you added already in the codebase, could you explain more about what have you added

I have just replaced two separate promotion paths with one unified method that handles scalars, vectors, and mixed scalar-vector and vector-vector cases. as the issue itself stated about unification

@omsherikar
Copy link
Contributor Author

@yuvimittal one more thing we can do is bring FMA into it too. but is it necessary?

Copy link
Member

Choose a reason for hiding this comment

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

Since _unify_numeric_operands already promotes operands to a common type, you no longer need the is_fp_type(llvm_lhs.type) or is_fp_type(llvm_rhs.type) guards , after unification, both sides already share the same type, so checking either one is sufficient. The or is now misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I just simplify those guards to check llvm_lhs.type only?

return FLOAT16_BITS
if FP128Type is not None and isinstance(ty, FP128Type):
return FLOAT128_BITS
return getattr(ty, "width", 0)
Copy link
Member

Choose a reason for hiding this comment

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

returns 0 for unknown types, which would silently fall through to FLOAT_TYPE instead of raising. The old fp_rank `returned 0 and the caller would just proceed with mismatched types, so the behavior is similar, but it would be cleaner to raise explicitly on unknown FP types there.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it to raise explicitly on unknown FP types so we catch it.

)

# If both operands are LLVM vectors, handle as vector ops
if is_vector(llvm_lhs) and is_vector(llvm_rhs):
Copy link
Member

Choose a reason for hiding this comment

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

The vector block itself still has a duplicate size/element-type mismatch check that _unify_numeric_operands already enforces, those two raise Exception lines inside the vector block can be removed.
like wherever the unecessary exceptions are used, they should be removed, that is the point of unified right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

my bad i missed that those were still there. I will remove those

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.

3 participants