') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ', 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); })(); [Wasm RyuJIT] Implement Wasm DFS and Loop Finding by AndyAyersMS · Pull Request #121457 · dotnet/runtime · GitHub
Skip to content

[Wasm RyuJIT] Implement Wasm DFS and Loop Finding - #121457

Merged
AndyAyersMS merged 11 commits into
dotnet:mainfrom
AndyAyersMS:WasmDepthFirstSearch
Nov 18, 2025
Merged

[Wasm RyuJIT] Implement Wasm DFS and Loop Finding#121457
AndyAyersMS merged 11 commits into
dotnet:mainfrom
AndyAyersMS:WasmDepthFirstSearch

Conversation

@AndyAyersMS

@AndyAyersMSAndyAyersMS commented Nov 7, 2025

Copy link
Copy Markdown
Member

Loops for Wasm control flow codegen don't involve EH or runtime mediated
control flow transfers.

Implement a custom block successor enumerator for Wasm, and adjust fgRunDFS
to allow using this and also to generalize how the DFS is initiated. Use
this to build a "Wasm" DFS. In that DFS handle both the main method and
all funclets (by specifying funclet entries as additional DFS starting points).

Update the loop finding code to make suitable changes when it is driven from
a "Wasm" DFS instead of the typical all successor / all predecessor DFS.

Remove the restriction in the Wasm control flow codegen that only handles
the main method; now it works for the main method and all funclets.

Contributes to #121178.

Dependent on #121417.

AndyAyersMSand others added 7 commits November 6, 2025 11:34
Determine how to emit Wasm control flow from the JIT's control flow graph.
Relies on loop-aware RPO to determine the block order. Currently only
handles the main method. Assumes irreducible loops have been fixed
upstream (which is not yet guaranteed; bails out if not so).
Doesn't actually do any emission, just prints a textual description in
the JIT dump (along with a dot markup version).
Uses only LOOP and BLOCK. Tries to limit the extent of BLOCK.
Run for now as an optional phase even if not targeting Wasm, to
do some stress testing.
Contributes to dotnet#121178
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Loops for Wasm control flow codegen don't involve EH or runtime mediated
control flow transfers.
Implement a custom block successor enumerator for Wasm, and adjust `fgRunDFS`
to allow using this and also to generalize how the DFS is initiated. Use
this to build a "Wasm" DFS. In that DFS handle both the main method and
all funclets (by specifying funclet entries as additional DFS starting points).
Update the loop finding code to make suitable changes when it is driven from
a "Wasm" DFS instead of the typical all successor / all predecessor DFS.
Remove the restriction in the Wasm control flow codegen that only handles
the main method; now it works for the main method and all funclets.
Contributes to dotnet#121178.
@github-actionsgithub-actionsBot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Nov 7, 2025
@AndyAyersMSAndyAyersMS changed the title Wasm depth first search[Wasm RyuJIT] Implement Wasm DFS and Loop FindingNov 7, 2025
@am11am11 added the arch-wasm WebAssembly architecture label Nov 7, 2025
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.

@AndyAyersMS
AndyAyersMS marked this pull request as ready for review November 15, 2025 00:13
CopilotAI review requested due to automatic review settings November 15, 2025 00:13

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements a custom DFS (depth-first search) and loop finding mechanism specifically for WebAssembly control flow code generation. The implementation introduces a Wasm-specific successor enumerator that excludes exceptional control flow and runtime-mediated transfers, allowing the Wasm control flow codegen to handle both the main method and all funclets.

Key changes include:

  • Introduction of WasmSuccessorEnumerator class that enumerates successors following Wasm control flow rules
  • Generalization of fgRunDfs to accept custom successor enumerators and multiple entry blocks
  • Addition of fgWasmDfs() method that builds a DFS tree treating funclets as separate disjoint regions
  • Updates to loop finding logic to handle Wasm-specific control flow patterns (callfinally pairs, funclet returns)

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

Show a summary per file
FileDescription
src/coreclr/jit/jitconfigvalues.hChanges default value of JitWasmControlFlow from 0 to 1
src/coreclr/jit/flowgraph.cppRefactors fgComputeDfs and FindNaturalLoopBlocks to support multiple entry blocks and Wasm-specific control flow
src/coreclr/jit/fgwasm.hAdds new header defining WasmSuccessorEnumerator and VisitWasmSuccs for Wasm control flow
src/coreclr/jit/fgwasm.cppImplements WasmSuccessorEnumerator, fgWasmDfs, and updates fgWasmControlFlow to handle funclets
src/coreclr/jit/fgdiagnostic.cppUpdates debug flow graph checking to use new fgRunDfs signature with entry blocks
src/coreclr/jit/compiler.hppGeneralizes fgRunDfs template to accept SuccessorEnumerator parameter and entry blocks vector
src/coreclr/jit/compiler.hAdds fgWasmDfs declaration, IsForWasm methods, and updates fgRunDfs signature
src/coreclr/jit/CMakeLists.txtAdds fgwasm.h to JIT_WASM_HEADERS list

Comment threadsrc/coreclr/jit/jitconfigvalues.h Outdated
Comment threadsrc/coreclr/jit/fgwasm.cpp Outdated
@AndyAyersMS

Copy link
Copy Markdown
MemberAuthor

@dotnet/jit-contrib PTAL

I will probably capture those global functions in a helper class in the next round of work.

@jakobbotsch this is the "smaller" generalization of DFS to accommodate some special needs for Wasm:

  • different successor enumeration
  • different set of entry blocks.

I held off folding in "only walk a subgraph" and "walk the reverse graph" from this. So there is a different wasm-specific DFS also in the world (currently just does the subgraph bit).

I have to make some assumptions at this point about how EH control flow is going to work. If we end up implementing some of the EH via Wasm EH we'll need to make corresponding adjustments here.

For example, non-funclet blocks that are only reachable via EH don't get properly handled here, as there is (in this current simple "model") no wasm-visible way for control to reach them. Methods with such blocks are fairly rare.

@AndyAyersMS

Copy link
Copy Markdown
MemberAuthor

@dotnet/jit-contrib ping

@jakobbotsch
jakobbotsch self-requested a review November 17, 2025 17:42
@AndyAyersMS

Copy link
Copy Markdown
MemberAuthor

The next bit of work after this adds the ability to transform SCCs. Preview here: #121728

Comment threadsrc/coreclr/jit/compiler.h
Comment threadsrc/coreclr/jit/compiler.hpp

@jakobbotschjakobbotsch left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think it looks ok. I am fine with merging this as is.

One thing that would be nice is if details about WASM didn't leak into FlowGraphDfsTree. Maybe an abstraction could be a GraphDetails class that gives pred/successor/entry blocks visitors, and we would have FlowGraphDetails and WasmFlowGraphDetails implementations of that. The current FlowGraphDfsTree would be defined as some using FlowGraphDfsTree = DfsTree<FlowGraphDetails> and similar for FlowGraphNaturalLoops. Then the WASM stuff could have its own definitions of DFS tree and loops created over its own structure of the flow graph.

@AndyAyersMS

Copy link
Copy Markdown
MemberAuthor

I think it looks ok. I am fine with merging this as is.

One thing that would be nice is if details about WASM didn't leak into FlowGraphDfsTree. Maybe an abstraction could be a GraphDetails class that gives pred/successor/entry blocks visitors, and we would have FlowGraphDetails and WasmFlowGraphDetails implementations of that. The current FlowGraphDfsTree would be defined as some using FlowGraphDfsTree = DfsTree<FlowGraphDetails> and similar for FlowGraphNaturalLoops. Then the WASM stuff could have its own definitions of DFS tree and loops created over its own structure of the flow graph.

It also leaks into the Loop classification, since that needs to walk the reverse flow.

Let me look into this after I get the next bit of work merged.

@AndyAyersMS

Copy link
Copy Markdown
MemberAuthor

/ba-g scheduled macos-13 brownout.

@AndyAyersMS
AndyAyersMS merged commit 41c9fa2 into dotnet:mainNov 18, 2025
91 of 106 checks passed
@AndyAyersMSAndyAyersMS mentioned this pull request Nov 19, 2025
12 tasks
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Dec 19, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

arch-wasmWebAssembly architecturearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@AndyAyersMS@kg@jakobbotsch@am11