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

Interpreter GC info stage 1: Use templates to specialize gc info encoder/decoder #113223

Merged
merged 25 commits into from
Mar 20, 2025

Conversation

kg
Copy link
Member

@kg kg commented Mar 6, 2025

Right now we have a single GcInfoEncoder/Decoder pair that is configured by platform-specific #defines in gcinfotypes.h. This makes it unsuitable for use in the interpreter, since the interpreter has its own IR independent of the host/target platforms, and things like the size of an opcode might not match.

This is the first step towards being able to configure the encoder/decoder to work for either target code (i.e. x86-64 or arm64) or interpreter code depending on whether the current frame is interpreted or not.

The platform-specific defines now move into a struct TargetGcInfoEncoding and become either consts or constexpr functions, and the encoder/decoder become template classes (TGcInfoEncoder<GcInfoEncoding> and TGcInfoDecoder<GcInfoEncoding>) that accept an encoding. In the future I'll add an encoding for the interpreter, right now there's only the one for the target.

To reduce the blast radius of this change I renamed the encoder/decoder when templatizing them, and then added typedefs with the previous names, i.e. typedef TGcInfoDecoder<TargetGcInfoEncoding> GcInfoDecoder. This opens a path to defining a typedef TGcInfoDecoder<InterpreterGcInfoEncoding> InterpreterGcInfoDecoder in the future or what have you.

I think this unavoidably will break the diagnostics repo the next time they update https://github.com/dotnet/diagnostics/tree/main/src/shared but maybe I did my job correctly and it won't?

kg added 2 commits March 6, 2025 10:14
… templates that accept a type argument that provides encoding traits
@kg
Copy link
Member Author

kg commented Mar 7, 2025

Changing the #defines to constants and constexpr functions seems to specifically break either GcInfoEncoder or GcInfoDecoder on ARM/ARM64 and only that target. I can't figure out why, I've experimented with a few different types for the constants in case it had something to do with numeric promotion and it doesn't seem to be that.

#0  DBG_DebugBreak () at /root/Projects/runtime/src/coreclr/pal/src/arch/arm64/debugbreak.S:7
#1  0x0000fffff7785bdc in DebugBreak () at /root/Projects/runtime/src/coreclr/pal/src/debug/debug.cpp:411
#2  0x0000fffff75918e0 in DbgAssertDialog (szFile=0xfffff6c9773a "/root/Projects/runtime/src/coreclr/vm/methodtable.cpp", iLine=6243, szExpr=0xfffff6cee86e "(GetComponentSize() <= 2) || IsArray()")
    at /root/Projects/runtime/src/coreclr/utilcode/debug.cpp:431
#3  0x0000fffff70aa110 in MethodTable::SanityCheck (this=0xffffa9edcee0) at /root/Projects/runtime/src/coreclr/vm/methodtable.cpp:6243
#4  0x0000fffff70b02b8 in MethodTable::Validate (this=0xffffa9edcee0) at /root/Projects/runtime/src/coreclr/vm/methodtable.cpp:8502
#5  0x0000fffff70b81d8 in Object::ValidateInner (this=0xffffa8050c38, bDeep=1, bVerifyNextHeader=1, bVerifySyncBlock=1) at /root/Projects/runtime/src/coreclr/vm/object.cpp:560
#6  0x0000fffff70b78bc in Object::Validate (this=0xffffa8050c38, bDeep=1, bVerifyNextHeader=1, bVerifySyncBlock=1) at /root/Projects/runtime/src/coreclr/vm/object.cpp:536
#7  0x0000fffff736470c in TGcInfoDecoder<TargetGcInfoEncoding>::ReportRegisterToGC (this=0xffff9dc3a288, regNum=20, gcFlags=0, pRD=0xffff9dc3bac0, flags=0, pCallBack=0xfffff7222d8c <GcEnumObject(void*, OBJECTREF*, unsigned int)>,
    hCallBack=0xffff9dc3c918) at /root/Projects/runtime/src/coreclr/vm/gcinfodecoder.cpp:1790
#8  0x0000fffff736302c in TGcInfoDecoder<TargetGcInfoEncoding>::ReportSlotToGC (this=0xffff9dc3a288, slotDecoder=..., slotIndex=7, pRD=0xffff9dc3bac0, reportScratchSlots=false, inputFlags=0,
    pCallBack=0xfffff7222d8c <GcEnumObject(void*, OBJECTREF*, unsigned int)>, hCallBack=0xffff9dc3c918) at /root/Projects/runtime/src/coreclr/inc/gcinfodecoder.h:713
#9  0x0000fffff7362428 in TGcInfoDecoder<TargetGcInfoEncoding>::EnumerateLiveSlots (this=0xffff9dc3a288, pRD=0xffff9dc3bac0, reportScratchSlots=false, inputFlags=0, pCallBack=0xfffff7222d8c <GcEnumObject(void*, OBJECTREF*, unsigned int)>,
    hCallBack=0xffff9dc3c918) at /root/Projects/runtime/src/coreclr/vm/gcinfodecoder.cpp:1018
#10 0x0000fffff6fd282c in EECodeManager::EnumGcRefs (this=0xaaaaaab99a40, pRD=0xffff9dc3bac0, pCodeInfo=0xffff9dc3b920, flags=0, pCallBack=0xfffff7222d8c <GcEnumObject(void*, OBJECTREF*, unsigned int)>, hCallBack=0xffff9dc3c918,
    relOffsetOverride=4294967295) at /root/Projects/runtime/src/coreclr/vm/eetwain.cpp:1482
#11 0x0000fffff7223838 in GcStackCrawlCallBack (pCF=0xffff9dc3b6f0, pData=0xffff9dc3c918) at /root/Projects/runtime/src/coreclr/vm/gcenv.ee.common.cpp:357
#12 0x0000fffff7107d8c in Thread::MakeStackwalkerCallback (this=0xaaaaaabaedb0, pCF=0xffff9dc3b6f0, pCallback=0xfffff7222fb8 <GcStackCrawlCallBack(CrawlFrame*, void*)>, pData=0xffff9dc3c918, uFramesProcessed=7)
    at /root/Projects/runtime/src/coreclr/vm/stackwalk.cpp:728
#13 0x0000fffff7107f98 in Thread::StackWalkFramesEx (this=0xaaaaaabaedb0, pRD=0xffff9dc3bac0, pCallback=0xfffff7222fb8 <GcStackCrawlCallBack(CrawlFrame*, void*)>, pData=0xffff9dc3c918, flags=34048, pStartFrame=0x0)
    at /root/Projects/runtime/src/coreclr/vm/stackwalk.cpp:808
#14 0x0000fffff7108b0c in Thread::StackWalkFrames (this=0xaaaaaabaedb0, pCallback=0xfffff7222fb8 <GcStackCrawlCallBack(CrawlFrame*, void*)>, pData=0xffff9dc3c918, flags=34048, pStartFrame=0x0) at /root/Projects/runtime/src/coreclr/vm/stackwalk.cpp:888
#15 0x0000fffff721bfc0 in ScanStackRoots (pThread=0xaaaaaabaedb0, fn=0xfffff747e4dc <WKS::GCHeap::Promote(Object**, ScanContext*, unsigned int)>, sc=0xffff9dc3ce20) at /root/Projects/runtime/src/coreclr/vm/gcenv.ee.cpp:205
#16 0x0000fffff721bc10 in GCToEEInterface::GcScanRoots (fn=0xfffff747e4dc <WKS::GCHeap::Promote(Object**, ScanContext*, unsigned int)>, condemned=0, max_gen=2, sc=0xffff9dc3ce20) at /root/Projects/runtime/src/coreclr/vm/gcenv.ee.cpp:307
#17 0x0000fffff739967c in GCScan::GcScanRoots (fn=0xfffff747e4dc <WKS::GCHeap::Promote(Object**, ScanContext*, unsigned int)>, condemned=0, max_gen=2, sc=0xffff9dc3ce20) at /root/Projects/runtime/src/coreclr/gc/gcscan.cpp:154
#18 0x0000fffff7462c84 in WKS::gc_heap::mark_phase (condemned_gen_number=0) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:30255
#19 0x0000fffff745ec20 in WKS::gc_heap::gc1 () at /root/Projects/runtime/src/coreclr/gc/gc.cpp:22694
#20 0x0000fffff7470bd8 in WKS::gc_heap::garbage_collect (n=0) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:24866
#21 0x0000fffff7454cb0 in WKS::GCHeap::GarbageCollectGeneration (this=0xaaaaaab8aa00, gen=0, reason=reason_alloc_soh) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:51505
#22 0x0000fffff7456b7c in WKS::gc_heap::trigger_gc_for_alloc (gen_number=0, gr=reason_alloc_soh, msl=0xfffff7929f08 <WKS::gc_heap::more_space_lock_soh>, loh_p=false, take_state=WKS::mt_try_budget) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:19191
#23 0x0000fffff7457dec in WKS::gc_heap::try_allocate_more_space (acontext=0xffbef4004070, size=32, flags=2, gen_number=0) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:19329
#24 0x0000fffff745802c in WKS::gc_heap::allocate_more_space (acontext=0xffbef4004070, size=32, flags=2, alloc_generation_number=0) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:19829
#25 0x0000fffff74a4e18 in WKS::gc_heap::allocate (jsize=32, acontext=0xffbef4004070, flags=2) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:19860
#26 WKS::GCHeap::Alloc (this=0xaaaaaab8aa00, context=0xffbef4004070, size=32, flags=2) at /root/Projects/runtime/src/coreclr/gc/gc.cpp:50410
#27 0x0000fffff72290b8 in Alloc (pEEAllocContext=0xffbef4004068, size=32, flags=GC_ALLOC_CONTAINS_REF) at /root/Projects/runtime/src/coreclr/vm/gchelpers.cpp:274
#28 0x0000fffff7226bf8 in Alloc (size=32, flags=GC_ALLOC_CONTAINS_REF) at /root/Projects/runtime/src/coreclr/vm/gchelpers.cpp:336
#29 0x0000fffff72287d4 in AllocateObject (pMT=0xffffa9f971c8, flags=GC_ALLOC_CONTAINS_REF) at /root/Projects/runtime/src/coreclr/vm/gchelpers.cpp:1160
#30 0x0000fffff6f104b0 in AllocateObject (pMT=0xffffa9f971c8) at /root/Projects/runtime/src/coreclr/vm/gchelpers.h:68
#31 0x0000fffff724b5f8 in JIT_New (typeHnd_=0xffffa9f971c8) at /root/Projects/runtime/src/coreclr/vm/jithelpers.cpp:788
#32 0x0000fffff724b1a4 in JIT_NewS_MP_FastPortable (typeHnd_=0xffffa9f971c8) at /root/Projects/runtime/src/coreclr/vm/jithelpers.cpp:753
#33 0x0000ffffa83c2678 in ?? ()
#34 0x0000ffffa9f96f28 in ?? ()
#35 0x0000ffffa8044a10 in ?? ()
#36 0x607473694c2e6369 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

@kg kg force-pushed the interp-gcinfo-1 branch from f4ef902 to 514f8db Compare March 11, 2025 16:42
@kg kg marked this pull request as ready for review March 11, 2025 16:58
@Copilot Copilot bot review requested due to automatic review settings March 11, 2025 16:58
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

@kg
Copy link
Member Author

kg commented Mar 11, 2025

kg and others added 3 commits March 11, 2025 10:21
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
This reverts commit bf03fed.

Revert "Switch back to templates for gcinfotypes helper functions"

This reverts commit 739b66d.
Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Many of the casts should not be needed anymore. (I am not sure whether I got all of them in the comments - you may want to double check.)

@kg
Copy link
Member Author

kg commented Mar 20, 2025

Will batch apply your cast removals and then try and do a cleanup pass myself. Gets a little fussy since clang and MSVC both complain about different implicit conversions from each other, so I have to test on both OSes.

@kg
Copy link
Member Author

kg commented Mar 20, 2025

OK, I think I found all the unnecessary casts and removed them. I also removed (DE)NORMALIZE_REGISTER since it was a no-op.

Copy link
Member

@jkotas jkotas left a comment

Choose a reason for hiding this comment

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

Thanks!

@kg kg merged commit 630de83 into dotnet:main Mar 20, 2025
106 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants