Skip to content

[wasm] Performance of GetGCStaticBase could be significantly faster #134450

Open

Description

@davidwrighton

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.

  1. 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.
  2. 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.
  3. 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
  4. 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

Activity

  1. added
    tenet-performance
    arch-wasm
    area-CodeGen-coreclr
    on Sep 22, 2026
  2. added
    untriaged
    on Sep 22, 2026
  3. dotnet-policy-service commented on Sep 22, 2026

    @dotnet-policy-service
    Contributor

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

  4. dotnet-policy-service commented on Sep 22, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
    See info in area-owners.md if you want to be subscribed.

  5. davidwrighton commented on Sep 22, 2026

    @davidwrighton
    MemberAuthor

    A profiler indicates these changes make this particular function about 3X faster.

  6. AndyAyersMS commented on Sep 22, 2026

    @AndyAyersMS
    Member

    The PEP spilling is fairly simple to fix, I'll have a PR up soon. It is a consequence of late-created calls.

  7. removed
    untriaged
    on Sep 22, 2026
  8. added this to the 12.0.0 milestone on Oct 9, 2026
  9. AndyAyersMS commented on Nov 2, 2026

    @AndyAyersMS
    Member

    Performance has improved over the past few weeks.

    Initialized fast path Original Current
    Memory loads 5 2
    Memory stores 4 2
    Shadow-stack frame 32 bytes 16 bytes
    Byref temporaries spilled to memory 2 0

    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.

    ;; Parameters: 0 = SP, 1 = MethodTable, 2 = PEP
    ;; Additional i32 locals: 3 through 7
    
    local.get 0
    i32.const 16
    i32.sub
    local.tee 0
    i32.const_frameidentityfunclet 0
    i32.store 0 0                  ;; Frame identity
    
    block
      block
        local.get 0
        i32.const 1
        i32.store 0 4              ;; Virtual IP
    
        local.get 1
        i32.const 24
        i32.add
        local.tee 7
        local.get 7
        i32.const 1023
        i32.le_u
        br_if 1                   ;; Null-throw path
        i32.load 0 0              ;; MethodTableAuxiliaryData
        local.tee 3
    
        i32.const -12
        i32.add
        local.tee 7
        local.get 7
        i32.const 1023
        i32.le_u
        br_if 1                   ;; Null-throw path
        i32.load 0 0              ;; GC static base
        local.tee 6
        local.tee 4
        i32.const 1
        i32.and
        br_if 0                   ;; Initialization required
    
        local.get 6
        return
      end
    
      local.get 0
      local.get 1
      global.get 4325760
      i32.load 0 reloc 0x421cc0
      local.tee 5
      local.get 5
      i32.load 0 0
      call_indirect 4327192 0      ;; GetGCStaticBaseSlow
      return
    end
    
    local.get 0
    i32.const 1
    i32.store 0 4                  ;; Virtual IP
    local.get 0
    i32.const 0                    ;; PEP argument
    call 4327176                   ;; CORINFO_HELP_THROWNULLREF
    unreachable
    end

    There are also two unnecessary Wasm-local writes: local.tee 3 and local.tee 4, whose locals are never subsequently read.

  10. davidwrighton commented on Nov 12, 2026

    @davidwrighton
    MemberAuthor

    @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.

  11. AndyAyersMS commented on Nov 12, 2026

    @AndyAyersMS
    Member

    Here is an updated GetGCStaticBase listing. Compared with the November 2 listing:

    Metric November 2 Updated image
    Total instructions 55 50
    Initialized fast-path instructions 36 34
    Unused Wasm-local writes 2 0
    Virtual-IP stores in the null-throw path 1 0

    The two additional improvements come from:

    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:

    (func $S_P_CoreLib_System_Runtime_CompilerServices_StaticsHelpers__GetGCStaticBase
      (param i32 i32 i32) (result i32)
      (local i32 i32 i32 i32 i32) ;; Locals 3 through 7; 3 and 4 are unused
    
      local.get 0
      i32.const 16
      i32.sub
      local.tee 0
      i32.const -54089
      i32.store align=1             ;; Frame identity
    
      block
        block
          local.get 0
          i32.const 1
          i32.store offset=4 align=1 ;; Virtual IP
    
          local.get 1
          i32.const 24
          i32.add
          local.tee 6
          local.get 6
          i32.const 1023
          i32.le_u
          br_if 1                  ;; Null-throw path
          i32.load align=1          ;; MethodTableAuxiliaryData
    
          i32.const -12
          i32.add
          local.tee 6
          local.get 6
          i32.const 1023
          i32.le_u
          br_if 1                  ;; Null-throw path
          i32.load align=1          ;; GC static base
          local.tee 5
          i32.const 1
          i32.and
          br_if 0                  ;; Initialization required
    
          local.get 5
          return
        end
    
        local.get 0
        local.get 1
        global.get 1               ;; Image base
        i32.load offset=1576400 align=4
        local.tee 7
        local.get 7
        i32.load align=4
        call_indirect (type 43)     ;; GetGCStaticBaseSlow
        return
      end
    
      local.get 0
      i32.const 0                  ;; PEP argument
      call 22867                   ;; CORINFO_HELP_THROWNULLREF
      unreachable)

    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 1 on 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.

  12. adamperlin commented on Nov 12, 2026

    @adamperlin
    Contributor

    @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 marked MultiplyUsed that didn't need to be?`

    Edit: I see you responded above.

  13. AndyAyersMS commented on Nov 12, 2026

    @AndyAyersMS
    Member

    You mean these? Or the ones that got optimized away?

          local.tee 6    ;; dup operand for null check
          local.get 6
          i32.const 1023
    
          ...
    
          local.tee 7   ;; dup pep to find entry point and pass as arg
          local.get 7
          i32.load align=4
  14. adamperlin commented on Nov 12, 2026

    @adamperlin
    Contributor

    You mean these? Or the ones that got optimized away?

    Sorry, you responded while I was writing my comment, it's outdated! I meant the ones that got optimized away in the latest iteration.

  15. AndyAyersMS commented on Nov 12, 2026

    @AndyAyersMS
    Member

    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.

  16. AndyAyersMS commented on Nov 12, 2026

    @AndyAyersMS
    Member

    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).

    image
  17. pavelsavara commented on Nov 13, 2026

    @pavelsavara
    Member

    If we were willing to lose stack trace fidelity

    I disabled stack trace numbers for publish by default.
    #136971

    You could possibly make this tradeoff based on the same System.Diagnostics.StackTrace.IsLineNumberSupported feature flag.

    I wonder if "stack trace fidelity" would also break CPU profiling or other non-debug scenarios.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-CodeGen-coreclrtenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions