Skip to content

Arm64 SVE: Do not remove embedded conditional selects #116144

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

Merged
merged 14 commits into from
Jun 12, 2025

Conversation

a74nh
Copy link
Contributor

@a74nh a74nh commented May 30, 2025

Fixes #116140

When an embedded SVE instrinsic is created, during lowering a conditional select is created to embed the operation. This required to ensure there is a mask for the embedded intrinsic to use.

Ensure that the embedding conditional select can only be removed if the embedded intrinsic is also removed.

Fixes dotnet#116140

When an embedded SVE instrinsic is created, during lowering a conditional select is created to embed the operation. This required to ensure there is a mask for the embedded intrinsic to use.

Ensure that the embedding conditional select can only be removed if the embedded intrinsic is also removed.
@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 May 30, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 30, 2025
Copy link
Contributor

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

@@ -591,6 +591,11 @@ const bool dspGCtbls = true;
#define DISPTREERANGE(range, t) \
if (JitTls::GetCompiler()->verbose) \
JitTls::GetCompiler()->gtDispTreeRange(range, t);
#define LABELEDDISPTREERANGE(label, range, t) \
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to remove this, but I do think it's useful. Any suggestions for a better name are welcome

@a74nh a74nh marked this pull request as ready for review May 30, 2025 16:13
@a74nh
Copy link
Contributor Author

a74nh commented May 30, 2025

@kunalspathak
@tannergooding - as mentioned in #115566 (comment)

@a74nh
Copy link
Contributor Author

a74nh commented Jun 6, 2025

Was having a further think about this. I've reworked LowerHWIntrinsicCndSel() a little so that it takes into account the new flag rather than the more convoluted existing checks.

Checked spmidiff and there are no changes.

I'm happy with this PR now.

if (IsEmbeddingMaskOp())
{
assert(this->AsHWIntrinsic()->GetHWIntrinsicId() == NI_Sve_ConditionalSelect);
return this->AsHWIntrinsic()->Op(2)->OperMayThrow(comp);
Copy link
Member

Choose a reason for hiding this comment

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

What if this is user written ConditionalSelect and contains something that throws in op1 or op3? Should we check for those as well?

Copy link
Member

Choose a reason for hiding this comment

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

Also OperMayThrow should be kept in sync with OperExceptions. It does not make sense for the latter to return an empty set of the former returns true.

Copy link
Member

@jakobbotsch jakobbotsch Jun 11, 2025

Choose a reason for hiding this comment

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

What if this is user written ConditionalSelect and contains something that throws in op1 or op3? Should we check for those as well?

These methods like OperMayThrow, OperRequiresCallFlag etc. do not consider operand nodes. They are meant to recompute the flag for the node itself only.

I would rather see us introducing a new variant of OperMayThrow that takes containment into account and which may be used in the rare case that the backend needs it. It should not need hardcoding to a specific oper type like this.

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 would rather see us introducing a new variant of OperMayThrow

Done. Not keen on the new name (OperOrEmbeddedChildrenMayThrow())

that takes containment into account and which may be used in the rare case that the backend needs it. It should not need hardcoding to a specific oper type like this.

Specifically, it still checks IsEmbeddingMaskOp() and not containment (as there is a period where the nodes are not yet contained).

What if this is user written ConditionalSelect and contains something that throws in op1 or op3? Should we check for those as well?

Also done.

Copy link
Member

Choose a reason for hiding this comment

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

Specifically, it still checks IsEmbeddingMaskOp() and not containment (as there is a period where the nodes are not yet contained).

The liveness that is using this runs after lowering. After lowering, all the containment checks should be done. If we are not properly marking these operands as contained during lowering then that seems like a bug. Have we actually introduced another concept of "containment" called "embedding" here?

Copy link
Contributor Author

@a74nh a74nh Jun 11, 2025

Choose a reason for hiding this comment

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

The way I interpreted this comment was that I shouldn't be relying on the changes that happen in lowering. Re-reading now I'm not sure if I still agree.

I'm happy to remove the new GTF_HW_EMBEDDING_OP flag and instead OperOrEmbeddedChildrenMayThrow() would check nodes based on containing.

??

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'm happy to remove the new GTF_HW_EMBEDDING_OP flag and instead OperOrEmbeddedChildrenMayThrow() would check nodes based on containing.

Fixed it up to work that way

Copy link
Member

@jakobbotsch jakobbotsch Jun 11, 2025

Choose a reason for hiding this comment

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

I upvoted that because changing OperMayThrow to check child nodes is outside the contract of what OperMayThrow is supposed to do. I think @tannergooding's comment was about the same thing. HIR uses this function to determine if the evaluation of a node itself may throw, and its contract is supposed to exclude child nodes (function header could be clearer on this point). For checking if a subtree rooted at a node may throw we have GTF_EXCEPT. So changing OperMayThrow could indeed be a pessimization for HIR.

However, in LIR things are different because of containment and because of how evaluation works. The flags no longer make sense there because the operands of the nodes are not actually its children, and are their own thing. The exception of course is containment; evaluation of a node with a contained operand does include the effects of its operand. So we need the spiritual equivalent of GTF_EXCEPT for LIR. I think it makes sense to introduce a version of OperMayThrow that does that. I would suggest calling it something like NodeOrContainedOperandsMayThrow().

Copy link
Member

Choose a reason for hiding this comment

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

I think @tannergooding's comment was about the same thing

Yep. There's quite a bit of nuance between LIR and HIR in some cases and the guarantees we have in place. We generally don't want to make modifications to general IR for something that is LIR only and instead want to have a Lowering::* specific function instead -or- try to work with what LIR may expect, such as by introducing a different LIR specific intrinsic ID (which is what we do for xarch in several cases).

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

Added a question about OperMayThrow

@jakobbotsch
Copy link
Member

/azp run Fuzzlyn

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@a74nh
Copy link
Contributor Author

a74nh commented Jun 11, 2025

Before I update the PR, and the results are lost, here are the results of the fuzzlyn tests:

Screenshot 2025-06-11 at 11 54 45

Copy link
Member

@jakobbotsch jakobbotsch left a comment

Choose a reason for hiding this comment

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

This looks good to me, but need to double check asm/tp diffs

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

LGTM

// Return Value:
// True if the given operator or contained children may cause an exception
//
bool GenTree::NodeOrContainedOperandsMayThrow(Compiler* comp)
Copy link
Member

Choose a reason for hiding this comment

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

At least in short term, this should only be for arm64. We are seeing diffs on x64 because this is also affecting x64.

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

Need to make the NodeOrContainedOperandsMayThrow check only for arm64.

@jakobbotsch
Copy link
Member

Need to make the NodeOrContainedOperandsMayThrow check only for arm64.

I think it's fine to keep this for x64, despite some small regressions. Alternatively we should figure out how x64 deals with the situation for its contained indirections and then do it the same way for arm64.

@kunalspathak
Copy link
Member

Alternatively we should figure out how x64 deals with the situation for its contained indirections and then do it the same way for arm64.

Can this be a follow-up work then, or should it be as part of this PR? I prefer the former.

@jakobbotsch
Copy link
Member

Alternatively we should figure out how x64 deals with the situation for its contained indirections and then do it the same way for arm64.

Can this be a follow-up work then, or should it be as part of this PR? I prefer the former.

I think we should just take this change for all platforms. It's only a handful of regressions on x64, and the logic is equally right everywhere.

@kunalspathak
Copy link
Member

@jakobbotsch - do you know what these errors are? I will rerun those legs.

Status: 403 (Key based authentication is not permitted on this storage account.)
ErrorCode: KeyBasedAuthenticationNotPermitted

@kunalspathak kunalspathak reopened this Jun 11, 2025
@a74nh a74nh closed this Jun 12, 2025
@a74nh a74nh reopened this Jun 12, 2025
@jakobbotsch
Copy link
Member

The superpmi collection after #116484 yesterday failed due to Helix problems, so that's why you're seeing the SPMI failures here. I am rerunning the collection internally.

@jakobbotsch
Copy link
Member

/azp run runtime-coreclr superpmi-diffs

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Member

@kunalspathak kunalspathak left a comment

Choose a reason for hiding this comment

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

LGTM

@kunalspathak
Copy link
Member

/ba-g failures unrelated

@kunalspathak kunalspathak merged commit 3091730 into dotnet:main Jun 12, 2025
119 of 121 checks passed
@a74nh a74nh deleted the cndselfixed_github branch June 13, 2025 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI arm-sve Work related to arm64 SVE/SVE2 support 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.

Arm64 SVE: Assert failure when TrueMask used in conditional select
4 participants