From b20cf2a0223ff0e62edea7c4fe987a07839d4cd1 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Thu, 30 Jun 2022 15:34:08 -0700 Subject: [PATCH 01/10] Define_InterlockMethod macro --- src/coreclr/pal/inc/pal.h | 272 ++++++++++++++++---------------------- 1 file changed, 114 insertions(+), 158 deletions(-) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index dd7e99c665b100..eb68e0e2267b49 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -3466,6 +3466,42 @@ FORCEINLINE void PAL_ArmInterlockedOperationBarrier() #endif } +#if defined(HOST_ARM64) + +#define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ +__attribute__((target("lse"))) \ +EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI Lse_##METHOD_DECL \ +{ \ + return INTRINSIC_NAME; \ +} \ + \ +EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ +{ \ + if (g_arm64_atomics_present) \ + { \ + return Lse_##METHOD_INVOC; \ + } \ + else \ + { \ + RETURN_TYPE result = INTRINSIC_NAME; \ + PAL_ArmInterlockedOperationBarrier(); \ + return result; \ + } \ +} \ + +#else + +#define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ +EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ +{ \ + RETURN_TYPE result = INTRINSIC_NAME; \ + PAL_ArmInterlockedOperationBarrier(); \ + return result; \ +} \ + +#endif + + /*++ Function: InterlockedAdd @@ -3486,33 +3522,19 @@ Return Values The return value is the resulting added value. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedAdd( - IN OUT LONG volatile *lpAddend, - IN LONG value) -{ - LONG result = __sync_add_and_fetch(lpAddend, value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedAdd64( - IN OUT LONGLONG volatile *lpAddend, - IN LONGLONG value) -{ - LONGLONG result = __sync_add_and_fetch(lpAddend, value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedAdd( IN OUT LONG volatile *lpAddend, IN LONG value), + InterlockedAdd(lpAddend, value), + __sync_add_and_fetch(lpAddend, value) +) + +Define_InterlockMethod( + LONGLONG, + InterlockedAdd64(IN OUT LONGLONG volatile *lpAddend, IN LONGLONG value), + InterlockedAdd64(lpAddend, value), + __sync_add_and_fetch(lpAddend, value) +) /*++ Function: @@ -3533,31 +3555,19 @@ Return Values The return value is the resulting incremented value. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedIncrement( - IN OUT LONG volatile *lpAddend) -{ - LONG result = __sync_add_and_fetch(lpAddend, (LONG)1); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedIncrement64( - IN OUT LONGLONG volatile *lpAddend) -{ - LONGLONG result = __sync_add_and_fetch(lpAddend, (LONGLONG)1); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedIncrement(IN OUT LONG volatile *lpAddend), + InterlockedIncrement(lpAddend), + __sync_add_and_fetch(lpAddend, (LONG)1) +) + +Define_InterlockMethod( + LONGLONG, + InterlockedIncrement64(IN OUT LONGLONG volatile *lpAddend), + InterlockedIncrement64(lpAddend), + __sync_add_and_fetch(lpAddend, (LONGLONG)1) +) /*++ Function: @@ -3578,33 +3588,21 @@ Return Values The return value is the resulting decremented value. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedDecrement( - IN OUT LONG volatile *lpAddend) -{ - LONG result = __sync_sub_and_fetch(lpAddend, (LONG)1); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedDecrement(IN OUT LONG volatile *lpAddend), + InterlockedDecrement(lpAddend), + __sync_sub_and_fetch(lpAddend, (LONG)1) +) #define InterlockedDecrementRelease InterlockedDecrement -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedDecrement64( - IN OUT LONGLONG volatile *lpAddend) -{ - LONGLONG result = __sync_sub_and_fetch(lpAddend, (LONGLONG)1); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONGLONG, + InterlockedDecrement64(IN OUT LONGLONG volatile *lpAddend), + InterlockedDecrement64(lpAddend), + __sync_sub_and_fetch(lpAddend, (LONGLONG)1) +) /*++ Function: @@ -3627,33 +3625,19 @@ Return Values The function returns the initial value pointed to by Target. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedExchange( - IN OUT LONG volatile *Target, - IN LONG Value) -{ - LONG result = __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedExchange64( - IN OUT LONGLONG volatile *Target, - IN LONGLONG Value) -{ - LONGLONG result = __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedExchange(IN OUT LONG volatile *Target, LONG Value), + InterlockedExchange(Target, Value), + __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL) +) + +Define_InterlockMethod( + LONGLONG, + InterlockedExchange64(IN OUT LONGLONG volatile *Target, IN LONGLONG Value), + InterlockedExchange64(Target, Value), + __atomic_exchange_n(Target, Value, __ATOMIC_ACQ_REL) +) /*++ Function: @@ -3737,61 +3721,33 @@ Return Values The return value is the original value that 'Addend' pointed to. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedExchangeAdd( - IN OUT LONG volatile *Addend, - IN LONG Value) -{ - LONG result = __sync_fetch_and_add(Addend, Value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedExchangeAdd64( - IN OUT LONGLONG volatile *Addend, - IN LONGLONG Value) -{ - LONGLONG result = __sync_fetch_and_add(Addend, Value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedAnd( - IN OUT LONG volatile *Destination, - IN LONG Value) -{ - LONG result = __sync_fetch_and_and(Destination, Value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} - -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedOr( - IN OUT LONG volatile *Destination, - IN LONG Value) -{ - LONG result = __sync_fetch_and_or(Destination, Value); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedExchangeAdd(IN OUT LONG volatile *Addend, IN LONG Value), + InterlockedExchangeAdd(Addend, Value), + __sync_fetch_and_add(Addend, Value) +) + +Define_InterlockMethod( + LONGLONG, + InterlockedExchangeAdd64(IN OUT LONGLONG volatile *Addend, IN LONGLONG Value), + InterlockedExchangeAdd64(Addend, Value), + __sync_fetch_and_add(Addend, Value) +) + +Define_InterlockMethod( + LONG, + InterlockedAnd(IN OUT LONG volatile *Destination, IN LONG Value), + InterlockedAnd(Destination, Value), + __sync_fetch_and_and(Destination, Value) +) + +Define_InterlockMethod( + LONG, + InterlockedOr(IN OUT LONG volatile *Destination, IN LONG Value), + InterlockedOr(Destination, Value), + __sync_fetch_and_or(Destination, Value) +) #if defined(HOST_64BIT) #define InterlockedExchangePointer(Target, Value) \ From 8ed74219085d1473f5a856b8546fded1b3db05f3 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Thu, 30 Jun 2022 15:34:29 -0700 Subject: [PATCH 02/10] compiler failure --- src/coreclr/pal/inc/pal.h | 2 +- src/coreclr/pal/src/init/pal.cpp | 6 ++++++ src/coreclr/utilcode/util.cpp | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index eb68e0e2267b49..7267764c9cbf9b 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -3466,7 +3466,7 @@ FORCEINLINE void PAL_ArmInterlockedOperationBarrier() #endif } -#if defined(HOST_ARM64) +#if defined(TARGET_ARM64) #define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ __attribute__((target("lse"))) \ diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index a5a6ac15743bab..7a7fec4856d09b 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -106,6 +106,12 @@ extern "C" BOOL CRTInitStdStreams( void ); extern bool g_running_in_exe; +// #if defined(TARGET_ARM64) +// // Flag to check if atomics feature is available on +// // the machine +// bool g_arm64_atomics_present = false; +// #endif + Volatile init_count = 0; Volatile shutdown_intent = 0; Volatile g_coreclrInitialized = 0; diff --git a/src/coreclr/utilcode/util.cpp b/src/coreclr/utilcode/util.cpp index b4fec28360d24d..f73476a4127736 100644 --- a/src/coreclr/utilcode/util.cpp +++ b/src/coreclr/utilcode/util.cpp @@ -24,7 +24,7 @@ #ifndef DACCESS_COMPILE UINT32 g_nClrInstanceId = 0; -#if defined(TARGET_WINDOWS) && defined(TARGET_ARM64) +#if defined(TARGET_ARM64) // Flag to check if atomics feature is available on // the machine bool g_arm64_atomics_present = false; From 7ead20a28f695532fbd5004d3bb52c919e1de31f Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Thu, 30 Jun 2022 16:00:09 -0700 Subject: [PATCH 03/10] fix build errors --- src/coreclr/debug/createdump/datatarget.cpp | 6 ++++++ src/coreclr/pal/src/init/pal.cpp | 10 +++++----- src/coreclr/utilcode/util.cpp | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/coreclr/debug/createdump/datatarget.cpp b/src/coreclr/debug/createdump/datatarget.cpp index d2efc6e2615f39..e06f97548d8f1e 100644 --- a/src/coreclr/debug/createdump/datatarget.cpp +++ b/src/coreclr/debug/createdump/datatarget.cpp @@ -3,6 +3,12 @@ #include "createdump.h" +#if defined(TARGET_ARM64) +// Flag to check if atomics feature is available on +// the machine +bool g_arm64_atomics_present = false; +#endif + DumpDataTarget::DumpDataTarget(CrashInfo& crashInfo) : m_ref(1), m_crashInfo(crashInfo) diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index 7a7fec4856d09b..94085b8299ae4d 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -106,11 +106,11 @@ extern "C" BOOL CRTInitStdStreams( void ); extern bool g_running_in_exe; -// #if defined(TARGET_ARM64) -// // Flag to check if atomics feature is available on -// // the machine -// bool g_arm64_atomics_present = false; -// #endif +#if defined(TARGET_ARM64) +// Flag to check if atomics feature is available on +// the machine +bool g_arm64_atomics_present = false; +#endif Volatile init_count = 0; Volatile shutdown_intent = 0; diff --git a/src/coreclr/utilcode/util.cpp b/src/coreclr/utilcode/util.cpp index f73476a4127736..b4fec28360d24d 100644 --- a/src/coreclr/utilcode/util.cpp +++ b/src/coreclr/utilcode/util.cpp @@ -24,7 +24,7 @@ #ifndef DACCESS_COMPILE UINT32 g_nClrInstanceId = 0; -#if defined(TARGET_ARM64) +#if defined(TARGET_WINDOWS) && defined(TARGET_ARM64) // Flag to check if atomics feature is available on // the machine bool g_arm64_atomics_present = false; From eb21958393a122f7390aff26781389225674cc52 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Thu, 30 Jun 2022 16:00:24 -0700 Subject: [PATCH 04/10] Set g_arm64_atomics_present at common place --- src/coreclr/vm/codeman.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/coreclr/vm/codeman.cpp b/src/coreclr/vm/codeman.cpp index 7ee4f99c54c65b..7ef3a86fa9f609 100644 --- a/src/coreclr/vm/codeman.cpp +++ b/src/coreclr/vm/codeman.cpp @@ -1566,7 +1566,6 @@ void EEJitManager::SetCpuInfo() if (IsProcessorFeaturePresent(PF_ARM_V81_ATOMIC_INSTRUCTIONS_AVAILABLE)) { CPUCompileFlags.Set(InstructionSet_Atomics); - g_arm64_atomics_present = true; } // PF_ARM_V82_DP_INSTRUCTIONS_AVAILABLE (43) @@ -1584,6 +1583,11 @@ void EEJitManager::SetCpuInfo() // We set the flag when the instruction is permitted and the block size is 64 bytes. CPUCompileFlags.Set(InstructionSet_Dczva); } + + if (CPUCompileFlags.IsSet(InstructionSet_Atomics)) + { + g_arm64_atomics_present = true; + } #endif // TARGET_ARM64 // Now that we've queried the actual hardware support, we need to adjust what is actually supported based From 73ee366207e5b5644baddffaa06ee0d2b6dd44f7 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 1 Jul 2022 07:34:08 -0700 Subject: [PATCH 05/10] Fix the missing declaration --- src/coreclr/pal/inc/pal.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index 7267764c9cbf9b..c08b1e64ed6159 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -74,6 +74,12 @@ extern "C" { // On Unix systems, NATIVE_LIBRARY_HANDLE type represents a library handle not registered with the PAL. typedef PVOID NATIVE_LIBRARY_HANDLE; +#if defined(TARGET_ARM64) +// Flag to check if atomics feature is available on +// the machine +extern bool g_arm64_atomics_present; +#endif + /******************* Processor-specific glue *****************************/ #ifndef _MSC_VER From 71be17ced8ab6124c6d5c3d84fb651a2b32fb677 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 1 Jul 2022 08:06:55 -0700 Subject: [PATCH 06/10] Change TARGET_ARM64 => HOST_ARM64 --- src/coreclr/debug/createdump/datatarget.cpp | 2 +- src/coreclr/pal/inc/pal.h | 4 ++-- src/coreclr/pal/src/init/pal.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/debug/createdump/datatarget.cpp b/src/coreclr/debug/createdump/datatarget.cpp index e06f97548d8f1e..5a4438fca5f086 100644 --- a/src/coreclr/debug/createdump/datatarget.cpp +++ b/src/coreclr/debug/createdump/datatarget.cpp @@ -3,7 +3,7 @@ #include "createdump.h" -#if defined(TARGET_ARM64) +#if defined(HOST_ARM64) // Flag to check if atomics feature is available on // the machine bool g_arm64_atomics_present = false; diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index c08b1e64ed6159..fa7a39dc5c49f8 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -74,7 +74,7 @@ extern "C" { // On Unix systems, NATIVE_LIBRARY_HANDLE type represents a library handle not registered with the PAL. typedef PVOID NATIVE_LIBRARY_HANDLE; -#if defined(TARGET_ARM64) +#if defined(HOST_ARM64) // Flag to check if atomics feature is available on // the machine extern bool g_arm64_atomics_present; @@ -3472,7 +3472,7 @@ FORCEINLINE void PAL_ArmInterlockedOperationBarrier() #endif } -#if defined(TARGET_ARM64) +#if defined(HOST_ARM64) #define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ __attribute__((target("lse"))) \ diff --git a/src/coreclr/pal/src/init/pal.cpp b/src/coreclr/pal/src/init/pal.cpp index 94085b8299ae4d..37576bdf077f8f 100644 --- a/src/coreclr/pal/src/init/pal.cpp +++ b/src/coreclr/pal/src/init/pal.cpp @@ -106,7 +106,7 @@ extern "C" BOOL CRTInitStdStreams( void ); extern bool g_running_in_exe; -#if defined(TARGET_ARM64) +#if defined(HOST_ARM64) // Flag to check if atomics feature is available on // the machine bool g_arm64_atomics_present = false; From f927cf2a87f4633e2e2403d488b77bde4acc2259 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Fri, 1 Jul 2022 15:14:38 -0700 Subject: [PATCH 07/10] Use LSE for InterlockedCompareExchange --- src/coreclr/pal/inc/pal.h | 54 +++++++++++++-------------------------- 1 file changed, 18 insertions(+), 36 deletions(-) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index fa7a39dc5c49f8..b6b6a5936613fc 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -3668,47 +3668,29 @@ Return Values The return value is the initial value of the destination. --*/ -EXTERN_C -PALIMPORT -inline -LONG -PALAPI -InterlockedCompareExchange( - IN OUT LONG volatile *Destination, - IN LONG Exchange, - IN LONG Comperand) -{ - LONG result = - __sync_val_compare_and_swap( - Destination, /* The pointer to a variable whose value is to be compared with. */ - Comperand, /* The value to be compared */ - Exchange /* The value to be stored */); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONG, + InterlockedCompareExchange(IN OUT LONG volatile *Destination, IN LONG Exchange, IN LONG Comperand), + InterlockedCompareExchange(Destination, Exchange, Comperand), + __sync_val_compare_and_swap( + Destination, /* The pointer to a variable whose value is to be compared with. */ + Comperand, /* The value to be compared */ + Exchange /* The value to be stored */) +) #define InterlockedCompareExchangeAcquire InterlockedCompareExchange #define InterlockedCompareExchangeRelease InterlockedCompareExchange // See the 32-bit variant in interlock2.s -EXTERN_C -PALIMPORT -inline -LONGLONG -PALAPI -InterlockedCompareExchange64( - IN OUT LONGLONG volatile *Destination, - IN LONGLONG Exchange, - IN LONGLONG Comperand) -{ - LONGLONG result = - __sync_val_compare_and_swap( - Destination, /* The pointer to a variable whose value is to be compared with. */ - Comperand, /* The value to be compared */ - Exchange /* The value to be stored */); - PAL_ArmInterlockedOperationBarrier(); - return result; -} +Define_InterlockMethod( + LONGLONG, + InterlockedCompareExchange64(IN OUT LONGLONG volatile *Destination, IN LONGLONG Exchange, IN LONGLONG Comperand), + InterlockedCompareExchange64(Destination, Exchange, Comperand), + __sync_val_compare_and_swap( + Destination, /* The pointer to a variable whose value is to be compared with. */ + Comperand, /* The value to be compared */ + Exchange /* The value to be stored */) +) /*++ Function: From b0de604dae883a50c7def5af28c1157a5513d8fa Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Mon, 4 Jul 2022 09:41:41 -0700 Subject: [PATCH 08/10] Attempt to fix osx-arm64 build issue --- src/coreclr/dlls/mscordbi/mscordbi.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/coreclr/dlls/mscordbi/mscordbi.cpp b/src/coreclr/dlls/mscordbi/mscordbi.cpp index afd2cfe8002257..42ad8a090a6f61 100644 --- a/src/coreclr/dlls/mscordbi/mscordbi.cpp +++ b/src/coreclr/dlls/mscordbi/mscordbi.cpp @@ -19,6 +19,13 @@ extern BOOL WINAPI DbgDllMain(HINSTANCE hInstance, DWORD dwReason, //***************************************************************************** extern "C" #ifdef TARGET_UNIX + +#if defined(HOST_ARM64) +// Flag to check if atomics feature is available on +// the machine +bool g_arm64_atomics_present = false; +#endif + DLLEXPORT // For Win32 PAL LoadLibrary emulation #endif BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved) From 800aa30747901b7b0600492796560604dbaf87a7 Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Mon, 4 Jul 2022 10:43:44 -0700 Subject: [PATCH 09/10] Introduce LSE_INSTRUCTIONS_ENABLED_BY_DEFAULT --- eng/native/configurecompiler.cmake | 4 ++++ src/coreclr/pal/inc/pal.h | 21 +++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index 047999bded88ff..ca6905a93c2b4b 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -375,6 +375,10 @@ if (CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_HOST_OSX OR CLR_CMAKE_HOST_MACCATALYST) # We cannot enable "stack-protector-strong" on OS X due to a bug in clang compiler (current version 7.0.2) add_compile_options(-fstack-protector) + if(CLR_CMAKE_HOST_UNIX_ARM64) + # For OSX-Arm64, LSE instructions are enabled by default + add_definitions(-DLSE_INSTRUCTIONS_ENABLED_BY_DEFAULT) + endif(CLR_CMAKE_HOST_UNIX_ARM64) elseif(NOT CLR_CMAKE_HOST_BROWSER) check_c_compiler_flag(-fstack-protector-strong COMPILER_SUPPORTS_F_STACK_PROTECTOR_STRONG) if (COMPILER_SUPPORTS_F_STACK_PROTECTOR_STRONG) diff --git a/src/coreclr/pal/inc/pal.h b/src/coreclr/pal/inc/pal.h index b6b6a5936613fc..124e355560ceee 100644 --- a/src/coreclr/pal/inc/pal.h +++ b/src/coreclr/pal/inc/pal.h @@ -3474,8 +3474,21 @@ FORCEINLINE void PAL_ArmInterlockedOperationBarrier() #if defined(HOST_ARM64) +#if defined(LSE_INSTRUCTIONS_ENABLED_BY_DEFAULT) + #define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ -__attribute__((target("lse"))) \ +EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ +{ \ + return INTRINSIC_NAME; \ +} \ + +#else // !LSE_INSTRUCTIONS_ENABLED_BY_DEFAULT + +#define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ +/* Function multiversioning will never inline a method that is \ + marked such. However, just to make sure that we don't see \ + surprises, explicitely mark them as noinline. */ \ +__attribute__((target("lse"))) __attribute__((noinline)) \ EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI Lse_##METHOD_DECL \ { \ return INTRINSIC_NAME; \ @@ -3495,7 +3508,8 @@ EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ } \ } \ -#else +#endif // LSE_INSTRUCTIONS_ENABLED_BY_DEFAULT +#else // !HOST_ARM64 #define Define_InterlockMethod(RETURN_TYPE, METHOD_DECL, METHOD_INVOC, INTRINSIC_NAME) \ EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ @@ -3505,8 +3519,7 @@ EXTERN_C PALIMPORT inline RETURN_TYPE PALAPI METHOD_DECL \ return result; \ } \ -#endif - +#endif // HOST_ARM64 /*++ Function: From fa2b92d5d91bf65958806af71da8ee723e5a34ff Mon Sep 17 00:00:00 2001 From: Kunal Pathak Date: Wed, 6 Jul 2022 14:49:49 -0700 Subject: [PATCH 10/10] Make sure that compiler knows that M1 has lse --- eng/native/configurecompiler.cmake | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/native/configurecompiler.cmake b/eng/native/configurecompiler.cmake index ca6905a93c2b4b..551a2dc7f2a2bc 100644 --- a/eng/native/configurecompiler.cmake +++ b/eng/native/configurecompiler.cmake @@ -378,6 +378,7 @@ if (CLR_CMAKE_HOST_UNIX) if(CLR_CMAKE_HOST_UNIX_ARM64) # For OSX-Arm64, LSE instructions are enabled by default add_definitions(-DLSE_INSTRUCTIONS_ENABLED_BY_DEFAULT) + add_compile_options(-mcpu=apple-m1) endif(CLR_CMAKE_HOST_UNIX_ARM64) elseif(NOT CLR_CMAKE_HOST_BROWSER) check_c_compiler_flag(-fstack-protector-strong COMPILER_SUPPORTS_F_STACK_PROTECTOR_STRONG)