Repository navigation
Conversation
superpmi, mcs and the collector, counter and simple shims are now C#, published with NativeAOT under the same names, command lines, outputs and locations, so they are drop-in replacements; the native sources are deleted. - SuperPmi.Shared has the method context format and the recording, replay and dumping of the JIT-EE calls. superpmi and the shims reuse crossgen2's JIT interface (CorInfoImpl_generated.cs and its native JitInterfaceWrapper). - The shims call the EE through native forwarders generated by the ThunkGenerator, which capture the EE's exceptions and rethrow them at the boundaries. The native glue is linked in statically. - The output matches native on Linux and Windows x64: replay, mcs, collections and call counts. The win-x86 and linux-arm64 cross builds were checked too. Co-authored-by: Cursor <cursoragent@cursor.com>
|
Azure Pipelines: Successfully started running 5 pipeline(s). 10 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
@jkoritzinsky, @jkotas, @jakobbotsch: the JIT-EE interface layer is shared with crossgen2/ILC; the rest is a C# rewrite that fast-forwards SuperPMI to the point where it no longer depends on the CoreCLR PAL, which is why I started on this during the PAL cleanup. The remaining C++ is about 600 lines of glue that uses only the JIT-EE interface headers. Based on my testing, it's a drop-in replacement: same binaries in the same places, same command lines and outputs, and no CI changes needed. |
|
@dotnet/jit-contrib Thoughts about this rewrite? It sounds good to me, as long as it does not negatively impact team productivity and validations based on superpmi. |
This is something we've thought about doing but never tried. I'd say it is worth serious consideration.
|
| // Dummy change to excercise in CI | ||
|
|
||
| /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
There was a problem hiding this comment.
Remove after pipeline run:
| // Dummy change to excercise in CI | |
| /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX | |
| /*XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
|
Thanks @AndyAyersMS.
|
a8c6eec to
cdb024b
Compare
cdb024b to
4db2623
Compare
We should verify if it is handled by TPDiff properly as well because we are thinking to utilize TPDiff for Arm PRs. |
|
I'm not so sure about making superpmi-shim-collector rely on NativeAOT. In bring-up scenarios I would expect that we want to be able to record SuperPMI collections well before we have a working NativeAOT for the target platform. I don't have too many concerns about moving the other tooling and replaying side, however. |
These days, we typically have a working coreclr+nativeaot runtimes ported in one shot, since most of the asm helpers are shared. Besides, we start needing SPMI type of tooling once we have a working stack. NativeAOT went from an optional to a required component on desktop platforms (where we run the spmi host). |
Well, working NativeAOT depends on working JIT. If the JIT is not working my go to dev loop would have me using natively compiled superpmi-shim-collector on the target when isolating the repro. |
AFAIK, (and I think it’s written in the porting docs) new platforms are always cross compiled during the port work, and we use host platform toolchain to emit for the target until the port is completed. At least this is how five platforms were ported in past few years. |
Sure, how superpmi-shim-collector and coreclr are compiled for for the target does not matter much to this workflow. |
I don't think the workflow changes in practice:
|
I do not think bring-ups like LoongArch64 had working crossgen2/ILC until a much later point-in-time. All the focus was on coreclr/corerun and the initial work was around running trivial examples with corerun. It is also the approach pointed at by https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/guide-for-porting.md. I assume most of this PR is LLM-coded? It seems likely that during bring-up an LLM could just create an ad-hoc native version of superpmi-shim-collector if necessary, so in practice this may not even matter. |
|
Yup, at the time LA64 and RV64 were ported, nativeaot and surrounding infra was handwritten. These days we are in a much better situation a) due to more code sharing (e.g. src/native/minipal, src/coreclr/minipal, src/coreclr/runtime etc. are shared layers) and b) LLM can accelerate going from zero to working hello-world app on a new target over a weekend: #128783. |
|
I agree with @am11 that that the historical way of doing new architecture ports is not necessarily the best way of doing new architecture ports going forward. #128783 suggests that NAOT is a better target for initial bring up. Also, I expect that we will eventually get to a place where AOT/R2R compilation is required for the JITed runtime startup. In that world, doing AOT bring up first is going to be the only viable path. |
|
@am11 This is a massive change, I still need to review and test it more thoroughly. I am doing that as time allows, but there's a lot of stuff coming in. |
|
/azp run runtime-coreclr superpmi-collect-test |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
# Conflicts: # src/coreclr/tools/superpmi/CMakeLists.txt # src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp # src/coreclr/tools/superpmi/superpmi-shared/standardpch.h # src/coreclr/tools/superpmi/superpmi/cycletimer.cpp # src/coreclr/tools/superpmi/superpmi/jitdebugger.cpp # src/coreclr/tools/superpmi/superpmi/jithost.cpp # src/coreclr/tools/superpmi/superpmi/parallelsuperpmi.cpp
- Count the calls of superpmi-shim-counter and write the counts natively, so no managed code runs when the shim is unloaded. - Let the counter shim set itself up in its own jitStartup/getJit instead of ForwardingShim.OnStartup. - Get the CPU time of the replay thread through P/Invoke instead of a native helper. - Clarify how RecordingJitInfo keeps the EE's exceptions out of managed frames. - Default the real JIT name to clrjit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
RecordingJitInfo calls the EE, then passes the arguments, the result and the exception code (if the EE threw) to the generated managed recorder, which only records them. This removes OriginalJitInfo, the native forwarders and the pending call. The few extra EE queries are made natively: the "fat" method context data and getArgType's getArgClass are recorded through RecordingJitInfo, and the collection filter and the GC layout length use unrecorded helpers. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| host->freeStringConfigValue(value); | ||
| } | ||
|
|
||
| extern "C" const char* Shim_GetProcessCommandLine() |
There was a problem hiding this comment.
Looks like that PR was merged -- can this be simplified now?
There was a problem hiding this comment.
We need an LKG update runtime->dotnet->runtime (global.json, sdk version). Currently at step 1 dotnet/dotnet#11429.
| static uintptr_t GetInstructionPointer(void* context) | ||
| { | ||
| #if defined(__APPLE__) | ||
| const mcontext_t mc = static_cast<ucontext_t*>(context)->uc_mcontext; | ||
| #if defined(__x86_64__) | ||
| return (uintptr_t)mc->__ss.__rip; | ||
| #else | ||
| return (uintptr_t)__darwin_arm_thread_state64_get_pc(mc->__ss); | ||
| #endif | ||
| #else | ||
| const mcontext_t& mc = static_cast<ucontext_t*>(context)->uc_mcontext; | ||
| #if defined(__x86_64__) | ||
| return (uintptr_t)mc.gregs[REG_RIP]; | ||
| #elif defined(__aarch64__) | ||
| return (uintptr_t)mc.pc; | ||
| #elif defined(__arm__) | ||
| return (uintptr_t)mc.arm_pc; | ||
| #elif defined(__riscv) | ||
| return (uintptr_t)mc.__gregs[REG_PC]; | ||
| #elif defined(__loongarch64) | ||
| return (uintptr_t)mc.__pc; | ||
| #endif | ||
| #endif | ||
| } | ||
|
|
||
| static bool IsInJit(uintptr_t ip) | ||
| { | ||
| int count = s_codeRangeCount.load(std::memory_order_acquire); | ||
| for (int i = 0; i < count; i++) | ||
| { | ||
| if (ip >= s_codeRanges[i].start && ip < s_codeRanges[i].end) | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| static void ChainToPreviousHandler(int signal, siginfo_t* info, void* context) | ||
| { | ||
| const struct sigaction* previous = nullptr; | ||
| for (int i = 0; i < SignalCount; i++) | ||
| { | ||
| if (s_signals[i] == signal) | ||
| previous = &s_previousActions[i]; | ||
| } | ||
|
|
||
| if ((previous->sa_flags & SA_SIGINFO) != 0) | ||
| { | ||
| previous->sa_sigaction(signal, info, context); | ||
| } | ||
| else if (previous->sa_handler != SIG_DFL && previous->sa_handler != SIG_IGN) | ||
| { | ||
| previous->sa_handler(signal); | ||
| } | ||
| else | ||
| { | ||
| // The fault happens again when the handler returns, with the default action; a signal that was sent | ||
| // rather than caused by an instruction is sent again. | ||
| struct sigaction action = {}; | ||
| action.sa_handler = SIG_DFL; | ||
| sigaction(signal, &action, nullptr); | ||
| if (info->si_code <= 0) | ||
| raise(signal); | ||
| } | ||
| } | ||
|
|
||
| static void Handler(int signal, siginfo_t* info, void* context) | ||
| { | ||
| Scope* scope = IsInJit(GetInstructionPointer(context)) ? t_scope : nullptr; | ||
| if (scope != nullptr) | ||
| { | ||
| scope->signal = signal; | ||
| scope->code = info->si_code; | ||
| siglongjmp(scope->jmp, 1); | ||
| } | ||
|
|
||
| ChainToPreviousHandler(signal, info, context); | ||
| } | ||
|
|
||
| static void AddCodeRange(uintptr_t start, uintptr_t end) | ||
| { | ||
| int count = s_codeRangeCount.load(std::memory_order_relaxed); | ||
| if (count < MaxCodeRanges) | ||
| { | ||
| s_codeRanges[count] = {start, end}; | ||
| s_codeRangeCount.store(count + 1, std::memory_order_release); | ||
| } | ||
| } | ||
|
|
||
| #ifdef __APPLE__ | ||
| // Adds the executable segments of the image that contains 'address'. | ||
| static void AddModuleContaining(uintptr_t address) | ||
| { | ||
| Dl_info info; | ||
| if (dladdr((const void*)address, &info) == 0 || info.dli_fbase == nullptr) | ||
| return; | ||
|
|
||
| const mach_header_64* header = static_cast<const mach_header_64*>(info.dli_fbase); | ||
| if (header->magic != MH_MAGIC_64) | ||
| return; | ||
|
|
||
| // The image is loaded at the address of its header, which starts its __TEXT segment. | ||
| const load_command* command = reinterpret_cast<const load_command*>(header + 1); | ||
| uintptr_t slide = 0; | ||
| for (uint32_t i = 0; i < header->ncmds; i++) | ||
| { | ||
| const segment_command_64* segment = reinterpret_cast<const segment_command_64*>(command); | ||
| if (command->cmd == LC_SEGMENT_64 && strcmp(segment->segname, SEG_TEXT) == 0) | ||
| slide = (uintptr_t)header - (uintptr_t)segment->vmaddr; | ||
| command = reinterpret_cast<const load_command*>(reinterpret_cast<const char*>(command) + command->cmdsize); | ||
| } | ||
|
|
||
| command = reinterpret_cast<const load_command*>(header + 1); | ||
| for (uint32_t i = 0; i < header->ncmds; i++) | ||
| { | ||
| const segment_command_64* segment = reinterpret_cast<const segment_command_64*>(command); | ||
| if (command->cmd == LC_SEGMENT_64 && (segment->initprot & VM_PROT_EXECUTE) != 0) | ||
| AddCodeRange(slide + (uintptr_t)segment->vmaddr, slide + (uintptr_t)segment->vmaddr + (uintptr_t)segment->vmsize); | ||
| command = reinterpret_cast<const load_command*>(reinterpret_cast<const char*>(command) + command->cmdsize); | ||
| } | ||
| } | ||
| #else | ||
| struct FindModuleParam | ||
| { | ||
| uintptr_t address; | ||
| bool found; | ||
| }; | ||
|
|
||
| // Adds the executable segments of the module that contains 'address', if it is the one. | ||
| static int AddModuleIfContains(dl_phdr_info* info, size_t, void* p) | ||
| { | ||
| FindModuleParam* param = static_cast<FindModuleParam*>(p); | ||
| bool contains = false; | ||
| for (int i = 0; i < info->dlpi_phnum; i++) | ||
| { | ||
| const ElfW(Phdr)& phdr = info->dlpi_phdr[i]; | ||
| uintptr_t start = info->dlpi_addr + phdr.p_vaddr; | ||
| if (phdr.p_type == PT_LOAD && param->address >= start && param->address < start + phdr.p_memsz) | ||
| contains = true; | ||
| } | ||
| if (!contains) | ||
| return 0; | ||
|
|
||
| for (int i = 0; i < info->dlpi_phnum; i++) | ||
| { | ||
| const ElfW(Phdr)& phdr = info->dlpi_phdr[i]; | ||
| if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X) != 0) | ||
| { | ||
| uintptr_t start = info->dlpi_addr + phdr.p_vaddr; | ||
| AddCodeRange(start, start + phdr.p_memsz); | ||
| } | ||
| } | ||
| param->found = true; | ||
| return 1; | ||
| } | ||
|
|
||
| // Adds the executable segments of the module that contains 'address'. | ||
| static void AddModuleContaining(uintptr_t address) | ||
| { | ||
| FindModuleParam param = {address, false}; | ||
| dl_iterate_phdr(AddModuleIfContains, ¶m); | ||
| } | ||
| #endif | ||
|
|
||
| // Adds the JIT (the module with the code at 'jitAddress') to those whose faults are caught, and installs the | ||
| // handlers the first time. | ||
| static void AddJit(const void* jitAddress) | ||
| { | ||
| std::lock_guard<std::mutex> lock(s_lock); | ||
|
|
||
| static bool s_installed = false; | ||
| if (!s_installed) | ||
| { | ||
| for (int i = 0; i < SignalCount; i++) | ||
| { | ||
| struct sigaction action = {}; | ||
| action.sa_sigaction = Handler; | ||
| action.sa_flags = SA_SIGINFO | SA_RESTART; | ||
| sigemptyset(&action.sa_mask); | ||
|
|
||
| // As NativeAOT's AddSignalHandler: the previous handler, which this one calls for the faults outside of | ||
| // the JIT, runs on the kind of stack that it expects (the runtime handles the managed exceptions on the | ||
| // thread's stack, and only the stack overflows on the alternate stack), with the signals it blocks. | ||
| sigaction(s_signals[i], nullptr, &s_previousActions[i]); | ||
| if ((s_previousActions[i].sa_flags & SA_ONSTACK) != 0) | ||
| { | ||
| action.sa_flags |= SA_ONSTACK; | ||
| action.sa_mask = s_previousActions[i].sa_mask; | ||
| } | ||
| sigaction(s_signals[i], &action, &s_previousActions[i]); | ||
| } | ||
| s_installed = true; | ||
| } | ||
|
|
||
| static const void* s_jits[MaxCodeRanges]; | ||
| static int s_jitCount = 0; | ||
| for (int i = 0; i < s_jitCount; i++) | ||
| { | ||
| if (s_jits[i] == jitAddress) | ||
| return; | ||
| } | ||
|
|
||
| AddModuleContaining((uintptr_t)jitAddress); | ||
| if (s_jitCount < MaxCodeRanges) | ||
| s_jits[s_jitCount++] = jitAddress; | ||
| } | ||
|
|
||
| // Restores the scope of the caller when the call returns, or when an exception that isn't a fault escapes it. | ||
| struct ScopeRestorer | ||
| { | ||
| Scope* previous; | ||
|
|
||
| ~ScopeRestorer() | ||
| { | ||
| t_scope = previous; | ||
| } | ||
| }; | ||
|
|
||
| // Calls 'call', which runs the JIT, and returns 0; or the signal (and in *pCode, the si_code) of a fault in the | ||
| // code of the JIT, which abandons the call. 'jit' is the ICorJitCompiler, whose vtable is in the JIT module. | ||
| template <typename F> | ||
| static int CallCatchingJitFaults(const void* jit, F& call, int* pCode) | ||
| { | ||
| AddJit(*static_cast<void* const*>(jit)); | ||
|
|
||
| Scope scope; | ||
| scope.signal = 0; | ||
| scope.code = 0; | ||
| ScopeRestorer restorer = {t_scope}; | ||
| if (sigsetjmp(scope.jmp, 1) == 0) | ||
| { | ||
| t_scope = &scope; | ||
| call(); | ||
| return 0; | ||
| } | ||
|
|
||
| *pCode = scope.code; | ||
| return scope.signal; | ||
| } |
There was a problem hiding this comment.
Seems complicated. Is it really necessary? We don't have to fully recover, we just need to make sure the collection gets saved and written to disk when it happens.
Before we call into the underlying JIT, could we just record in TLS that we are inside the JIT, check that from the signal handler, and then ensure the current recorded data is saved if so? Then pass the signal along.
There was a problem hiding this comment.
Simplified: the thread is marked in TLS while it runs the JIT (cleared while the JIT calls out to the EE, the host or the collector), and a fault there jumps back to compileMethod, which saves the method context and re-raises the signal. The save itself can't run in the handler: it chains to the runtime's handler, which runs on a small alternate stack, and the managed save needs the thread's stack. The replay side no longer recovers from JIT crashes on Unix, as with the native superpmi.
| [UnmanagedCallersOnly] | ||
| private static void _canValueClassInstancePointerEscape(IntPtr thisHandle, IntPtr* ppException, CORINFO_METHOD_STRUCT_* ftn, byte result, uint exceptionCode) | ||
| { | ||
| var _this = GetThis(thisHandle); | ||
| try | ||
| { | ||
| _this.AddCall("canValueClassInstancePointerEscape"); | ||
| if (exceptionCode == 0) | ||
| _this.canValueClassInstancePointerEscape(ftn, result != 0); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| *ppException = _this.AllocException(ex); | ||
| } | ||
| } |
There was a problem hiding this comment.
Most of these functions are going to be effective no-ops when an exception was thrown (exceptionCode != 0). Can we avoid trying to handle exceptions in the native code entirely for those cases?
There was a problem hiding this comment.
Only the methods that record exceptions (getMethodInfo, canInline, resolveToken, getNewHelper, getArgType, getArgClass, getCallInfo) go through the exception handling now; the others call the EE directly and are recorded only when it returns.
# Conflicts: # src/coreclr/tools/superpmi/superpmi-shared/spmiutil.cpp


superpmi, mcs and the collector, counter and simple shims are now C#, published with NativeAOT under the same names, command lines, outputs and locations, so they are drop-in replacements; the native sources are deleted.