Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify implicit cast ctors for vector & matrix. #6408

Merged
merged 6 commits into from
Feb 20, 2025

Conversation

csyonghe
Copy link
Collaborator

@csyonghe csyonghe commented Feb 20, 2025

Closes #6405, and helps with #6358.

This pull request introduces a new builtin type constraint to express "T is implicitly convertible to U", and use this constraint to turn many vector/matrix ctor overloads into a single generic ctor.

Previously, our core module defines:

vector<int, N>.__init(vector<uint, N>);
vector<int, N>.__init(vector<float, N>);
vector<int, N>.__init(vector<bool, N>);
...

These overloads results in large core module size, and makes overload resolution slower than it needs to be.

With this cleanup, we are now expressing this with a generic ctor:

extension<T, int N> vector<T, N>
{
     __init<U>(vector<U,N> v) where T(U) implicit;
}

The main change here is to implicit the new T(U) implicit type constraint (TypeCoercionConstraint) in a minimal form that is just enough to express the core module functionality.

In the future we can expand the language to fully support this new type of constraint for user code. For now the implementation is very limited in that we are cutting a lot of corners by not forming any real witness object to represent how the constraint can be satisfied, and the implementation is not functional for anything but the core module use.

Summary from copilot attached below.

Type Coercion and Conversion Enhancements:

  • Added TypeCoercionWitness class and corresponding methods to handle type coercion witnesses in slang-ast-val.h and slang-ast-val.cpp. [1] [2]
  • Introduced TypeCoercionConstraintDecl class in slang-ast-decl.h to represent type coercion constraints.
  • Implemented getTypeCoercionWitness method in ASTBuilder class to create or retrieve type coercion witnesses in slang-ast-builder.cpp and slang-ast-builder.h. [1] [2]
  • Updated getImplicitConversionCostWithKnownArg and _coerce methods to handle type coercion constraints in slang-check-conversion.cpp. [1] [2] [3]
  • Added visitTypeCoercionConstraintDecl method to SemanticsDeclHeaderVisitor for validating type coercion constraints in slang-check-decl.cpp. [1] [2]

IR Instructions and Handling:

  • Introduced BuiltinCast IR instruction in slang-ir-inst-defs.h and updated IRBuilder to handle it in slang-ir-insts.h and slang-ir.cpp. [1] [2] [3]
  • Enhanced peephole optimization to handle BuiltinCast in slang-ir-peephole.cpp.

Miscellaneous:

  • Removed outdated vector extension code and introduced new generic vector and matrix extensions in core.meta.slang. [1] [2]
  • Added a new conversion cost kConversionCost_TypeCoercionConstraint and related constants in slang-ast-support-types.h.
  • Updated ImplicitConversionModifier to set default values for conversion cost and kind in slang-ast-modifier.h.
  • Modified overload candidate comparison to prefer implicit conversions in slang-check-overload.cpp.
  • Adjusted constraint system solving to verify type coercion constraints in slang-check-constraint.cpp. [1] [2]

These changes collectively enhance the type coercion and conversion mechanisms, improve IR handling, and streamline the codebase.

@csyonghe csyonghe added the pr: non-breaking PRs without breaking changes label Feb 20, 2025
@csyonghe csyonghe requested a review from a team as a code owner February 20, 2025 02:06
Copy link
Collaborator

@jkwak-work jkwak-work left a comment

Choose a reason for hiding this comment

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

Looks good to me.

The copilot summary on the PR description isn't very helpful because it describes trivial changes explicitly too much.

@csyonghe csyonghe merged commit 19867ff into shader-slang:master Feb 20, 2025
15 of 16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cleanup vector/matrix implicit cast constructors with a builtin __coerceable type constraint.
3 participants