Skip to content

Commit 2023020

Browse files
shushanhfpull[bot]
authored andcommitted
[LoongArch64] Enable TLS on linux/loongarch64 only for static resolver. (#106250)
1 parent 0176860 commit 2023020

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/coreclr/vm/loongarch64/asmhelpers.S

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@ GenerateProfileHelper ProfileTailcall, PROFILE_TAILCALL
10881088
NESTED_ENTRY OnCallCountThresholdReachedStub, _TEXT, NoHandler
10891089
PROLOG_WITH_TRANSITION_BLOCK
10901090

1091-
addi.d $a0, $sp, __PWTB_TransitionBlock // TransitionBlock *
1091+
addi.d $a0, $sp, __PWTB_TransitionBlock // TransitionBlock *
10921092
ori $a1, $t1, 0 // stub-identifying token
10931093
bl C_FUNC(OnCallCountThresholdReached)
10941094
ori $t4,$a0,0
@@ -1112,3 +1112,18 @@ LEAF_ENTRY GetThreadStaticsVariableOffset, _TEXT
11121112
EPILOG_RETURN
11131113
LEAF_END GetThreadStaticsVariableOffset, _TEXT
11141114
// ------------------------------------------------------------------
1115+
1116+
// ------------------------------------------------------------------
1117+
// size_t GetTLSResolverAddress()
1118+
1119+
// Helper to get the TLS resolver address. This will be then used to determine if we have a static or dynamic resolver.
1120+
LEAF_ENTRY GetTLSResolverAddress, _TEXT
1121+
// $fp,$ra
1122+
PROLOG_SAVE_REG_PAIR_INDEXED 22, 1, 16
1123+
pcalau12i $a0, %desc_pc_hi20(t_ThreadStatics)
1124+
addi.d $a0, $a0, %desc_pc_lo12(t_ThreadStatics)
1125+
ld.d $a0, $a0, %desc_ld(t_ThreadStatics)
1126+
EPILOG_RESTORE_REG_PAIR_INDEXED 22, 1, 16
1127+
EPILOG_RETURN
1128+
LEAF_END GetTLSResolverAddress, _TEXT
1129+
// ------------------------------------------------------------------

src/coreclr/vm/threadstatics.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -784,9 +784,9 @@ void FreeTLSIndicesForLoaderAllocator(LoaderAllocator *pLoaderAllocator)
784784

785785
static void* GetTlsIndexObjectAddress();
786786

787-
#if !defined(TARGET_OSX) && defined(TARGET_UNIX) && defined(TARGET_ARM64)
787+
#if !defined(TARGET_OSX) && defined(TARGET_UNIX) && (defined(TARGET_ARM64) || defined(TARGET_LOONGARCH64))
788788
extern "C" size_t GetTLSResolverAddress();
789-
#endif // !TARGET_OSX && TARGET_UNIX && TARGET_ARM64
789+
#endif // !TARGET_OSX && TARGET_UNIX && (TARGET_ARM64 || TARGET_LOONGARCH64)
790790

791791
bool CanJITOptimizeTLSAccess()
792792
{
@@ -833,6 +833,28 @@ bool CanJITOptimizeTLSAccess()
833833
}
834834
#endif
835835
}
836+
#elif defined(TARGET_LOONGARCH64)
837+
// Optimization is enabled for linux/loongarch64 only for static resolver.
838+
// For static resolver, the TP offset is same for all threads.
839+
// For dynamic resolver, TP offset returned is for the current thread and
840+
// will be different for the other threads.
841+
uint32_t* resolverAddress = reinterpret_cast<uint32_t*>(GetTLSResolverAddress());
842+
843+
if (
844+
// ld.d a0, a0, 8
845+
(resolverAddress[0] == 0x28c02084) &&
846+
// ret
847+
(resolverAddress[1] == 0x4c000020)
848+
)
849+
{
850+
optimizeThreadStaticAccess = true;
851+
#ifdef _DEBUG
852+
if (CLRConfig::GetConfigValue(CLRConfig::EXTERNAL_AssertNotStaticTlsResolver) != 0)
853+
{
854+
_ASSERTE(!"Detected static resolver in use when not expected");
855+
}
856+
#endif
857+
}
836858
#else
837859
optimizeThreadStaticAccess = true;
838860
#if !defined(TARGET_OSX) && defined(TARGET_UNIX) && defined(TARGET_AMD64)

0 commit comments

Comments
 (0)