Skip to content
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

Improve the handling of SVE state as part of threadsuspend #105059

Merged
merged 4 commits into from
Jul 22, 2024

Conversation

tannergooding
Copy link
Member

This does not validate that all the handling in pal/context.cpp is correct. There's some known places it isn't handling full width registers and where it may be inconsistent with the Win32 CONTEXT layout.


#define XSTATE_MASK_SVE (UI64(1) << (XSTATE_SVE))

#define XSTATE_ARM64_SVE_BIT (2)
Copy link
Member

Choose a reason for hiding this comment

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

why is this change?

Copy link
Member Author

Choose a reason for hiding this comment

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

The general CONTEXT struct as is currently used by PAL is supposed to mirror the win32 layout/defines so they can translate 1-to-1

The win32 naming convention here is then XSTATE_ARM64_SVE and it is using bit 2, not bit 1

Copy link
Member

Choose a reason for hiding this comment

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

The win32 naming convention here is then XSTATE_ARM64_SVE and it is using bit 2, not bit 1

So wrong bit was set for linux in #103801 as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Right.

I don't think it would technically cause problems because we're just emulating the CONTEXT struct and it doesn't actually have to match the Win32 struct 1-to-1 (since its only going to be other pal APIs consuming it)

But it's overall better/simpler to ensure they match so we don't end up with potential conflicts or other issues.

The best option here would be to just consume the native context_t struct from Unix directly (and not do any of this shimming), but that's a more involved PR.

Copy link
Member

Choose a reason for hiding this comment

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

it doesn't actually have to match the Win32 struct 1-to-1

In fact, it does not match the Win32 struct 1-to-1: #103801 (comment)

The best option here would be to just consume the native context_t struct from Unix directly (and not do any of this shimming), but that's a more involved PR.

+1. The Windows CONTEXT structure is very entrenched throughout CoreCLR VM.

@kunalspathak kunalspathak requested review from janvorli and jkotas July 18, 2024 03:32
@kunalspathak
Copy link
Member

@JasonLinMS

#define XSTATE_SVE (0)

#define XSTATE_MASK_SVE (UI64(1) << (XSTATE_SVE))
#define XSTATE_ARM64_SVE (2)
Copy link
Member Author

Choose a reason for hiding this comment

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

I think there might need to be an additional set of changes in vm/arm64/asmconstants.h as there is currently a SIZEOF__CONTEXT struct that exists and which is meant to represent the size of the struct on Win32 vs UNIX and I believe it is currently incorrect for Unix (or may need additional handling to ensure its large enough to hold the additional SVE state based on the vector length).

Copy link
Member Author

Choose a reason for hiding this comment

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

That shouldn't be related to the Windows handling being added, however, so I've left it to a future PR

Copy link
Member

Choose a reason for hiding this comment

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

I am not sure how it could be incorrect when there is an assert that it matches the sizeof (T_CONTEXT) in the asmconstants.h.

Copy link
Member Author

Choose a reason for hiding this comment

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

The consideration is that SVE registers don’t have a fixed width and so the current logic asserting the size statically is already wrong (it’s assuming SVE registers are exactly 128-bits, when they can be up to 2048 bits)

Copy link
Member

Choose a reason for hiding this comment

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

That's true, but the constant is to represent the size of the CONTEXT structure and that's fixed for now, so it is correct. I believe that when we'll be adding support for longer SVE registers, we should finally move to the Windows way of separate context and extended context, so the SIZEOF__CONTEXT would still be constant - and it will actually match Windows one after that.

@kunalspathak kunalspathak added the arm-sve Work related to arm64 SVE/SVE2 support label Jul 18, 2024
@tannergooding
Copy link
Member Author

There's some handling under https://github.com/dotnet/runtime/blob/main/src/coreclr/vm/excep.h#L772-L777 for AMD64 where it looks to strip CONTEXT_XSTATE, it's not clear if that's also needed for Arm64

Also some handling in src/coreclr/debug/daccess/dacdbiimplstackwalk.cpp and src/coreclr/debug/inc/common.h that likely needs to be covered for the debugger to work.

The Unix handling does need more fixups as well but I think that can wait for a separate PR. This includes ensuring it handles the full register width, that it correctly checks CONTEXT_XSTATE is set prior to doing any SVE handling, etc

#endif // CONTEXT_ARM64_XSTATE

#ifndef CONTEXT_XSTATE
#define CONTEXT_XSTATE CONTEXT_ARM64_XSTATE
Copy link
Member Author

Choose a reason for hiding this comment

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

Windows doesn't define CONTEXT_XSTATE for arm64, it only defines CONTEXT_ARM64_XSTATE. So this is primarily to avoid a lot of other ifdefs elsewhere in the file.

@JulieLeeMSFT
Copy link
Member

@MichalStrehovsky, could you review this PR before the preview 7 snap, 7/22 4pm PST?

@JulieLeeMSFT
Copy link
Member

cc @kunalspathak for a code review.

@JulieLeeMSFT JulieLeeMSFT added this to the 9.0.0 milestone Jul 22, 2024
@janvorli
Copy link
Member

A nit - the title of this PR says it is ensuring saving SVE state on Windows, but it also modifies quite a number of Unix only files. It would be nice to rename it.

Copy link
Member

@janvorli janvorli left a comment

Choose a reason for hiding this comment

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

LGTM

@tannergooding tannergooding changed the title Ensure that Windows is correctly saving SVE state as part of threadsuspend Improve the handling of SVE state as part of threadsuspend Jul 22, 2024
@tannergooding
Copy link
Member Author

I believe this is just waiting on review from @MichalStrehovsky and otherwise should be good to merge

@kunalspathak
Copy link
Member

I believe this is just waiting on review from @MichalStrehovsky and otherwise should be good to merge

Not sure if @MichalStrehovsky might be around until tomorrow. @JulieLeeMSFT - since we already got 2 signoffs, can we merge this in?

@JulieLeeMSFT
Copy link
Member

Yes, let's merge in.

@kunalspathak kunalspathak merged commit 4287101 into dotnet:main Jul 22, 2024
90 checks passed
@tannergooding tannergooding deleted the sve-threadsuspend branch July 22, 2024 23:04
@tannergooding
Copy link
Member Author

This missed the snap and will need to be backported, so it will need to go through tactics for approval.

@JulieLeeMSFT
Copy link
Member

Missed by a minute or so. Please go ahead with a tactics approval.

@tannergooding
Copy link
Member Author

/backport to release/9.0-preview7

Copy link
Contributor

Started backporting to release/9.0-preview7: https://github.com/dotnet/runtime/actions/runs/10051581416

@MichalStrehovsky
Copy link
Member

@MichalStrehovsky, could you review this PR before the preview 7 snap, 7/22 4pm PST?

I know next to nothing about thread suspension, @VSadov FYI

@github-actions github-actions bot locked and limited conversation to collaborators Aug 23, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-PAL-coreclr arm-sve Work related to arm64 SVE/SVE2 support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants