Skip to content

[wasm] CQ: Lack of Implicit Tail Calls Leads to Size-Expensive GDV Expansion #136246

Open

Description

@adamperlin

This is a potential code size issue; The method in question where this came up is System.Collections.Hashtable.Contains(object); The x64 codegen looks like:

564330: 4c 8d 1d 89 79 b0 00    lea     r11, [0x106bcc0]      // bool System.Collections.Hashtable.ContainsKey(object) (VIRTUAL_ENTRY)
564337: 39 09                   cmp     dword ptr [rcx], ecx
564339: 49 ff 23                jmp     qword ptr [r11]

Whereas the wasm codegen is substantially larger, and contains a GDV expansion:

    ; WASM table index: 22773, function index: 18151
    ; Parameters:
    ;   [0] i32
    ;   [1] i32
    ;   [2] i32
    ;   [3] i32
    ; Locals:
    ;   [4] i32
    ;   [5] i32
    ;   [6] i32
    ;   [7] i32
    ; Results: i32

    0000: local.get 0
    0002: i32.const 16
    0004: i32.sub    
    0005: local.tee 0 // establish sp for new frame
    0007: local.get 3 
    0009: i32.store align=1 // store PEP
    000C: local.get 0
    000E: local.get 1
    0010: i32.store align=1 offset=12 ; spill `this` arg (this should now be elided)
    0013: local.get 0
    0015: local.get 2
    0017: i32.store align=1 offset=8 ; spill `object` arg (this should now be elided)
    001A: block
    001C:   block
    001E:     block
    0020:       local.get 0
    0022:       i32.const 1
    0024:       i32.store align=1 offset=4 // VIP update
    0027:       global.get 1 // imageBase
    0029:       i32.load align=1 offset=6555736 // System.Collections.Hashtable (TYPE_HANDLE) 
    002F:       local.get 0
    0031:       i32.load align=1 offset=12 // load `this` arg
    0034:       local.tee 7
    0036:       local.get 7 
    0038:       i32.eqz // null check `this`
    0039:       br_if 2
    003B:       i32.load align=1 // load method table from `this`
    003E:       i32.ne // compare to `System.Collections.Hashtable` method table for fast path
    003F:       br_if 0
// GDV fastpath for exact match System.Collections.Hashtable
    0041:       local.get 0
    0043:       local.get 0
    0045:       i32.load align=1 offset=12
    0048:       local.get 0
    004A:       i32.load align=1 offset=8
    004D:       global.get 1 // imageBase
    004F:       i32.load align=1 offset=6522148 // bool System.Collections.Hashtable.ContainsKey(object) (METHOD_ENTRY_DEF_TOKEN)
    0055:       local.tee 4
    0057:       local.get 4
    0059:       i32.load align=1
    005C:       call_indirect 0 (type 152)
    0060:       local.set 5
    0062:       br 1
    0064:     end
// virtual fallback
    0065:     local.get 0
    0067:     local.get 0
    0069:     i32.load align=1 offset=12
    006C:     local.get 0
    006E:     i32.load align=1 offset=8
    0071:     global.get 1 // imageBase
    0073:     i32.load align=1 offset=6472628 // bool System.Collections.Hashtable.ContainsKey(object) (VIRTUAL_ENTRY)
    0079:     local.tee 6
    007B:     local.get 6
    007D:     i32.load align=1
    0080:     call_indirect 0 (type 152)
    0084:     local.set 5
    0086:   end
    0087:   local.get 5
    0089:   return
    008A: end
    008B: local.get 0
    008D: i32.const 1
    008F: i32.store align=1 offset=4
    0092: local.get 0
    0094: i32.const 0
    0096: call 19847
    009A: unreachable
    009B: end

The reason for the expansion on the Wasm side seems to be the lack of implicit tail calls; The virtual call to System.Collections.Hashtable.ContainsKey(object) can be an implicit tail call on x64 and so it is excluded as a gdv candidate:

if (call->CanTailCall())

However, we've disabled implicit tail calls on Wasm and so we instead keep the gdv. This is a fairly size-expensive expansion, and though it might improve perf we might consider whether allowing a tail call here would be better to reduce size.

Activity

  1. added
    untriaged
    on Oct 21, 2026
  2. self-assigned this
    on Oct 21, 2026
  3. added
    area-CodeGen-coreclr
    on Oct 21, 2026
  4. changed the title [-][wasm] Lack of Implicit Tail Calls Leads to GDV Expansion[/-] [+][wasm] CQ: Lack of Implicit Tail Calls Leads to Size-Expensive GDV Expansion[/+] on Oct 21, 2026
  5. added
    arch-wasm
    and removed
    untriaged
    on Oct 21, 2026
  6. dotnet-policy-service commented on Oct 21, 2026

    @dotnet-policy-service
    Contributor

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

  7. dotnet-policy-service commented on Oct 21, 2026

    @dotnet-policy-service
    Contributor

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

  8. added this to the 12.0.0 milestone on Oct 21, 2026
  9. jakobbotsch commented on Oct 30, 2026

    @jakobbotsch
    Member

    allowing a tail call here would be better to reduce size.

    I think we should separate more clearly what a tailcall in wasm entails, because I do not think it makes sense to emit return_call on wasm. As @SingleAccretion has said previously, this is purely a constraint on the WASM code generator. We would not want IL generators to opportunistically emit tail. prefixes either.

    If tailcalls allow us to simplify prolog/epilog generation then that sounds worthwhile, but it does not necessarily require emission of return_call.
    The other consideration is that it sounds like we may want a different heuristic for perf vs size trade offs when it comes to GDV.

  10. adamperlin commented on Oct 30, 2026

    @adamperlin
    ContributorAuthor

    That definitely makes sense. It sounds like a very good idea to separate what a tail call on Wasm entails and what we would hope to gain by emitting them in our code generator and perhaps this could be a separate issue.
    I agree that we may want a different heuristic for GDV when it comes to size vs. perf and I do think this is worthwhile to investigate for Wasm. cc @AndyAyersMS

  11. AndyAyersMS commented on Nov 12, 2026

    @AndyAyersMS
    Member

    Are we seeing GDVs in current apps like Blazor Havig? I do not see us using a MIBC, though it seems like eventually we should.

  12. adamperlin commented on Nov 13, 2026

    @adamperlin
    ContributorAuthor

    Are we seeing GDVs in current apps like Blazor Havig? I do not see us using a MIBC, though it seems like eventually we should.

    This example came from a wasm build of System.Private.CoreLib from the default browser runtime build. I'm not sure whether we use a MIBC file there? I haven't looked into this issue on Havit at all, but perhaps we should?

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-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions