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

System.Runtime.Intrinsics.Arm.Sha256 APIs are unavailable on Apple Silicon #62832

Closed
neon-sunset opened this issue Dec 15, 2021 · 7 comments · Fixed by #62958
Closed

System.Runtime.Intrinsics.Arm.Sha256 APIs are unavailable on Apple Silicon #62832

neon-sunset opened this issue Dec 15, 2021 · 7 comments · Fixed by #62958
Assignees
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner

Comments

@neon-sunset
Copy link
Contributor

neon-sunset commented Dec 15, 2021

Description

It appears that System.Runtime.Intrinsics.Arm.Sha256.IsSupported returns false on M1 Pro and, consequently, fails JIT when attempting to use the corresponding intrinsics.

Please let me know if you need any additional details. Unfortunately, I have no experience with C++ specifically but otherwise would be happy to investigate if you point me to the right direction where to look for the bits and pieces responsible for CPU feature detection on macOS.

Reproduction Steps

Execute the following snippet:

using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;

Console.WriteLine(
    $"{nameof(ArmBase)}:{ArmBase.IsSupported}, " +
    $"{nameof(ArmBase.Arm64)}:{ArmBase.Arm64.IsSupported}, " +
    $"{nameof(AdvSimd)}:{AdvSimd.IsSupported}, " +
    $"{nameof(Sha256)}:{Sha256.IsSupported}\n");

var msg0 = Vector128<uint>.Zero;
var msg1 = Vector128<uint>.Zero;

msg0 = Sha256.ScheduleUpdate0(msg0, msg1);

Expected behavior

Output:

ArmBase:True, Arm64:True, AdvSimd:True, Sha256:True

Actual behavior

Output:

ArmBase:True, Arm64:True, AdvSimd:True, Sha256:False

Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.Arm.Sha256.ScheduleUpdate0(Vector128`1 w0_3, Vector128`1 w4_7)
   at Program.<Main>$(String[] args) in /Users/____/Code/LOC/Sha256Repro/Program.cs:line 11

Regression?

No response

Known Workarounds

No response

Configuration

NB: Reproducible with both 6.0.100 and 6.0.101
dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  12.1
 OS Platform: Darwin
 RID:         osx.12-arm64
 Base Path:   /usr/local/share/dotnet/sdk/6.0.101/

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

sysctl -a | grep hw.optional:

hw.optional.arm.FEAT_AES: 1
hw.optional.arm.FEAT_BF16: 0
hw.optional.arm.FEAT_BTI: 0
hw.optional.arm.FEAT_CSV2: 1
hw.optional.arm.FEAT_CSV3: 1
hw.optional.arm.FEAT_DPB: 1
hw.optional.arm.FEAT_DPB2: 1
hw.optional.arm.FEAT_DotProd: 1
hw.optional.arm.FEAT_ECV: 1
hw.optional.arm.FEAT_FCMA: 1
hw.optional.arm.FEAT_FHM: 1
hw.optional.arm.FEAT_FP16: 1
hw.optional.arm.FEAT_FPAC: 0
hw.optional.arm.FEAT_FRINTTS: 1
hw.optional.arm.FEAT_FlagM: 1
hw.optional.arm.FEAT_FlagM2: 1
hw.optional.arm.FEAT_I8MM: 0
hw.optional.arm.FEAT_JSCVT: 1
hw.optional.arm.FEAT_LRCPC: 1
hw.optional.arm.FEAT_LRCPC2: 1
hw.optional.arm.FEAT_LSE: 1
hw.optional.arm.FEAT_LSE2: 1
hw.optional.arm.FEAT_PAuth: 1
hw.optional.arm.FEAT_PAuth2: 0
hw.optional.arm.FEAT_PMULL: 1
hw.optional.arm.FEAT_RDM: 1
hw.optional.arm.FEAT_SB: 1
hw.optional.arm.FEAT_SHA1: 1
hw.optional.arm.FEAT_SHA256: 1
hw.optional.arm.FEAT_SHA3: 1
hw.optional.arm.FEAT_SHA512: 1
hw.optional.arm.FEAT_SPECRES: 0
hw.optional.arm.FEAT_SSBS: 1
hw.optional.AdvSIMD: 1
hw.optional.AdvSIMD_HPFPCvt: 1
hw.optional.arm64: 1
hw.optional.armv8_1_atomics: 1
hw.optional.armv8_2_fhm: 1
hw.optional.armv8_2_sha3: 1
hw.optional.armv8_2_sha512: 1
hw.optional.armv8_3_compnum: 1
hw.optional.armv8_crc32: 1
hw.optional.armv8_gpi: 1
hw.optional.breakpoint: 6
hw.optional.floatingpoint: 1
hw.optional.neon: 1
hw.optional.neon_fp16: 1
hw.optional.neon_hpfp: 1
hw.optional.ucnormal_mem: 1
hw.optional.watchpoint: 4

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI untriaged New issue has not been triaged by the area owner labels Dec 15, 2021
@ghost
Copy link

ghost commented Dec 15, 2021

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

Issue Details

Description

It appears that System.Runtime.Intrinsics.Arm.Sha256.IsSupported returns false on M1 Pro and, consequently, fails JIT when attempting to use the corresponding intrinsics.

Please let me know if you need any additional details. Unfortunately, I have no experience with C++ specifically but otherwise would be happy to investigate if you point me to the right direction where to look for the bits and pieces responsible for CPU feature detection on macOS.

Reproduction Steps

Execute the following snippet:

using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.Arm;

Console.WriteLine(
    $"{nameof(ArmBase)}:{ArmBase.IsSupported}, " +
    $"{nameof(ArmBase.Arm64)}:{ArmBase.Arm64.IsSupported}, " +
    $"{nameof(AdvSimd)}:{AdvSimd.IsSupported}, " +
    $"{nameof(Sha256)}:{Sha256.IsSupported}\n");

var msg0 = Vector128<uint>.Zero;
var msg1 = Vector128<uint>.Zero;

msg0 = Sha256.ScheduleUpdate0(msg0, msg1);

Expected behavior

Output:

ArmBase:True, Arm64:True, AdvSimd:True, Sha256:False

Actual behavior

Output:

ArmBase:True, Arm64:True, AdvSimd:True, Sha256:False

Unhandled exception. System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.Runtime.Intrinsics.Arm.Sha256.ScheduleUpdate0(Vector128`1 w0_3, Vector128`1 w4_7)
   at Program.<Main>$(String[] args) in /Users/____/Code/LOC/Sha256Repro/Program.cs:line 11

Regression?

No response

Known Workarounds

No response

Configuration

NB: Reproducible with both 6.0.100 and 6.0.101
dotnet --info

.NET SDK (reflecting any global.json):
 Version:   6.0.101
 Commit:    ef49f6213a

Runtime Environment:
 OS Name:     Mac OS X
 OS Version:  12.1
 OS Platform: Darwin
 RID:         osx.12-arm64
 Base Path:   /usr/local/share/dotnet/sdk/6.0.101/

Host (useful for support):
  Version: 6.0.1
  Commit:  3a25a7f1cc

sysctl -a | grep hw.optional:

hw.optional.arm.FEAT_AES: 1
hw.optional.arm.FEAT_BF16: 0
hw.optional.arm.FEAT_BTI: 0
hw.optional.arm.FEAT_CSV2: 1
hw.optional.arm.FEAT_CSV3: 1
hw.optional.arm.FEAT_DPB: 1
hw.optional.arm.FEAT_DPB2: 1
hw.optional.arm.FEAT_DotProd: 1
hw.optional.arm.FEAT_ECV: 1
hw.optional.arm.FEAT_FCMA: 1
hw.optional.arm.FEAT_FHM: 1
hw.optional.arm.FEAT_FP16: 1
hw.optional.arm.FEAT_FPAC: 0
hw.optional.arm.FEAT_FRINTTS: 1
hw.optional.arm.FEAT_FlagM: 1
hw.optional.arm.FEAT_FlagM2: 1
hw.optional.arm.FEAT_I8MM: 0
hw.optional.arm.FEAT_JSCVT: 1
hw.optional.arm.FEAT_LRCPC: 1
hw.optional.arm.FEAT_LRCPC2: 1
hw.optional.arm.FEAT_LSE: 1
hw.optional.arm.FEAT_LSE2: 1
hw.optional.arm.FEAT_PAuth: 1
hw.optional.arm.FEAT_PAuth2: 0
hw.optional.arm.FEAT_PMULL: 1
hw.optional.arm.FEAT_RDM: 1
hw.optional.arm.FEAT_SB: 1
hw.optional.arm.FEAT_SHA1: 1
hw.optional.arm.FEAT_SHA256: 1
hw.optional.arm.FEAT_SHA3: 1
hw.optional.arm.FEAT_SHA512: 1
hw.optional.arm.FEAT_SPECRES: 0
hw.optional.arm.FEAT_SSBS: 1
hw.optional.AdvSIMD: 1
hw.optional.AdvSIMD_HPFPCvt: 1
hw.optional.arm64: 1
hw.optional.armv8_1_atomics: 1
hw.optional.armv8_2_fhm: 1
hw.optional.armv8_2_sha3: 1
hw.optional.armv8_2_sha512: 1
hw.optional.armv8_3_compnum: 1
hw.optional.armv8_crc32: 1
hw.optional.armv8_gpi: 1
hw.optional.breakpoint: 6
hw.optional.floatingpoint: 1
hw.optional.neon: 1
hw.optional.neon_fp16: 1
hw.optional.neon_hpfp: 1
hw.optional.ucnormal_mem: 1
hw.optional.watchpoint: 4

Other information

No response

Author: neon-sunset
Assignees: -
Labels:

area-CodeGen-coreclr, untriaged

Milestone: -

@EgorBo
Copy link
Member

EgorBo commented Dec 15, 2021

Looks like some ISA checks are missed here https://github.com/dotnet/runtime/blob/main/src/coreclr/pal/src/misc/jitsupport.cpp#L263 I'll take a look on my m1

@EgorBo
Copy link
Member

EgorBo commented Dec 15, 2021

I assume we need to check these:

hw.optional.arm.FEAT_AES
hw.optional.arm.FEAT_DotProd
hw.optional.arm.FEAT_RDM
hw.optional.arm.FEAT_SHA256

the only small problem that macOS 11.x on M1 doesn't report these, they were added in macOS 12.0 but I guess it's ok - I'm updating my M1 mini to verify it now

@neon-sunset
Copy link
Contributor Author

On one hand we can't just assume SHA256 is supported because according to https://developer.arm.com/documentation/ddi0487/gb/ page 278 the cryptographic extensions instruction set is optional.

On the other, all osx-arm64 targets support SHA256 and other bits so I think we can safely assume a specific minimum supported feature set on darwin.

You can see the same assumptions made in various codebases:

p.s.: yes, getauxval is unavailable on macOS.

@EgorBo
Copy link
Member

EgorBo commented Dec 15, 2021

@neon-sunset are you interested in contributing it?

@neon-sunset
Copy link
Contributor Author

That would be awesome. Will probably get this fixed over the upcoming weekend 😀

@neon-sunset
Copy link
Contributor Author

neon-sunset commented Dec 17, 2021

Change submitted in #62958

Despite flag detection now performing as expected, I think this is a somewhat fragile approach and a better option would be to implement it directly in coreclr/pal via reading AT_HWCAP and other registers data and moving away from sysctlbyname or getauxval env dependencies completely. I'm currently looking into this but would prefer submitting it in the future as a separate change if you agree on going forward with it.

CC: @EgorBo

@ghost ghost added in-pr There is an active PR which will close this issue when it is merged and removed in-pr There is an active PR which will close this issue when it is merged labels Dec 17, 2021
echesakov pushed a commit that referenced this issue Jan 4, 2022
* Extend CPU capabilities detection for osx-arm64 (#62832)

* Revert uncoditional enable for dczva on osx-arm64
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Jan 4, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Feb 3, 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 untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants