Repository navigation
Open
Labels
arch-wasmWebAssembly architecturearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue
Milestone
Description
Description
I've done an analysis of the WebAssembly code generated by the JIT for GetGCStaticBase, and I've identified a number of issues that reduce its performance.
Current state of the function ...
(func $S_P_CoreLib_System_Runtime_CompilerServices_StaticsHelpers__GetGCStaticBase (type 58) (param i32 i32 i32) (result i32)
(local i32 i32 i32)
local.get 0
i32.const 32 ;; Allocate 32bytes on stack
i32.sub
local.tee 0
global.get 2
i32.const 16141
i32.add
i32.store align=1 ;; Set initial Function pointer into stack frame
block ;; label = @1
block ;; label = @2
local.get 0
i32.const 1
i32.store offset=4 align=1 ;; Store relative ip of 1 into stack frame
local.get 0
local.get 1
i32.const 24
i32.add
local.tee 5
local.get 5
i32.const 1023
i32.le_u
br_if 1 (;@1;) ;; Null check on read of MethodTableAuxiliaryData pointer (pointer less than 1024)
i32.load align=1 ;; Load MethodTableAuxiliaryData from MethodTable pointer
i32.store offset=20 align=1 ;; Store pointer to MethodTableAuxiliaryData at offset 20.
local.get 0
local.get 0
i32.load offset=20 align=1 ;; Load pointer to MethodTableAuxiliaryData from offset 20.
i32.const -12
i32.add
local.tee 5
local.get 5
i32.const 1023
i32.le_u
br_if 1 (;@1;) ;; Null check on read of gcStaticBase
i32.load align=1 ;; read gcStaticBase from location relative to MethodTableAuxiliaryData
i32.store offset=24 align=1 ;; Store gcStaticBase value to memory local
local.get 0
i32.load offset=24 align=1 ;; Load gcStaticBase value from memory local
local.tee 3 ;; Save gcStaticBase to WASM local, and leave on stack (Not sure why this is here. We don't read the local ever)
i32.const 1
i32.and
i32.const 0
i32.ne
br_if 0 (;@2;) ;; Check gcStaticBase against having the low bit set Fallback to GetGCStaticBaseSlow.
local.get 0
i32.load offset=24 align=1 ;; load gcStaticBase from memory local
return
end
local.get 0
local.get 1
global.get 1
i32.load offset=1026044 align=1
local.tee 4
local.get 4
i32.load align=1
call_indirect (type 58) ;; Call GetGCStaticBase slow
return
end
local.get 0
i32.const 1
i32.store offset=4 align=1 ;; Set VirtualIP offset to 1
local.get 0
local.get 0
global.get 1
i32.load offset=968744 align=1 ;; Load address of null throw helper
i32.store offset=28 align=1 ;; Stash PEP in memory local
local.get 0
i32.load offset=28 align=1 ;; load PEP from local for null throw helper for PEP argument
local.get 0
i32.load offset=28 align=1 ;; load PEP from local for null throw helper for function index load
i32.load align=1 ;; load target function index
call_indirect (type 8)
unreachable)In there I identified a number of issues we could fix, and measured the overall improvement to a small application which used static access very often.
- For this function in particular, if the ByRef's it reads from are null, we could consider it to be a runtime crash, since that should never happen. So, we could remove the null checks.
- We have a number of cases where we stored a temporary byref to the stack, but we didn't actually need to do so, since we didn't actually cross a GC point when the locals were logically live. Instead we could use WASM locals to hold the data.
- Given that there are no memory locals after fixing issue Get core-setup building in the consolidated repo. #2, and no throwing after handling issue WIP: repo consolidation scouting kick-off - make clr build locally on Windows #1, we could get away without establishing a stack from for the stack walker to walk. We certainly don't need it at function entry. If we wanted to, we could establish the frame just before calling through GetGCStaticBaseSlow
- Also noticed... the call to the null throw helper is using dispatch through a PEP, but for some reason it was stashing some temps into a memory based local. (The call to GetGCStaticBaseSlow was using the more efficient wasm local). Not sure what's happening there.
My hand-adjusted code looks like:
(func $S_P_CoreLib_System_Runtime_CompilerServices_StaticsHelpers__GetGCStaticBase (type 58) (param i32 i32 i32) (result i32)
(local i32 i32 i32)
block ;; label = @2
local.get 1
i32.const 24
i32.add
i32.load ;; Load MethodTableAuxiliaryData from MethodTable pointer
i32.const -12
i32.add
i32.load ;; read gcStaticBase from location relative to MethodTableAuxiliaryData
local.tee 5 ;; Store gcStaticBase pointer to WASM local
i32.const 1
i32.and
i32.const 0
i32.ne
br_if 0 (;@1;) ;; Check gcStaticBase against having the low bit set Fallback to GetGCStaticBaseSlow.
local.get 5 ;; Load gcStaticBase
return
end
local.get 0
local.get 1
global.get 1
i32.load offset=1026044 align=1
local.tee 4
local.get 4
i32.load align=1
call_indirect (type 58)
return)Configuration
Browser wasm R2R
Regression?
No
Data
With the above changes, a small test app which was spending about 10% of the application in this helper went from
| Scenario | Performance |
|---|---|
| Initial state | 28,211.44 ns/op |
| After removing unnecessary memory locals | 27,254.35 ns/op |
| After also removing prolog | 25,832.26 ns/op |
Metadata
Metadata
Assignees
Labels
arch-wasmWebAssembly architecturearea-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue
Activity
dotnet-policy-service commentedon Sep 22, 2026
Tagging subscribers to 'arch-wasm': @lewing, @pavelsavara
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Sep 22, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
davidwrighton commentedon Sep 22, 2026
A profiler indicates these changes make this particular function about 3X faster.
AndyAyersMS commentedon Sep 22, 2026
The PEP spilling is fairly simple to fix, I'll have a PR up soon. It is a consequence of late-created calls.
AndyAyersMS commentedon Nov 2, 2026
Performance has improved over the past few weeks.
The major improvement comes from dotnet/runtime#135543, which permits GC references that do not survive safepoints to remain in Wasm locals. The unnecessary PEP spilling was fixed by dotnet/runtime#134490. The null-throw path now calls its helper directly following dotnet/runtime#134555. Prolog and comparison sequences have also become smaller.
Both redundant null checks are still present. Every successful call still establishes the frame and stores the virtual IP, so the helper has not reached the issue's hand-optimized form.
There are also two unnecessary Wasm-local writes:
local.tee 3andlocal.tee 4, whose locals are never subsequently read.davidwrighton commentedon Nov 12, 2026
@AndyAyersMS One thought here is the setting of the VirtualIP in the throw helper clause. If we know that all the throw helper clauses are dispatched to after the virtual IP is established (and there are no try/catch/finally blocks in the function, we should be able to avoid actually storing the virtual ip value. In fact.... now that I think on it. We could make it possible to avoid needing to set the virtual IP at all in a method like this which has no EH logic in it. The only meaningful virtual IP value stored on the stack is always 1, so why are we bothering to write it at all... we already report to the VM that the only virtual IP value we'll ever set is 1, so we should be able to detect that and avoid reading the virtual ip from the call stack entirely... which means we could avoid storing it.
AndyAyersMS commentedon Nov 12, 2026
Here is an updated
GetGCStaticBaselisting. Compared with the November 2 listing:The two additional improvements come from:
local.set/local.getpairs at known last uses. This removes the two otherwise-unusedlocal.teewrites from this helper.This body is 106 bytes including local declarations, with 103 instruction bytes. The following is the linked code, so the frame identity, import-cell offset, and function/type indices are image-specific:
The initialized fast path still performs two loads and two stores, establishes a 16-byte frame, and retains both redundant null checks. It still writes virtual IP
1on the fast path. The duplicate write in the throw-helper block is gone.Eliminating the remaining virtual-IP store entirely, as suggested above, is something we can look at.
adamperlin commentedon Nov 12, 2026
@AndyAyersMS I wonder where the extra
local.tees are coming from? I wonder if we're missing eliminating some dead local stores on wasm, or maybe a tree was markedMultiplyUsedthat didn't need to be?`Edit: I see you responded above.
AndyAyersMS commentedon Nov 12, 2026
You mean these? Or the ones that got optimized away?
adamperlin commentedon Nov 12, 2026
Sorry, you responded while I was writing my comment, it's outdated! I meant the ones that got optimized away in the latest iteration.
AndyAyersMS commentedon Nov 12, 2026
If we were willing to lose stack trace fidelity we could omit both the VIP update an the frame identity, and so would no longer need a frame. As David mentioned offline this might be reasonable for (some?) methods in SPC as these methods are implementation details.
Or for methods like these with a shortish exception free path we could "shrink wrap" the frame setup to just the throw helper path. That wouldn't help size but might help perf.
AndyAyersMS commentedon Nov 12, 2026
Omitting VIP=1 projected size impact on Blazor Havit. As we have seen elsewhere a repeated common code pattern compresses well, so the compressed savings are small. There should be a small perf benefit.
Roughly 66K methods out of 77K would benefit (we only need VIP updates in method with EH, and most methods don't have EH).
pavelsavara commentedon Nov 13, 2026
I disabled stack trace numbers for publish by default.
#136971
You could possibly make this tradeoff based on the same
System.Diagnostics.StackTrace.IsLineNumberSupportedfeature flag.I wonder if "stack trace fidelity" would also break CPU profiling or other non-debug scenarios.
[Wasm] Elide singleton virtual IP stores (#137465)