Skip to content

[Analyzer Proposal]: Remove redundant cast on vectors #103557

Closed

Description

@EgorBo

Background and motivation

Strong typing for SIMD often leads to frequent casts from one type of Vector to another (typically, no-op in codegen). Sometimes user might accidentally introduce redundant casts that can be safely removed. I propose an analyzer that highlights such casts. Example:

return AdvSimd.MultiplyWideningLowerAndAdd(
    AdvSimd.ShiftLeftLogical(
        AdvSimd.AddPairwiseWidening(
            AdvSimd.Multiply(b.AsInt32(),
                AdvSimd.ReverseElement32(a).AsInt32()).AsUInt32()).AsInt64(), 32),
    AdvSimd.ExtractNarrowingLower(a),
    AdvSimd.ExtractNarrowingLower(b)).As<long, T>();

Here I had to insert quite a few casts to make it work as expected and it's not obvious whether some of them might be redundant or not. It's not a codegen issue, just an opportunity to make code smaller = easier to read.

API Usage

Vector128<ulong> v = Vector128.Create(ulong.MaxValue);
DoWork(v.AsUInt64()); // Flagged with "redundant cast"

Alternative Designs

No response

Risks

N/A

Activity

  1. added
    api-suggestionEarly API idea and discussion, it is NOT ready for implementation
    on Jun 17, 2024
  2. ghost added
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Jun 17, 2024
  3. EgorBo commented on Jun 17, 2024

    @EgorBo
    MemberAuthor
  4. added and removed
    needs-area-labelAn area label is needed to ensure this gets routed to the appropriate area owners
    on Jun 17, 2024
  5. dotnet-policy-service commented on Jun 17, 2024

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-numerics
    See info in area-owners.md if you want to be subscribed.

  6. tannergooding commented on Jun 17, 2024

    @tannergooding
    Member

    (typically, no-op in codegen).

    Notably this is always a no-op, it's fully elided in importation and never produces IR today (barring the case we throw because it is invalid, but that's also statically determined).

    We can likely just close this and lump it together with the already approved: #82488, which tracks a general analyzer for suggesting more optimal SIMD patterns.

  7. locked and limited conversation to collaborators on Jul 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions