-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Interpreter block-count PGO for WebAssembly CoreCLR #132721
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
Open
pavelsavara
wants to merge
26
commits into
dotnet:main
Choose a base branch
from
pavelsavara:wasm_collect_PGO
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
fe5bb85
wip
pavelsavara ab76425
Define ep_rt_session_stopping no-op for NativeAOT eventpipe
pavelsavara 1d4dd47
Flush PGO data before taking the EventPipe lock on session stop
pavelsavara 47d4eab
enable BlazorEventPipeTestWithCpuSamples
pavelsavara b7df191
WBT test for PGO trace
pavelsavara 38ef900
feedback
pavelsavara 0b4176a
fix event pipe early collection
pavelsavara 62594a2
Fix WBT dotnet-pgo copy: pre-create nested output dirs (amd64/arm64)
pavelsavara 9e738b6
fix WBT linux
pavelsavara 3f302aa
Deploy dotnet-pgo into WBT Helix payload via CopyToOutputDirectory
pavelsavara 6c453c9
Restore dotnet-pgo for WBT via Private=false ProjectReference
pavelsavara 9c215f5
Fix dotnet-pgo payload glob dropping runtimeconfig.json
pavelsavara 1abefcb
Address PR review feedback for interpreter PGO
pavelsavara a983705
Correct ep_rt_session_stopping lock-contract comment
pavelsavara 194b6a8
Address follow-up PR review on interpreter PGO
pavelsavara b3ec395
Scope interpreter PGO flush to the stopping session
pavelsavara 97d7a19
Scope PGO block instrumentation to header IL range; assert on MT WASM…
pavelsavara c9b1e4b
Route PGO flush to the stopping session via rundown thread
pavelsavara 3f7c7cd
Scope interpreter PGO to single-threaded WASM; mask-gate MT assert
pavelsavara c79ea57
Pass session mask to session-stopping hook; add pgo-trace to rollup s…
pavelsavara 0426f3d
Drop 'desktop' from PGO keyword comment
pavelsavara 3c52a09
Merge branch 'main' into wasm_collect_PGO
pavelsavara 9727640
Split EventPipe PGO emission from text-file export
pavelsavara 6055010
Make browser PGO trace collection one-shot per process
pavelsavara a47e597
Merge branch 'main' into wasm_collect_PGO
pavelsavara dd1081a
cleanup
pavelsavara File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,6 +51,9 @@ bool InterpCompiler::s_samplingProfilerEnabled = false; | |
| bool InterpCompiler::s_browserProfilerEnabled = false; | ||
| #endif | ||
| #endif // PERFTRACING_DISABLE_THREADS | ||
| #if (defined(TARGET_BROWSER) || defined(TARGET_WASI)) && defined(PERFTRACING_DISABLE_THREADS) | ||
| bool InterpCompiler::s_interpPgoEnabled = false; | ||
| #endif // (TARGET_BROWSER || TARGET_WASI) && PERFTRACING_DISABLE_THREADS | ||
|
|
||
| #if MEASURE_MEM_ALLOC | ||
| #include <minipal/mutex.h> | ||
|
|
@@ -2246,6 +2249,12 @@ InterpCompiler::InterpCompiler(COMP_HANDLE compHnd, | |
| #endif | ||
| #endif // PERFTRACING_DISABLE_THREADS | ||
|
|
||
| #if (defined(TARGET_BROWSER) || defined(TARGET_WASI)) && defined(PERFTRACING_DISABLE_THREADS) | ||
| m_emitInterpPGO = s_interpPgoEnabled | ||
| && (InterpConfig.InterpPGOMethods().isEmpty() | ||
| || InterpConfig.InterpPGOMethods().contains(compHnd, m_methodHnd, m_classHnd, &m_methodInfo->args)); | ||
| #endif // (TARGET_BROWSER || TARGET_WASI) && PERFTRACING_DISABLE_THREADS | ||
|
|
||
| #ifdef DEBUG | ||
| m_methodName = ::PrintMethodName(compHnd, m_classHnd, m_methodHnd, &m_methodInfo->args, | ||
| /* includeAssembly */ false, | ||
|
|
@@ -2348,6 +2357,11 @@ bool InterpCompiler::CompileMethod() | |
| } | ||
| #endif | ||
|
|
||
| #if (defined(TARGET_BROWSER) || defined(TARGET_WASI)) && defined(PERFTRACING_DISABLE_THREADS) | ||
| if (m_emitInterpPGO) | ||
| InstrumentBlockCounts(); | ||
| #endif // (TARGET_BROWSER || TARGET_WASI) && PERFTRACING_DISABLE_THREADS | ||
|
|
||
| AllocOffsets(); | ||
| PatchInitLocals(m_methodInfo); | ||
|
|
||
|
|
@@ -8668,6 +8682,87 @@ void InterpCompiler::CreateSynchronizedRetValVar() | |
| INTERP_DUMP("Created ret val var V%d\n", m_synchronizedOrAsyncRetValVarIndex); | ||
| } | ||
|
|
||
| #if (defined(TARGET_BROWSER) || defined(TARGET_WASI)) && defined(PERFTRACING_DISABLE_THREADS) | ||
| // Instrument each basic block with a block-count PGO probe. The counters are allocated by | ||
| // allocPgoInstrumentationBySchema (native PgoManager memory), so they persist independently of | ||
| // EventPipe session lifetime; the accumulated profile is flushed to the trace as | ||
| // JitInstrumentationDataVerbose events, which dotnet-pgo consumes to build an .mibc. | ||
| void InterpCompiler::InstrumentBlockCounts() | ||
| { | ||
| // Mark blocks that are the target of a branch or switch (loop and branch heads). Together with | ||
| // the method entry, these are the block heads whose execution count can't be inferred from a | ||
| // single predecessor, so only they are worth a counter; the precompiler reconstructs the rest of | ||
| // the flow graph from them. This deliberately avoids the JIT's edge/spanning-tree scheme (#130517). | ||
| bool *isBranchTarget = getAllocator(IMK_BasicBlock).allocateZeroed<bool>(m_BBCount); | ||
| for (InterpBasicBlock *bb = m_pEntryBB; bb != NULL; bb = bb->pNextBB) | ||
| { | ||
| for (InterpInst *ins = bb->pFirstIns; ins != NULL; ins = ins->pNext) | ||
| { | ||
| if (ins->opcode == INTOP_SWITCH) | ||
| { | ||
| int32_t n = ins->data[0]; | ||
| for (int32_t i = 0; i < n; i++) | ||
| isBranchTarget[ins->info.ppTargetBBTable[i]->index] = true; | ||
| } | ||
| else if (InterpOpIsUncondBranch(ins->opcode) || InterpOpIsCondBranch(ins->opcode) || | ||
| ins->opcode == INTOP_LEAVE_CATCH || ins->opcode == INTOP_CALL_FINALLY) | ||
| { | ||
| isBranchTarget[ins->info.pTargetBB->index] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Collect the canonical block for each real IL offset that is the method entry (IL offset 0) or | ||
| // a branch/loop target. Clones (funclet / leave-chain islands) and blocks removed by optimization | ||
| // are skipped, so every schema entry carries a unique IL offset, matching what | ||
| // getPgoInstrumentationResults and dotnet-pgo expect. | ||
| TArray<InterpBasicBlock*, MemPoolAllocator> blocks(GetMemPoolAllocator(IMK_DataItem)); | ||
| for (InterpBasicBlock *bb = m_pEntryBB; bb != NULL; bb = bb->pNextBB) | ||
| { | ||
| if (bb->ilOffset < 0 || bb->ilOffset >= m_ILCodeSizeFromILHeader || m_ppOffsetToBB[bb->ilOffset] != bb) | ||
| continue; | ||
| if (bb->ilOffset == 0 || isBranchTarget[bb->index]) | ||
| blocks.Add(bb); | ||
|
Comment on lines
+8724
to
+8725
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is flip side of Vlad's question above. |
||
| } | ||
|
|
||
| int32_t numBlocks = blocks.GetSize(); | ||
| if (numBlocks == 0) | ||
| return; | ||
|
|
||
| // One 4-byte block counter per block. | ||
| TArray<ICorJitInfo::PgoInstrumentationSchema, MemPoolAllocator> schema(GetMemPoolAllocator(IMK_DataItem)); | ||
| for (int32_t i = 0; i < numBlocks; i++) | ||
| { | ||
| ICorJitInfo::PgoInstrumentationSchema schemaElem; | ||
| schemaElem.Offset = 0; | ||
| schemaElem.InstrumentationKind = ICorJitInfo::PgoInstrumentationKind::BasicBlockIntCount; | ||
| schemaElem.ILOffset = blocks.Get(i)->ilOffset; | ||
| schemaElem.Count = 1; | ||
| schemaElem.Other = 0; | ||
| schema.Add(schemaElem); | ||
| } | ||
|
|
||
| ICorJitInfo::PgoInstrumentationSchema *pSchema = schema.GetUnderlyingArray(); | ||
| uint8_t *pInstrumentationData = NULL; | ||
| HRESULT hr = m_compHnd->allocPgoInstrumentationBySchema(m_methodHnd, pSchema, (uint32_t)numBlocks, &pInstrumentationData); | ||
| if (FAILED(hr) || pInstrumentationData == NULL) | ||
| { | ||
| INTERP_DUMP("InstrumentBlockCounts: allocPgoInstrumentationBySchema failed (hr=0x%08x)\n", hr); | ||
| return; | ||
| } | ||
|
|
||
| // Insert an INTOP_PGO_COUNT probe at the start of each block, pointing at its counter. | ||
| for (int32_t i = 0; i < numBlocks; i++) | ||
| { | ||
| uint32_t *pCounter = (uint32_t*)(pInstrumentationData + pSchema[i].Offset); | ||
| InterpInst *ins = InsertInsBB(blocks.Get(i), NULL, INTOP_PGO_COUNT); | ||
| // Probe is a pure counter increment with no IL mapping; keep it out of the debug maps. | ||
| ins->ilOffset = -1; | ||
| ins->data[0] = GetDataItemIndex((void*)pCounter); | ||
|
pavelsavara marked this conversation as resolved.
|
||
| } | ||
| } | ||
| #endif // (TARGET_BROWSER || TARGET_WASI) && PERFTRACING_DISABLE_THREADS | ||
|
|
||
| void InterpCompiler::GenerateCode(CORINFO_METHOD_INFO* methodInfo) | ||
| { | ||
| bool readonly = false; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need all this logic from here ? Can we simply set a bit on the basic block to signal that it is the target of a backwards branch (as we already check here for safepoint purposes). Then we could simply emit the
INTOP_PGO_COUNTas separate simple pass or when doing final code emit for the bblock in question (inEmitCode). I thought this profiling was meant for loops but we seem to generate the tracking opcode for all branch targets.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤖 "The real reason is weighted flow-graph reconstruction"
https://gist.github.com/pavelsavara/c53070aff3e2f46d5a6e840f13d34607
I don't know how relevant that is to WASM. @AndyAyersMS @davidwrighton, should we simplify the trace ?
@BrzVlad if we don't need all targets, then we can re-use the
INTOP_PROF_SAMPLEPOINTplace.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot review pointed out below that maybe we need even more blocks to be instrumented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well... it is plausible that information from all branches is valuable, but without building and measuring, it's hard to say how valuable. If you can predict non-looping branches you can predict hot vs cold paths, which could possibly inform details for branch hinting(https://github.com/WebAssembly/branch-hinting/blob/master/proposals/branch-hinting/Overview.md) or adjust optimization decisions (should the optimizer make size/speed tradeoffs). Historically we've seen some value from that level of precision, but I wouldn't say that its perfect. The mibc format and this data collection pipeline is capable of capturing edge/block level data and which could be fed from the interpreter into the jit. So, my preference would be to err on the side of capturing too much data. Unfortunately, we currently don't have what I would consider to be a clean set of performance data in any form, so the improvements from branch level adjustments is likely impossible to see right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we only collect hits from method entry and end of the loop, would it still be a valid .mibc ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#133795