Skip to content

Commit 02dcca1

Browse files
authored
Move the thread alloc context off of Thread in CoreCLR (#103055)
1 parent e3384ac commit 02dcca1

23 files changed

+433
-538
lines changed

src/coreclr/debug/daccess/dacdbiimpl.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4713,8 +4713,16 @@ void DacDbiInterfaceImpl::GetThreadAllocInfo(VMPTR_Thread vmThread,
47134713

47144714
Thread * pThread = vmThread.GetDacPtr();
47154715
gc_alloc_context* allocContext = pThread->GetAllocContext();
4716-
threadAllocInfo->m_allocBytesSOH = allocContext->alloc_bytes - (allocContext->alloc_limit - allocContext->alloc_ptr);
4717-
threadAllocInfo->m_allocBytesUOH = allocContext->alloc_bytes_uoh;
4716+
if (allocContext != nullptr)
4717+
{
4718+
threadAllocInfo->m_allocBytesSOH = allocContext->alloc_bytes - (allocContext->alloc_limit - allocContext->alloc_ptr);
4719+
threadAllocInfo->m_allocBytesUOH = allocContext->alloc_bytes_uoh;
4720+
}
4721+
else
4722+
{
4723+
threadAllocInfo->m_allocBytesSOH = 0;
4724+
threadAllocInfo->m_allocBytesUOH = 0;
4725+
}
47184726
}
47194727

47204728
// Set and reset the TSNC_DebuggerUserSuspend bit on the state of the specified thread

src/coreclr/debug/daccess/request.cpp

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,8 +720,18 @@ ClrDataAccess::GetThreadAllocData(CLRDATA_ADDRESS addr, struct DacpAllocData *da
720720

721721
Thread* thread = PTR_Thread(TO_TADDR(addr));
722722

723-
data->allocBytes = TO_CDADDR(thread->m_alloc_context.alloc_bytes);
724-
data->allocBytesLoh = TO_CDADDR(thread->m_alloc_context.alloc_bytes_uoh);
723+
gc_alloc_context* pAllocContext = thread->GetAllocContext();
724+
725+
if (pAllocContext != NULL)
726+
{
727+
data->allocBytes = TO_CDADDR(pAllocContext->alloc_bytes);
728+
data->allocBytesLoh = TO_CDADDR(pAllocContext->alloc_bytes_uoh);
729+
}
730+
else
731+
{
732+
data->allocBytes = TO_CDADDR(0);
733+
data->allocBytesLoh = TO_CDADDR(0);
734+
}
725735

726736
SOSDacLeave();
727737
return hr;
@@ -816,8 +826,18 @@ HRESULT ClrDataAccess::GetThreadDataImpl(CLRDATA_ADDRESS threadAddr, struct Dacp
816826
threadData->osThreadId = (DWORD)thread->m_OSThreadId;
817827
threadData->state = thread->m_State;
818828
threadData->preemptiveGCDisabled = thread->m_fPreemptiveGCDisabled;
819-
threadData->allocContextPtr = TO_CDADDR(thread->m_alloc_context.alloc_ptr);
820-
threadData->allocContextLimit = TO_CDADDR(thread->m_alloc_context.alloc_limit);
829+
830+
gc_alloc_context* allocContext = thread->GetAllocContext();
831+
if (allocContext)
832+
{
833+
threadData->allocContextPtr = TO_CDADDR(allocContext->alloc_ptr);
834+
threadData->allocContextLimit = TO_CDADDR(allocContext->alloc_limit);
835+
}
836+
else
837+
{
838+
threadData->allocContextPtr = TO_CDADDR(0);
839+
threadData->allocContextLimit = TO_CDADDR(0);
840+
}
821841

822842
threadData->fiberData = (CLRDATA_ADDRESS)NULL;
823843

src/coreclr/vm/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ if(CLR_CMAKE_TARGET_ARCH_AMD64)
621621
${ARCH_SOURCES_DIR}/GenericCLRToCOMCallStubs.asm
622622
${ARCH_SOURCES_DIR}/getstate.asm
623623
${ARCH_SOURCES_DIR}/JitHelpers_Fast.asm
624+
${ARCH_SOURCES_DIR}/JitHelpers_FastMP.asm
624625
${ARCH_SOURCES_DIR}/JitHelpers_FastWriteBarriers.asm
625-
${ARCH_SOURCES_DIR}/JitHelpers_InlineGetThread.asm
626626
${ARCH_SOURCES_DIR}/JitHelpers_SingleAppDomain.asm
627627
${ARCH_SOURCES_DIR}/JitHelpers_Slow.asm
628628
${ARCH_SOURCES_DIR}/patchedcode.asm

src/coreclr/vm/amd64/AsmMacros.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,26 @@ INLINE_GETTHREAD macro Reg
206206

207207
endm
208208

209+
;
210+
; Inlined macro to get the current thread's allocation context
211+
; Trashes rax and r11
212+
;
213+
214+
INLINE_GET_ALLOC_CONTEXT macro Reg
215+
216+
EXTERN _tls_index: DWORD
217+
EXTERN t_thread_alloc_context: DWORD
218+
219+
mov r11d, [_tls_index]
220+
mov rax, gs:[OFFSET__TEB__ThreadLocalStoragePointer]
221+
mov rax, [rax + r11 * 8]
222+
mov r11d, SECTIONREL t_thread_alloc_context
223+
add rax, r11
224+
mov Reg, rax
225+
226+
endm
227+
228+
209229
; if you change this code there will be corresponding code in JITInterfaceGen.cpp which will need to be changed
210230
;
211231

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; Licensed to the .NET Foundation under one or more agreements.
2+
; The .NET Foundation licenses this file to you under the MIT license.
3+
4+
; ***********************************************************************
5+
; File: JitHelpers_InlineGetThread.asm, see history in jithelp.asm
6+
;
7+
; ***********************************************************************
8+
9+
include AsmMacros.inc
10+
include asmconstants.inc
11+
12+
CopyValueClassUnchecked equ ?CopyValueClassUnchecked@@YAXPEAX0PEAVMethodTable@@@Z
13+
JIT_Box equ ?JIT_Box@@YAPEAVObject@@PEAUCORINFO_CLASS_STRUCT_@@PEAX@Z
14+
15+
extern CopyValueClassUnchecked:proc
16+
extern JIT_Box:proc
17+
18+
; HCIMPL2(Object*, JIT_Box, CORINFO_CLASS_HANDLE type, void* unboxedData)
19+
NESTED_ENTRY JIT_BoxFastMP, _TEXT
20+
21+
; m_BaseSize is guaranteed to be a multiple of 8.
22+
mov r8d, [rcx + OFFSET__MethodTable__m_BaseSize]
23+
24+
INLINE_GET_ALLOC_CONTEXT r11
25+
mov r10, [r11 + OFFSETOF__gc_alloc_context__alloc_limit]
26+
mov rax, [r11 + OFFSETOF__gc_alloc_context__alloc_ptr]
27+
28+
add r8, rax
29+
30+
cmp r8, r10
31+
ja AllocFailed
32+
33+
test rdx, rdx
34+
je NullRef
35+
36+
mov [r11 + OFFSETOF__gc_alloc_context__alloc_ptr], r8
37+
mov [rax], rcx
38+
39+
; Check whether the object contains pointers
40+
test dword ptr [rcx + OFFSETOF__MethodTable__m_dwFlags], MethodTable__enum_flag_ContainsPointers
41+
jnz ContainsPointers
42+
43+
; We have no pointers - emit a simple inline copy loop
44+
; Copy the contents from the end
45+
mov ecx, [rcx + OFFSET__MethodTable__m_BaseSize]
46+
sub ecx, 18h ; sizeof(ObjHeader) + sizeof(Object) + last slot
47+
48+
align 16
49+
CopyLoop:
50+
mov r8, [rdx+rcx]
51+
mov [rax+rcx+8], r8
52+
sub ecx, 8
53+
jge CopyLoop
54+
REPRET
55+
56+
ContainsPointers:
57+
; Do call to CopyValueClassUnchecked(object, data, pMT)
58+
push_vol_reg rax
59+
alloc_stack 20h
60+
END_PROLOGUE
61+
62+
mov r8, rcx
63+
lea rcx, [rax + 8]
64+
call CopyValueClassUnchecked
65+
66+
add rsp, 20h
67+
pop rax
68+
ret
69+
70+
AllocFailed:
71+
NullRef:
72+
jmp JIT_Box
73+
NESTED_END JIT_BoxFastMP, _TEXT
74+
75+
end

0 commit comments

Comments
 (0)