Skip to content

Update fgMorphHWIntrinsic to more closely follow fgMorphSmpOp - #116892

Merged
tannergooding merged 9 commits into
dotnet:mainfrom
tannergooding:morph-hwintrin
Jun 24, 2025
Merged

tannergooding merged 9 commits into
dotnet:mainfrom
tannergooding:morph-hwintrin

Conversation

@tannergooding

@tannergooding tannergooding commented Jun 22, 2025 •

Copy link
Copy Markdown
Member

This updates fgMorphHWIntrinsic to have many of the same arithmetic and logical transforms as fgMorphSmpOp and more closely matches the conditions under which those transforms are done by the latter. This results in a some decent diffs due to other transformations that are unlocked.

As part of this, I also ensured that fgMorphSmpOp was lighting up certain optimizations for floating-point where it is safe to do. For example, the following transforms are IEEE 754 compliant (many of these are only done when one input is a constant as the transform isn't beneficial otherwise):

  • a - b becoming a + (-b)
  • (-a) - (-b) becoming b - a
  • -(a * b) becoming a * (-b)
  • (-a) * b becoming a * (-b)
  • -(a / b) becoming a / (-b)
  • (-a) / b becoming a / (-b)

Most importantly it covers handling ~(a cmp b) and transforming it to a cmp* b; that is, it reverses the comparison. This allows significantly smaller codegen around various branches and allows other optimizations to better light up.

The few number of codegen regressions are in T0 code where lowering ends up placing a value in a local for reuse and this causes a spill/reload of the value on the stack.

@github-actions github-actions Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jun 22, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

@tannergooding
tannergooding marked this pull request as ready for review June 23, 2025 14:24
Copilot AI review requested due to automatic review settings June 23, 2025 14:24

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 enhances fgMorphHWIntrinsic by introducing an optional “effective operation” mode in GetOperForHWIntrinsicId—bringing its behavior in line with fgMorphSmpOp—and splits the morphing logic into required and optional paths in the compiler.

  • Changed the GetOperForHWIntrinsicId signature in gentree.h and implemented the getEffectiveOp logic in gentree.cpp.
  • Added fgMorphHWIntrinsicRequired and fgMorphHWIntrinsicOptional declarations in compiler.h.
  • Removed the old single-parameter overload for GetOperForHWIntrinsicId.

Reviewed Changes

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

File Description
src/coreclr/jit/gentree.h Updated GetOperForHWIntrinsicId signature and removed the old overload
src/coreclr/jit/gentree.cpp Added getEffectiveOp handling for GT_SUB and GT_XOR patterns
src/coreclr/jit/compiler.h Declared new fgMorphHWIntrinsicRequired and fgMorphHWIntrinsicOptional methods
Comments suppressed due to low confidence (2)

@kunalspathak

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@kunalspathak

This comment was marked as resolved.

@tannergooding

This comment was marked as resolved.

Comment thread src/coreclr/jit/morph.cpp
// 1. (v1 ^ AllBitsSet) to VectorXxx.OnesComplement(v1)
// 2. (v1 ^ -0.0) to VectorXxx.Negate(v1); for floating-point
case GT_XOR:
// 1. (-v1) - (-v2) to v2 - v1

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.

comment not related to this PR in particular. But I wish we had infrastructure to pattern match and replace this in morph....currently we are just adding this manually and making it more bug prone.

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

Overall looks good, but since there is quite a bit of code churn, would recommend running some jitstress pipelines. I have already kicked off antigen/fuzzlyn

@tannergooding

This comment was marked as outdated.

@kunalspathak

Copy link
Copy Markdown
Contributor

looks like there are several failures in Antigen and Fuzzlyn

@tannergooding

Copy link
Copy Markdown
Member Author

looks like there are several failures in Antigen and Fuzzlyn

Yep, there was an edge case missed where we didn't create a needed CvtMaskToVector node and a fallback path (which shouldn't have really existed) caused the wrong type to be created.

I've removed the fallback, replacing it with an assert instead, and updated the relevant path to correctly reuse the existing conversion intrinsic.

@dotnet dotnet deleted a comment from azure-pipelines Bot Jun 24, 2025
@tannergooding

This comment was marked as outdated.

@azure-pipelines

This comment was marked as outdated.

@tannergooding

Copy link
Copy Markdown
Member Author

/azp run Antigen, Fuzzlyn

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@tannergooding

Copy link
Copy Markdown
Member Author

/azp run runtime-coreclr jitstress-isas-x86, runtime-coreclr jitstress, runtime-coreclr jitstressregs

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@tannergooding

Copy link
Copy Markdown
Member Author

/azp run Antigen, Fuzzlyn

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@tannergooding
tannergooding merged commit f5fee8f into dotnet:main Jun 24, 2025
@tannergooding
tannergooding deleted the morph-hwintrin branch June 24, 2025 14:46
@tannergooding

Copy link
Copy Markdown
Member Author

Logged kunalspathak/Antigen#12 for some of the Antigen failures. The other failures are the Arm64 Sve issue being looked at and are unrelated.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants