Skip to content

Add xarch blsmsk - #66561

Merged
EgorBo merged 5 commits into
dotnet:mainfrom
Wraith2:add-xarch-blsmsk
Jun 13, 2022
Merged

EgorBo merged 5 commits into
dotnet:mainfrom
Wraith2:add-xarch-blsmsk

Conversation

@Wraith2

@Wraith2 Wraith2 commented Mar 13, 2022

Copy link
Copy Markdown
Contributor

This adds a lowering for the pattern XOR(x, x - 1) to the GetMaskUpToLowestSetSit hwintrinsic.
There are no uses of this pattern in the runtime. SPMI replay is clean. Test coverage is added by #66193 and this PR is opened for completeness of the simple BMI1 intrinsic.

/cc @dotnet/jit-contrib @AndyAyersMS

@ghost ghost added community-contribution Indicates that the PR has been added by a community member area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI labels Mar 13, 2022
@ghost

ghost commented Mar 13, 2022

Copy link
Copy Markdown

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

Issue Details

This adds a lowering for the pattern XOR(x, x - 1) to the GetMaskUpToLowestSetSit hwintrinsic.
There are no uses of this pattern in the runtime. SPMI replay is clean. Test coverage is added by #66193 and this PR is opened for completeness of the simple BMI1 intrinsic.

/cc @dotnet/jit-contrib @AndyAyersMS

Author: Wraith2
Assignees: -
Labels:

area-CodeGen-coreclr, community-contribution

Milestone: -

@EgorBo

EgorBo commented Mar 14, 2022

Copy link
Copy Markdown
Member

I am personally not against such changes, but I wish we had a DSL or much simpler way to express such transformations - 100 LOC for a pattern which is never met in the codebase sounds too much.

@deeprobin

Copy link
Copy Markdown
Contributor

Can you add a test, verifying this optimization?

@Wraith2

Wraith2 commented Mar 17, 2022

Copy link
Copy Markdown
Contributor Author

Test coverage was added in the blsi PR here

private static uint GetMaskUpToLowestSetBit(uint x) => (uint)(x ^ (-x)); // bmi1 blmsk
It's also updated in the bug fix PR I have open to cover 64 bit as well.

@deeprobin

Copy link
Copy Markdown
Contributor

Oh sorry, I overread that 👍🏼

@JulieLeeMSFT
JulieLeeMSFT requested a review from EgorBo April 4, 2022 16:44
@JulieLeeMSFT JulieLeeMSFT added this to the 7.0.0 milestone Apr 4, 2022
@EgorBo

EgorBo commented Apr 11, 2022

Copy link
Copy Markdown
Member

@Wraith2 could you please rebase this PR?

@JulieLeeMSFT JulieLeeMSFT added the needs-author-action An issue or pull request that requires more info or actions from the author. label Apr 11, 2022
@ghost ghost removed the needs-author-action An issue or pull request that requires more info or actions from the author. label Apr 11, 2022
@Wraith2

Wraith2 commented Apr 11, 2022

Copy link
Copy Markdown
Contributor Author

rebased, lets see what CI thinks.

@Wraith2

Wraith2 commented Apr 12, 2022

Copy link
Copy Markdown
Contributor Author

Looks fixed.

@Wraith2
Wraith2 force-pushed the add-xarch-blsmsk branch from a4bf2a8 to 649e5a2 Compare May 31, 2022 00:27
@Wraith2

Wraith2 commented May 31, 2022

Copy link
Copy Markdown
Contributor Author

@EgorBo rebased, current failures don't look related.

@EgorBo

EgorBo commented Jun 9, 2022

Copy link
Copy Markdown
Member

@Wraith2 sorry for the delay, I cloned your branch to try locally and see this:

image

do you have any idea why it emitted that failfast helper?

@EgorBo

EgorBo commented Jun 9, 2022

Copy link
Copy Markdown
Member

Ah, nvm, it's DOTNET_JITStress=1 😄

Comment thread src/coreclr/jit/instrsxarch.h Outdated
INST3(blsi, "blsi", IUM_WR, BAD_CODE, BAD_CODE, SSE38(0xF3), Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF | INS_Flags_IsDstDstSrcAVXInstruction) // Extract Lowest Set Isolated Bit
INST3(blsmsk, "blsmsk", IUM_WR, BAD_CODE, BAD_CODE, SSE38(0xF3), INS_Flags_IsDstDstSrcAVXInstruction) // Get Mask Up to Lowest Set Bit
INST3(blsi, "blsi", IUM_WR, BAD_CODE, BAD_CODE, SSE38(0xF3), Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF | INS_Flags_IsDstDstSrcAVXInstruction) // Extract Lowest Set Isolated Bit
INST3(blsmsk, "blsmsk", IUM_WR, BAD_CODE, BAD_CODE, SSE38(0xF3), Resets_OF | Writes_SF | Writes_ZF | Undefined_AF | Undefined_PF | Writes_CF | INS_Flags_IsDstDstSrcAVXInstruction) // Get Mask Up to Lowest Set Bit

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Are you sure Writes_ZF is correct? shouldn't it be Resets_ZF?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The intel manual says:

SF is updated based on the result. CF is set if the source if zero. ZF and OF flags are cleared. AF and PF flag are
undefined.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ZF and OF flags are cleared so it means it has to be Resets_ZF rather than Writes_ZF, right?

@Wraith2 Wraith2 Jun 9, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure i'm qualified to answer. The manual lists the pseudo code for the operation as:

temp ← (SRC-1) bitwiseAND ( SRC );
SF ← temp[OperandSize -1];
ZF ← (temp = 0);
IF SRC = 0
CF ← 1;
ELSE
CF ← 0;
FI
DEST ← temp;

which to me writes to SF ZF and CF, additionally it says that OF is cleared which I read as being reset.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so is ZF, even from your pseudocode it looks like it's always cleared
And the doc says ZF and OF flags are cleared.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, changed.

Comment on lines +4152 to +4153
if (((addOp2->gtFlags & GTF_SET_FLAGS) != 0) || ((op2->gtFlags & GTF_SET_FLAGS) != 0) ||
((xorNode->gtFlags & GTF_SET_FLAGS) != 0))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Correct if I am wrong but you only need to check xorNode here, e.g. if (xorNode->gtSetFlags())

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Possibly, I copied the check used in TryLowerAndOpToResetLowestSetBit Would ci pick up if it doesn't work?

@EgorBo

EgorBo commented Jun 9, 2022

Copy link
Copy Markdown
Member

/azp run Fuzzlyn

@azure-pipelines

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

@EgorBo
EgorBo merged commit 7b1fd25 into dotnet:main Jun 13, 2022
@EgorBo

EgorBo commented Jun 13, 2022

Copy link
Copy Markdown
Member

@Wraith2 thanks! 🙂

@Wraith2
Wraith2 deleted the add-xarch-blsmsk branch June 13, 2022 19:56
@ghost ghost locked as resolved and limited conversation to collaborators Jul 14, 2022
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 community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants