-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Changes from all commits
4b06ac6
e53ec69
25b678f
4417b32
bdff4f9
cfac1ff
1ff9026
25cd48c
1089f88
7d2c5a8
7307578
c216c79
8ae24ac
15a6f82
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7233,6 +7233,34 @@ bool GenTree::OperMayThrow(Compiler* comp) | |
| return OperExceptions(comp) != ExceptionSetFlags::None; | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // NodeOrContainedOperandsMayThrow : Check whether the operation or any contained | ||
| // children will throw | ||
| // | ||
| // Arguments: | ||
| // comp - Compiler instance | ||
| // | ||
| // Return Value: | ||
| // True if the given operator or contained children may cause an exception | ||
| // | ||
| bool GenTree::NodeOrContainedOperandsMayThrow(Compiler* comp) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| { | ||
| if (OperMayThrow(comp)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| // Check all contained children | ||
| for (GenTree* operand : Operands()) | ||
| { | ||
| if (operand->isContained() && operand->NodeOrContainedOperandsMayThrow(comp)) | ||
| { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| //------------------------------------------------------------------------------ | ||
| // OperRequiresGlobRefFlag : Check whether the operation requires GTF_GLOB_REF | ||
| // flag regardless of the children's flags. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) \ | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| JITDUMP(label ":\n"); \ | ||
| if (JitTls::GetCompiler()->verbose) \ | ||
| JitTls::GetCompiler()->gtDispTreeRange(range, t); \ | ||
| JITDUMP("\n"); | ||
| #define DISPBLOCK(b) \ | ||
| if (JitTls::GetCompiler()->verbose) \ | ||
| JitTls::GetCompiler()->fgTableDispBasicBlock(b); | ||
|
|
@@ -609,6 +614,7 @@ const bool dspGCtbls = true; | |
| #define DISPSTMT(t) | ||
| #define DISPRANGE(range) | ||
| #define DISPTREERANGE(range, t) | ||
| #define LABELEDDISPTREERANGE(title, range, t) | ||
| #define DISPBLOCK(b) | ||
| #define VERBOSE 0 | ||
| #endif // !DEBUG | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| // Unit tests for the masks conversion optimization | ||
| // Uses vectors as masks and vice versa. | ||
|
|
||
| using System; | ||
| using System.Numerics; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Runtime.InteropServices; | ||
| using System.Runtime.Intrinsics; | ||
| using System.Runtime.Intrinsics.Arm; | ||
| using System.Threading; | ||
| using Xunit; | ||
|
|
||
| public class EmbeddedLoads | ||
| { | ||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| private static void Consume<T>(T value) { } | ||
|
|
||
| [Fact] | ||
| public static void TestEntryPoint() | ||
| { | ||
|
|
||
| if (Sve.IsSupported) | ||
| { | ||
| int[] array = new int[10]; | ||
|
|
||
| Vector<int> op1 = Vector.Create<int>(11); | ||
| Vector<int> op2 = Vector.Create<int>(22); | ||
| Vector<int> op3 = Vector.Create<int>(33); | ||
| Vector<long> opl1 = Vector.Create<long>(44); | ||
| Vector<long> opl2 = Vector.Create<long>(55); | ||
|
|
||
| CndSelectEmbeddedOp3LoadTrueMask(array, op1); | ||
| CndSelectEmbeddedOp3LoadAllBits(array, op1); | ||
| CndSelectEmbeddedOp3LoadFalseMask(array, op1); | ||
| CndSelectEmbeddedOp3LoadZero(array, op1); | ||
| } | ||
| } | ||
|
|
||
| // SVE load operation with embedded mask inside a conditional select | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static unsafe void CndSelectEmbeddedOp3LoadTrueMask(int[] array, Vector<int> op1) { | ||
| //ARM6-FULL-LINE: ldnf1w { {{z[0-9]+}}.s }, {{p[0-9]+}}/m, [{{x[0-9]+}}] | ||
| fixed (int* arr_ptr = array) | ||
| { | ||
| var result = Sve.ConditionalSelect(Sve.CreateTrueMaskInt32(), op1, Sve.LoadVectorNonFaulting(arr_ptr)); | ||
a74nh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Consume(result); | ||
| } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static unsafe void CndSelectEmbeddedOp3LoadAllBits(int[] array, Vector<int> op1) { | ||
| //ARM6-FULL-LINE: ldnf1w { {{z[0-9]+}}.s }, {{p[0-9]+}}/m, [{{x[0-9]+}}] | ||
| fixed (int* arr_ptr = array) | ||
| { | ||
| var result = Sve.ConditionalSelect(Vector<int>.AllBitsSet, op1, Sve.LoadVectorNonFaulting(arr_ptr)); | ||
| Consume(result); | ||
| } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static unsafe void CndSelectEmbeddedOp3LoadFalseMask(int[] array, Vector<int> op1) { | ||
| //ARM6-FULL-LINE: ldnf1w { {{z[0-9]+}}.s }, {{p[0-9]+}}/m, [{{x[0-9]+}}] | ||
| fixed (int* arr_ptr = array) | ||
| { | ||
| var result = Sve.ConditionalSelect(Sve.CreateFalseMaskInt32(), op1, Sve.LoadVectorNonFaulting(arr_ptr)); | ||
| Consume(result); | ||
| } | ||
| } | ||
|
|
||
| [MethodImpl(MethodImplOptions.NoInlining)] | ||
| static unsafe void CndSelectEmbeddedOp3LoadZero(int[] array, Vector<int> op1) { | ||
| //ARM6-FULL-LINE: ldnf1w { {{z[0-9]+}}.s }, {{p[0-9]+}}/m, [{{x[0-9]+}}] | ||
| fixed (int* arr_ptr = array) | ||
| { | ||
| var result = Sve.ConditionalSelect(Vector<int>.Zero, op1, Sve.LoadVectorNonFaulting(arr_ptr)); | ||
| Consume(result); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <!-- Needed for CLRTestEnvironmentVariable --> | ||
| <RequiresProcessIsolation>true</RequiresProcessIsolation> | ||
| </PropertyGroup> | ||
| <PropertyGroup> | ||
| <DebugType>None</DebugType> | ||
| <Optimize>True</Optimize> | ||
| <NoWarn>$(NoWarn),SYSLIB5003</NoWarn> | ||
| </PropertyGroup> | ||
| <ItemGroup> | ||
| <Compile Include="$(MSBuildProjectName).cs"> | ||
| <HasDisasmCheck>true</HasDisasmCheck> | ||
| </Compile> | ||
|
|
||
| <CLRTestEnvironmentVariable Include="DOTNET_TieredCompilation" Value="0" /> | ||
| <CLRTestEnvironmentVariable Include="DOTNET_JITMinOpts" Value="0" /> | ||
a74nh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <CLRTestEnvironmentVariable Include="DOTNET_EnableHWIntrinsic" Value="1" /> | ||
| </ItemGroup> | ||
| </Project> | ||
There was a problem hiding this comment.
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
ConditionalSelectand contains something that throws inop1orop3? Should we check for those as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also
OperMayThrowshould be kept in sync withOperExceptions. It does not make sense for the latter to return an empty set of the former returns true.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These methods like
OperMayThrow,OperRequiresCallFlagetc. 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
OperMayThrowthat 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.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Not keen on the new name (
OperOrEmbeddedChildrenMayThrow())Specifically, it still checks IsEmbeddingMaskOp() and not containment (as there is a period where the nodes are not yet contained).
Also done.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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_OPflag and insteadOperOrEmbeddedChildrenMayThrow()would check nodes based on containing.??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed it up to work that way
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
OperMayThrowto check child nodes is outside the contract of whatOperMayThrowis 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 haveGTF_EXCEPT. So changingOperMayThrowcould 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_EXCEPTfor LIR. I think it makes sense to introduce a version ofOperMayThrowthat does that. I would suggest calling it something likeNodeOrContainedOperandsMayThrow().There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 forxarchin several cases).